Sunday, July 12, 2009

Java tutorial Sample Source Code for StudentRecord class

Java tutorial Sample Source Code for StudentRecord class

Here is the code for our StudentRecord class,

public class StudentRecord
{
private String name;
private String address;
private int age;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;

private static int studentCount;

/**
* Returns the name of the student
*/
public String getName(){
return name;
}

/**
* Changes the name of the student
*/
public void setName( String temp ){
name = temp;
}

// other code here ....

/**
* Computes the average of the english, math and science
* grades
*/
public double getAverage(){

double result = 0;
result = ( mathGrade+englishGrade+scienceGrade )/3;

return result;
}

/**
* returns the number of instances of StudentRecords
*/
public static int getStudentCount(){
return studentCount;
}
}
Now, here's a sample code of a class that uses our StudentRecord class.

public class StudentRecordExample
{
public static void main( String[] args ){

//create three objects for Student record
StudentRecord annaRecord = new StudentRecord();
StudentRecord beahRecord = new StudentRecord();
StudentRecord crisRecord = new StudentRecord();

//set the name of the students
annaRecord.setName("Anna");
beahRecord.setName("Beah");
crisRecord.setName("Cris");

//print anna's name
System.out.println( annaRecord.getName() );

//print number of students
System.out.println("Count="+StudentRecord.getStudentCount());
}
}

Java tutorial Sample Source Code for StudentRecord class

The output of this program is,

Anna
Student Count = 0

Related Post:

Java Tutorial switch statement source code

Java tutorial source code get input from keyboard

Java Tutorial Using JOptionPane to get input

Java tutorial Decision Control Structures: if statement

PHP Tutorial

Java programing source code sample printing characters

Java tutorial if-else statement

C programming sample source code: Sum of 2 numbers

Java Programming Features