View Single Post
  #2 (permalink)  
Old 08-06-2007, 10:13 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
public class ChairRx extends Item{ // Let the superclass keep these items. // Keep things here that pertain more // specifically to Chair and less to Item, ie, // member variables not shared by Item. // private int chair1; // private float chair1_cost; // private String name; public ChairRx(){ } public ChairRx(int chair1, float chair1_cost, String name){ // Send these values to the superclass. super(chair1, chair1_cost, name); } public String toString() { return "ChairRx[item_number:" + item_number + ", item_cost:" + item_cost + ", manufacturers_name:" + manufacturers_name + "]"; } public static void main(String[] args) { ChairRx chair1 = new ChairRx(1, 10.00f, "Allen"); System.out.println("chair1 = " + chair1); ChairRx chair2 = new ChairRx(2, 25f, "Thomas"); System.out.println("chair2 = " + chair2); System.out.println("chair1.getItemNum = " + chair1.getItemNum()); System.out.println("chair1.getCost = " + chair1.getCost()); System.out.println("chair1.getManufacturersName = " + chair1.getManufacturersName()); } } class Item{ protected int item_number; protected float item_cost; protected String manufacturers_name; public boolean TF; public boolean TF1; /* default constructor */ Item(){ } Item(int item_number, float item_cost, String manufacturers_name){ // Use this way of initializing member variables this.item_number = item_number; this.item_cost = item_cost; this.manufacturers_name = manufacturers_name; // or use this way. No need to use both. // this.setItemNum(item_number); // this.setItemCost(item_cost); // this.setManufacturersName(manufacturers_name); } //////// These methods are inherited by subclasses //////// public boolean isItemnum1(){ return TF; } public boolean isCost1(){ return TF1; } public int getItemNum(){ return item_number; } public float getCost(){ return item_cost; } public String getManufacturersName(){ return manufacturers_name; } public void setItemNum(int newitem_number){ if (newitem_number < 0){ TF = false; item_number = 0; } else { TF = true; item_number = newitem_number; } } public void setItemCost(float newitem_cost){ if (newitem_cost < 0){ TF1 = false; item_cost = 0; } else { TF1 = true; item_cost = newitem_cost; } } public void setManufacturersName(String newmanufacturers_name){ manufacturers_name = newmanufacturers_name; } }
Reply With Quote