



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This exam covers fundamental concepts in object-oriented programming (oop) and their implementation in java. It includes questions on data abstraction, procedural abstraction, inheritance, interfaces, static binding, dynamic binding, and exception handling. The exam also features exercises that require students to implement classes and methods, demonstrating their understanding of oop principles and java syntax.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




#1 Problem #1 (Miscellaneous) 40 #2 Problem #2 (Class Implementation) 60 Total Total 100
Problem #1 (Miscellaneous)
Answer: b
Answer: a With static binding, the declared type of a variable is used to determine what methods are called. With dynamic binding, the method that is called depends on an object’s underlying type (the type of the actual object instantiated that is assigned to a variable, no matter the type of that variable).
Problem #2 (Class Implementation) Assume the following 2 classes that you should not change. public abstract class Gadget { private int id; public Gadget(int id) { this.id = id;} @Override public String toString() { return "Gadget [id=" + id + "]";} public abstract String howToUse(); } public class PortableTimeGadget extends TimeGadget { public PortableTimeGadget(int id, int hour, int min) { super(id, hour, min); } } For this problem you will complete the implementation of the TimeGadget , TimeComparator , and Util classes (whose partial definitions are provided below). A TimeGadget object has a hour and min field. The other 2 do not have fields. All 5 classes are in the same package. Y ou may not add any instance nor static variables and you may not add any auxiliary methods to the classes. public class TimeGadget extends Gadget{ private int hour; private int min; public int getHour() { return hour; } public int getMin() { return min; } / INCOMPLETE CLASS / } import java.util.Comparator; public class TimeComparator implements Comparator
import java.util.ArrayList; import java.util.Arrays; public class SampleDriver { public static void main(String[] args) { TimeGadget t1 = new TimeGadget(35, 7, 17); TimeGadget t2 = new TimeGadget(36, 7, 18); TimeGadget t3 = new TimeGadget(37, 7, 17); TimeComparator timeComparator = new TimeComparator(); int [] result = {timeComparator.compare(t1, t2),timeComparator.compare(t1, t3), timeComparator.compare(t2, t3)}; System.out.println(Arrays.toString(result)); Gadget t4 = new TimeGadget(38, 12, 3); Gadget t5 = new PortableTimeGadget(39, 2, 45); ArrayList
is outside of the range of 1 to 12 (inclusive) or the min is outside of the range 0 to 59 (inclusive) it will throw the IllegalArgumentException with the message Bad Time, otherwise it will initialize the corresponding instance variables.
public TimeGadget(int id, int hour, int min) { super(id); if(hour < 1 || hour > 12 || min < 0 || min > 59) { throw new IllegalArgumentException("Bad Time"); } this.hour = hour; this.min = min; } b. getTime (10 pts) – The public non-static getTime method will return a String and take in no parameters. The return string will be the hour followed by a : followed by the min. If the min (not the hour) is less than 10, it should be padded with a zero. Therefore, 7:03 is valid but 7:3 is not when hour is 7 and min is 3.
public String getTime() { return hour + ":" + (min < 10? " 0 " : "") + min; } c. howToUse (6 pts) – The public non-static howToUse method will return a String and take in no parameters. The return string will be simply be Set time and look at it. You must have the Override annotation before the method definition.
@Override public String howToUse() { return "Set time and look at it"; }
public int compare(TimeGadget t1, TimeGadget t2) { if(t1.getHour() == t2.getHour() && t1.getMin() == t2.getMin()) { return 0; } else if(t1.getHour() > t2.getHour() || (t1.getHour() == t2.getHour() && t1.getMin() > t2.getMin())) { return 1; } return - 1 ; }