Hello everyone, I am close to finishing this project, but i am stuck on my PhoneTest class. To be specific I am stuck on the Method printBill. (This is an Inheritance project).
Their is a Date class for the month/day/year

Their is a parent class which is the Phone class
Then the children are CellPhone class and LandLinePhone class

Then their is the PhoneTest class, which is the one that I need to complete this project.

I have created the main method which creates a two-dimensional string array, calls createPhones, calculates monthly charge, and calls printBill. I have inserted all the data in the 2D string array already, I have also completed the createPhones method. My main issue is coming from method printBill, I am not exactly sure as of how to start it and complete it, so any guidance would be appreciated.

This is what method printBill needs to do:

Method printBill prints the the information for each instance to the system prompt as follows:

Type Minutes $xxx.xx x/x/xxxx
Type $xxx.xx x/x/xxxx
Type $xxx.xx x/x/xxxx
Type Minutes $xxx.xx x/x/xxxx
Total $xxx.xx

  • Where Type is the type of phone (cell or land line), Minutes is minutes used (only for cell phone),
    $xxx.xx is the monthly charge, and x/x/xxxx is the bill date.

Could someone show me how to code this?
-Here are my classes-

public class Date
{
    private int month;
    private int day;
    private int year;

    public Date ()
    {
        setMonth (0);
        setDay (0);
        setYear (0);

    }

    public Date (int m, int d, int y)
    {
        setMonth (m);
        setDay (d);
        setYear (y);
    }

    public void setMonth (int m)
    {
        month = m;
    }

    public int getMonth ()
    {
        return month;
    }

    public void setDay (int d)
    {
        day =d;
    }

    public int getDay()
    {
        return day;
    }

    public void setYear (int y)
    {
        year =y;
    }

    public int getYear ()
    {
        return year;
    }

    public String toString()
    {
        return ("Month" + month +
            "Day" + day +
            "Year" + year);
    }
}
public class Phone
{
    private String brand;
    private String phoneNumber;
    private double monthlyCharge;
    private Date billDate;


    public Phone ()
    {
        setBrand ("");
        setPhoneNumber ("");
        setMonthlyCharge (0.0);
        setBillDate (new Date () );
    }

    public Phone (String b, String pn, double mc, Date bd)
    {
        setBrand (B);
        setPhoneNumber (pn);
        setMonthlyCharge (mc);
        setBillDate (bd);
    }

    public void setBillDate (Date bd)
    {
        billDate = bd;
    }

    public Date getBillDate()
    {
        return billDate;
    }

    public void setBrand (String B)
    {
        brand = b;
    }

    public String getBrand ()
    {
        return brand;
    }

    public void setPhoneNumber ( String pn )
    {
        phoneNumber = pn;
    }

    public String getPhoneNumber ()
    {
        return phoneNumber;
    }

    public void setMonthlyCharge (double mc)
    {
        monthlyCharge = mc;
    }

    public double getMonthlyCharge ()
    {
        return monthlyCharge;
    }

    public String toString ()
    {
        return ("Brand: " + brand + 
             "Phone Numbe: " + phoneNumber +
             "Monthly Charge: " + monthlyCharge +
              billDate.toString () );
    }

    public void calculateMonthlyCharge ()
    {
        setMonthlyCharge(12.0);
    }
}
public class CellPhone extends Phone
{
    private double pricePerMinute;
    private int minutesUsed;

    public CellPhone ()
    {
        super();
        setPricePerMinute(0.0);
        setMinutesUsed (0);
    }

    public CellPhone (String b, String pn, double mc, Date bd, double ppm,int mu)
    {
        super (b, pn, mc, bd);
        setPricePerMinute(ppm);
        setMinutesUsed (mu);
    }

    public void setPricePerMinute (double ppm)
    {
        pricePerMinute = ppm;
    }

    public double getPricePerMinute ()
    {
        return pricePerMinute;
    }

    public void setMinutesUsed (int mu)
    {
        minutesUsed = mu;
    }

    public int getMinutesUsed()
    {
        return minutesUsed;
    }

    public String toString()
    {
        return (super.toString() + 
            "Price Per Minute " + pricePerMinute +
            "Minutes Used " + minutesUsed);
    }

    public void calculateMonthlyCharge()
    {
        super.calculateMonthlyCharge();

        setMonthlyCharge (pricePerMinute * minutesUsed + super.getMonthlyCharge());
    }
}
public class LandLinePhone extends Phone
{
    private double longDistanceFee;
    private int longDistanceMinutes;

    public LandLinePhone()
    {
        super();
        setLongDistanceFee(0.0);
        setLongDistanceMinutes(0);
    }

    public LandLinePhone (String b, String pn, double mc, Date bd, double ldf, int ldm)
    {
        super(b, pn, mc, bd);
        setLongDistanceFee(ldf);
        setLongDistanceMinutes(ldm);
    }

    public void setLongDistanceFee(double ldf)
    {
        longDistanceFee = ldf;
    }

    public double getLongDistanceFee ()
    {

        return longDistanceFee;
    }

    public void setLongDistanceMinutes (int ldm)
    {
        longDistanceMinutes = ldm;
    }

    public int getLongDistanceMinutes ()
    {
        return longDistanceMinutes;
    }

    public String toString()
    {
        return (super.toString() +
             "Long Distance Fee" + longDistanceFee +
             "Long Distance Minutes" + longDistanceMinutes);
    }

    public void calculateMonthlyCharge()
    {
        super.calculateMonthlyCharge();

        setMonthlyCharge (longDistanceFee + super.getMonthlyCharge());

    }
}

==========================================================================================================

public class PhoneTest
{
    public static void main (String args [])
    {

        double total;

        String d[][] = { {"Cell", "Samsung", "817-272-1234", "0", "3","31","2012", "0.6", "750"},
                 {"Land", "Apple", "817-272-4567", "0", "3","15","2012", "9.25", "300"},
                 {"Land", "Verizon", "817-272-9886", "0", "4","1","2012", "12.60", "1125"},
                 {"Cell", "LG", "817-272-6283","0","4", "15","2012", ".035", "250"} };

        Phone myPhones [] = createPhones(d);


        for (Phone m:myPhones)
        {
            System.out.println( m.getMonthlyCharge() );
        }



        //calculates the monthly charge

        // calls printBill


    }

    public static Phone[] createPhones(String d[][])
    {

        Phone myPhones[] = new Phone [d.length];

        for (int i = 0; i < d.length; i++)
        {

            if (d[i][0].equals("Cell"))

                myPhones [i] = new CellPhone (d[i][1], d[i][2], Double.parseDouble(d[i][3]),
                                 new Date (Integer.parseInt(d[i][4]), Integer.parseInt(d[i][5]), Integer.parseInt(d[i][6])),
                                 Double.parseDouble(d[i][7]),
                                 Integer.parseInt(d[i][8]) );
            else

                myPhones [i] = new LandLinePhone (d[i][1], d[i][2], Double.parseDouble(d[i][3]),
                                     new Date (Integer.parseInt(d[i][4]), Integer.parseInt(d[i][5]), Integer.parseInt(d[i][6])),
                                     Double.parseDouble(d[i][7]),
                                     Integer.parseInt(d[i][8]) );


        }

        return myPhones;

    }

    public static void printBill (Phone p[])
    {


        //need help as of what goes here
    }

}

To format the numbers like you want you can use the DecimalFormat class. It has lots of formatting characters to generate many different formats.
For the date, see the SimpleDateFormat class. It also has many formatting options.

Find the API docs for these classes at:
Link Anchor Text

Unfortunately, I doubt the professor will allow anything that she has not teached. I just dont know how to set up printBill and then call it in the main method, with the output required.

What methods have been taught for formatting the numeric data like you want? Those two classes are what are normally used.
The printBill method is called with a Phone class object which must contain all the data that is to be printed. Use the Phone class's methods to get the data and use println statements to print it in the order and format that you want.

You would call the printBill method when you wanted to print out the bill for a Phone class object passing a reference to the Phone class whose bill is to be printed.
printBill(aPhoneClassObjectRef);

Edited 4 Years Ago by NormR1

Ok yes, I understand what you are stating now. My issue is coming from the printBill method, I am simpy clueless as of how to code it so that I can call it from the main method and have the output that I need. Could you guide me or help me get a jump start as of what would need to go in their or how to do it?

What class is the printBill method in? Is there a reference to that class in the main() method?
If so you would use that reference to call the method:
refToClassWithMethod.printBill(refToThePhoneClassWithBillToPrint);

If printBill is in the same class as tne main() method then call it without the class reference:
printBill(refToThePhoneClassWithBillToPrint);

Where are the Phone class objects that you want to print the bills for?

In the method, get the values from the Phone class object and use the println statement to print them.

Edited 4 Years Ago by NormR1

Well my PhoneTest class I created the main method, the createPhones method, and printBill method. The main method calls createPhones, calcualtes the monthly charge, and calls printBill.

Here is my PhoneTest class

public class PhoneTest
{
    public static void main (String args [])
    {

        double total;

        String d[][] = { {"Cell", "Samsung", "817-272-1234", "0", "3","31","2012", "0.6", "750"},
                 {"Land", "Apple", "817-272-4567", "0", "3","15","2012", "9.25", "300"},
                 {"Land", "Verizon", "817-272-9886", "0", "4","1","2012", "12.60", "1125"},
                 {"Cell", "LG", "817-272-6283","0","4", "15","2012", ".035", "250"} };

        Phone myPhones [] = createPhones(d);



        for (Phone m:myPhones)
        {
            System.out.println( m.toString());
        }




        //calculates the monthly charge

        // calls printBill


    }

    public static Phone[] createPhones(String d[][])
    {

        Phone myPhones[] = new Phone [d.length];

        for (int i = 0; i < d.length; i++)
        {

            if (d[i][0].equals("Cell"))

                myPhones [i] = new CellPhone (d[i][1], d[i][2], Double.parseDouble(d[i][3]),
                                 new Date (Integer.parseInt(d[i][4]), Integer.parseInt(d[i][5]), Integer.parseInt(d[i][6])),
                                 Double.parseDouble(d[i][7]),
                                 Integer.parseInt(d[i][8]) );
            else

                myPhones [i] = new LandLinePhone (d[i][1], d[i][2], Double.parseDouble(d[i][3]),
                                     new Date (Integer.parseInt(d[i][4]), Integer.parseInt(d[i][5]), Integer.parseInt(d[i][6])),
                                     Double.parseDouble(d[i][7]),
                                     Integer.parseInt(d[i][8]) );


        }

        return myPhones;

    }

    public static void printBill (Phone p[])
    {

        //need help as of what goes here


    }

}

Where does the main() method call the printBill() method?

What have you changed? This code looks the same what you posted earlier.

Comments
right

Well it is, I was showing you that the printBill was in the same class as the main method, due to your previous post.

This article has been dead for over six months. Start a new discussion instead.