Solutions Manual: Chapter 16 Big Java, by Cay Horstmann 42
Review Exercises
R16.1
The software life cycle comprises all activities between the initial conception of a software product and its final retirement.
R16.3
Classes correspond to nouns in the task description.
R16.5
In an object-oriented programming language such as Java, a method must belong to a class, namely the class that is responsible for the method.
·
R16.7
The class BMW can inherit from the class Car--every BMW is a car. However, the BMW company (which is different from the class of all BMWs) is an object of the class CarManufacturer. Inheritance is not appropriate for describing the relationship between an object and a class.
R16.9
Coinget value
get name
Purse
add coins / Coin
get total
R16.11
R16.13
Countryget name
get population
get area
get density
Record
keep list of counties / Country
add country to list
get country
RecordReader
reads the file / Record
get largest area
get largest population
get largest density
Country.java / Country.html
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
Class Country
java.lang.Object|
+-Country
public class Country
extends java.lang.Object
A country.
Constructor Summary
Country()
Construct a Country object.
Method Summary
double
getArea()
Gets the area of the country.
double
getDensity()
Gets the density of the country.
java.lang.String
getName()
Gets the name of the country.
double
getPopulation()
Gets the population of the country.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
Country
public Country()Construct a Country object.
Method Detail
getName
public java.lang.String getName()Gets the name of the country.
Returns:
name of country.
getPopulation
public double getPopulation()Gets the population of the country.
Returns:
population of country.
getArea
public double getArea()Gets the area of the country.
Returns:
area of country.
getDensity
public double getDensity()Gets the density of the country.
Returns:
density of country.
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
Record.java / Record.html
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
Class Record
java.lang.Object|
+-Record
public class Record
extends java.lang.Object
A record of countries.
Constructor Summary
Record(Countrycountry)
Construct a Record object.
Method Summary
void
add()
Add a country to the record.
Country
get()
Gets the country of the record.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
Record
public Record(Countrycountry)Construct a Record object.
Parameters:
country - the name of the country
Method Detail
add
public void add()Add a country to the record.
get
public Country get()Gets the country of the record.
Returns:
the country.
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
RecordReader.java / RecordReader.html
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
Class RecordReader
java.lang.Object|
+-RecordReader
public class RecordReader
extends java.lang.Object
A class to read records.
Constructor Summary
RecordReader()
Construct a RecordReader object.
Method Summary
java.lang.String
getLargestArea()
Gets the name of the country with the largest area.
java.lang.String
getLargestDensity()
Gets the name of the country with the largest density.
java.lang.String
getLargestPopulation()
Gets the name of the country with the largest population.
void
readFile(java.lang.Stringfilename)
Reads a file.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
RecordReader
public RecordReader()Construct a RecordReader object.
Method Detail
readFile
public void readFile(java.lang.Stringfilename)throws java.io.IOException
Reads a file.
Parameters:
filename - the name of the file.
getLargestPopulation
public java.lang.String getLargestPopulation()Gets the name of the country with the largest population.
Returns:
country with largest population.
getLargestDensity
public java.lang.String getLargestDensity()Gets the name of the country with the largest density.
Returns:
country with largest density.
getLargestArea
public java.lang.String getLargestArea()Gets the name of the country with the largest area.
Returns:
country with largest area.
Class
Tree
Deprecated
Index
Help
PREV CLASS NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER|FIELD|CONSTR|METHOD
DETAIL: FIELD|CONSTR|METHOD
Programming Exercises
P16.1
Product.java
/**
Describes a product with a description and a price
*/
public class Product
{
/**
Constructs a product from a description and a price.
@param aDescription the product description
@param aPrice the product price
*/
public Product(String aDescription, double aPrice)
{
description = aDescription;
price = aPrice;
}
/**
Gets the product description.
@return the description
*/
public String getDescription()
{
return description;
}
/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
return price;
}
private String description;
private double price;
}
Address.java
/**
Describes a mailing address.
*/
public class Address
{
/**
Constructs a mailing address.
@param aName the recipient name
@param aStreet the street
@param aCity the city
@param aState the 2-letter state code
@param aZip the ZIP postal code
*/
public Address(String aName, String aStreet,
String aCity, String aState, String aZip)
{
name = aName;
street = aStreet;
city = aCity;
state = aState;
zip = aZip;
}
/**
Formats the address.
@return the address as a string with 3 lines
*/
public String format()
{
return name + "\n" + street + "\n"
+ city + ", " + state + " " + zip;
}
private String name;
private String street;
private String city;
private String state;
private String zip;
}
AbstractItem.java
public abstract class AbstractItem
{
/**
Constructs a AbstractItem object.
@param p the product
*/
public AbstractItem(Product p)
{
theProduct = p;
}
/**
Gets the total price.
@return the total price
*/
public abstract double getTotalPrice();
/**
Formats this item.
@return a formatted string of this item
*/
public abstract String format();
/**
Gets the product.
@return the product
*/
public Product getProduct()
{
return theProduct;
}
private Product theProduct;
}
QuantityItem.java
/**
Describes a quantity an article to purchase and its price.
*/
public class QuantityItem extends AbstractItem
{
/**
Constructs an item from the product and quantity
@param aProduct the product
@param aQuantity the item quantity
*/
public QuantityItem(Product aProduct, int aQuantity)
{
super(aProduct);
quantity = aQuantity;
}
/**
Computes the total cost of this item.
@return the total price
*/
public double getTotalPrice()
{
return getProduct().getPrice() * quantity;
}
/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
final int COLUMN_WIDTH = 30;
String description = getProduct().getDescription();
String r = description;
// pad with spaces to fill column
int pad = COLUMN_WIDTH - description.length();
for (int i = 1; i <= pad; i++)
r = r + " ";
r = r + getProduct().getPrice()
+ " " + quantity
+ " " + getTotalPrice();
return r;
}
private int quantity;
}
FixedPriceItem.java
/**
Describes an article to purchase and its price.
*/
public class FixedPriceItem extends AbstractItem
{
/**
Constructs an item from the product
@param aProduct the product
*/
public FixedPriceItem(Product aProduct, double aShippingCharge)
{
super(aProduct);
shipping = aShippingCharge;
}
/**
Computes the total cost of this item.
@return the total price
*/
public double getTotalPrice()
{
return getProduct().getPrice() + shipping;
}
/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
final int COLUMN_WIDTH = 30;
String description = getProduct().getDescription();
String r = description;
// pad with spaces to fill column
int pad = COLUMN_WIDTH - description.length();
for (int i = 1; i <= pad; i++)
r = r + " ";
r = r + getProduct().getPrice()
+ " " + shipping
+ " " + getTotalPrice();
return r;
}
private double shipping;
}
Invoice.java
import java.util.ArrayList;
/**
Describes an invoice for a set of purchased products.
*/
public class Invoice
{
/**
Constructs an invoice.
@param anAddress the billing address
*/
public Invoice(Address anAddress)
{
items = new ArrayList();
billingAddress = anAddress;
header = "";
}
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{
QuantityItem anItem = new QuantityItem(aProduct, quantity);
items.add(anItem);
header = "Qty";
}
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param aShippingCharge the shipping charge of the product
*/
public void add(Product aProduct, double aShippingCharge)
{
FixedPriceItem anItem = new FixedPriceItem(aProduct, aShippingCharge);
items.add(anItem);
header = "Shp";
}
/**
Formats the invoice.
@return the formatted invoice
*/
public String format()
{
String heading = " I N V O I C E\n\n"
+ billingAddress.format();
String r = "\n\nDescription Price " + header + " Total\n";
for (int i = 0; i < items.size(); i++)
{
AbstractItem nextItem = (AbstractItem)items.get(i);
r = r + nextItem.format() + "\n";
}
r = r + "\nAMOUNT DUE: $" + getAmountDue();
return heading + r;
}
/**
Computes the total amount due.
@return the amount due
*/
public double getAmountDue()
{
double amountDue = 0;
for (int i = 0; i < items.size(); i++)
{
AbstractItem nextItem = (AbstractItem)items.get(i);
amountDue = amountDue + nextItem.getTotalPrice();
}
return amountDue;
}
private Address billingAddress;
private ArrayList items;
private String header;
}
InvoiceTest.java
/**
This program tests the invoice classes by printing
a sample invoice.
*/
public class InvoiceTest
{
public static void main(String[] args)
{
Address samsAddress
= new Address("Sam's Small Appliances",
"100 Main Street", "Anytown", "CA", "98765");
Invoice samsInvoice = new Invoice(samsAddress);
samsInvoice.add(new Product("Toaster", 29.95), 3);
samsInvoice.add(new Product("Hair dryer", 24.95), 1);
samsInvoice.add(new Product("Car vacuum", 19.99), 2);
System.out.println(samsInvoice.format());
Address edsAddress
= new Address("Ed's Small Appliances",
"101 Main Street", "Anytown", "CA", "98765");
Invoice edsInvoice = new Invoice(edsAddress);
edsInvoice.add(new Product("Toaster", 24.95), 3.5);
edsInvoice.add(new Product("Hair dryer", 19.95), 1.0);
edsInvoice.add(new Product("Car vacuum", 19.99), 4.5);
System.out.println(edsInvoice.format());
}
}
P16.3
Address.java
/**
Describes a mailing address.
*/
public class Address
{
/**
Constructs a mailing address.
@param aName the recipient name
@param aStreet the street
@param aCity the city
@param aState the 2-letter state code
@param aZip the ZIP postal code
*/
public Address(String aName, String aStreet,
String aCity, String aState, String aZip)
{
name = aName;
street = aStreet;
city = aCity;
state = aState;
zip = aZip;
}
/**
Gets the name of the recipient.
@return the name of the recipient
*/
public String getName()
{
return name;
}
/**
Gets the street.
@return the street
*/
public String getStreet()
{
return street;
}
/**
Gets the city.
@return the city
*/
public String getCity()
{
return city;
}
/**
Gets the state.
@return the state
*/
public String getState()
{
return state;
}
/**
Gets the zip code.
@return the zip code
*/
public String getZip()
{
return zip;
}
private String name;
private String street;
private String city;
private String state;
private String zip;
}
Item.java
/**
Describes a quantity an article to purchase and its price.
*/
public class Item
{
/**
Constructs an item from the product and quantity
@param aProduct the product
@param aQuantity the item quantity
*/
public Item(Product aProduct, int aQuantity)
{
theProduct = aProduct;
quantity = aQuantity;
}
/**
Computes the total cost of this item.
@return the total price
*/
public double getTotalPrice()
{
return theProduct.getPrice() * quantity;
}
/**
Gets the description of the item.
@return the description of the item
*/
public String getDescription()
{
return theProduct.getDescription();
}
/**
Gets the price of the item.
@return the price of the item
*/
public double getPrice()
{
return theProduct.getPrice();
}
/**
Gets the quantity of the item.
@return the quantity of the item
*/
public int getQuantity()
{
return quantity;
}
private int quantity;
private Product theProduct;
}
Product.java
/**
Describes a product with a description and a price
*/
public class Product
{
/**
Constructs a product from a description and a price.
@param aDescription the product description
@param aPrice the product price
*/
public Product(String aDescription, double aPrice)
{
description = aDescription;
price = aPrice;
}
/**
Gets the product description.
@return the description
*/
public String getDescription()
{
return description;
}
/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
return price;
}
private String description;
private double price;
}
Invoice.java
import java.util.ArrayList;
/**
Describes an invoice for a set of purchased products.
*/
public class Invoice
{
/**
Constructs an invoice.
@param anAddress the billing address
*/
public Invoice()
{
items = new ArrayList();
}
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{
Item anItem = new Item(aProduct, quantity);
items.add(anItem);
}
/**
Computes the total amount due.
@return the amount due
*/
public double getAmountDue()
{
double amountDue = 0;
for (int i = 0; i < items.size(); i++)
{
Item nextItem = (Item)items.get(i);
amountDue = amountDue + nextItem.getTotalPrice();
}
return amountDue;
}
/**
Gets an item from the list.
@param i the index
@return the item at the specified index
*/
public Item getItem(int i)
{
return (Item)items.get(i);
}
/**
Gets the size of the list.
@return the size of the list
*/
public int getItemCount()
{
return items.size();
}
private ArrayList items;
}
InvoiceFormatter.java
import java.util.StringTokenizer;
/**
Describes a formatted invoice
*/
public class InvoiceFormatter
{
/**
Constructs an InvoiceFormatter object
@param anInvoice the invoice
@param anAddress the billing address
*/
public InvoiceFormatter(Invoice anInvoice, Address anAddress)
{
invoice = anInvoice;
address = anAddress;
}
/**
Formats the invoice.
@return the formatted invoice
*/
public String invoiceFormat()
{
String r = " I N V O I C E\n\n"
+ address.getName() + "\n"
+ address.getStreet() + "\n"
+ address.getCity() + ", "
+ address.getState() + " "
+ address.getZip() + "\n"
+ "\n\nDescription Price Qty Total\n";