10 Replies - 1107 Views - Last Post: 25 May 2009 - 05:09 AM Rate Topic: -----

#1 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Help with Inheritance

Post icon  Posted 24 May 2009 - 10:54 PM

Hey everyone!

I am in serious need of help in this program that i am doing. It's actually inheritance. Pretty much this is what the assignment says:

Create a class called person that contains the common information. It has three private instance variables:
- name(String)
-unique-id(String)
- year-of-birth (int)
- gender (char)

A constructor should be specified that accepts name as a parameter ie every person object created must have a name.

Getter and setter methods should be specified for each instance variable ie getName, setName, getBirthYear, setBirthYear, getGender, setGender, setUniqueId, getUniqueId.

Write a method that will calculate and return the age of the person. 2009 - birthyear = age

This is what i have so far with it..

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person
{
	private String theName;
	private String uniqueID;
	private int dateofbirth;
	private char gender;

	
	public Person(Person personObject)
	{
		personObject.setName(personObject.theName);
		personObject.setUniqueid(personObject.uniqueID);
		personObject.setBirthYear(personObject.dateofbirth);
		personObject.setGender(personObject.gender);
	}
	
	public Person(String someName, String theID, int birthYear, char theGender)
	{
		setName(someName);
		setUniqueid(theID);
		setBirthYear(birthYear);
		setGender(theGender);
	}
	
	public void setName(String personName)
	{
		theName=personName;
	}
	
	public void setUniqueid(String personID)
	{
		uniqueID=personID;
	}
	
	public void setBirthYear(int personBirth)
	{
		dateofbirth= personBirth;
		
	}
	
	public void setGender(char personGender)
	{
		gender= personGender;
	}
	
	private String getName()
	{
		return theName;
	}
	
	private String getUniqueid()
	{
		return uniqueID;
	}
	
	private int getBirthYear()
	{
		return dateofbirth;
	}
	
	private char getGender()
	{
		return gender;
	}
	
	private int getAge()
	{
		return (2009-dateofbirth);
	}
}
	
	
	


the program compiles perfectly fine.. however i am not sure if what i did seems correct because next it tells me:

Create a Student class. It should be a subclass of the Person class ie extend the Person class and therefore inherit its instance variables and methods. This class should define the following instance variables:

-School (String) eg what school are they from? (School of computing etc etc)
-Year(int) eg for 1st year or 2nd year
-average(double) eg average mark for the subjects

The constructor should accept a name and invoke the super class constructor, passing name to it. Getter and setter methods should be specified for each sub class instance variables.

This is my Student class as i have so far:

/**
 * Write a description of class Student here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Student extends Person
{
	
	private String theSchool;
	private int thesemYear;
	private double theAverage;
	
	public Student(String someName, String theID, int birthYear, char theGender)
	{
		super(someName, theID, birthYear, theGender);
	}
	
	public void setSchool(String School)
	{
		theSchool= School;
	}
	
	public void setSemYear(int semYear)
	{
		thesemYear= semYear;
	}
	public void settheAverage(double Average)
	{
		theAverage= Average;
	}
	


I am again not sure about how to work this out... i have initiated setters and getters but im not sure about the inheritance... help!! where am i going wrong? what do i next?? anything to add here?

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Inheritance

#2 kinjaldarji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 12:16 AM

Hi,

This is perfectly fine. You have already achieved inheritance. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. You can probably read more about inheritance on some good java website.

In your program, what you need to prepare is one argument constructor that takes only person's name and the same has to be called from your subclass student using super().

Now, if you also need to access theName from your subclass you need to declare it protected.

I think this will help.. :)
Was This Post Helpful? 0
  • +
  • -

#3 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 12:49 AM

Hi kinjal, thanks for your reply! :D i just had a couple more questions regarding this..

I have changed my code as below... now this is the Person class

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person
{
	protected String theName;
	protected String uniqueID;
	protected int dateofbirth;
	protected char gender;
		
	   public Person()
	   {
		 }

	public Person(String someName, String theID, int birthYear, char theGender)
	{
		theName=someName;
		theID= uniqueID;
		dateofbirth=birthYear;
		gender= theGender;
	 
	}
	
	public void setName(String personName)
	{
		theName=personName;
	}
	
	public void setUniqueid(String personID)
	{
		uniqueID=personID;
	}
	
	public void setBirthYear(int personBirth)
	{
		dateofbirth= personBirth;
		
	}
	
	public void setGender(char personGender)
	{
		gender= personGender;
	}
	
	private String getName()
	{
		return theName;
	}
	
	private String getUniqueid()
	{
		return uniqueID;
	}
	
	private int getBirthYear()
	{
		return dateofbirth;
	}
	
	private char getGender()
	{
		return gender;
	}
	
	private int getAge()
	{
		return (2009-dateofbirth);
	}
}
	
	
	



This is the Student subclass...

/**
 * Write a description of class Student here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Student extends Person
{
	
	private String theSchool;
	private int thesemYear;
	private double theAverage;
	
		public Student()
		{
		 super();
		 }  
		 
	public Student(String someName, String theID, int birthYear, char theGender, String School, int semYear, double Average)
	{
		super(someName, theID, birthYear, theGender);
		theSchool=School;
		thesemYear=semYear;
		theAverage= Average;
		
	}
	
	public void setSchool(String School)
	{
		theSchool= School;
	}
	
	public void setSemYear(int semYear)
	{
		thesemYear= semYear;
	}
	public void settheAverage(double Average)
	{
		theAverage= Average;
	}
	
	public String gettheSchool()
	{
		return theSchool;
	}
	
	public int getSemYear()
	{
		return thesemYear;
	}
	
	public double gettheAverage()
	{
		return theAverage;
	}
}



I was wondering if these are alright now? Because i have to create a driver program... it should Create a student object with my name in it.. say "Joe Mahoney" ..

it should set the student's year of birth
set the gender
set the school
set the year
set the average
and print out all the information...

Any ideas of how i can go about achieving this?

This is what i have so far with the driver program..

whenever i put "Joe mahoney" inside the object... i get an error which doesn't recognize the constructor "cannot find symbol- constructor Student().. why is that so? How do i enter my name in Student stud= new Student ("joe")??

Is there something i am missing??


public class DriverProgram
{
	public static void main (String[]args)
	{
		Student stud= new Student(); 
		
		stud.setUniqueid("1003221");
		stud.setBirthYear(1988);
		stud.setGender(M);
		stud.setSemYear(1);
		stud.setSchool("WAAPA");
		stud.settheAverage(90);
		
	}
}


Someone help please =(

This post has been edited by Markus110: 25 May 2009 - 02:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 kinjaldarji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:16 AM

Hi,

See, what is happening here is that, you have created your own constructor of student which takes few arguements. Now, once you define your own constructor you have to use that one only. You can't have default constructor generated by JVM any more. So if you want to make student() work, you need to add a comstructor like student() {} in student class.
Was This Post Helpful? 0
  • +
  • -

#5 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:22 AM

Hi Kinjal, thanks for your kind reply again... however, im a bit lost as to what you're saying... can you point out how i can go about doing this? im sorry im only a beginner =(
Was This Post Helpful? 0
  • +
  • -

#6 kinjaldarji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:29 AM

Hey,

You will require one default constructor in Person class too...

Hello,

Sorry...

well you need to define student constructor like

public Student(String someName)
{
super(someName);
}

and in your person class you need to have a constructor like


public Person(String someName)
{
theName=someName;
}
Was This Post Helpful? 0
  • +
  • -

#7 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:31 AM

omg thank you so much kinjal! i appreciate it! i just had one last question though, is there a reason why the setGender(m) is not recognized? i have declared gender as a char...but there seems to be a problem there... do you know why??
Was This Post Helpful? 0
  • +
  • -

#8 kinjaldarji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:42 AM

when you want to assign a character value you have to enclose it in single quote.

setGander('M') will do in your case.
Was This Post Helpful? 0
  • +
  • -

#9 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 02:47 AM

VERy much thanks Kinjal! you have been of great help! I appreciate it! :) now i know that you have to initiaet a defualt constructor in case of inheritance..
Was This Post Helpful? 0
  • +
  • -

#10 kinjaldarji  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 25-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 03:12 AM

hey mark... welcome..
Was This Post Helpful? 0
  • +
  • -

#11 Markus110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-May 09

Re: Help with Inheritance

Posted 25 May 2009 - 05:09 AM

HI Kinjal , thanks again for your kind help! =) ... i just had one series of last questions if you could assist me in...

as you know whatever i've done above works thanks to you... however, now i have to create another class called Staff and this inherits from the Person class... so it'd be like public class staff extends person..

However, in this staff class i have to have instance variables of roomNumber(double), Title(string), and subjectsTaught(an array of String ojects)... im not sure how i can use the array of string objects in this one... this is what i had so far...

public class Staff extends Person
{
	private double roomNumber;
	private String Title;
	String []subjects= {"Math", "Science", "Reading", "English"};
}



Is this correct? I mean of course, i don't get why it's actually having that "cannot find symbol - constructor person().. is it because i havent declared it there? how do i implement an array of string objects in this/?

I hope you can help me out or guide me...

Thanks again kinjal!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1