Top
Previous Next
The Java Language CS 161 - Java

A Student Object with associated data

Source: examples/Student2.java



/** Encapsulate information relating to a single student.
** @author: Jonathan Doughty
**/
 
public class Student2 {
 
public static void main(String args[]) {
 
Student2 aStudent = new Student2();
 
aStudent.setName("Jonathan Doughty");
aStudent.setId("123-45-6789");
 
// When the preceding lines are executed, a Student2 object is
// created and its two fields are set.
 
// What does the following do? What does the result look like?
System.out.println(aStudent);
 
Student2.waitForUser();
}
 
// Instance variables (fields) that will be associated with each student
 
private String GMU_Id;
private String name;
private int homeworkGrade;
 
// An accessor method to set the Student's name field
public void setName( String studentName ) {
name = studentName;
}
 
// An accessor method to set the Student's GMU_Id field
public void setId( String id ) {
GMU_Id = id;
}
 
// The following is only needed for running the program from
// within the Javaedit application on Windows
public static void waitForUser() {
try {
byte[] line = new byte[80];
System.out.println("press enter key to end");
System.in.read(line);
}
catch (java.io.IOException e) {
// ignored
}
}
 
}

Try compiling and running this
Top
Previous Next
jwd