/** The item class -- an item has a name and a price. From here we can * return the values of the attributes, and we also have toString(). */ import java.text.*; public class Item { private String name; private double price; /** Initial value constructor */ public Item(String n, double p) { name = n; price = p; } // The following 2 functions are called "accessor methods" because // they obtain information about the object, namely the value of an // attribute. public String getName() { return name; } public double getPrice() { return price; } /** Print the price followed by the name of the item. The purpose of * the while loop is to right justify the price into a field that is * 8 characters wide. */ public String toString() { NumberFormat f = NumberFormat.getNumberInstance(); f.setMaximumIntegerDigits(4); f.setMinimumIntegerDigits(1); f.setMaximumFractionDigits(2); f.setMinimumFractionDigits(2); String expr = f.format(price); while (expr.length() < 8) expr = " " + expr; return " $ " + expr + " " + name; } }