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 4 of 4

Thread: Having trouble understanding assignment dealing with constructors with classes??

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble understanding assignment dealing with constructors with classes??

    Hey all, I'm having quite a bit of trouble understanding the assignment that was given to me. I don't understand how you can have several constructors... when you just need one? I'm not quite sure I understand what it's asking for as the teacher wasn't as clear as I'd like.

    EDIT: just changed the class file and the main file... it is now updated... is that what they meant by three constructors?

    Program 5: 	Class Construct & Constructors 
     
    Problem Description:
     
      An Internet service provider has three different subscription 
       packages for its customers:
     
       Package A:  For $15 per month with 50 hours of access provided.
                   Additional hours are $2.00 per hour over 50 hours.
                   Assume usage is recorded in one-hour increments, 
     
       Package B:  For $20 per month with 100 hours of access provided.
                   Additional hours are $1.50 per hour over 100 hours.
     
       Package C:  For $25 per month with 150 hours access is provided.
                   Additional hours are $1.00 per hour over 150 hours.
       Assume 30-day billing cycle.
     
     
       Design a Customer Class. The Customer Object has one
       principal behavior: PayBill. You will need other methods
       to support that behavior.
     
       Design three Constructors:
          Full set of parameters.
          Customer ID.
          No-Arg.
     
          Write a program that creates four Customer Objects.
          Each object pays the bill.
          Invoke each constructor. 
          Validate all input.
     
     
       Demonstrate your design using the following Test Cases:
     
       Cust ID Test Case    Package     Hours
       ------- ---------    -------     -----
        1101       1           A          90
        2202       2           B         140
        3303       3           C         190
        4404       4           b         730



    And here is what I have so far for the main method...


    public class customerPayBill{
     
     
    	public static void main(String[] args){
     
    		Customer mark = new Customer("1101", "1", "A", 90);
    		Customer julie = new Customer("2202");
    		Customer dubstep = new Customer("3303");
    		Customer joey = new Customer();
    		mark.payBill();
    		julie.payBill();
    		dubstep.payBill();
    		joey.payBill();
     
    	}
    }

    And here is what I have for the class called Customer...


     
    public class Customer{
     
    	private String customerID;
    	private String testCase;
    	private String ispPackage;
    	private double hours;
     
     
    	public Customer(String custID, String testC, String pack, double h){
     
     
    		customerID = custID; //customer ID
    		testCase = testC; // test case #
    		ispPackage = pack; // which package the customer has
    		hours = h; //# of hours used up by customer
     
    	}
     
    	public Customer(String custID){
     
    		customerID = custID;
     
     
     
    		if(customerID == "2202")
    		{
    			testCase = "2";
    			ispPackage = "B";
    				hours = 140;
    		}
     
    		else if (customerID == "3303"){
     
    			testCase = "3";
    			ispPackage = "C";
    				hours = 190;
    		}
     
     
    	}
     
    	public Customer(){
    		customerID = "4404";
    		testCase = "4";
    		ispPackage = "B";
    		hours = 730;
     
     
    	}
     
    	public void payBill(){
     
    		double price = 0;
     
    		if(ispPackage == "A")
    		{
    			price = 15 + (2.00*(hours - 50));
    			System.out.println("Customer ID: " + customerID +  " owes the company $" + price);
    		}
     
    			else if(ispPackage == "B")
    			{	price = 20 + (1.50*(hours - 100));
    				System.out.println("Customer ID: " + customerID +  " owes the company $" + price);
    			}
    					else if (ispPackage == "C")
    				{
    					price = 25 + (1.00*(hours - 150));
    					System.out.println("Customer ID: " + customerID +  " owes the company $" + price);
    				}
     
    	}
    }

    My output is

     
    Customer ID: 1101 owes the company $95.0
    Customer ID: 2202 owes the company $80.0
    Customer ID: 3303 owes the company $65.0
    Customer ID: 4404 owes the company $965.0
    Last edited by orbin; July 6th, 2012 at 11:01 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble understanding assignment dealing with constructors with classes??

    Possibly, though I'm not sure if that's the intent. It's best to check with a member of the teaching staff to make sure.

    In general the test cases should be separate from the constructors (i.e. don't hard-code customer ID 2202 to package B with 140 hours).

    For standard Java programming practices, what's likely to happen is you'll leave some fields initiated to a "default" value which creates a complete object.

    For example, take take the second constructor which accepts just a customer ID. It's obvious that it should simply accept a customer ID and assign that to the appropriate field. However, you shouldn't assume that its ID indicates anything special about what package they have or how many hours they've used. So an appropriate choice may be to say all new customers which don't specify these fields get package A and they haven't used any hours.

    The constructor which takes no arguments would like-wise have a similar behavior as above, though you'd have to pick a customer ID for them (for example a random number, or default to 0).

    Now you can create setter methods which allow the client to modify which package a customer has, how many hours they've used, and even what their ID is. It's also possible to make checks to ensure that inputs are correct. For example, it makes no sense for a customer to have used -10 hours of internet. These methods typically have a form similar to this:

    // example setter method
    public void setHours(double hours)
    {
        if(hours >= 0)
        {
            // ok to replace current hours
            this.hours = hours;
        }
    }

    Then when you want to modify a particular customer's properties, you simply need to call that method.

    // inside your main method
    Customer dave = new Customer();
    dave.setCustomerID("9999");
    dave.setISPPackage("C");
    dave.setHours(1.5);


    One other thing I'd like to point out which isn't highlighted by the test cases is the way you're handling the payBill method.

    Currently you're simply subtracting off the amount of hours each package allows you to have without overage charges. This works so long as the customer always uses as many or more hours than their plan allows. However, if the customer uses less than that amount the code would lead one to believe that the customer gets reimbursed for unused hours. In fact, if a customer has plan A and uses 0 hours of internet, the payBill method would say they owe -$85, or that the company needs to pay the customer $85. I'll leave it to you to try and figure out a way to remedy this problem (hint: use if/else statements).

    Last note: If you're implementing the Customer class in this manner, you don't need a testCase field. Again, that's something specific to a test case, not something which would be found in a generic Customer. This information can be captured elsewhere.

    Ask if you have any questions about the above information.
    Last edited by helloworld922; July 7th, 2012 at 01:06 AM.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding assignment dealing with constructors with classes??

    Thanks for the in-depth reply! I had trouble understanding what you meant by dealing with test cases seperately. I know you've probably seen more real world experience than what I'm dealing with, but I figured that was just a field that didn't really deal with anything other than to just fill space. We are a beginner java class, so I don't believe that we would have to be that in-depth(even though I'm sure it's better practice)...but the reason I coded it the way I did was because I figure it just wants us to call all three constructors with four objects to show that we actually understood the concept and less about the actual customer database. I need to fix the hours part, because I realize that the company would owe them money. I still don't understand the testcase thing... I figure it was just some random field I was using but didn't really matter?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble understanding assignment dealing with constructors with classes??

    What I mean by keeping test cases separate is that your implementation shouldn't depend on the provided test cases, but must be able to handle them correctly from an "end user" perspective, i.e. your implementation of Customer shouldn't need to be changed to be able to handle other test cases, such as a new Customer with id = 1234, package C, with 30 hours used.

Similar Threads

  1. Replies: 1
    Last Post: April 14th, 2012, 01:45 PM
  2. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 10:11 PM
  3. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2012, 09:29 AM
  4. Need help with classes and constructors!!
    By rayp4993 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 14th, 2011, 01:02 PM
  5. Having trouble understanding what teacher said for build Tree.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 08:22 PM