Question #1
The commented codes in the Driver class are either causing asyntax error or run time error. Make sure to identify the erroryourself without running it.
predict the output once you fix all the errors.
- un-comment one line at a time
- identify the error
- fix just that line, You are not allowed to change anydeclarations of the objects.
- if it is a runtime error explain the error
- compile the code.
- move to the next error
Make sure you are able to answer similar question if it appearsin 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 apre-existing class BankAccount that represents users' accounts andmoney deposited at a given bank. The class has the following dataand 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 BankAccountobjects comparable to each other using theComparable interface.
use the following rules to implement the compareTo method
- The bank wants to sort by who has the most money, so accountsare compared by balance.
- One with a lower balance is considered to be \"less than\" onewith a higher balance.
- If two accounts have the same balance, they are compared byID.
- The one with the lower ID is considered to be \"less than\" theone with the higher ID.
- If two accounts have the same balance and ID, they areconsidered to be \"equal.\" Your method should not modify anyaccount's state. You may assume the parameter passed is notnull.
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 createdfor the previous assignment. Passengers should be compared based ontheir last names. If two passengers have the same last name thenthe 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 twomethods called increment and decrement. both of these methods havea parameter of type integer and they don't return anything.
part 2:
The following code does not compile. what is the issue? how canyou 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 behaviorbelow. Don't just print/return the output; use inheritance to reusebehavior 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 mustimplement the method1, method2, method3 . After creating the Earclass 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 1Nose 1Ear1calling the method 2Ear 2Mouth 2calling the method 3Ear 3Nose 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 inthe array must be type casted to the proper type. then the methodfrom 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 tothe following question.
The following driver has created a an ArrayList of Pointobjects. write a method that moves all the points with the value ofx 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 points = new ArrayList(); 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 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+\" )\"; } }
****************************************************************************************************************