Java Tutorials

A Java Student Collection Class

‘A Java Student Collection Class’¬†is an example of a Java collection class. In this example I am trying to show you how to make arrays of object and how to work with them. In this example I use the word ‘Static’ for a couple of attributes and methods have a deep look at them to understand how to use them.

The program contains 4 classes:

1-The Student Class

2-The GroupTutorial Class

3-The StudentTester Class

4-The GroupTutorialTester Class

The Student Class :



/** This is a class for recording the student name and their mark.
*
* @author Parham Majdabadi
*
*/
public class Student {
private static int numberOfStudents = 0;
private static double totalMark = 0;
private String name;
private double mark;

/**
* The Constructor method.
* @param name and mark
* @return creates an object of student class and update static values
*/
public Student(String nameIn, double markIn) {
name = nameIn;
mark = markIn;
numberOfStudents ++;
totalMark += mark;
}

public double getmark() {
return mark;
}

public String getName() {
return name;
}

public void setMark(double markIn) {
totalMark -= mark;
mark = markIn;
totalMark += mark;
}

public void setName(String nameIn) {
name = nameIn;
}

public static double getMeanMark() {
return (totalMark) / numberOfStudents;
}

}

The StudentTester:



import java.text.*;

public class StudentTester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name[] = {"John", "George", "Hassan", "Luck", "Lisa"};
double mark[] = {40, 48.34, 56, 76, 91.34};
Student myStudents[] = new Student[name.length];
DecimalFormat df = new DecimalFormat("#.##");

for(int i = 0; i < myStudents.length; i++) {
myStudents[i] = new Student(name[i], mark[i]);
}

double mean = Student.getMeanMark();
String s = df.format(mean);
System.out.println("The mean mark is " + s);

}

}

The TutorialGroup Class:


/** This is a class for recording the student names and their marks for a particular tutorial group.
*
* @author Parham Majdabadi
*
*/
public class TutorialGroup {
private Student[] group;
private int size;
private static int maxSize = 50;

/**
* The Constructor method.
* @param an array Object of Student class
* @return copy students to an internal array
*/
public TutorialGroup(Student[] studentIn) {
size = 0;
group = new Student[maxSize];
for(int i = 0; i < studentIn.length; i++) {
group[i] = studentIn[i];
size ++;
}
}

public int groupSize() {
return size;
}

public void add(Student studentIn) {
if (size < maxSize - 1) {
group[size] = studentIn;
size ++;
} else {
System.out.println("The Group is Full");
}
}
}

The TutorialGroupTester Class:


public class TutorialGroupTester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name[] = {"John", "George", "Hassan", "Luck", "Lisa"};
double mark[] = {40, 48.34, 56, 76, 91.34};
Student myStudents[] = new Student[name.length];

for(int i = 0; i < myStudents.length; i++) {
myStudents[i] = new Student(name[i], mark[i]);
}

TutorialGroup myTG = new TutorialGroup(myStudents);
System.out.println("Group size is " + myTG.groupSize());
}

}


Leave a Reply

Your email address will not be published. Required fields are marked *