Results 1 to 3 of 3
  1. #1
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default Abstract, toString, inherits Class & other Problem !

    Below are the Instruction of my Problem, the Coding what I get so far and my Question about my problem .

    Part A) Instruction

    - Write a PersonAbstract class that has one abstract method: ToString

    - Write a Person class that contain the following info. and inherit the PersonAbstract class.

    Attribues Name & Java Type

    m_Age (stores the age) & Java Type public Int
    m_Name (stores the name) & Java Type public String

    Method Name & Accessor Type

    Person Constructor that takes a String for the name. This single parameter should be assigned to m_Name. m_Age initially should be set to zero. & Accessor Type default

    ToString method that returns String. This method must concatenates the name and age together in a descriptive way and returns the result. & Acccessor Type protected

    Access methods to get/set each data member & Accessor Type default

    Java Code:
    // My Part A Coding
    abstract class PersonAbstract { // <<<< Question 2 Problem
    	protected String name;
    	protected int age;
    
    	public String toString() {
    		String message = "Name: " + name + "\n" + "Age: " + age + "\n";
    		return message;
    	}
    }
    
    class Person extends PersonAbstract{ // <<<< Question 1 Problem
    
    	int m_Age;
    	String m_Name;
    
    	public Person(String name) {
    		this.m_Name = name;
    		this.m_Age = 0;
    	}	
    
    	public String getName() {
    		return m_Name;
    	}
    
    	public void setName(String name) {
    		this.m_Name = name;
    	}	
    	
    	public int getAge() {
    		return m_Age;
    	}	
    
    	public void setAge(int age) {
    		this.m_Age = age;
    	}	
    }
    Question 1 : Not sure my toString() setup on class PersonAbstract are right by the above Part A Instruction, if wrong please help fix it ?
    Question 2 : Not sure my class Person inherit PersonAbstract class are right by the above Part A Instruction, if wrong please help fix it ?





    Part B) Instruction

    - Create Teacher Class that inherites the Person Class and extend that to the following:

    - A single Private Boolean data member named m_Tenure. m_Tenure indicates if the teacher has tenure or not.

    - Access methods (get/set) for m_Tenure.

    - A Constructor that simply takes the name of the teacher as a parameter. In this constructor,
    i. Pass this name up to the base class constructor.
    ii. Initialize m_Tenure to false.

    Java Code:
    // My Part B Coding
    class Teacher extends Person{
    	
    	private boolean m_Tenure;
    		
    	public Teacher(String name) {  // <<<< Question 3 Problem
    		super(name);
    		this.m_Tenure = false;
    	}
    
    	public boolean getTenure() {
    		return m_Tenure;
    	}
    
    	public void setTenure(boolean tenure) {
    		this.m_Tenure = tenure;
    	}	
    	
    
            // <<<< Question 4 Problem
    }

    Question 3 : Is my Constructor set up right by the above Part B Instruction, if wrong please help fix it ?
    Question 4 : How can I set up an ToString method in Teacher should first call up to the ToString method in Person in to order to retrieve the Person part of the string. It should then return a string output saying whether the teacher has tenure or not, according to m_Tenure





    Part C) Instruction

    - Create another class Student and add the following:

    - A single int data member named m_StudentNumber. m_StudentNumber will store the student’s student number.

    - A Constructor that simply takes the name of the student as a parameter. In this constructor,
    i. Pass this name up to the base class constructor.
    ii. Initialize m_StudentNumber to 0.

    - Access methods for m_StudentNumber

    Java Code:
    // My Part C Coding
    class Student extends Person{
    	
    	int m_StudentNumber;
    	
    	public Student(String name) {  // <<<< Question 5 Problem
    		super(name);
    		this.m_StudentNumber = 0;
    	}	
    
    	public int getStudentNumber() {
    		return m_StudentNumber;
    	}
    
    	
    	public void setStudentNumber(int studentNumber) {
     		this.m_StudentNumber = studentNumber;
    	}	
    
    	// <<<< Question 6 Problem
    }
    Question 5 : Is my Constructor set up right by the above Part C Instruction, if wrong please help fix it ?
    Question 6 : How can I add another toString method, check whether m_StudentNumber is zero or not, then return a string with the student number if it is not zero






    Part D) Instruction

    - Create a PersonTest Class and do the following:

    - Create an instance of Person, Teacher and Student. with empty constructor. Print out the object reference name that you have just construced.

    - Create 10 instances of Student with Student Number form 1 to 10 and name the Students as Student1 to Studend10. and use a loop to display a list

    eg, 1 Student1
    2 Student2
    3 Student3
    .
    .
    10 Student10

    - Create two new instances of Teacher with the name Teacher 1 and Teacher 2 Initialize both teachers to teh same name.

    - Compare the two teachers with the equals method Print out if the teachers are the same or different.

    Java Code:
    class PersonTest extends Person{   // <<<< Question 7 Problem
    	
    	// <<<< Question 8 Problem
    	
    	// <<<< Question 9 Problem
    
    	// <<<< Question 10 Problem
    
    	// <<<< Question 11 Problem
    
    }

    Question 7 : Is my PersonTest Class setup right by the above Part D Instruction?
    Question 8 : How I create a instance of Person, Teacher and Student. with empty constructor. Print out the object reference name that you have just construced.
    Question 9 : How Create 10 instances of Student with Student Number form 1 to 10 and name the Students as Student1 to Studend10. and use a loop to display a list
    Question 10 : How to Create two new instances of Teacher with the name Teacher 1 and Teacher 2 Initialize both teachers to teh same name.
    Question 11 : How to Compare the two teachers with the equals method Print out if the teachers are the same or different.


    I just start learning Java so I don't know much about Java so need help ! Please help me with that !

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Java Code:
    // My Part A Coding
    abstract class PersonAbstract { // <<<< Question 2 Problem
    	protected String name;
    	protected int age;
    
    	public String toString() {
    		String message = "Name: " + name + "\n" + "Age: " + age + "\n";
    		return message;
    	}
    }
    
    class Person extends PersonAbstract{ // <<<< Question 1 Problem
    
    	int m_Age;
    	String m_Name;
    
    	public Person(String name) {
    		this.m_Name = name;
    		this.m_Age = 0;
    	}	
    
    	public String getName() {
    		return m_Name;
    	}
    
    	public void setName(String name) {
    		this.m_Name = name;
    	}	
    	
    	public int getAge() {
    		return m_Age;
    	}	
    
    	public void setAge(int age) {
    		this.m_Age = age;
    	}	
    }
    Can you explain what a abstract method is?.

    hmm check this link
    Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    Here in a abstract class
    Java Code:
    abstract class GraphicObject {
        int x, y;
        ...
        void moveTo(int newX, int newY) {..} <--- normal method.
        abstract void draw(); <----abstract METHOD.
        abstract void resize();
    }
    Person ABSTRACT class ----> should have a tostring abstract method.
    i.e abstract String toString();

    Person class ------> shoulld override that method that is
    i.e
    Java Code:
    public String toString() {
    		String message = "Name: " + name + "\n" + "Age: " + age + "\n";
    		return message;
    	}

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. abstract class help
    By jhering1986 in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 09:18 AM
  2. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  3. abstract class
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 04:11 PM
  4. problem with super.tostring()
    By faw in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 05:27 PM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •