Hi, I have to do the following :
(1)Write a class called Student with the following private member variables:
name [String]
id [long]
marks [float]
(2)It must have the following methods:
Methods to set name, set ID, and set marks
Eg: public void setName(String newName) { … }
Methods getName, getID, getMarks, eg:
public long getID() {
return id;
}
(3)Note that the set and get methods do not normally display messages on screen. However, setMarks should display an error if the marks are not in the range 0 - 100, and set the member variable marks to 0.
(4)To test the Student class, write a Test Class that creates a Student object and sets its name, ID and marks. The program should then display the student's name and marks in a message box.
Its parts (3) and (4) Im having trouble with. Im not sure is part 3 correct and cant check it if I dont know what to do for part 4
import javax.swing.JOptionPane;
public class Student {
/**
* OBP
*/
private String name; // Student's name.
private long ID; // Unique ID number for this student.
private float marks; // Grade
private static int nextUniqueID = 0;
// keep track of next available unique ID number
Student(String newName)
{
// Names Student, assigns the student a unique ID number
name = newName;
nextUniqueID++;
ID = nextUniqueID;
}
public String getName()
{
return name;
}
public long getID()
{
return ID;
}
public float getMarks()
{
return marks;
}
public void setName(String newName)
{
name = newName;
}
public void setID(long newID)
{
ID = newID;
}
public void setMarks(float newMarks)
{
if (newMarks >=0 && newMarks <=100)
{
marks = newMarks;
}
else
{
JOptionPane.showMessageDialog(null, "Marks must be >=0 and <=100");
marks=0;
}
}
}
Im still new to this so be gentle :P
Thanks