In: Computer Science
Question #1
The commented codes in the Driver class are either causing a syntax error or run time error. Make sure to identify the error yourself without running it.
predict the output once you fix all the errors.
Make sure you are able to answer similar question if it appears in the exam.
public class Cup extends Box { public void method1() { System.out.println("Cup 1"); } public void method2() { System.out.println("Cup 2"); super.method2(); } } class Pill { public void method2() { System.out.println("Pill 2"); } } class Jar extends Box { public void method1() { System.out.println("Jar 1"); } public void method2() { System.out.println("Jar 2"); } } class Box extends Pill { public void method2() { System.out.println("Box 2"); } public void method3() { method2(); System.out.println("Box 3"); } } class Driver { public static void main(String[] args) { Box var1 = new Box(); Pill var2 = new Jar(); Box var3 = new Cup(); Box var4 = new Jar(); Object var5 = new Box(); Pill var6 = new Pill(); var1.method2(); var2.method2(); var3.method2(); var4.method2(); // var5.method2(); //error var6.method2(); var1.method3(); // var2.method3();//error var3.method3(); var4.method3(); //((Cup)var1).method1(); //runtime error ((Jar)var2).method1(); ((Cup)var3).method1(); // ((Cup)var4).method1(); //runtime error ((Jar)var4).method2(); ((Box)var5).method2(); // ((Pill)var5).method3();//error ((Jar)var2).method3(); ((Cup)var3).method3(); // ((Cup)var5).method3();//runtime error } }
****************************************************************************************************************
Question 2
Comparable Programming. Suppose you have a pre-existing class BankAccount that represents users' accounts and money deposited at a given bank. The class has the following data and behavior:
Field/Constructor/Method |
Description |
private String name |
name of the person using the account |
private int id |
ID number of this account |
private double balance |
amount of money currently in the account |
public BankAccount(String name, int id) |
makes account with given name/ID and $0.00 |
public void deposit(double amount) |
adds given amount to account's balance |
public int getID() |
returns the account's ID number |
public String getName() |
returns the account's name |
public double getBalance() |
returns the account's balance |
public String toString() |
returns String such as "Ed Smith: $12.56" |
public void withdraw(double amount) |
subtracts given amount from account's balance |
Make BankAccount objects comparable to each other using the Comparable interface.
use the following rules to implement the compareTo method
public class BankAccount implements Comparable {
public int compareTo(Object o) { BankAccount other = (BankAccount)o; if (balance > other.getBalance()) { return 1; } else if (balance < other.getBalance()) { return -1; } else if (id > other.getID()) { return 1; } else if (id < other.getID()) { return -1; } else { return 0; } } }
****************************************************************************************************************
Question 3
Add a compreTo method to the Passenger class that you created for the previous assignment. Passengers should be compared based on their last names. If two passengers have the same last name then the seat numbers must be compared.
Solution
public int compareTo(Object o) { Passenger p = (Passenger)o; //checking to see if the last names are the same, if it is then compare based on the seat number if (this.getLast().equalsIgnoreCase(p.getLast())) return this.seatNumber - p.seatNumber; else return this.getLast().compareTo(p.getLast()); //if the last names are not the same then compare the last names }
PreviousNext
****************************************************************************************************************
Question 4
You should be able to answer the following questions
Part 1.create an interface called Incrementable that has two methods called increment and decrement. both of these methods have a parameter of type integer and they don't return anything.
part 2:
The following code does not compile. what is the issue? how can you fix the problem
interface Pay { public double getRaise(); public String getPromotion(double num); } class Secretary implements Pay { public Secretary() { } public void getRaise() { System.out.println("You got a Raise"); } public double getPromotion(int num) { System.out.println("You got " +num + " promotion"); } }
dsfdfdf
PreviousNext
****************************************************************************************************************
question 5
Suppose that the following two classes have been declared:
public class Car { public void m1() { System.out.println("car 1"); } public void m2() { System.out.println("car 2"); } public String toString() { return "vroom"; } } class Truck extends Car { public void m1() { System.out.println("truck 1"); } public void m2() { super.m1(); } public String toString() { return super.toString() + super.toString(); } }
Write a class MonsterTruck whose methods have the behavior below. Don't just print/return the output; use inheritance to reuse behavior from the superclass.
Method Output/Return
m1 : monster 1
m2 : truck 1
car 1
toString: "monster vroomvroom"
Solution
class MonsterTruck extends Truck { public void m1() { System.out.println("Monster 1"); } public void m2() { super.m1(); super.m2(); } public String toString() { return "Monster "+ super.toString(); } } class Driver { public static void main(String[]args) { MonsterTruck m = new MonsterTruck(); m.m1(); m.m2(); System.out.println(m); } }
your turn:
the following classes have been created.
class Eye extends Mouth { public void method1() { System.out.println("Eye 1"); } } class Mouth { public void method1() { System.out.println("Mouth 1"); } public void method2() { System.out.println("Mouth 2"); } } class Nose extends Eye { public void method1() { System.out.println("Nose 1"); } public void method3() { System.out.println("Nose 3"); } }
create a class called Ear that extends Nose. this class must implement the method1, method2, method3 . After creating the Ear class the following drive should generate the given output
class Driver { public static void main(String[] args) { Ear e = new Ear(); System.out.println("calling method 1"); e.method1(); System.out.println("calling the method 2"); e.method2(); System.out.println("calling the method 3"); e.method3(); } } Output: calling method 1 Nose 1 Ear1 calling the method 2 Ear 2 Mouth 2 calling the method 3 Ear 3 Nose 1
public class Ear extends Nose { //your code }
****************************************************************************************************************
Question 6
The following classes have been created.
public class Plate { public void method1() { System.out.println("Plate"); } } class Fork { public void method2() { System.out.println("Fork"); } } class Spoon { public void method3() { System.out.println("Spoon"); } }
The following driver has been created but it does not compile. what is the cause of the error and how can you fix it.
class Driver { public static void main(String[] args) { Object[] stuff = {new Spoon(), new Fork(), new Plate()}; for(int i = 0; i < stuff.length; i++) { stuff[i].method1(); Stuff[i].method2(); stuff[i].method3(); } } }
solution: since there is an array of Objects, every element in the array must be type casted to the proper type. then the method from the class can be called on it.
class Driver { public static void main(String[] args) { Object[] stuff = {new Spoon(), new Fork(), new Plate()}; for(int i = 0; i < stuff.length; i++) { if(stuff[i] instanceof Plate) { Plate p = (Plate)stuff[i]; p.method1(); } else if(stuff[i] instanceof Fork) { Fork f = (Fork)stuff[i]; f.method2(); } else if(stuff[i] instanceof Spoon) { Spoon s = (Spoon)stuff[i]; s.method3(); } } } }
****************************************************************************************************************
Question 7
There will one question related to ArrayList class similar to the following question.
The following driver has created a an ArrayList of Point objects. write a method that moves all the points with the value of x greater than its value of y to the end of the list. .
import java.util.*; public class ArrayistQuestion { public static void main(String[] args) { ArrayList<Point> points = new ArrayList<Point>(); points.add(new Point(12, 5)); points.add(new Point(2,23)); points.add(new Point(6,3)); points.add(new Point(9,2)); points.add(new Point(12,23)); System.out.println(points); shift(points); System.out.println(points); } //this method shifts all the points with x greater than y to the end of the list public static void shift(ArrayList<Point> list) { int count = list.size(); for(int i = 0; i < count; i++) { if(list.get(i).getX() > list.get(i).getY()) { Point p = list.get(i); //make a copy of the Point object list.remove(i);//removes it from the list list.add(p); // adds it ot the endof the list i--; count--; // decrement the count by 1 since one element has been removed } } } } class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "(x = "+ x + " y = " + y+" )"; } }
****************************************************************************************************************
Ans 1)
i) First Error : Object var5 = new Box()
Instead of writiing the above Syntax it must be written as :
Box var5 = new Box();
ii ) Second Error: Pill var2 = new Jar();
First we need to create an object of child class in inheritence. Therefore we have to create an object of Jar class by writing the syntax as follows:
Jar var2 = new Jar();
Then we can be able to invoke the function method3() of its Parent class Box by using syntax:
var2.method3();
in question it is given, Pill var2 = new Jar(); which is a Syntax error
iii) Third Error : Cup(var1).method1(); which is not possible to convert from parent object to child class object.
v ) Fifth Error is the Syntax Error because of the reason behind is that :
Since,
Object var5 = new Box(); is the wrong Syntax which should be modified as :
Box var5 = new Box();
Then Again var5.method3() should be invoked instead of (Pill)var5.method3();
Ans 4)
i) The Interface Incremental can be written as :
interface Incremental
{
public void increment();
public void decrement();
}
ii)
In Secretary class which implements Pay interface in order to define the body of the getRaise() method that must be declared as double because
getRaise() function is the member of the interface Pay which is declared with double return type. Hence it must also
be defined with double return type while defining the body of the same function in Secretary class.