- (25%) What does the following program print out when run?
public class Boat extends Ship { private int seaWorthinessRating; public Boat(double maxSpeed) { super(maxSpeed); seaWorthinessRating = 33; } public Boat(double maxSpeed, int rating) { super(maxSpeed); seaWorthinessRating = rating; } public String toString(){ return "mp=" + maxSpeed + " seaworth=" + seaWorthinessRating; } } public class SailBoat extends Boat { public String toString(){ return "Sailboat"; } } public class Ship { protected double maxSpeed; public Ship(double maxSpeed){ this.maxSpeed = maxSpeed; } public String toString(){ return "maxSpeed=" + maxSpeed; } public static void main(String[] args){ Ship s1 = new Ship(1); System.out.println(s1); Ship s2 = new Boat(2,66); Ship s22 = new Boat(8); System.out.println(s2); System.out.println(s22); SailBoat s3 = new SailBoat(3,77); System.out.println(s3); Ship s4 = (Ship)s3; System.out.println(s4); } }
- (25%) What is wrong with the program below? Explain.
public interface Drinkable { public void drink(); public boolean isEmpty(){ return false;} } public class RedBull implements Drinkable { private int size; public RedBull(int size){ this.size = size; } public void drink(){ System.out.println("Yuuuuuckkkkk"); } public boolean isEmpty(){ return true; } }Answer: An interface cannot implement a method.
- (25%) Implement a static method which takes as an argument an array of integers
aand returns a new array, lets call itr, wherer[0]has the sum ofa[0] + a[1], andr[1]has the sum ofa[2] + a[3], and so on. You can assume that the length ofais even.
Answer: - (25%) Take a look at this code:
public class Plant { private String name; public Plant(String name){ this.name = name; } public String getName(){ return name; } public static void main(String[] args){ Plant[] garden = new Plant[3]; garden[0] = new Plant("Vine"); //Create a Rosemary plant with the name Vanity // it will have 1024 leaves by default garden[1] = new Rosemary("Vanity"); //This one will have 2048 leaves garden[2] = new Rosemary("Pride", 2048); for (Plant p: garden){ System.out.println(p.getName()); //print their names } //getRosemaries() returns the total number of rosemaries // that have been created. System.out.println("There are " + Rosemary.getNumRosemaries() + " rosemary plants."); Rosemary r = new Rosemary("Loathing"); System.out.println(r.getName()); System.out.println("There are " + Rosemary.getNumRosemaries() + " rosemary plants."); } }Implement the missingRosemaryclass so that when we run the code above it will print:
Vine Rosemary:Vanity numLeaves=1024 Rosemary:Pride numLeaves=2048 There are 2 rosemary plants. Rosemary:Loathing numLeaves=1024 There are 3 rosemary plants.
See the comments in the code for hints on what the various
methods and attributesRosemarymust have.Answer:
public class Rosemary extends Plant { private int numLeaves; private static int numCreated = 0; public Rosemary(String name) { super("Rosemary:" + name ); numLeaves = 1024; numCreated++; } public Rosemary(String name, int numLeaves) { super("Rosemary:" + name ); this.numLeaves= numLeaves; numCreated++; } public String getName(){ return super.getName() + " numLeaves=" + numLeaves; } public static int getNumRosemaries(){ return numCreated; } }
public static int[] everyOther(int[] a){
int[] result = new int[a.length/2];
for (int i = 0; i < a.length; i+=2) {
result[i/2] = a[i] + a[i+1];
}
return result;
}
- (25%) What does the following program print out when run?
public class Boot extends Shoe { protected int height; public Boot(int height){ super(9); this.height = height; } public Boot(int height, int size){ super(size); this.height = height; } } public class Ugg extends Boot { public Ugg(int height, int size) { super(height,size); height--; } public String toString(){ return "Ugg size=" + super.toString() + " height=" + height; } } public class Shoe { private int size; public Shoe(){ size = 7; } public Shoe(int size) { this.size = size; } public String toString() { return "Shoe [size=" + size + "]"; } public static void main(String[] args) { Shoe s1 = new Shoe(); System.out.println(s1); Boot s3 = new Ugg(7,9); System.out.println(s3); Boot s4 = new Boot(5,15); System.out.println(s4); Ugg s5 = new Ugg(9,16); System.out.println(s5); Boot s6 = (Boot)s5; System.out.println(s6); } }
- (25%) What is wrong with the program below? Explain.
public interface Playable{ public void play() { super(); }; public int timeLeft(); } public class Media { protected String title; public Media(String title){ this.title = title; } } public class Song extends Media implements Playable{ protected int runningTime; public Song(String name, int runningTime){ super(name); this.runningTime = runningTime; } public void play(){ System.out.println("Playing...."); } public int timeLeft(){ return runningTime - 10; } }Answer: An interface cannot implement a method.
- (25%) Implement a static method which takes as an argument an array of integers
aand returns a new array, lets call itr, wherer[0]contains the sum ofa[0]anda[last index in a],r[1]contains the sum ofa[1]anda[last index in a minus 1], and so on. You can assume that the length ofais even.
Answer:
public static int[] fold(int[] a){ int[] result = new int[a.length/2]; for (int i = 0; i < a.length / 2; i++) { result[i] = a[i] + a[a.length - 1 - i]; } return result; } - (25%) Take a look at this code:
public interface LeafEater { public void eatLeaf(); } public class Mammal { protected int numToes; protected static int count = 0; public Mammal(){ numToes = 5; count++; } public String toString(){ return "Mammal with " + numToes + " toes."; } public static void main(String[] args) { Mammal[] animals = new Mammal[4]; animals[0] = new Mammal(); //Creates a giraffe with 8 toes and height of 10.0 feet. animals[1] = new Giraffe(); //Creates a giraffe with 5 toes and height of 14.6 feet. animals[2] = new Giraffe(14.6); //Creates a giraffe with 8 toes and height of 10.0 feet. animals[3] = new Giraffe(); for (Mammal m : animals){ System.out.println(m); } //getCount() returns the number of Mammals that have been created. System.out.println("There are " + Giraffe.getCount() + " mammals"); } }
Implement the missingGiraffeclass so that when we run the code above it will print:
Mammal with 5 toes. Mammal with 8 toes.-- Giraffe: 10.0 feet. Mammal with 5 toes.-- Giraffe: 14.6 feet. Mammal with 8 toes.-- Giraffe: 10.0 feet. There are 4 mammals
See the comments in the code for hints on what the various methods and attributesGiraffemust have.
Answer:
public class Giraffe extends Mammal implements LeafEater{ private double height; public Giraffe(){ super(); height = 10; numToes = 8; } public Giraffe(double height){ super(); this.height = height; } public String toString(){ return super.toString() + "-- Giraffe: " + height + " feet."; } public static int getCount(){ return count; } public void eatLeaf() { System.out.println("Yummm, leaves are good."); } }