100 likes | 367 Views
Chapter 7 answers. m/c 6-10 t/f 6-10 AP free style s/a 7.6-7.7. Multiple choice 6. Give classes A and B what will print for Class A{void foo(){ prints “A’s foo”} Class B extends A {void foo() {prints “B’s foo”} A aRef = new B(); aRef.foo(); b.
E N D
Chapter 7 answers m/c 6-10 t/f 6-10 AP free style s/a 7.6-7.7
Multiple choice 6 Give classes A and B what will print for Class A{void foo(){ prints “A’s foo”} Class B extends A{void foo() {prints “B’s foo”} A aRef = new B(); aRef.foo(); • b
Given Object obj = new String(“hello”); which will NOT cause a compile error? • obj.toUpperCase(); • obj.substring(0,5); • obj.equals(“hi”); • obj.compareTo(“hi”); • All of them will cause a compile error • c
8. Given void doSomething(Object param)which is NOT a valid call? • doSomething(“Java”); • doSomething(new String(“Java”); • doSomething(14); • doSomething(new Integer(14)); • All are valid calls • C according to the book but they are wrong because of autoboxing which changes the int in c to an Integer object
9 • b
10 • e
True/False • A class can be both a superclass and a subclass • T • All classes are derived from the Object class • T • An abstract class cannot be instantiated • T • A polymorphic reference variable can point to different types of objects at different times • T • Polymorphism may occur with inheritance but not with interfaces • F
AP Multiple Choice • 6. A In warmup file
See BlueJ for short answer 7.6 traffic light and 7.7 rubber circle
AP-Style Free Response Solution 7.1a. public class Perishable extends InventoryItem { private Date expiration; private static final double DISCOUNT = 0.01; public Perishable (Date entry, double price, Date expirationDate) { super (entry, price); expiration = expirationDate; } public double getPrice() { if (expiration.compareTo(getEntryDate()) < 0) return getBasePrice() * DISCOUNT; else return getBasePrice(); } } b. public ArrayList<InventoryItem> getItems (double loPrice, double hiPrice) { ArrayList<InventoryItem> list = new ArrayList<InventoryItem>(); for (InventoryItem ii : items) { if ((ii.getPrice() >= loPrice) && (ii.getPrice() <= hiPrice)) list.add(ii); } return list; }