Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

New to Java. Please Help. The Talk-A-Lot Cell Phone Company provides phone servi

ID: 3602296 • Letter: N

Question

New to Java. Please Help.

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.

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.

//PhoneCall.java
public abstract class PhoneCall
{
//protected members of class
protected String phoneNumber;
protected double price;
//constructor of class PhoneCall
public PhoneCall(String phoneNumber)
{
this.phoneNumbere=phoneNumber;
//set price to zero
price=0;
}
//set the price to zero
public void setPrice(double price)
{
this.price=price;
}
//abstract get methods
public abstract String getPhoneNumber();
public abstract double getPrice();
public abstract String getDesciption();
}
}

//IncomingPhoneCall.java
public class IncomingPhoneCall extends PhoneCall
{
//instance variable rate
private double rate;
//constructor that takes phone numb and set price to 0.02
public IncomingPhoneCall(String phoneNumber)
{
super(phoneNumber);
price=0.02;
//rate same as price
rate=price;
}
//returns the phone nunmber
@Override
public String getPhoneNumber()
{
return phoneNumber;
}
//returns price
@Override
public double getPrice()
{
return price;
}
//Override the getDesciption that returns the phone numeber, price and rate
@Override
public String getDesciption()
{
return "Phone Number:"+phoneNumber+"Price:$"+price+"Rate:"+rate;
}
}

//OutgoingPhoneCall.java
public class OutgoingPhoneCall extends PhoneCall
{
private int callMinutes;
//Constructor that takes phone number ad calling minutes
public OutgoingPhoneCall(String phoneNumber, int callMinutes)
{
super(phoneNumber);
//set price to 0.04
price=0.04;
//set calling minutes
this.callMinutes=callMinutes;
}
//returns pone number
@Override
public String getPhoneNumber()
{
return phoneNumber;
}
//returns price
@Override
public double getPrice()
{
return price;
}
//Override the method getDesciption that returns phone number,price,calling,minutes and total cost
@Override
public String getDesciption()
{
return"Phone Number:"+phoneNumber+"Price$"+price+"Minutes:"+callMinutes+"Total cost:$"+price*callMinutes;
}
}

//DemoPhoneCalls.java
public class DemoPhoneCalls
{
public static void main(String[]args)
{
//Create instance of IncomingPhoneCall class with a phone number
IncominPhoneCall incomingCall=new IncomingPhoneCall("727-237-8036");
System.out.println("Incoming Call");
System.out.println(incomingCall.getDesciption());
//Create instance of OutgoingPhoneCall class with a phone number
OutgoingPhoneCall outgoingCall=new OutgoingPhoneCall("727-237-8036",10);
System.out.println("Outgoing Call");
System.out.println(outgoingCall.getDesciption());
}
}

//PhoneCallArray.java
public class PhoneCallArray
{
public static void main(String[]args)
{
final int size=10;
//create an array of type PhoneCall of size=10
PhoneCall[]phoneCalls=new PhoneCall[size];
//mix up of incoming and outgoing calls
phoneCalls[0]=new IncomingPhoneCall("123-456-789");
phoneCalls[1]=new OutgoingPhoneCall("123-48-789",5);
phoneCalls[2]=new IncomingPhonceCall("123-445-459");
phoneCalls[3]=new OutgoingPhoneCall("123-445-789",10);
phoneCalls[4]=new IncomingPhonceCall("123-456-459");
phoneCalls[5]=new OutgoingPhoneCall("123-434-789",15);
phoneCalls[6]=new IncomingPhonceCall("123-434-149");
phoneCalls[7]=new OutgoingPhoneCall("123-48-789",25);
phoneCalls[8]=new IncomingPhonceCall("123-456-789");
phoneCalls[9]=new OutgoingPhoneCall("123-456-789",15);
//print array objects of both Incoming and Outgoing
for(int i=0; i<phoneCalls.length; i++)
{
//Calling getDesciption
System.out.println(phoneCalls[i].getDesciption());
}
}
}

Explanation / Answer

I can give you the tested code with the detailed comments. If you were unable to understand the program these comments help you understand.

DemoPhoneCall.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demophonecalls; //package for all files

/**
*
* @author Shivakumar
*/
public class DemoPhoneCalls { // class contatins main method to demonstrate priliminarly

/**
* @param args the command line arguments
*/
public static void main(String[] args) { //main method
// TODO code application logic here
IncomingPhoneCall inCall = new IncomingPhoneCall("212-555-3096"); //incall object
OutgoingPhoneCall outCall = new OutgoingPhoneCall("312-874-0232", 10); //Object fo outcall outgoingcall
inCall.display(); //calling display function to get infor from incoming call object
outCall.display();
}

}


PhoneCallArray.java //Part b of question goes here
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demophonecalls;

/**
*
* @author Shivakumar
*/
public class PhoneCallArray { //PhoneCallArray class call this fo part b

/**
* @param args the command line arguments
*/
public static void main(String[] args) { //main for question b
// TODO code application logic here
PhoneCall calls[] = new PhoneCall[10]; //Array variable referencing from memory location phoneclass //class (remember this is not an instance of call phonecall )
int i;
calls[0] = new IncomingPhoneCall("957-388-3212"); // creating object and assigning to array of incomi cal
calls[1] = new OutgoingPhoneCall("810-625-1254", 12);//creating obj and assingning to array of outgoincall
calls[2] = new IncomingPhoneCall("343-194-3372");
calls[3] = new OutgoingPhoneCall("655-999-6372", 13);
calls[4] = new IncomingPhoneCall("545-065-2362");
calls[5] = new OutgoingPhoneCall("655-999-6372", 25);
calls[6] = new IncomingPhoneCall("125-345-4857");
calls[7] = new OutgoingPhoneCall("235-955-1371",14);
calls[8] = new IncomingPhoneCall("644-564-8572");
calls[9] = new OutgoingPhoneCall("920-787-8919", 10);
for(i = 0; i < calls.length; ++i) // Using for loop to display information
calls[i].display(); //callinng display with every object
}

}

PhoneCall.java // Abstract class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demophonecalls;

/**
*
* @author Shivakumar
*/
public abstract class PhoneCall {

String phoneNumber; //Phonenumber and price fields
double price;
public PhoneCall(String num) //Constructor with the argument phonenumber
{
phoneNumber = num; // setting phone number
price = 0.0; // Setting default price 0.0
}
public void setPrice(double pr) // Method to set price
{
price = pr; //setting
}
public abstract String getPhoneNumber(); //abstract method to get phonenumber
public abstract double getPrice(); // abstract method to get price

public abstract void display(); // abstract method to get info displayed
}


IncomingPhoneCall.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demophonecalls;

/**
*
* @author Shivakumar
*/
public class IncomingPhoneCall extends PhoneCall { //Incomin class extends phonecall
public final static double RATE = 0.02; // Setting rate as 0.02 cannot be changed
public IncomingPhoneCall(String phoneNumber) // Constructor with phone number
{
super(phoneNumber); // Calling abstract class' constructor super-parent
price = RATE; // Setting price as rate for incoming call
}
public void display() // Function to display incoming call info
{
System.out.println("Incoming phone call " +
getPhoneNumber() + " " + RATE + " per call. Total is $" +
+ getPrice()); // Statement to be printed as info
}
public String getPhoneNumber() // Getter for phone number
{
return phoneNumber;
}
public double getPrice() // Getter for pricee
{
return price;
}

}

OutgoingPhoneCall.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demophonecalls;

/**
*
* @author Shivakumar
*/
public class OutgoingPhoneCall extends PhoneCall { // outgoing call child of abstract

public final static double RATE = 0.04; // Setting rate
private int minutes; // minutes additional field for outgoing call
public OutgoingPhoneCall(String phoneNumber, int minutes) // Constructor with two parameters
{
super(phoneNumber); // Calling PhoneCall's constructor
this.minutes = minutes; //Setting minutes using this keyword
price = minutes * RATE; // calculating pice
}
public void display() // Function to display info
{
System.out.println("Outgoing phone call " +
getPhoneNumber() + " " + RATE +
" per minute at " + minutes +
" minutes. Total is $" +
getPrice()); // Display info
}
public String getPhoneNumber() // getter for phone number
{
return phoneNumber;
}
public double getPrice() // Getter for price
{
return price;
}
}