Unformatted text preview:

CS100J Dec 2003 Sample Questions1CS100J 03 Dec 2003 Sample questions (with answers at the end)Questions on OOOO1. Consider the classes below and write code to complete the class SportsCarpublic class Car{ private double milesPerGallon; // = the miles per gallon for this car public Car(double mpg){ milesPerGallon = mpg; } // = a string representation of this car public String toString(){ return "This car gets " + milesPerGallon + " miles per gallon."; }}public class SportsCar extends Car{ private int topSpeed; // Constructor: an instance with miles per gallon mpg and top speed topSpeedpublic SportsCar(double mpg, int topSpeed){ //Write code to complete the constructor here} /* = A string that gives the fuel economy and top speed of this sports car. Hint: This string will be more than one sentence. */ public String toString(){ //Write your code here} // = the top speed of this carpublic int getTopSpeed(){ return topSpeed; }}OO2. (a) With reference to the class Car in OO1, draw the folder that results from the call Car myCar= new Car(25.5); (b) With reference to the picture you just drew, during execution of the following statement, what would be thevalue of the variable 'this' if it occurred in method toString? myCar.toString(); (c) True or False: Variable topSpeed can be referenced in class SportsCar.OO3. With reference to the Car and SportsCar classes of OO1, consider the following statements. Next to eachstatement write whether it is a legal statement or not.SportsCar sc= new SportsCar(15.0, 170); Car mySportsCar= sc; int ts= mySportsCar.getTopSpeed(); double ts2= sc.getTopSpeed(); Car c= new Car(true, "Red"); Car myOtherCar= new Car(45.2); SportsCar sc2= myOtherCar;OO4. Consider class PrintedMaterial:CS100J Dec 2003 Sample Questions2public abstract class PrintedMaterial{public int numPages; public String printedLanguage;public abstract String getPrintedLanguage();}(a) If you write a class Book to extend PrintedMaterial:public class Book extends PrintedMaterial{ private String title; private String author;//there are no other fields, but the rest of the details are omitted...}can you say anything about the methods that you must include?(b) Write a constructor for class Book whose arguments are the number of pages, the printed language, the title, and the author.(c) Next to the following statements, write whether they are legal or not: Book b= new Book(301, "English", "The Brothers Karamazov", "Dostoyevsky"); PrintedMaterial pm= b; PrintedMaterial pm2= new PrintedMaterial(); PrintedMaterial[] pmArray= new PrintedMaterial[9];OO5. An online retail store sells shirts, pants, and jackets. Below are two classes representing RetailItems andShirts.(a) Fill in the code for method putOnSale().(b) (b) fill in the code of class Shirt:c) For each of the following statements, answer the questions that appear indented under the statements. Assume thatthe statement(s) in each part is (are) written in a main method in a class called Test and are executed independentlyof other parts of the question.Shirt s = new Shirt(45.50, “Land’s End”, “L”);What is/are the apparent type of r? ______________________What is/are the real type of r? ______________________RetailItem t = new Shirt(80.50, “Ralph Lauren”, “M”);What is/are the apparent type of t? ____________________What is/are the real type of t? ______________________RetailItem t = new Shirt(80.50, “Ralph Lauren”, “M”);t.getSize();Will the second statement produce a compiler error (Yes or No)? ____________Why or why not? _____________________________________________RetailItem t = new Shirt(80.50, “Ralph Lauren”, “M”);t.toString();Will the second statement produce a compiler error (Yes or No)? _____________If yes, what is the error? If no, what String will be returned?/** An instance represents a retail item */public class RetailItem {private double price; // price of itemprivate String manufacturer; // manufacturer of item/** Constructor: a RetailItem with given price and manufacturer */public RetailItem(double price, String manufacturer) {this.price = price;this.manufacturer = manufacturer;CS100J Dec 2003 Sample Questions3}/** = the price of this item */public double getPrice() { return price; }/** = the manufacturer of this item */public String getManufacturer() { return manufacturer; }/** put item on sale by reducing cost by percentage given; return true if successful and false otherwise */public boolean putOnSale(double percentage) {if (0.0 < percentage && percentage < 1.0) {price = price * (1.0 – percentage);return true;}return false;}/** put item on sale by reducing cost by 50 percent */public boolean putOnSale() {// put code here}/** = a String representation of this item */public String toString() {return “RetailItem[price = “ + price + “, manufacturer = “ + manufacturer + “]”;}}/** An instance represents a Shirt */public class Shirt extends RetailItem {private String size; // size of shirt – S, M, L, XL/** Constructor: a Shirt item with given price, manufacturer, and size*/public Shirt(double price, String manufacturer, String size) {// fill in code}/** = the size of this shirt*/public String getSize() {// fill in code}/** = a representation of the shirt */public String toString() {// fill in code}}OO6. You are working for an online retail store that sells clothing. Your manager has asked you to modify classCustomer so that it assigns a new unique customer ID number when a customer object is created. The currentimplementation constructs a new Customer object using the ID number supplied as a parameter.Your manager no longer trusts client code to assign unique ID numbers when creating Customer objects. Showthe changes that you need to make to the existing class Customer below so that each new Customer object has aunique ID number. The ID number of the first newly created customer object should be 1, the next customer objectcreated should have ID number 2, and so on./** An instance represents a Customer */public class Customer {private int idNumber; // customer ID numberprivate String name; // customer nameCS100J Dec 2003 Sample Questions4private ShoppingCart items; // customer’s shopping cartprivate Address mailingAddress; // customer’s mailing address/** Constructor: a new customer object with idNumber, name, and mailing address */public Customer(int idNumber, String name,


View Full Document

CORNELL CS 100 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?