Data Structures and Java Collection Framework - William Collins - Chapter 1 Solutions

CONCEPT EXERCISES
1.1 Given that HourlyEmployee and SalariedEmployee are subclasses of FullTimeEmployee, suppose
we have:
FullTimeEmployee full = new FullTimeEmployee();
HourlyEmployee hourly = new HourlyEmployee ();
SalariedEmployee salaried = new SalariedEmployee ();
full = salaried;
Which one of the following assignments would be legal both at compile-time and at run-time?
a. salaried = (SalariedEmployee) full;
b. salaried = full;
c. salaried = (FullTimeEmployee) full;
d. hourly = (HourlyEmployee) full;
Create a small project to validate your claim. Get the solution of 1.1


1.2 Assume that the classes below are all in the file Polymorphism.java. Determine the output when
the project is run. Would the output be different if the call to println were System.out.println
(a.toString())?
import java.util.*;
public class Polymorphism
{
public static void main (String args [ ])
{
new Polymorphism().run();
} // method main
public void run()
{
Scanner sc = new Scanner (System.in));
A a;
int code = sc.nextInt();
if (code == 0)
a = new A();
else // non-zero int entered
a = new D();
System.out.println (a);
} // method run
} // class Polymorphism

class A
{
public String toString ()
{
return "A";
} // method toString
} // class A
class D extends A
{
public String toString ()
{
return "D";
} // method toString
} // class D  Get the solution of 1.2


1.3 In the Employee class, modify the toString method so that the gross pay is printed with a comma to the
left of the hundreds digit. For example, if the name is “O’Brien,Theresa” and the gross pay is 74400.00, the
toString method will return
O’Brien,Theresa $74,400.00 Get the solution of 1.3


1.4 What can you infer about the identifier out from the following message?
System.out.println ("Eureka!");
What is the complete declaration for the identifier out? Look in java.lang.System.java. Get the solution of 1.4


PROGRAMMING EXERCISES

1.1 Here is a simple class—but with method specifications instead of method definitions—to find the highest age from the ages scanned in:
public class Age
{
protected int highestAge;
/**
* Initializes this Age object.
*
*/
public Age ()
/**
* Returns the highest age of the ages scanned in from the keyboard.

* The sentinel is -1.
*
* @param sc – The Scanner used to scan in the ages.
*
* @return the highest age of the ages scanned in from sc.
*
*/
public int findHighestAge (Scanner sc)
} // class Age
a. Fill in the method definitions for the Age class.
b. Test your Age class by developing a project and running the project. Get the solution of 1.1


1.2 With the Age class in Programming Exercise 1.1.a. as a guide, develop a Salary class to scan in salaries from the input until the sentinel (−1.00) is reached, and to print out the average of those salaries. The average salary is the total of the salaries divided by the number of salaries. Get the solution of 1.2


1.3 This exercise presents an alternative to having protected fields. Modify the FullTimeEmployee class as follows: Change the visibility of the name and grossPay fields from protected to private, and develop public methods to get and set the values of those fields. A method that alters a field in an object is called a mutator, and a method that returns a copy of a field is called an accessor.
Here are the method specifications corresponding to the name field:
/**
* Returns this FullTimeEmployee object’s name.
*
* @return a (reference to a) copy of this FullTimeEmployee object’s
* name.
*
*/
public String getName ()
/**
* Sets this FullTimeEmployee object’s name to a specifed string.
*
* @param nameIn – the String object whose value is assigned to this
* FullTimeEmployee object’s name.
*
*/
public void setName (String nameIn) Get the solution of 1.3


1.4 Create a class to determine which hourly employee in a file received the most overtime pay. The name of the file is to be scanned in from System.in. Get the solution of 1.4


1.5 In the toString() method of the FullTimeEmployee class, there is a call to the format method. The
heading of that method is public final String format(double number)
What is the definition of that method? Get the solution of 1.5