4/4/2012CS 110 Exam 3Page 4

Computer Science I

Spring 2012, Wednesday, 4/4/12

100 points

Name ______Key______

1.  Fill in the blanks for a class Item that represents basic information about any item for sale in a grocery store.

The instance variables of an Item object are:

·  the name of the item

·  the UPC of the item (Universal Product Code)

·  the price of the item

·  The weight of the item in pounds, which can be fractional. If the weight is 0, then the price represents the price per pound, and the actual price would then have to be computed when an actual weight is provided on check out (from the scales at the register)

The methods are able to

·  “get” each instance variable

·  change the name

·  change the price

·  compute a price based on a provided weight.

[35 pts]

public class Item {

private String name;

_private_ _String__ upc; //composed of digits but no computation occurs

private _double__ price; //in lbs.

private __double_ weight;

//constructor

public ___Item___ (String nm, _ String _ u, double pr, double wt){

__name___ = nm;

upc = _u_;

__price_____ = pr;

weight = __wt_;

}

//accessors

public __ String __ getName() {return ___ name ___;}

public __ String ___ getUPC() {return __upc______;}

public ___ double ___ getWeight() {return __weight__;}

public __ double ___ getPrice() {return ___ price ___;}

public __ double ____ getPrice( _ double _ wt) //calculated price

{return _ price * ___wt___ ;}

//mutators and toString

_public_ void setPrice(__double ___ pr) { __ price __ = pr;}

_ public __void_ setName( ______nm) { ______= ______;}

_ public _ String__ toString(){//report back all object contents

return “Item:”+_getName() + “ UPC:”+ getUPC() + “wt=”+_

_____getWeight() + “price=$”+getPrice()___;}}

2.  Show what is printed from the following application segment that uses the Item class from question #1.

[15 pts]

Item b1 = new Item (“Milk 1%”,”1234500001”,3.95,8.0);

Item b2 = new Item (“Bananas”,”1234510335”, 0.59, 0); //is weighed

Item b3 = new Item (“Corn Pops”,”5432139482”, 3.45, 0.87);

Item b4 = b2;

System.out.println(b1.getName()+” costs $“+b1.getPrice());

System.out.println(b2.getName()+” costs $“+b2.getPrice(2.0));

b3.setPrice(b3.getPrice()+0.55);

System.out.printf(b3.getName()+” is now $%6.2f\n”,b3.getPrice());

System.out.println(b4.getName()+” has UPC: “+b4.getUPC());

b4.setPrice(0.45);

System.out.println(“10 lbs of “+b2.getName()+” cost “+b2.getPrice(10.0));

System.out.println(s1);

Milk 1% costs $3.95

Bananas costs $1.08

Corn Pops is now $4.00

Bananas has UPC: 1234510335

10 lbs of Bananas cost 4.5

Item: Milk 1% UPC: 123450001 wt=8.0 price=$3.95

…[according to the toString above]

3.  Assume we want to keep track of the number of items instantiated, i.e., the number of unique products for sale. We could do this with a static variable: itemCount .

[2 pts each = 6]

a.  Should this static variable be private or public? ___private_____

b.  In what method (mutator or constructor) would we update the value of this variable?
__Item constructor______

c.  If there is a static accessor method called getItemCount(),
with what should we prefix this method name in order to invoke it? _____Item_.getItemCount() ?

4.  Show how you would set up an array named “inventory” to hold (reference) 1000 Item objects.

[7 pts]

Item inventory = new Item [1000];

What “value” is each of the array elements of inventory initialized to? ___null_____

5.  GUI and event driven programming. Fill in the blanks for the program below to provide a simple GUI to access the Item data so that a customer can enter a UPC and get the name and price. There will be some additional methods that take care of the data lookup in a class that the inventory array is implemented above. You should not need to refer back to previous questions to answer this.

[15 pts]

public class ItemGUI extends JApplet implements ActionListener {

JTextField nameTxt = null;

JTextField UPCTxt = null;

JTextField priceTxt = null;

Inventory myInv = null;

public void init ()

{

Container contentPane = getContentPane ();

contentPane.setBackground (Color.WHITE); contentPane.setLayout (new ___FlowLayout___());

JLabel nameLbl = new JLabel("Item name: "); contentPane.add(__nameLbl_);

nameTxt = new __JTextField___(30); contentPane.add(__nameTxt___);

JLabel upcLbl = new JLabel("UPC: "); contentPane.add(___upcLbl__);

UPCTxt = new _ JTextField __(12); contentPane.add(__UPCTxt____);

JLabel priceLbl = new JLabel("Price: "); contentPane.add(__priceLbl__);

priceTxt = new ___ JTextField ___(10); contentPane.add(__pricetxt___);

JButton lookUpPrice = new _JButton__(“Look up Price ");

contentPane.add (_lookUpPrice__);

__ lookUpPrice __.addActionListener (this);

myInv = new __Inventory___ (1000); // hold up to 1000 different items

}

public __void__ actionPerformed (ActionEvent event)

{

if ( _event__.getActionCommand ().equals ("__Look up Price____")){

String upc = UPCtxt.getText(); //retrieve the entered UPC

Item itm = Inventory.Search(upc); //look up item object based on UPC

_double_ prc = itm.getPrice();

priceTxt.__setPrice___ (“$”+prc); // hint: what setter might be used here?

}

else

System.out.println ("Error in button interface.");

}

6.  True/false on objects and classes.

[12 pts]

_T___ Java objects model real life objects or abstrations.

_ T ___ A class defines the variables that hold the state of the object instantiations.

_F___ All object instance shares one set of instance variables with all other instances.

_ F ___ Instance variables should be declared public so that the user can abuse the object implementation.

_ F ___ The types of instance variables are limited to the primitives of int, double, boolean and Strings.

__ T ___ A well-defined constructor for a class should ensure all instance variables start with reasonable values.

__ T __ A mutator method is used to change one or more instance variables.

__F___ An accessor method is used to change two or more instance variables.

__T__ A static variable exists immediately when the Java program runs.

__F__ A reference variable literally holds the instance variables of the object in memory.

__F__ Only one constructor may be defined per class.

__F__ A method must always return a type.

7.  Give an interpretation of all the components of the main program header line as directed.

[10 pts]

public static void main (String [] args){…}

It is “public” because: it must be available to the Java Virtual Machine

It is “static” because: There must only be one instance of this method

It is “void” because: it does not compute a value to be returned

What is “args”? this is an array of Strings[ that receive parameters from the system (I didn’t really tell the class this last piece]