Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Linked Lists

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Linked Lists

    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
    Last edited by Jnoobs; October 22nd, 2010 at 03:24 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Linked Lists

    Quote Originally Posted by Jnoobs View Post
    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);

Similar Threads

  1. Array Lists, PLEASE HALP
    By Iamthecheese in forum Threads
    Replies: 10
    Last Post: September 7th, 2010, 08:43 PM
  2. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  3. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  4. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 07:13 PM
  5. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 02:46 AM