Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

java derived program


  • Please log in to reply

#1
Luke1988

Luke1988

    Member

  • Member
  • PipPip
  • 37 posts
hey. i am trying to derive a student class from my a person class. i cant seem to get past 2 compile errors. these are:

personStudent.java:6: cannot find symbol
symbol : constructor Person(java.lang.String,int)
location: class Person
super(theName, theAge);
^



personStudent.java:6: cannot find symbol
symbol : constructor Person(java.lang.String,int)
location: class Person
super(theName, theAge);
^



the code i have written for the two programs is below...any help would be much appreciated.

thanks **Luke**

PERSON CLASS

[codebox]
public class Person
{
protected String name;
protected int age;

public void setName (String theName)
{
name = theName;
}

public void setAge (int theAge)
{
age = theAge;
}

public String getName ()
{
return name;
}

public int getAge ()
{
return age;
}

public void displayDetails ()
{
System.out.println("Name is : " + name );
System.out.println("Age is : " + age );
}
}
[/codebox]


STUDENT_PERSON CLASS

[codebox]
public class personStudent extends Person
{
private int studentNo ;
public personStudent (String theName, int theAge, int num)
{
super(theName, theAge);
studentNo = num;
}
public int getStudentNo()
{
return studentNo;
}

public int setStudentNo()
{
studentNo = num;
}

public void displayDetails ()
{
super.displayDetails();
System.out.println("Student Number Is: " + studentNo);
}
}
[/codebox]


TEST CLASS

[codebox]
public class TestPerson
{
public static void main(String []args)
{
System.out.println("Testing Person class");
System.out.println("---------------------------------------");

// instantiating the class
// constructing an object of type Person
Person Ryan = new Person();


Person Luke = new Person();
Luke.setName("Luke");
Luke.setAge(19);
Luke.setStudentNo(776);

// using the set methods
Ryan.setName ("Ryan") ;
Ryan.setAge (53) ;

Ryan.displayDetails () ;
System.out.println();
Luke.displayDetails();

}
}
[/codebox]
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
Hi Luke,

Do you still need help?

Please let us know what progress you have made.

bdlt
  • 0

#3
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
As far as i can see, the problem is here:

super(theName, theAge);
You are calling this from the personStudent class, which extends Person.
Calling super would call the constructor for Person, passing those parameters in.
The problem is, you haven't set a constructor for Person with those parameters.

So, if you added this constructor to your Person class:
public Person(String theName, theAge)
{
	name = theName;
	age = theAge;
}
Then calling ther super constructor like you did should work.

Or, instead of calling your super constructor, you called use you setter methods of the person class:
public personStudent (String theName, int theAge, int num)
{
setName(theName);
setAge(theAge);

studentNo = num;
}
By using this as your constructor in the PersonStudent class.

Hope that solves your problem, any questions just ask

jpshortstuff
  • 0

#4
Luke1988

Luke1988

    Member

  • Topic Starter
  • Member
  • PipPip
  • 37 posts
thanks for your help jpshortstuff :whistling:
  • 0

#5
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
not a problem, glad i could be of assistance :whistling:

hope its all running smoothly now
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP