Java Student in trouble!

EoinZero's Avatar, Join Date: Apr 2011
Newbie Member
Well, I am in the middle of doing a programming question for an assignment. Think I have gotten most of it right but its just killing me that I don't have enough time to finish everything... (Due in later tonight). I still have to start the 2nd question myself, but this is what I got for the first one!

This is the question:
Part 1 (50 marks)
a) Write the Java code for a class called Patient.
• The Patient class should have the following four private instance variables:
o name
o iDNumber
o age
o gender
Choose the correct type for each of the instance variables.
• Include a default constructor.
• Include a constructor that accepts four parameter values: name, iDNumber, age, and gender.
• Include get and set methods for each of the instance variables.
• Include a method called print() that prints the value of all the instance variables to the screen, e.g.
Patient name: Joe Bloggs
IDNumber: 520
Age: 86
Gender: Male
b) Write the Java code for a class called PatientTestApplication (containing a public static void main(String [] args) method) which tests the two constructors, the get and set methods, and the print() method of the Patient class.
• This class should create two objects of the class Patient, using each constructor in the Patient class definition.
• It should then test all the get and set methods on each object.
• It then uses the print() method on both objects to print the object’s details to screen.

================================================== =======
This is what i have got so far...

Code:
public class Patient



	{

	//Attributes: Object characteristics
	private String name;
	private int iDNumber;
	private int age;
	private String gender;

	public Patient()
		{
			name = "none";
			iDNumber = 0;
			age = 0;
			gender = "none";
		}

	public Patient(String n, int i, int a, String g)
		{
			 name = n;
			 iDNumber = i;
			 age = a;
			 gender = g;
		}




	//Methods: object behavior, mutator method
	public void setName(String n)
		{
			name = n;
		}
		//accessor methods
		public String getName()
			{
				return name;
		}




		//Methods: object behavior, mutator method
	public void setIdNumber(int i)
		{
			iDNumber = i;
		}

	//accessor methods
	public int getiDNumber()
		{
			return iDNumber;
		}


	//Methods: object behavior, mutator method
	public void setAge(int a)
		{
			age = a;
		}

	//accessor methods
	public int getAge()
		{
			return age;
		}



	//Methods: object behavior, mutator method
	public void setGender(String g)
		{
			gender = g;
		}
	//accessor methods
	public String getGender()
	{
		return gender;
	}



	}
=======================================================
And this is my test... it doesnt seem to work properly and I dont know why :(


public class PatientTest
{

	//this is an array of strings
	public static void main(String [] args){


		Patient p1 = new Patient("Joe Bloggs", 4125, 45, "Male");

		Patient p2 = new Patient();


		 // new set methods to overwrite the old ones on patient 2
		System.out.println("Name:"+ p2.getName());
		System.out.println("iDNumber:"+ p2.getiDNumber());
		System.out.println("Age:"+ p2.getAge());
		System.out.println("Gender:"+ p2.getGender());

		p2.setName("Larry");
		p2.setIdNumber(12345);
		p2.setAge(25);
		p2.setGender("male");


		  //gets code from patient and gives values
		System.out.println("Name:"+ p1.getName());
		System.out.println("iDNumber:"+ p1.getiDNumber());
		System.out.println("Age:"+ p1.getAge());
		System.out.println("Gender:"+ p1.getGender());

		//this is default code here which has no values in patient
		System.out.println("Name:"+ p2.getName());
		System.out.println("iDNumber:"+ p2.getiDNumber());
		System.out.println("Age:"+ p2.getAge());
		System.out.println("Gender:"+ p2.getGender());
	}




}
If anyone can help me out it would be amazing, until then I'm going to start my 2nd question... FML

Last edited by shabbir; 5Apr2011 at 09:44.. Reason: Code blocks
0
EoinZero's Avatar, Join Date: Apr 2011
Newbie Member
Well... no one decided to even bother helping me Its ok I still managed to actually get it done and get pretty good marks for most of it!

No thanks to anyone here...

Happy but dissapointed

EZ.