Originally Posted by
Jnoobs
Okay so i thought i would get a jumpstart on my homework for my CPS181 (Data structures) course. I got an assignment that contains 4 problems each having to do with the one before (like 2 continues on from 1).
-------------------------------------------------------------------------------------------------------------------------------------
Directions: First, implement a class named
Patient. This class will represent one patient, and must have the following data attributes and methods:
1. name: a String
2. age: an int
3. gender: a char
4. priority: an int
5. a default constructor
6. a parameterized constructor to set the attributes above
7. a
toString method to print out the data attributes above.
-------------------------------------------------------------------------------------------------------------------------------------
Okay so i understand on making the program (sort of). Basically my questions is: How do i run this program but still allow another class to use everything inside?
Here is my code (which i cant execute bc there is no main function...due to my thought of using this class for the next one)
public class Patient { // Default constructor
private String name;
private int age;
private char gender;
private int priority;
private Patient next;
public Patient(String name, int age, char gender, int priority) { // Parameterized constructor
super();
this.name = "Patrick Kinnicutt";
this.age = 20;
this.gender = 'M';
this.priority = 0;
next= null;
}
public String toString() {
String msg = String.format("Patient: " + name + "\n Age: " + age + "\n Gender: " + gender + "\n Priority: " + priority);
return msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public Patient getNext() {
return next;
}
public void setNext(Patient next) {
this.next = next;
}
}
Thanks for the help
Simply use it as an object in another class. As long as all the methods and variables you want to use, and the Patient class, and its constructor are public, then you can do this:
Patient p = new Patient(String name, int age, char gender, int priority) ;
However, why do you have a call to super() in your constructor? The only superclass your Patient class appears to have is Object.
public Patient(String name, int age, char gender, int priority) { // Parameterized constructor
super();
this.name = "Patrick Kinnicutt";
this.age = 20;
this.gender = 'M';
this.priority = 0;
next= null;
Not sure if that's what you want.
this.name = name;
this.age = age;
this.gender = gender;
this.priority = priority;
Perhaps in the other class you could do this though:
Patient p2 = new Patient("Patrick Kinnicutt", 20, 'M', 0);