You are on page 1of 4

Unit 5: Writing Classes Name: _________________

HW 4 – this Keyword Period: _____

Multiple Choice Practice


Questions 1 - 4 refer to the Book class defined below:

public class Book


{
private String title;
private String author;
private int year;

/* constructor header not shown */


{
author = a;
title = t;
setYear(y);
}

public String toString()


{
return title + "\n\t" + author + "\n\t" + year;
}

public int getYear()


{
return year;
}

public void setYear(int y)


{
if (y >= 1450)
{
year = y;
}
else
{
year = 1900;
}
}

public boolean equals(Book other)


{
if (this.title.equals(other.title) && this.author.equals(other.author)
&& this.year == other.year)
{
return true;
}
return false;
}
}
_____ 1) The header for the constructor of this class is missing. Which of the following would be the best
header to use for this constructor?

(A) private Book newBook(String a, String t, int y)


(B) private Book(String a, String t, int y)
(C) Book(String a, String t, int y)
(D) public Book(String a, String t, int y)
(E) public Book newBook(String a, String t, int y)

_____ 2) The following code appears in the main method of another class.

Book b = new Book("JK Rowling", "Harry Potter", 1997);


System.out.println(b);

Which of the following best describes what appears on the screen when this code is executed?

(A) The values "Harry Potter", "JK Rowling" and "1997" with each on a new line, and the second two lines
indented.
(B) The text "Book@" followed by a memory address made up of numbers and letters.
(C) The values "Harry Potter", "JK Rowling" and "1997" all on the same line.
(D) The letter "b" only.
(E) An error message.

_____ 3) The year variable for a Book is set to 2001. Suppose the variable b in a separate class points to this
book. Which of the following calls would return the year value of 2001?

(A) b.getYear(2001)
(B) b.year
(C) b.setYear(2001)
(D) b.setYear()
(E) b.getYear()

_____ 4) What is printed when the following code, which appears in the main method of a separate class to
Book, is run?
Book b1 = new Book("JRR Tolkien", "Lord of the Rings", 1954);
Book b2 = new Book(""JRR Tolkien", "Lord of the Rings", 1954);

if (b1 == b2){
System.out.print("equal, ");
}
else{
System.out.print("not equal, ");
}

if (b1.equals(b2)){
System.out.print("equal");
}
else{
System.out.print("not equal");
}

(A) Nothing is printed, an error occurs.


(B) equal, not equal
(C) equal, equal
(D) not equal, not equal
(E) not equal, equal

_____ 5) Consider the following class that stores information the temperature.

public class Temperature {


private int temp;

//constructors not shown

//postcondition: returns true if the temperatures in each object


//are equal integers
public boolean equals(Temperature t)
{
/* missing code */
}

//accessor and mutators not shown


}

Consider the following code segments that are potential replacements for /* missing code */.

I. return this.temp == t.temp


II. return this.temp == t.getTemp()
III. return this.temp == this.t
IV. return this.temp == this.getTemp()

Which of the above code segments could be used to replace /* missing code */ so that equals
method works as intended?

(A) II only
(B) I and II only
(C) II and IV only
(D) I and IV only
(E) I, II, and IV

Free Response Practice

6) Create a class named Vehicle that simulates a car moving along a 40 block stretch of road.

Your class will build a vehicle and keep track of its location on the road. Location values may range from -20 to
20. A location value of 0 represents block 0, a location value of 1 represents block 1, a location value of 2
represents block 2, etc. If the user tries to move the vehicle beyond block +20 or -20, set the location to +/- 20
respectively.

Variables
 int location - An integer that holds the current block location of the car on the road, with possible
values ranging from -20 to 20.

Methods
 Vehicle() - Sets location to 0.
 Vehicle(int loc) - If loc is between -20 and 20 inclusive, sets location to loc. Otherwise, sets
location to 0 using this.
 void forward() - Increments the vehicle forward one block. Do not let the user move past block
20.
 void backward() - Increments the vehicle backward one block. Do not let the user move past block
-20.
 int getLocation() - Returns an integer representing the block location of the car.
 String toString() - Returns a String representation showing a visual of the vehicle location. The
vehicle is an @ character, with underscores and commas to show its location. When the vehicle is at
location -20 the @ character appears at the start of the String. When the vehicle is at a higher position,
one space for each number from -20 to the car's current location appears before the @. For example:
o If a vehicle is at -13, there should be 7 underscores and commas before the vehicle:
 _,_,_,_,_,_,_,@
o If a vehicle is at 5, there should be 25 underscores and commas before the vehicle:
 _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,@

*Turn in your repl code to me

You might also like