Phone Class using Java
Posted by Samath
Last Updated: January 17, 2024

Write a class named Phone to represent a telephone. This class is to be the parent class for several types of phones. The class should define the following methods:
a) A constructor that accepts the name of a telecoms provider, for example “LIME” (use a string for this), and the phones telephone number (use a string for this also).
b) The getter methods getProvider, and getNumber to return the respective values;
c) A method named call that accepts a telephone number and adds it to a list of calls made. This method should not return a value.

Phone Class:

import java.util.*;
public class Phone 
{
    protected String telecoms_provider;
    protected String tel_number;
    protected ArrayList<String> calls_made = new ArrayList();
    
    public Phone(String tele_p, String tele_n)
    {
        telecoms_provider = tele_p;
        tel_number = tele_n;
    }
    
    public String getProvider()
    {
        return telecoms_provider;
    }
    
    public String getNumber()
    {
        return tel_number;
    }
    
    public void call(String tel_num)
    {
        calls_made.add(tel_num);
    }
}

Write the class BangerPhone as a subclass of Phone. A banger phone is a normal mobile phone that can only place calls. The class shall have the following methods:
a) A constructor that accepts the name of the telecoms provider, and the telephone number.
b) A constructor that accepts the telephone number only and sets the name of the telecoms provider to “ROAM”;
c) A method named call that accepts a telephone number and adds it the list of calls made. In addition, the method should add $10 to the total cost of calls made.
d) A toString method that returns a string formatted as follows:

Phone:  <telephone number>                   Provider: <provider’s name>
Calls made:         <number 1>
                           <number 2>
                                  …
Total Calls: <number of calls>
Total Cost: <total cost of calls made>

BangerPhone Class:

public class BangerPhone extends Phone
{
    private double total_cost;
    
    public BangerPhone(String t_p, String t_n)
    {
        super(t_p, t_n);
    }
    
    public BangerPhone(String t_n)
    {
        super("ROAM", t_n);
    }
    
    public void call(String tel_num)
    {
        super.call(tel_num);
        total_cost += 10;
    }
    
    public String toString()
    {
        String result;
        String calls ="";
        int count = 0;
        
        for(String c : calls_made)
        {
            calls += "\n\t\t"+c;
            count++;
        }
        
        result = "Phone: "+ tel_number +"  Provider: "+telecoms_provider;
        result += "\nCalls Made: "+ calls;
        result+="\nTotal Calls: "+ count;
        result+="\nTotal Cost: "+ total_cost+"\n\n";
        
        return result;
    }
}

Write the class SmartPhone as a subclass of the class Phone. A smart phone has a provider name, a telephone, and an IP address..
a) A constructor that accepts the phone number and IP address (a string) in that order. The constructor should set the provider’s name to “WUZUP” by default.
b) A method named call that accepts a telephone number and adds it the list of calls made.
c) A toString method that returns a string formatted as follows:

Phone: <telephone number>              Provider: <name of provider>
Calls made: <number 1>
                   <number 2>
                          ….
Total Calls: <number of calls

SmartPhone Class:

public class SmartPhone extends Phone
{
    private String IP_address;
    public SmartPhone(String pn, String ip)
    {
        super("WUZUP", pn);
        IP_address = ip;
    }
    
    public void call(String tel_num)
    {
        calls_made.add(tel_num);
    }
    
    public String toString()
    {
        String result;
        String calls ="";
        int count = 0;
        
        for(String c : calls_made)
        {
            calls += "\n\t\t"+c;
            count++;
        }
        
        result = "Phone: "+ tel_number +"  Provider: "+telecoms_provider;
        result += "\nCalls Made:"+ calls;
        result+="\nTotal Calls: "+ count+"\n\n";
        
        return result;
    }
}

Write code to test your classes by performing the following tasks:
a) Create a banger phone using the first constructor of the BangerPhone class.
b) Create a second banger phone using the second constructor of the BangerPhone classs;
c) Create a Smartphone using the constructor of the SmartPhone class.
d) Call the police (“119”) on each of the phones then print each object.

PhoneTest:

public class PhoneTest {
    public static void main(String[] args) {
        BangerPhone bp = new BangerPhone("Digi","123456789");
        BangerPhone bp2 = new BangerPhone("564789658");
        SmartPhone sp = new SmartPhone("987563214","192.1.1.2");
        
        bp.call("119");
        bp2.call("119");
        sp.call("119");
        
        System.out.print(bp);
        System.out.print(bp2);
        System.out.print(sp);
    }
}
Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024