Polymorphism issue - java

I have been reading on how to program Java 8 Polymorphism.
I have this code:
public class Person
{
// instance variables - replace the example below with your own
private String lastname;
private String firstname;
private int age;
private boolean married;
private float salary;
/**
* Constructor for objects of class Person
*/
public Person(String lastname, String firstname, int age, boolean married, float salary)
{
// initialise instance variables
this.lastname = new String(lastname);
this.firstname = new String(firstname);
this.age = age;
this.married = married;
this.salary = salary;
}
public String getLastName() {return lastname;}
public String getFirstName() {return firstname;}
public int getAge() {return age;}
public boolean isMarried() {return married;}
public float getSalary() {return salary;}
}
public class MarriedPerson extends Person
{
// instance variables - replace the example below with your own
private int children;
/**
* Constructor for objects of class MarriedPerson
*/
public MarriedPerson(String lastname, String firstname, int age, float salary, int children)
{
// initialise instance variables
super(lastname, firstname, age, true, salary);
this.children = children;
}
public int getNoOfChildren()
{
// put your code here
return children;
}
}
abstract class MyTester
{
public static void main(String[] args) {
Person p1 = new Person("Kings", "Paul", 22, true, 1200f);
MarriedPerson mp1 = new MarriedPerson("Tront", "Betty", 31, 980.5f, 3);
System.out.print(p1.getFirstName()+" "+p1.getLastName()+" is "
+p1.getAge()+" years old, gets a "+p1.getSalary()
+" Euros salary and is");
if (p1.isMarried() == false)
System.out.print(" not");
System.out.println(" married.");
System.out.print(mp1.getFirstName()+" "+mp1.getLastName()
+" is " +mp1.getAge()+ " years old, gets a " + mp1.getSalary()
+" Euros salary and is" + " married with ");
if (mp1.getNoOfChildren() > 0)
System.out.print(mp1.getNoOfChildren());
else System.out.print("no");
System.out.println(" children.");
}
}
Reading from a book, I have not seen an abstract class that contains the main function so I am a bit confused.
Why have we declared MyTester class as abstract? Is this necessary?
I have now created a printInfo method in class Person. See following code:
public class Person
{
// instance variables - replace the example below with your own
private String lastname;
private String firstname;
private int age;
private boolean married;
private float salary;
/**
* Constructor for objects of class Person
*/
public Person(String lastname, String firstname, int age, boolean married, float salary)
{
// initialise instance variables
this.lastname = new String(lastname);
this.firstname = new String(firstname);
this.age = age;
this.married = married;
this.salary = salary;
}
public String getLastName() {return lastname;}
public String getFirstName() {return firstname;}
public int getAge() {return age;}
public boolean isMarried() {return married;}
public float getSalary() {return salary;}
public void printInfo(){
System.out.print(p1.getFirstName()+" "+p1.getLastName()+" is "
+p1.getAge()+" years old, gets a "+p1.getSalary()
+" Euros salary and is");
if (p1.isMarried() == false)
System.out.print(" not");
System.out.println(" married.");
}
}
abstract class MyTester
{
public static void main(String[] args) {
Person p1 = new Person("Kings", "Paul", 22, true, 1200f);
MarriedPerson mp1 = new MarriedPerson("Tront", "Betty", 31, 980.5f, 3);
p1.printInfo();
mp1.printInfo();
}
}
When I compile the modified code it give me the error: cannot find symbol - variable p1. Why is that? What do I need to do in order to fix that?
What advantages second code has over first one?

It does not make sense MyTester to be abstract since it does not have any abstract method. The only reason might be to prevent the instantiation of MyTester class because it might not make sense for an instance to exist.

MyTester is declared abstract so you can't create any instances of it (there is no reason you would want to). See this link, it says:
An abstract class is a class that is declared abstract—it may or may
not include abstract methods. Abstract classes cannot be instantiated,
but they can be subclassed.

When I compile the modified code it give me the error: cannot find symbol - variable p1. Why is that? What do I need to do in order to fix that?
For future reference: You should create a new question and not edit your existing one. Also, if someone solved your first problem you should allways be so nice as to accept their answer. ;)
Anyway: You are getting that error because your object is only "known" as p1 in your main class.
In your "printInfo()" method you are trying to have the objects do calls on itself. So either just remove the "p1." part and just call the methods like "getFirstName()", or explicitly call "this.getFirstName()".
("this" is a java keyword refering to the current object instance.)
A tipp for you: Get a good JAVA IDE like Eclipse (https://eclipse.org/) and you will see simple errors like those even before compiling.

No. It makes no difference, as the class is never extended or used except as an entrypoint.

Related

If I create an array of the parental class, how do I access a method from the sub class through the array object?

I have a program I am working with to help me practice my coding skills. The program has the following scenario: there is a classroom of 20 students, where the record is taken of the students' names, surnames, and age. Half of these students take part in the school's athletics. Here, record is kept of their races that they have done and the ones they've won.
In this program, I have three classes:
runStudents - class with main method
Students (String name, String surname, int age) - parental class
AthleticStudents (String name, String surname, int age, int races, int victories) - sub class
The user should be able to add another race (and win) to the object. As seen by the code provided, an Array is created to store the 20 Students objects. I have to be able to access a method to alter the object in the array, but this method is not in the parental class (the class the objects are created from.
public class Students
{
private String name;
private String surname;
private int age;
public Students()
{
}
public Students(String name, String surname, int age)
{
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName()
{
return this.name;
}
public String getSurname()
{
return this.surname;
}
public double getAge()
{
return this.age;
}
public void setName(String name)
{
this.name = name;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return String.format("name\t\t: %s\nsurname\t\t: %s\nage\t\t: %s",
this.name, this.surname, this.age);
}
}
public class AthleticStudents extends Students
{
private int races;
private int victories;
public AthleticStudents()
{
}
public AthleticStudents(String name, String surname, int age, int
races, int victories)
{
super(name, surname, age);
this.races = races;
this.victories = victories;
}
public int getRaces()
{
return this.races;
}
public int getVictories()
{
return this.victories;
}
public void setRaces(int races)
{
this.races = races;
}
public void setVictories(int victories)
{
this.victories = victories;
}
public void anotherRace()
{
this.races = this.races + 1;
}
public void anotherWin()
{
this.victories = this.victories + 1;
}
public String toString()
{
return super.toString() + String.format("\nnumber of races\t:
%s\nnumber of wins\t: %s", this.races, this.victories);
}
}
public class runStudents
{
public static void main(String[]args)
{
Students[] myStudents = new Students[20];
myStudents[0] = new Students("John", "Richards", 15);
myStudents[1] = new AthleticStudents("Eva", "Grey", 14, 3, 1);
myStudents[2] = new Students("Lena", "Brie", 15);
for (int i = 0; i < 3; i++)
System.out.println(myStudents[i].toString() + "\n\n");
}
}
I want to be able to do the following:
AthleticStudents[1].anotherRace();
but cannot do so as the array object is derived from the parental class, and I declared the method in the sub class. How can I link the two?
I assume that you create an array of the parent class instances. Just cast the instance this way (you better check whether the element is the instance of a subclass):
if (AthleticStudents[1] instanceof AthleticStudents)
((AthleticStudents) AthleticStudents[1]).anotherRace();
I'm not sure if this is exactly what you're looking for but it worked well for me. Instead of trying to access AthleticStudents method anotherRace() like that, try this in your main method.
Students[] myStudents = new Students[20];
myStudents[0] = new Students("John", "Richards", 15);
myStudents[1] = new AthleticStudents("Eva", "Grey", 14, 3, 1);
myStudents[2] = new Students("Lena", "Brie", 15);
AthleticStudents addRace= (AthleticStudents)myStudents[1];
addRace.anotherRace(); //This will increment Eva's race count to 4
for (int i = 0; i < 3; i++)
System.out.println(myStudents[i].toString() + "\n\n");
All I did was cast the element into an object AthleticStudents named 'addRace'. By casting myStudents[1] to this new object you are able to access all of AthleticStudents methods.
I just saw the other answer posted which works just as well!
Hope this helps!
I’m not sure that i understand your question, because you are a bit inconsistent with your capitalization. runStudents is a class, while AthleticStudents is both a class and an array. But i’ll try.
IF i did understand your question, you have an array Student[] studentArray. Some Student objects in studentArray are AthleticStudents, others are not. You have a specific AthleticStudent eva which is in studentArray[] having let’s say index 1, and you want to add to her anotherRace(). Your call studentArray[1].anotherRace does not compile because the compiler treats that element as a Student and not as a AthleticStudent.
The trick is to cast the element to AthleticStudent. I omit the test of the element of being really an AthleticStudent; you will have to do that test in your code.
((AthleticStudent) studentArray[1]).anotherRace();

Variable within constructor is returning null?

I am doing a project based around the concepts of inheritance and have created a super constructor which has two variables within itself (String, int), this super constructor is then called within a sub constructor that inherited the super constructors class. I then use two methods to return the properties of those variables within the constructors. The age property is outputting fine but the String property is returning null. Here's the code:
Animal super-class
abstract public class Animal
{
int age;
String name;
Animal(String name, int age)
{
this.age = age;
this.name = name;
}
Animal()
{
this("newborn", 0);
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
}
Wolf sub-class
public class Wolf extends Carnivore
{
String name;
int age;
Wolf(String name, int age)
{
this.name = name;
this.age = age;
}
Wolf()
{
super();
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
Main method class
public class Main {
public static void main(String[] args)
{
Wolf newWolf = new Wolf();
System.out.println("Name = " + newWolf.getName());
System.out.println("Age = " + newWolf.getAge());
}
}
Age is returning as 0 which is correct but System.out.println("Name = " + newWolf.getName()); seems to be returning null instead of "newborn". Any help on resolving this issue is appreciated thanks.
Update - I need the getName() method for another constructor that I haven't included in this example so is there a way to have them both exist?
The issue here is that you are defining your fields in the sub-class, you don't need to as they are inherited from the parent.
Your class has two sets of fields, one from the super (these are the ones set by your constructor, which is calling super() and the other from the child class (these are the ones returned by your getters, which are not initialized. the zero is int's default, not set either).
So simply remove the fields definition from the child class

Drive Tester for a Class Issues

I have been putting together a Drive Tester to test a class I have been working on for an assignment, but I have hit a dead end with the tester and I am not sure how to finish it up and remove any errors.
Here is the tester:
public class PersonTester
{
public static void main(String[] args)
{
System.out.println("PersonClassTester");
System.out.println("*****************");
System.out.println("");
Person joeSmith = new Person();
String "smith" = joeSmith.setSurName(); // All these statements with set surname and forname etc are apperantly not statements and require a semi-colon, even though they are there.
String "joe" = joeSmith.setForName();
int 25 = joeSmith.setAge();
double 1.57 = joeSmith.setHeight();
String "male" = joeSmith.setGender();
joeSmith.toString();
joeSmith.format();
}
}
The main issue with this is that the Netbeans client is stating that the setter statements highlighted are not actually statements, and are saying that it needs a semi-colon for each of them despite them actually being there. It is also saying that there are no formal or actual arguments. I know what they are but I'm getting confused on them regardless.
And this is the class I need to run through the tester:
public class Person
{
private String surName;
private String forName;
private int age;
private double height;
private String gender;
#Override
public String toString()
{
return getClass().getName() + "[surName= " + surName + " forName= " + forName + " age= " + age + " height= " + height + " gender " + gender + "]";
}
public void format()
{
System.out.format("%10s%10s%10d%10f%10s", surName, forName, age, height, gender);
}
public String getSurName()
{
return surName;
}
public String getForName()
{
return forName;
}
public int getAge()
{
return age;
}
public double getHeight()
{
return height;
}
public String getGender()
{
return gender;
}
public void setSurName(String surName)
{
this.surName = surName;
}
public void setForName(String forName)
{
this.forName = forName;
}
public void setAge(int age)
{
this.age = age;
}
public void setHeight(double height)
{
this.height = height;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
Any advice on getting the class tester to function properly? Once the tester works the rest of the assignment shouldnt be much of a problem.
Edit: The program compiles, but is unable to print the String statements.
PersonClassTester
*****************
surName forName 25 1.570000 gender
String "smith" = joeSmith.setSurName(); is not correct syntax. It should look like this:
joeSmith.setSurName("smith")
This tells Java to execute the method setSurName() on the object joeSmith, with the given string as an argument.
The same goes for the rest of your assignments in main.
Your setter method looks like:
public void setSurName(String surName)
{
this.surName = surName;
}
Which says your setter is not going to return anything and it expects one parameter which is of String type.
Now here's how you are using your setter method:
String "smith" = joeSmith.setSurName();
So here it means you are expecting a surname from setter which is one part of compiler error that you see. And as stated, it expects a string argument and you are not passing it and that's another part of compiler issue.
So you may want to change it to:
joeSmith.setSurName("smith");//similar changes with other setter method.
Which means, now you are passing string argument and not expecting anything in return by calling this method and hence Compiler would be happy with this.

Objected Oriented Programming- How to use the toString() method and format()?

I have just started doing object oriented programming as part of my course, but I am struggling with it, specifically the toString method in a Person Class. I need to write a toString() method to display the contents of instance variables.
I need to by sample print out:
Person[forName=joe, surname= smith, age= 25, height= 1.57, gender= male]
I also need to format it like this using the format method:
smith joe 25 1.57 male
davis sian 18 1.73 female
*** *** *** *** ***
I havent written a tester yet, but here is what I have written so far for the class and now I'm stuck, I'm not even sure if I am getting the toString statement wrong. I am using netBeans for this:
public class Person
{
private String surname;
private String forname;
private int age;
private double height;
private String gender;
public String toString()
{
return getClass().getName() + "[surname= " + surname + " forname= " + forname + " age= " + age + " height= " + height + " gender " + gender + "]";
}
}
What I need to do is make a class called Person that I can test. It needs to be able to hold the five variables above (surname etc) for different people. I need to be able to print out each of the instance variables with a toString() method and to use a format() method to produce a string with formatting infomation in order for the string printed out by the toString() method to be formatted like the second quotation.
Am I on the right track and regardless, how can I work through this?
EDIT: I have looked at the Person Class and have done what I can with it, does it seem decent enough? I am going to try and get a PersonTester together.
public class Person
{
private String surName;
private String forName;
private int age;
private double height;
private String gender;
#Override
public String toString()
{
return getClass().getName() + "[surName= " + surName + " forName= " + forName + " age= " + age + " height= " + height + " gender " + gender + "]";
}
public void format()
{
System.out.format("%10s%10s%10d%10f%10s", "surName", "forName", age, height, "gender");
}
public String getSurName()
{
return surName;
}
public String getForName()
{
return forName;
}
public int getAge()
{
return age;
}
public double getHeight()
{
return height;
}
public String getGender()
{
return gender;
}
public void setSurName(String surName)
{
this.surName = surName;
}
public void setForName(String forName)
{
this.forName = surName;
}
public void setAge(int age)
{
this.age = age;
}
public void setHeight(double height)
{
this.height = height;
}
public void setGender(String height)
{
this.gender = gender;
}
}
EDIT 2: Started on a class Tester, but I am running into errors again about the setter's not having a ; and not being a statement.
Here's the tester so far:
public class PersonTester
{
public static void main(String[] args)
{
System.out.println("PersonClassTester");
System.out.println("*****************");
System.out.println("");
Person joeSmith = new Person();
String "smith" = joeSmith.setSurName();
String "joe" = joeSmith.setForName();
int 25 = joeSmith.setAge();
double 1.57 = joeSmith.setHeight();
String "male" = joeSmith.setGender();
joeSmith.toString();
joeSmith.format();
}
}
First of all you have to noticed that every object you create extends class Object. This Object class contains methods like toString, equals, hashCode...
your object have also this methods(inherited from Object). When you override (you should annotate this method with #Override) for eg. toString you will always use this toString method instead of inherited one. Its called polymorphism. Your toString method looks fine. In your main method you should use some kind of loop through all Persons and there format the output from toString method.
You have error in your code
public String toString(); {
remove the ; after ()
public class Main {
public static void main(String[] args) {
Person a = new Person("smith", "joe", 25, 1.57, "male");
Person b = new Person("davis", "sian", 18, 1.73, "female");
List<Person> persons = new ArrayList<Person>();
persons.add(a);
persons.add(b);
for(Person p : persons){
System.out.format("%s %s %s %d %.2f %s", p.getClass().getName(), p.getSurname(), p.getForname(), p.getAge(), p.getHeight(), p.getGender());
System.out.println();
}
}
}
If you call this
System.out.println(p.toString());
than you ll get your person via toString method.
I just edit your Person class and add constructor and geters + seters
public Person(String surname, String forname, int age, double height,
String gender) {
super();
this.surname = surname;
this.forname = forname;
this.age = age;
this.height = height;
this.gender = gender;
}
Here is geter and seter sample.
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
As already mentioned your toString() is fine.
Please note that the toString() method and the format() method are IMO supposed to work independently as they do serve different purposes.
I suggest to put the format method not in the person class (or at least make it static method). This is because a single Person instance has not enough information for it to be printed in the table format. It at least needs to know the column widths. Otherwise you could end up with something like this:
smith joe 25 1.57 male
someVeryLongFirstName sian 18 1.73 female
*** *** *** *** ***
So the format method should take a list of persons that should be printed out and then first calculate the column widths. After this is done you then just pad the property value to the column width and print this out.
You are on the right track:
Inside of the Person class you need to add public methods for each private variable to set the data:
public void setAge(int age) {
this.age = age;
}
Then you can create a Person object in your main class and set his age:
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setAge(15);
}
}
As an alternative, you can use a constructor inside of your Person class to set your object's variables:
public Person(String surname, int age) {
this.surname = surname;
this.age = age;
}
And create the object in the main method like this:
Person p = new Person("Nillas", 25);
You can always run your toString() method in the main class after you've created the object and see the result:
System.out.println(p.toString());
Hope this helps, good luck!

Display class objects from a class method

I've created class Person, which is extended by classes Student and Employee (which is extended by other Employee type classes). The person class looks like:
String name;
int ssn;
int age;
String gender;
String address;
String PNumber;
static int count;
//empty constructor
public Person(){
count++;
}
//print count
public static void printCount(){
System.out.println("The number of people is: "+ count);
}
//constructor with name
public Person(String name){
this.name = name;
count++;
}
/*constructor to create default person object*/
public Person(String name, int ssn, int age, String gender, String address, String PNumber)
{
this.name = name;
this.ssn = ssn;
this.age = age;
this.gender = gender;
this.address = address;
this.PNumber = PNumber;
count++;
}
I'm currently trying to create a method that will display all Persons if they're gender = "Male". I have:
//display Males
public void print(String gender){
if(this.gender.contentEquals(gender)){
//print out person objects that meet this if statement
}
}
I'm not sure how to refer to the objects (students and employees that are all persons) within the method to return them. And I also don't know how to refer to this method in the main method. I can't use Person.print, but if I use
Person james = new Person();
and then use
james.print("Males");
I'm only returning james (and the method doesn't make sense in that context).
Any help appreciated.
First, the print method should be made into a static method. It is independent of each individual Person object, so making it static will allow you to call it in the main method as
Person.print("Male");
To refer to Person objects in the print method, you will need to pass it a collection of Person objects as a parameter. You should keep all instances of Person in an array and pass that into the print method when you call it. Then the print method can be
public static void print(String gender, Person[] people) {
for(Person x : people)
if (x.gender.equals(gender))
//print the person
}
With this modification you should call it from the main method as
Person.print("Male", people);
where people is the array you keep all Person objects in.

Categories