| ||||||||||
public abstract class Animal { public abstract boolean isExtinct(); public String toString() { return "An animal";} } } public class Dinosaur extends Animal { public static final Dinosaur BARNEY = new Dinosaur(); public boolean isExtinct() { return true; } }
(a) Which of the following declarations are valid? Check all that apply. [4 points]
(i) Animal dino = new Dinosaur();
(ii) Dinosaur dino = new Dinosaur();
(iii) Animal anne = new Animal();
(iv) Dinosaur anne = new Animal();
(b) Given a Dinosaur dave, what do the following commands return when typed in the Dr. Java interactions pane? [3 points]
(i) dave.toString()
(ii) dave.isExtinct()
(iii) dave
(c) How would you refer to BARNEY in the following? [1 point]
_________________________.toString()
public class BookElement { private String bookDetails = ""; private BookElement next = null; public String getDetails() {return this.bookDetails;} public BookElement getNext() {return this.next;} public void setDetails(String details) {this.bookDetails = details;} public void setNext(BookElement be) {this.next = be;} }
public int length() { // Hint: Initialize number here. // Also, you need a current element, starting at this. // Hint: Then comes the standard while loop for the traversal while (_________________________________________) { // Hint: do something inside the loop that helps you count. // Hint: do something so that the loop doesn't repeat for ever. } return number }