2. a. The Talk-A-Lot Cell Phone Company provides phone services for its customer
ID: 671555 • Letter: 2
Question
2. a.
The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods—one that returns the phone number, another that returns the price of the call, and a third that displays information about the call. Create two child classes of PhoneCall: IncomingPhoneCall and OutgoingPhoneCall. The IncomingPhoneCall constructor passes its phone number parameter to its parent’s constructor and sets the price of the call to 0.02. The method that displays the phone call information displays the phone number, the rate, and the price of the call (which is the same as the rate). The OutgoingPhoneCall class includes an additional field that holds the time of the call in minutes. The constructor requires both a phone number and the time. The price is 0.04 per minute, and the display method shows the details of the call, including the phone number, the rate per minute, the number of minutes, and the total price. Write an application that demonstrates you can instantiate and display both IncomingPhoneCall and OutgoingPhoneCall objects. Save the files as PhoneCall.java, IncomingPhoneCall.java, OutgoingPhoneCall.java, and DemoPhoneCalls.java.
b. Writeanapplicationinwhichyouassigndatatoamixof10IncomingPhoneCall and OutgoingPhoneCall objects into an array. Use a for loop to display the data. Save the file as PhoneCallArray.java.
Explanation / Answer
/**Abstract class definition of PhoneCall method */
//PhoneCall.java
public abstract class PhoneCall
{
//members of PhoneCall class
private String phoneNumber;
private double price;
//Constructor to set phone number
public PhoneCall(String phoneNumber)
{
this.phoneNumber=phoneNumber;
set(0.0);
}
//Set the price of the call
public void set(double price)
{
this.price=price;
}
//Returns the phone number
public String getPhoneNumber()
{
return phoneNumber;
}
//Return price of call
public double getPrice()
{
return price;
}
//Returns the phone number and price
public String getCallInformation()
{
return "Phone Number : "+phoneNumber+" Price : "+price;
}
}
------------------------------------------------------------------------------------------------------------------------------------
//IncomingPhoneCall.java that extends the PhoneCall abstract class
public class IncomingPhoneCall extends PhoneCall
{
//private members of the class
private double price;
private double rateOfCall;
//Constructor to set the phone number
public IncomingPhoneCall(String phoneNumber)
{
//call the parent class constructor with phone number
super(phoneNumber);
//set price to 0.2
set(0.2);
//set rate of call as price
rateOfCall=price;
}
//Overrides the phone number method that returns the phone number
//from the parent class using super keyword
@Override
public String getPhoneNumber() {
return super.getPhoneNumber();
}
//Overrides the getPrice method that returns the price
//from the parent class using super keyword
@Override
public double getPrice() {
return super.getPrice();
}
//Overrides the getCallInformation method that returns the phone number
//and price and add rate of call in this method
@Override
public String getCallInformation() {
return super.getCallInformation()+" Rate of Call : "+rateOfCall;
}
}
------------------------------------------------------------------------------------------------------------------------------------
//OutgoingPhoneCall.java that extends the PhoneCall abstract class
public class OutgoingPhoneCall extends PhoneCall
{
//private member of class
private int minutes;
//Constructor of the class that sets the phone number and time in minutes
public OutgoingPhoneCall(String phoneNumber, int time)
{
//call parent class constructor with phone number
super(phoneNumber);
//set minutes
minutes=time;
//set price of call as 0.04
set(0.04);
}
//Overrides the phone number method that returns the phone number
//from the parent class using super keyword
@Override
public String getPhoneNumber()
{
return super.getPhoneNumber();
}
//Overrides the getPrice method that returns the price
//from the parent class using super keyword
@Override
public double getPrice()
{
return super.getPrice();
}
//Overrides the getCallInformation method that returns the phone number
//and minutes and total
@Override
public String getCallInformation() {
return super.getCallInformation()+" Minutes : "+minutes+
" Total : "+getPrice()*minutes;
}
}
------------------------------------------------------------------------------------------------------------------------------------
/**The java program that demonstrates the incoming and out going
* calls using PhoneCall class and print the infromation of incoming
* and outgoing calls*/
//DemoPhoneCalls.java
public class DemoPhoneCalls
{
public static void main(String[] args)
{
//Create an array of incoming calls type
PhoneCall[] incomingPhoneCalls
=new IncomingPhoneCall[10];
incomingPhoneCalls[0]= new IncomingPhoneCall("123-456-189");
incomingPhoneCalls[1]= new IncomingPhoneCall("123-456-289");
incomingPhoneCalls[2]= new IncomingPhoneCall("123-456-389");
incomingPhoneCalls[3]= new IncomingPhoneCall("123-456-489");
incomingPhoneCalls[4]= new IncomingPhoneCall("123-456-589");
incomingPhoneCalls[5]= new IncomingPhoneCall("123-456-689");
incomingPhoneCalls[6]= new IncomingPhoneCall("123-456-789");
incomingPhoneCalls[7]= new IncomingPhoneCall("123-456-889");
incomingPhoneCalls[8]= new IncomingPhoneCall("123-456-989");
incomingPhoneCalls[9]= new IncomingPhoneCall("123-456-199");
System.out.println("Incoming Calls Information");
//print incoming calls
for (PhoneCall phoneCall : incomingPhoneCalls) {
System.out.println(phoneCall.getCallInformation());
}
//Create an array of outgoing calls type
PhoneCall[] outgoingPhoneCalls
=new OutgoingPhoneCall[10];
//set outgoing calls with phone number and number of minutes
outgoingPhoneCalls[0]= new OutgoingPhoneCall("123-456-189",1);
outgoingPhoneCalls[1]= new OutgoingPhoneCall("123-456-289",2);
outgoingPhoneCalls[2]= new OutgoingPhoneCall("123-456-389",3);
outgoingPhoneCalls[3]= new OutgoingPhoneCall("123-456-489",4);
outgoingPhoneCalls[4]= new OutgoingPhoneCall("123-456-589",5);
outgoingPhoneCalls[5]= new OutgoingPhoneCall("123-456-689",6);
outgoingPhoneCalls[6]= new OutgoingPhoneCall("123-456-789",7);
outgoingPhoneCalls[7]= new OutgoingPhoneCall("123-456-889",8);
outgoingPhoneCalls[8]= new OutgoingPhoneCall("123-456-989",9);
outgoingPhoneCalls[9]= new OutgoingPhoneCall("123-456-199",10);
System.out.println("Outgoing Calls Information");
//print outgoing calls
for (PhoneCall phoneCall : outgoingPhoneCalls)
{
System.out.println(phoneCall.getCallInformation());
}
}
}
------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Incoming Calls Information
Phone Number : 123-456-189 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-289 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-389 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-489 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-589 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-689 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-789 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-889 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-989 Price : 0.2 Rate of Call : 0.0
Phone Number : 123-456-199 Price : 0.2 Rate of Call : 0.0
Outgoing Calls Information
Phone Number : 123-456-189 Price : 0.04 Minutes : 1 Total : 0.04
Phone Number : 123-456-289 Price : 0.04 Minutes : 2 Total : 0.08
Phone Number : 123-456-389 Price : 0.04 Minutes : 3 Total : 0.12
Phone Number : 123-456-489 Price : 0.04 Minutes : 4 Total : 0.16
Phone Number : 123-456-589 Price : 0.04 Minutes : 5 Total : 0.2
Phone Number : 123-456-689 Price : 0.04 Minutes : 6 Total : 0.24
Phone Number : 123-456-789 Price : 0.04 Minutes : 7 Total : 0.28
Phone Number : 123-456-889 Price : 0.04 Minutes : 8 Total : 0.32
Phone Number : 123-456-989 Price : 0.04 Minutes : 9 Total : 0.36
Phone Number : 123-456-199 Price : 0.04 Minutes : 10 Total : 0.4
Hope this helps
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.