This is the question:
public class Postman {
private String Name;
private int Age;
}
How you can update and retrieve the private variables Name and Age from another class Worker.
public class Worker {
//An object a is created from class Postman
And this is what i came up with.
//Postman.java
public class Postman{
private String name;
private int age;
}
//constructor
public Postman(String name, int age){
this.name=name;
this.age=age;
}
//accessors
public String getName(){
return name;
}
public int getAge(){
return age;
}
//worker.java
public class worker{
public static void main(String args[]){
Postman employee = new employee("Tom the postman", 55);
System.out.println(employee.getName());
System.out.println(employee.getAge());
}
}
But when i build them, i keep getting class, interface, or enum expected. Why is that?
thanks.
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Where is:
Postman employee = new employee("Tom the postman", 55);
must be:
Postman employee = new Postman("Tom the postman", 55);
Also: the constructor Postman (String, int) and methods getName() and getAge() must be inside class Postman, but they are not inside it.
I 'think' this assignment s/b...
class Worker extends Postman {
public Worker( String n, int a ) {
age = a;
name = n;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String toString() {
return "Name: " + name + ", age: " + age;
}
public class TestEmployees {
public static void main( String [] args ) {
Worker tom = new Worker( "Tom South", 55);
Worker jerry = new Worker("Jerry White", 44);
System.out.println( tom);
System.out.println( jerry );
System.out.println("Age of " + jerry.getName() + ": " + jerry.getAge() );
}
}