a The TalkALot Cell Phone Company provides phone services fo
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. Write an application in which you assign data to a mix of 10 IncomingPhoneCall and OutgoingPhoneCall objects into an array. Use a for loop to display the data. Save the file as PhoneCallArray.java.
Solution
Hi,
Please see below the Java classes. Please comment for any queries/feedbacks.
Thanks,
Anita
PhoneCall.java
public abstract class PhoneCall {
protected String phoneNumber;
protected double price;
//PhoneCall constructor
public PhoneCall(String phoneNumber){
this.phoneNumber = phoneNumber;
this.price = 0.0;
}
//abstract methods
public abstract String getPhoneNumber();
public abstract double getPrice();
public abstract void callInfo();
//set method for price
public void setPrice(double price) {
this.price = price;
}
}
IncomingPhoneCall.java
public class IncomingPhoneCall extends PhoneCall {
//IncomingPhoneCall COnstructor
public IncomingPhoneCall(String phoneNumber) {
super(phoneNumber);
setPrice(0.02);
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public double getPrice() {
return this.price;
}
//Displaying the info of IncomingPhoneCall
public void callInfo() {
System.out.println(\"Phone Number : \"+this.getPhoneNumber() +\" Rate: \"+this.getPrice()
+\" Price: $\"+this.getPrice());
}
}
OutgoingPhoneCall.java
public class OutgoingPhoneCall extends PhoneCall {
private int timeOfCall ;
//Constructor for OutgoingPhoneCall
public OutgoingPhoneCall(String phoneNumber,int timeOfCall) {
super(phoneNumber);
this.setPrice(0.04);
this.setTimeOfCall(timeOfCall);
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public double getPrice() {
return this.price;
}
public int getTimeOfCall() {
return timeOfCall;
}
public void setTimeOfCall(int timeOfCall) {
this.timeOfCall = timeOfCall;
}
//Displaying the info of OutgoingPhoneCall
public void callInfo() {
System.out.println(\"Phone Number : \"+this.getPhoneNumber() +\" Rate Per Minute: \"+this.getPrice()
+\" No of Minutes: \"+this.getTimeOfCall()+ \" Total Price: $\"+this.getTimeOfCall()*this.getPrice());
}
}
DemoPhoneCalls.java
public class DemoPhoneCalls {
public static void main(String [] args){
//Creating an IncomingPhoneCall and an OutgoingPhoneCall objects
PhoneCall incoming = new IncomingPhoneCall(\"61677777\");
PhoneCall outgoing = new OutgoingPhoneCall(\"61888888\", 10);
//to display the info
incoming.callInfo();
outgoing.callInfo();
}
}
Sample output:
Phone Number : 61677777 Rate: 0.02 Price: $0.02
Phone Number : 61888888 Rate Per Minute: 0.04 No of Minutes: 10 Total Price: $0.4
PhoneCallArray.java
public class PhoneCallArray {
public static void main(String [] args){
//PhoneCall Array of size 10
PhoneCall [] phoneCallArr = new PhoneCall[10];
//creating 5 IncomingPhoneCall objects
IncomingPhoneCall incoming1 = new IncomingPhoneCall(\"611111111\");
IncomingPhoneCall incoming2 = new IncomingPhoneCall(\"612222222\");
IncomingPhoneCall incoming3 = new IncomingPhoneCall(\"613333333\");
IncomingPhoneCall incoming4 = new IncomingPhoneCall(\"613333333\");
IncomingPhoneCall incoming5 = new IncomingPhoneCall(\"615555555\");
//creating 5 OutgoingPhoneCall objects
OutgoingPhoneCall outgoing1 = new OutgoingPhoneCall(\"611111111\",13);
OutgoingPhoneCall outgoing2 = new OutgoingPhoneCall(\"612222222\",14);
OutgoingPhoneCall outgoing3 = new OutgoingPhoneCall(\"613333333\",15);
OutgoingPhoneCall outgoing4 = new OutgoingPhoneCall(\"613333333\",16);
OutgoingPhoneCall outgoing5 = new OutgoingPhoneCall(\"615555555\",17);
//Adding data to the array- random order
phoneCallArr[0] = incoming1;
phoneCallArr[1] = outgoing2;
phoneCallArr[2] = incoming3;
phoneCallArr[3] = outgoing4;
phoneCallArr[4] = incoming5;
phoneCallArr[5] = outgoing1;
phoneCallArr[6] = incoming2;
phoneCallArr[7] = outgoing3;
phoneCallArr[8] = incoming4;
phoneCallArr[9] = outgoing5;
//Displaying the data in a loop
for(int i =0; i<phoneCallArr.length; i++){
PhoneCall phonecallObj = phoneCallArr[i];
phonecallObj.callInfo();
}
}
}
Sample output:
Phone Number : 611111111 Rate: 0.02 Price: $0.02
Phone Number : 612222222 Rate Per Minute: 0.04 No of Minutes: 14 Total Price: $0.56
Phone Number : 613333333 Rate: 0.02 Price: $0.02
Phone Number : 613333333 Rate Per Minute: 0.04 No of Minutes: 16 Total Price: $0.64
Phone Number : 615555555 Rate: 0.02 Price: $0.02
Phone Number : 611111111 Rate Per Minute: 0.04 No of Minutes: 13 Total Price: $0.52
Phone Number : 612222222 Rate: 0.02 Price: $0.02
Phone Number : 613333333 Rate Per Minute: 0.04 No of Minutes: 15 Total Price: $0.6
Phone Number : 613333333 Rate: 0.02 Price: $0.02
Phone Number : 615555555 Rate Per Minute: 0.04 No of Minutes: 17 Total Price: $0.68



