Investigate! Directions: Type each of the following statements into your main method. Test them one-by-one, commenting each one out when you're done with it. For each one, (i) write the output or write "compiler error"; (i) explain why you got the output you did, or why you got an error. System.out.println (mary.getSalary()): System.out.println (jill.getLanguage ()) ; System.out.println (mary.tostring ()): System.out.println (bob.tostring ()); Worker tl - bob; Worker t2 - mary: System.out.printin (t1.getSalary (); System.out.println (t2.getLanguage (); Programmer jane - new Programmer ("Jane", "Python", 47000) ; Worker temp - jane; temp.raisesalary (30000); System.out.println (temp.getsalary ()):

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%
Java
Directions: Create separate classes for each class defined below. Then, you will need to create a
fourth class with a main method to test out the other classes.
public class Worker
private String name;
private double salary;
public Worker (String aName, double sal)
name = aName;
salary = sal;
public String getName ()
return name;
public double getSalary ()
return salary;
public void work ()
System.out.println ("Employee working");
public void raiseSalary (double byAmount)
salary += byAmount;
public String toString ()
return name + " has a yearly salary: " + salary;
public class Programmer extends Worker
private String programmingLanguage;
public Programmer (String aName, String lang, double sal)
{
super (aName, sal);
programmingLanguage = lang;
public String getLanguage ()
{
return programmingLanguage;
public void work ()
System.out.println ("Programmer working");
public String toString ()
{
return "Programmer: " + super.toString () + " and programs in
programmingLanguage;
Transcribed Image Text:Directions: Create separate classes for each class defined below. Then, you will need to create a fourth class with a main method to test out the other classes. public class Worker private String name; private double salary; public Worker (String aName, double sal) name = aName; salary = sal; public String getName () return name; public double getSalary () return salary; public void work () System.out.println ("Employee working"); public void raiseSalary (double byAmount) salary += byAmount; public String toString () return name + " has a yearly salary: " + salary; public class Programmer extends Worker private String programmingLanguage; public Programmer (String aName, String lang, double sal) { super (aName, sal); programmingLanguage = lang; public String getLanguage () { return programmingLanguage; public void work () System.out.println ("Programmer working"); public String toString () { return "Programmer: " + super.toString () + " and programs in programmingLanguage;
public void work ()
System.out.println ("Programmer working");
public String tostring ()
return "Programmer: " + super.tostring () + " and programs in " +
programmingLanguage;
public class Manager extends Worker
public Manager (String aName, double sal)
super (aName, sal);
public void work ()
System.out.println ("Manager working");
public String tostring ()
return "Manager: " + super.tostring () ;
Now, type the following declarations into the main method in your driver class:
Worker bob - new Worker ( "Bob", 15000 );
Programmer mary = new Programmer ( "Mary", "Java", 70000 );
Manager jill = new Manager ( "Jill", 56000 );
Investigate!
Directions: Type each of the following statements into your main method. Test
them one-by-one, commenting each one out when you're done with it.
For each one, (i) write the output or write "compiler error"; (ii) explain why you got
the output you did, or why you got an error.
System.out.println (mary.getSalary ());
System.out.println (jill.getLanguage () );
System.out.println (mary.toString ());
System.out.println (bob.tostring ());
Worker tl - bob;
Worker t2 = mary:
System.out.println (t1.getSalary ());
System.out.println (t2.getLanguage ());
Programmer jane - new Programmer ("Jane", "Python", 47000);
Worker temp = jane;
temp.raisesalary (30000);
System.out.println (temp.getSalary ());
Transcribed Image Text:public void work () System.out.println ("Programmer working"); public String tostring () return "Programmer: " + super.tostring () + " and programs in " + programmingLanguage; public class Manager extends Worker public Manager (String aName, double sal) super (aName, sal); public void work () System.out.println ("Manager working"); public String tostring () return "Manager: " + super.tostring () ; Now, type the following declarations into the main method in your driver class: Worker bob - new Worker ( "Bob", 15000 ); Programmer mary = new Programmer ( "Mary", "Java", 70000 ); Manager jill = new Manager ( "Jill", 56000 ); Investigate! Directions: Type each of the following statements into your main method. Test them one-by-one, commenting each one out when you're done with it. For each one, (i) write the output or write "compiler error"; (ii) explain why you got the output you did, or why you got an error. System.out.println (mary.getSalary ()); System.out.println (jill.getLanguage () ); System.out.println (mary.toString ()); System.out.println (bob.tostring ()); Worker tl - bob; Worker t2 = mary: System.out.println (t1.getSalary ()); System.out.println (t2.getLanguage ()); Programmer jane - new Programmer ("Jane", "Python", 47000); Worker temp = jane; temp.raisesalary (30000); System.out.println (temp.getSalary ());
Expert Solution
Step 1


public class Worker {
 private String name;
 private double salary;

 public Worker(String aName, double sal) {
  name = aName;
  salary = sal;
 }

 public String getName() {
  return name;
 }

 public double getSalary() {
  return salary;
 }

 public void work() {
  System.out.println("Employee working");
 }

 public void raiseSalary(double byAmount) {
  salary += byAmount;
 }

 public String toString() {
  return name + " has a yearly salary: " + salary;
 }

}

 


public class Programmer extends Worker {
 private String programmingLanguage;

 public Programmer(String aName, String lang, double sal) {
  super(aName, sal);
  programmingLanguage = lang;
 }

 public String getLanguage() {
  return programmingLanguage;
 }

 public void work() {
  System.out.println("Programmer working");
 }

 public String toString() {
  return "Programmer: " + super.toString() + " and programs in " + programmingLanguage;
 }
}

 


public class Manager extends Worker {

 public Manager(String aName, double sal) {
  super(aName, sal);
 }

 public void work() {
  System.out.println("Manager working");
 }

 public String toString() {
  return "Manager: " + super.toString();
 }
}

 

 

public class Main {
 public static void main(String[] args) {
  Worker bob = new Worker("Bob", 15000);
  Programmer mary = new Programmer("Mary", "Java", 70000);
  Manager jill = new Manager("Jill", 56000);
  /*
   * Note : before compiling this we need to comment jill.getLanguage() and
   * t2.getLanguage() as there is a compiler time error and the reason is stated
   * below
   * 
   * Output: 70000.0 this is because Mary is an object of class Programmer which
   * is a subclass of Worker Worker class has a method getSalary which Programmer
   * class can use because of inheritance value 70000 was set in the field salary
   * at the time of object creation through constructor using super we passed
   * 70000 but the output is 70000.0 because the data type is double.
   */
  System.out.println(mary.getSalary()); // returns 700000

  /*
   * Output: compiler error
   * 
   * There is a compile time error because jill is an object of class Manager
   * which does not have the method getLanguage. It is present inside class
   * Programmer.
   */
//  System.out.println(jill.getLanguage());

  /*
   * Output: Programmer: Mary has a yearly salary: 70000.0 and programs in Java
   * 
   * First toString() method of Programmer class is called which created a string
   * starting with 'Programmer: ' and ending with ' and programs in Java' it calls
   * the toString() method of its super class Worker which adds the string 'Mary
   * has a yearly salary: 70000.0' in between the earlier two strings.
   */
  System.out.println(mary.toString());
  /*
   * Output: Bob has a yearly salary: 15000.0
   * 
   * toString() method of Worker class is called which created a string 'Bob has a
   * yearly salary: 15000.0'.
   */
  System.out.println(bob.toString());

  Worker t1 = bob;
  Worker t2 = mary;

  /*
   * Output: 15000.0
   * 
   * t1 and bob are pointing to the same address therefore they point to the same
   * object which has the name 'Bob' and salary '15000'
   */
  System.out.println(t1.getSalary());
  /*
   * Output: compiler error
   * 
   * There is a compile time error because although t2 is same as mary which is a
   * programmer t2 is a variable of class Worker and Worker class does not have a
   * method getLanguage(). to make the below line work we can write
   * System.out.println(((Programmer) t2).getLanguage());
   */
//  System.out.println((t2.getLanguage());

  Programmer jane = new Programmer("Jane", "Python", 47000);
  Worker temp = jane;
  temp.raiseSalary(30000);
  /*
   * Output: 77000.0
   * 
   * an Object of class Programmer is created which has salary 47000
   * temp points to this same object
   * using method raiseSalary() from class Programmer salary increased from 47000 to 77000
   * temp using getSalary() method from the parent class Worker prints the salary.
   */
  System.out.println(temp.getSalary());
 }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY