DPIT121 Spring 2021 Final Exam Answer Sheet
//Write your full name and student ID here
//Don't delete anything from the template not even the comments and the questions Just add your code to make the template complete
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap;
//Complete the Java code to define the class MobilePhone and class MyDate as well as enum Type.
enum Type {Android, IOS, Windows};
//1- Complete the Code below. (0.25 marks)
class MobilePhone implements Cloneable
{
private String model; private MobileType type; private double price;
public MobilePhone(String model,MobileType type, double price)
{
this.model=model; this.type=type; this.price=price;
}
@Override
public String toString()
{
return "Model: "+model+" Type: "+type+" Price: "+price;
}
public String toDelimatedString()
{
return model+","+type+","+price;
}
public MobilePhone clone() throws CloneNotSupportedException
{
//2- Complete the Code. (0.25 marks)
return (MobilePhone) super.clone();
}
public double getPrice()
{
return price;
}
public String getModel()
{
//3- Complete the Code. (0.25 marks)
return model;
}
public void setPrice(double price)
{
//4- Complete the Code. (0.25 marks)
this.price = price;
}
public int compareTo(MobilePhone other)
{
//5- Complete the Code to compare based on the model. (0.25 marks)
return this.model.compareTo(other.model);
}
public void priceRise(double rate)
{
price*=(1+rate);
}
}
class MyDate implements Cloneable
{
private int year; private int month; private int day;
public MyDate(int y,int m,int d)
{
year=y; month=m; day=d;
}
@Override
public String toString()
{
// 6- Complete the Code. (0.25 marks)
return year+"-"+month+"-"+day;
}
public String toDelimatedString()
{
// 7- Complete the Code. (0.25 marks)
}
public MyDate clone() throws CloneNotSupportedException
{
// 8- Complete the Code. (0.25 marks)
return year+","+month+","+day;
}
}
// Write an interface named PlanMonthlyPayment which has a method called calcPayment to calculate the monthly payment.
interface PlanMonthlyPayment
{
//9- Complete the Code. (0.5 marks)
double calcPayment(double flatRate);
}
// Write an abstract base class Plan for the above scenario. This class supports the common features of all children.
//10- Complete the Code below. (0.5 marks)
abstract class Plan implements Cloneable, PlanMonthlyPayment, Comparable
{
protected int id; protected String name;
protected MobilePhone handset; protected MyDate dateOfExpiry;
protected int capLimit;
public Plan ( int id,String name,MobilePhone handset, MyDate date, int capLimit)
{
//11- Complete the Code. (0.5 marks)
this.id = id; this.name = name;
this.handset = handset;
this.dateOfExpiry = date;
this.capLimit = capLimit;
}
public String toString()
{
//12- Complete the Code. (0.5 marks)
return "ID: "+id+" Name: "+name+" Handset: "+handset+" Expiry Date: "+dateOfExpiry+" Cap Limit: "+capLimit;
}
abstract public double calcPayment(double flatRate);
//Implement the Comparable Interface based on mobile phone model.
//13- Complete the Code for Comparable. (1.5 marks)
public int compareTo(Plan other) {
return this.handset.compareTo(other.handset);
}
//Implement the Cloneable Interface
// 14- Complete the Code to implement clone(). (2 marks)
public Plan clone() throws CloneNotSupportedException {
Plan plan = (Plan) super.clone();
plan.handset = this.handset.clone();
plan.dateOfExpiry = this.dateOfExpiry.clone();
return plan;
//Write two static methods inside Plan class to make shallow and a deep copy of a list of plans. public static ArrayList shallowCopy(ArrayList plans)
{
//15- Complete the Code. (1 mark)
return new ArrayList<>(plans);
}
public static ArrayList deepCopy(ArrayList plans) throws CloneNotSupportedException
{
//16- Complete the Code. (1 mark)
ArrayList copy = new ArrayList<>();...