Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Inheritance; Problem with Test class

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Inheritance; Problem with Test class

    Hello Everyone, everything has been going good with the whole JAVA learning process until today. I have been working on Inheritance and have not been able to complete this project.
    My problem is coming from the test class, I simply do not know how to set it up to output the proper information. So first I have to create a Date class, then a Phone class which is the parent of the CellPhone class and LandLinePhone class. I then have to create a test class called PhoneTest. Everything has been completed except for the PhoneTest class which is the one that outputs everything. Thanks for looking at my issue, hopefully someone can point me in the correct direction.

    First I need to develop the classes according to the UML Provide (ill attach a picture of the UML for a visual representation)

    in the PhoneTest class the main method created the two dimensional string array, calls createPhones, calculates the monthly charge, and calls printBill

    The Method printBill prints the information for each instance to the system prompt.

    (along with the UML attachment I will also attach the instruction page of the program for better understanding of what I need to do.

    -----Here are my current programs-------
    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 Number" + 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", "3/31/2012", "0.6", "750"},
    				 {"Land", "Apple", "817-272-4567", "3","15","2012", "9.25", "300"},
    				 {"Land", "Verizon", "817-272-9886", "4","1","2012", "12.60", "1125"},
    				 {"Cell", "LG", "817-272-6283", "4","15","2012", ".035", "250"} };
     
    		Phone myPhones [] = new Phone [4];
     
    		myPhones = createPhones(d);
     
    		System.out.println (myPhones);
     
    		//calls createPhones
     
    		//calculates the monthly charge
     
    		// calls printBill
     
     
    	}
     
    	public Phone[] createPhones(String d[][])
    	{
     
    		Phone myPhones[] = new Phone [d.length];
     
    		for (int i = 0; i < d.length; i++)
    		{
     
    			if (d[i][0].equals("CellPhone"))
     
    				myPhones [i] = new CellPhone (d[i][1], d[i][2], 
    						   	     new Date (Integer.parseInt(d[i][3])),
    						   	     Double.parseDouble(d[i][4]),
    						  	     Integer.parseInt(d[i][5]) );
    			else
     
    				myPhones [i] = new LandLinePhone (d[i][1], d[i][2], 
    						       	         new Date (Integer.parseInt(d[i][3])),
    						      	         Double.parseDouble(d[i][4]),
    						      	         Integer.parseInt(d[i][5]) );
     
    			//need help as of what would go in here
    			//Something is still wrong with myPhones
     
     
    		}
    	}
     
    	public void printBill (Phone p[])
    	{
    		//need help as of what goes here
    	}
     
    }



    Screen shot 2012-04-05 at 3.24.17 PM.jpg

    Screen shot 2012-04-05 at 3.24.25 PM.jpg


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    There are a few problems in you createPhones method:

    1) You String array does not contain "CellPhone" which is used in your logic test. This will then default to always creating a new LandLinePhone.
    2) The number/type of arguments pass in new CellPhone and new LandLinePhone do not match the respective constructors.
    2.1) The new Date() format also does not conform to the constructor. You only give one int value instead of three.

    If you can get those things worked out then you can check to see if the values are correct by outputting the value of the toString() method for each Phone. After you get that then I would move on to the printBill method.

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 6th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Got it I made the changes needed. Thanks alot for your input, I''ll post my PhoneTest class to see if my changes are correct in regards to your response.

    public class PhoneTest
    {
    	public static void main (String args [])
    	{
     
    		double total;
     
    		String d[][] = { {"Cell", "Samsung", "817-272-1234", "3","31","2012", "0.6", "750"},
    				 {"Land", "Apple", "817-272-4567", "3","15","2012", "9.25", "300"},
    				 {"Land", "Verizon", "817-272-9886", "4","1","2012", "12.60", "1125"},
    				 {"Cell", "LG", "817-272-6283", "4","15","2012", ".035", "250"} };
     
    		Phone myPhones [] = new Phone [4];
     
    		myPhones = createPhones(d);
     
    		System.out.println (myPhones);
     
    		//calls createPhones
     
    		//calculates the monthly charge
     
    		// calls printBill
     
     
    	}
     
    	public 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], 
    						   	     new Date (Integer.parseInt(d[i][3])),
    						   	     new Date (Integer.parseInt(d[i][4])),
    						   	     new Date (Integer.parseInt(d[i][5])),
    						   	     Double.parseDouble(d[i][6]),
    						  	     Integer.parseInt(d[i][7]) );
    			else
     
    				myPhones [i] = new LandLinePhone (d[i][1], d[i][2], 
    						       	         new Date (Integer.parseInt(d[i][3])),
    						   	     	 new Date (Integer.parseInt(d[i][4])),
    						   	     	 new Date (Integer.parseInt(d[i][5])),
    							         Double.parseDouble(d[i][6]),
    						      	         Integer.parseInt(d[i][7]) );
     
    			//need help as of what would go in here
    			//Something is still wrong with myPhones
     
     
    		}
    	}
     
    	public void printBill (Phone p[])
    	{
    		//need help as of what goes here
    	}
     
    }

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Scratch that, I managed to fix the new Date to

    if (d[i][0].equals("Cell"))
     
           myPhones [i] = new CellPhone (d[i][1], d[i][2], 
    			                          new Date (Integer.parseInt(d[i][3]), Integer.parseInt(d[i][4]),    
                                                      Integer.parseInt(d[i][5])),
    				                  Double.parseDouble(d[i][6]),
    					          Integer.parseInt(d[i][7]) );
    else
     
    	myPhones [i] = new LandLinePhone (d[i][1], d[i][2], 
    						      new Date (Integer.parseInt(d[i][3]), Integer.parseInt(d[i][4]), 
                                                          Integer.parseInt(d[i][5])),  
    						      Double.parseDouble(d[i][6]),
    						      Integer.parseInt(d[i][7]) );

    but now when I try to compile it is complaining about new CellPhone and newLandLinePhone.
    This is what it states when I try to compile:


    PhoneTest.java:39: cannot find symbol
    symbol : constructor CellPhone(java.lang.String,java.lang.String,Date,d ouble,int)
    location: class CellPhone
    myPhones [i] = new CellPhone (d[i][1], d[i][2],
    ^
    PhoneTest.java:45: cannot find symbol
    symbol : constructor LandLinePhone(java.lang.String,java.lang.String,Da te,double,int)
    location: class LandLinePhone
    myPhones [i] = new LandLinePhone (d[i][1], d[i][2],
    Last edited by Charlie.beat; April 6th, 2012 at 04:42 PM.

  6. #5
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    You are on the right track but there are still come issues that need fixing. Also there is something that I failed to realize the first time.

    - So your basic constructor for each Phone "child" class is Object(String, String, Double, Date, Double, Int). The real issue you are having is making sure to have enough arguments when you create a new instance of the object, in this case there are 6 parameters. The problem really starts (which I missed :/ sry) with your array. The array values will be used as the new Object's parameters which would usually mean we need 6 array values (not necessarily true but we will go with it) and it is true that each 'row' of the array contains 6 values or 'columns' (I am counting the 3 date values as 1) but one of the columns is only used to figure out the type of phone. This means that there are only 5 values that contain data for the constructor, which requires 6, and this will not work. Basically you are missing a value in your array we can do this by comparing the basic format of your array to your constructor(s).

    String | String | Date | Double | Int <----Array (omitting the first value as it isn't used in the constructor)
    String | String | Double | Date | Double | Int <----Constructor(s)

    From this we can see (after a little code scanning) the double value indicating the monthly charge is missing from the array between the phone number and the date. So before anything else can work you need to enter that value into the array.

    - Now we will tackle the issues with creating the new phone Objects:
    myPhones [i] = new CellPhone (d[i][1], d[i][2], 
    new Date (Integer.parseInt(d[i][3])),//     \\\\\
    new Date (Integer.parseInt(d[i][4])),//     |||||> You are trying to create 3 date objects that take 1 int value where you only need 1 date object that takes 3 int values
    new Date (Integer.parseInt(d[i][5])),//     /////
    Double.parseDouble(d[i][6]),
    Integer.parseInt(d[i][7]) );
    // Since both the CellPhone and LandLinePhone have the same signature (number of parameters and parameter types) I will only go over the one
    // The constructor signature, in this case, is CellPhone(String, String, Double, Date, Double, Int)
    // What you have is CellPhone(String, String, Date, Date, Date, Double, Integer)
    // What you need to do is get both to match up
    // Don't forget to retrieve the correct array values after you alter it

    - One last thing is that System.out.println(myPhones) won't do much since myPhones is an Array. To get some meaningful output you can iterate through each object in the array with a loop and call the toString() method for each.

    So in review you need to: Correct the Array, Change the new CellPhone/LandLinePhone to match the constructor signature, and then iterate through the returned array to see if everything came out alright.


    EDIT:
    Started writing before your last post :\ got side-tracked
    Last edited by KucerakJM; April 6th, 2012 at 04:48 PM. Reason: Slow Typing :\

  7. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 6th, 2012)

  8. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Wow you have definitely been a great help today, feels like I am getting somewhere now.
    I believe i have all the things I needed to fix up to right now done, now in regards to the the to call the toString(). I should place it in the main method correct? I placed a for loop in the main method, but I am also getting 1 error here. Been trying to fix it also, but its just out of my knowledge =/

    Here is my code-

    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", "4", "15","2012", ".035", "250"} };
     
    		Phone myPhones [] = new Phone [4];
     
    		myPhones = createPhones(d);
     
    		for (CellPhone m:myPhones)
    		{
    			System.out.println(d.toString());
    		}
     
     
     
    		//calls createPhones
     
    		//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]) );
     
    		}
    	}
     
    	//public static void printBill (Phone p[])
    	//{
    		//need help as of what goes here
    	//}
     
    }
    Last edited by Charlie.beat; April 6th, 2012 at 06:13 PM.

  9. #7
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    4 Things left to adjust for it to work out.

    1) The last row in your array has duplicate values (an extra "4").
    2) The for loop is just about right, but myPhones is not an array of CellPhone but rather Phone.
    3) You will also want to call the toString() method of the Phone object and not the array, in this case 'm'.
    4) The createPhones method states that it will return Phone[], but there isn't any return statement in the method. The good news is that you create a Phone[] in your method called myPhones; return that and all is well.

    Remember to always ensure that if a method is supposed to return something then there should be a return statement.

  10. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 7th, 2012)

  11. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Ok got it, I am glad to see that it finally compiled, but I get this in my output:


    new-host-3:desktop carlosalvarez$ java PhoneTest
    [[Ljava.lang.String;@33f42b49
    [[Ljava.lang.String;@33f42b49
    [[Ljava.lang.String;@33f42b49
    [[Ljava.lang.String;@33f42b49
    new-host-3:desktop carlosalvarez$
    ===================================

    Here is my code:

    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(d.toString());
    		}
     
     
     
    		//calls createPhones
     
    		//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
    	//}
     
    }

    Thanks for guiding me through this.
    Last edited by Charlie.beat; April 7th, 2012 at 12:29 AM.

  12. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Ok i finally got some output back:
    I made this change
    for (Phone m:myPhones)
    		{
    			System.out.println(m.toString());
    		}

  13. #10
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    Wonderful, glad to see things are coming together for you. Now that you are able to iterate through each Phone object successfully, the rest is a matter of calling the class methods and formatting the output as described in the Instructions (which is so small it's practically impossible to read).

  14. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 7th, 2012)

  15. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Awesome, now in regards to printBill, thats the one thats going to print all the information in the way the requirements requires me to do correct?

  16. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Thank you, in regards to printBill, this is the part that will output the Type, Minutes, monthly charge and the bill date. 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.

    So is the for loop suppose to be in printBill instead of the main method?

  17. #13
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    The loop could be in either, just depends on what you want. You can either pass a single Phone object in the printBill method or the whole Phone array in which case a loop would be needed in printBill.

  18. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Yes, what I am trying to do is for 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.

  19. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    then the main method calls printBill

  20. #16
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    So what is your specific problem at this point? Looking at your instructions again it would seem the loop would go in the printBill method.

  21. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 9th, 2012)

  22. #17
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    So like something like this?

    public static void printBill (Phone p[])
    {
     
    		for (Phone m:myPhones)
    		{
     
     
    		}
     
     
     
    		//need help as of what goes here
     
     
    	}
     
    }

    Honestly I have no idea as of how to even start this one. It sucks.

  23. #18
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    Since you need to print data, look at your classes to see what method returns a String value containing all the information. Your for loop isn't quite right, remember that the Phone array used by the method is 'p'. Finding the total is a matter of adding the specific instance variable of each Phone i.e. total += m.getVariable.

  24. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 9th, 2012)

  25. #19
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance; Problem with Test class

    Ok, dang feels like I am lost here, its a bad feeling, sorry for the dumb headed mistakes.
    But, is this somewhere along the right direction?

    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());
     
    			printBill(CellPhone);
     
    		}
     
     
     
     
    		//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[])
    	{
     
    		for (Phone p:myPhones)
    		{
     
    			System.out.println (p.toString);
     
                          total+= m.getMonthlyCharge;
     
     
    		}
     
     
     
     
    	}
     
    }

  26. #20
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Inheritance; Problem with Test class

    There are still some issues that need to get worked out, for instance you are trying to use the variable total outside it's scope. It is defined in the main method but you try to use it in the printBill method.
    Last edited by KucerakJM; April 16th, 2012 at 05:34 PM.

  27. The Following User Says Thank You to KucerakJM For This Useful Post:

    Charlie.beat (April 9th, 2012)

Similar Threads

  1. [SOLVED] Small problem regarding inheritance of classes
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 10th, 2011, 02:11 PM
  2. Multilevel inheritance of an abstract class
    By user2205 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 27th, 2011, 05:00 PM
  3. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  4. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM