Cs506 Assignment # 02
/*
* CS506 Fall 2015
* Assignment No. 2 Solution
* */
import java.io.* ;
import java.util.* ;
import java.awt.* ;
import javax.swing.* ;
// definition of Student class
class Student{
// data members
String name ;
int rollNo ;
int marks ;
// default constructor which initializes data members with default values
public Student()
{
name = null ;
rollNo = 0;
marks = 0 ;
}
// parameterized constructor which sets the values of data members with passes arguments
public Student(String n, int r, int m)
{
name = n ;
rollNo = r;
marks = m ;
}
// getter funtion for name
public String getName()
{
return name ;
}
// getter function for roll no
public int getRollNo()
{
return rollNo ;
}
// getter function for marks
public int getMarks()
{
return marks ;
}
} // end of Student class
// Public driver class
public class Test {
// main() method
public static void main(String[] args)
{
FileReader fr = null ;
BufferedReader br = null ;
ArrayList<Student> std = new ArrayList<Student>() ;
int totalMarks = 0, averageMarks = 0 ;
int count = 0 ;
// reading data from input text file and storing student records in students object
try{
fr = new FileReader("input.txt") ;
br = new BufferedReader(fr) ;
String[]tokens ;
String line = br.readLine() ;
while(line!= null)
{
tokens= line.split(" ");
String name = tokens[0];
String r = tokens[1] ;
String m = tokens[2] ;
int rollNo = Integer.parseInt(r);
int marks = Integer.parseInt(m);
Student s = new Student(name, rollNo, marks) ;
System.out.println("Name: " + name + " Roll No: " + rollNo + " Marks: " + marks) ;
totalMarks = totalMarks+marks ;
std.add(s) ;
line = br.readLine();
count++ ;
}
}catch (IOException e){
System.out.println(e) ;
}
// calculating average marks
averageMarks = totalMarks / count ;
System.out.println("Average Marks: " + averageMarks);
// developing GUI
int size = std.size() + 2;
JFrame frame = new JFrame();
JLabel label1, label2, label3 ;
Container c = frame.getContentPane() ;
frame.setLayout(new GridLayout(size,3));
c.add(new JLabel("Name")) ;
c.add(new JLabel("Roll No")) ;
c.add(new JLabel("Marks")) ;
for (int i = 0; i<std.size(); i++)
{
Student s = (Student)std.get(i) ;
label1 = new JLabel("" + s.getName());
label2 = new JLabel("" + s.getRollNo());
label3 = new JLabel("" + s.getMarks());
c.add(label1) ;
c.add(label2) ;
c.add(label3) ;
} // end of for loop
c.add(new JLabel("Average Marks")) ;
c.add(new JLabel("" + averageMarks )) ;
frame.setSize(300, 250) ;
frame.setVisible(true) ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
} // end of main maethod
} // end of Test class
- No comment Available