Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Testing a class help needed

810103Nov 9 2010 — edited Nov 11 2010
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
This post has been answered by hsc71 on Nov 11 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2010
Added on Nov 9 2010
21 comments
214 views