1 / 10

Chapter 7 answers

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.

sal
Download Presentation

Chapter 7 answers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 answers m/c 6-10 t/f 6-10 AP free style s/a 7.6-7.7

  2. 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

  3. 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

  4. 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

  5. 9 • b

  6. 10 • e

  7. 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

  8. AP Multiple Choice • 6. A In warmup file

  9. See BlueJ for short answer 7.6 traffic light and 7.7 rubber circle

  10. 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; }

More Related