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

Thread: cable company billing..need help with methods

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question cable company billing..need help with methods

    Hello everyone, I have been stuck on this program for about a week... I am trying to learn how to use and create my own methods and such, but I don't have a book and am just learning from random things I read. Anyway, this program is supposed to use two methods that I made (residentcust and businesscust). These are supposed to contain the math used to find the prices for each, and the main method should call these to give the answer and such. I keep getting errors about things not initialized and a friend was trying to help me saying that I need to pass noOfPremChannels to the methods, but not sure how to do that really... any help is appreciated. Here is the code...
    -thank you-

    import java.util.*;
    import java.io.*;

    public class CableCompanyBilling
    {
    static Scanner console = new Scanner(System.in);

    //Named constants - residential customers
    static final double R_BILL_PROC_FEE = 4.50;
    static final double R_BASIC_SERV_COST = 20.50;
    static final double R_COST_PREM_CHANNEL = 7.50;

    //Named constants - business customers
    static final double B_BILL_PROC_FEE = 15.00;
    static final double B_BASIC_SERV_COST = 75.00;
    static final double B_BASIC_CONN_COST = 5.00;
    static final double B_COST_PREM_CHANNEL = 50.00;

    public static void main(String[] args)
    {
    //Variable declaration
    int accountNumber;
    char customerType;
    int noOfPremChannels;
    int noOfBasicServConn;
    double amountDue;

    System.out.println("This program computes "
    + "a cable bill.");

    System.out.print("Enter the account "
    + "number: "); //Step 1
    accountNumber = console.nextInt(); //Step 2
    System.out.println();

    System.out.print("Enter the customer type: "
    + "R or r (Residential), "
    + "B or b(Business): "); //Step 3
    customerType = console.next().charAt(0);

    System.out.print("Enter the number of "
    + "premium channels: ");
    noOfPremChannels = console.nextInt();
    System.out.println();

    System.out.println("Account number = "
    + accountNumber);



    if (customerType == 'R' || customerType == 'r')
    {
    amountDue = residentcust();

    }
    else if (customerType == 'B' || customerType == 'b'){
    amountDue = businesscust();

    }
    System.out.printf("Amount due = $%.2f %n",
    amountDue);
    }
    public static double residentcust()
    {
    int noOfPremChannels;
    double amountDue;
    int accountNumber;


    amountDue = R_BILL_PROC_FEE + //Step 5c
    R_BASIC_SERV_COST +
    noOfPremChannels *
    R_COST_PREM_CHANNEL;

    return amountDue;

    }

    public static double businesscust()
    {
    int noOfPremChannels;
    double amountDue;
    int accountNumber;
    int noOfBasicServConn;


    if (noOfBasicServConn <= 10) //Step 6e
    amountDue = B_BILL_PROC_FEE +
    B_BASIC_SERV_COST +
    noOfPremChannels *
    B_COST_PREM_CHANNEL;
    else
    amountDue = B_BILL_PROC_FEE +
    B_BASIC_SERV_COST +
    (noOfBasicServConn - 10) *
    B_BASIC_CONN_COST +
    noOfPremChannels *
    B_COST_PREM_CHANNEL;

    return amountDue;
    }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    To pass a parameter, say an int, you do this:

    public doubt methodName(int anInt)

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    Note, when you call residentcust, you have to have the parameter, though you don't need the data type again:

    so for public double residentcust(int anInt);

    you have residentcust(anInt);

    For instance, if you had

    int x = 5;

    double value = residentcust(x);

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    It appears that you're using both numberOfPremiimum.... and numberOfBasic ....

    You should probably pass 2 ints as parameters.

    public static double businesscust(int premium, int basic)

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    Also,

    amountDue = businesscust(numberOfPremium, numberOfBasic);

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    Quote Originally Posted by printmatic View Post
    Hello everyone, I have been stuck on this program for about a week... I am trying to learn how to use and create my own methods and such, but I don't have a book and am just learning from random things I read. Anyway, this program is supposed to use two methods that I made (residentcust and businesscust). These are supposed to contain the math used to find the prices for each, and the main method should call these to give the answer and such. I keep getting errors about things not initialized and a friend was trying to help me saying that I need to pass noOfPremChannels to the methods, but not sure how to do that really... any help is appreciated. Here is the code...
    -thank you-

    import java.util.*;
    import java.io.*;
     
    public class CableCompanyBilling
    {
        static Scanner console = new Scanner(System.in);
     
            //Named constants - residential customers
        static final double R_BILL_PROC_FEE = 4.50;
        static final double R_BASIC_SERV_COST = 20.50;
        static final double R_COST_PREM_CHANNEL = 7.50;
     
          //Named constants - business customers
        static final double B_BILL_PROC_FEE = 15.00;
        static final double B_BASIC_SERV_COST = 75.00;
        static final double B_BASIC_CONN_COST = 5.00;
        static final double B_COST_PREM_CHANNEL = 50.00;
     
        public static void main(String[] args)
        {
              //Variable declaration
            int accountNumber;
            char customerType;
            int noOfPremChannels;
            int noOfBasicServConn;
            double amountDue;
     
            System.out.println("This program computes "
                             + "a cable bill.");
     
            System.out.print("Enter the account "
                           + "number: ");                //Step 1
            accountNumber = console.nextInt();           //Step 2
            System.out.println();
     
            System.out.print("Enter the customer type: "
                           + "R or r (Residential), "
                           + "B or b(Business): ");      //Step 3
             customerType = console.next().charAt(0);
     
             System.out.print("Enter the number of "
                               + "premium channels: ");  
                noOfPremChannels = console.nextInt();    
               System.out.println();
     
                  System.out.println("Account number = "
                                  + accountNumber);  
     
     
     
            if (customerType == 'R' || customerType == 'r')
            {
              amountDue = residentcust();
     
            }
            else if (customerType == 'B' || customerType == 'b'){
              amountDue = businesscust();
     
            }
                   System.out.printf("Amount due = $%.2f %n",
                                  amountDue); 
        }
     public static double residentcust()
        {
          int noOfPremChannels;
          double amountDue;
          int accountNumber;
     
     
                amountDue = R_BILL_PROC_FEE +            //Step 5c
                            R_BASIC_SERV_COST +
                            noOfPremChannels *
                            R_COST_PREM_CHANNEL;
     
                return amountDue;
     
     }
     
        public static double businesscust()
        {
          int noOfPremChannels;
          double amountDue;
          int accountNumber;
          int noOfBasicServConn;
     
     
                if (noOfBasicServConn <= 10)             //Step 6e
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
                else
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                (noOfBasicServConn - 10) *
                                B_BASIC_CONN_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
     
              return amountDue;
        }
     
        }

    Have formatted your code to make it more readable.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    Also, I have no idea how long the account numbers are, but

    9000000000 may likely be invalid as ints go up to (2^32) - 1.

    It may be better to take the account number in as a String if the account numbers are really long.

  8. The Following User Says Thank You to javapenguin For This Useful Post:

    printmatic (November 7th, 2010)

  9. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: cable company billing..need help with methods

    thank you for the quick response! ok so I made the changes you suggested but I still get these errors about things not being initialized.... for instance

    amountDue is not initialized for line System.out.printf("Amount due = $%.2f %n", amountDue);

    noOfPremChannels is not initialized pretty much everywhere and same with noOfBasiceServConn.

    I tried to set these = 0 when I declared them so like double amountDue = 0; etc for each time it is declared...
    This actually lets the program compile but does not give me the right answer... if I do residential it always gives me 25, and 90 always for business?

    also how did you put the code in that nifty java box?

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    [highlight=java] put code here [/highlight]
    Last edited by javapenguin; November 7th, 2010 at 03:41 PM.

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    Also, could you show your new code?

    As for always getting that value, it should change it you pass it a different value.

    For instance,

    businesscust(200, 200);

    shouldn't, at least not always, be the same value as

    businesscust(100,100);


    did you have it like this:

    businesscust(numberOfPremiumChannels, numberOfBasicChannels);

  12. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: cable company billing..need help with methods

    I am trying to input the values of the number of premium/basic through console.
    import java.util.*;
    import java.io.*;
     
    public class CableCompanyBilling
    {
        static Scanner console = new Scanner(System.in);
     
            //Named constants - residential customers
        static final double R_BILL_PROC_FEE = 4.50;
        static final double R_BASIC_SERV_COST = 20.50;
        static final double R_COST_PREM_CHANNEL = 7.50;
     
          //Named constants - business customers
        static final double B_BILL_PROC_FEE = 15.00;
        static final double B_BASIC_SERV_COST = 75.00;
        static final double B_BASIC_CONN_COST = 5.00;
        static final double B_COST_PREM_CHANNEL = 50.00;
     
        public static void main(String[] args)
        {
              //Variable declaration
            int accountNumber;
            char customerType;
            int noOfPremChannels;
            int noOfBasicServConn;
            double amountDue;
     
            System.out.println("This program computes "
                             + "a cable bill.");
     
            System.out.print("Enter the account "
                           + "number: ");                //Step 1
            accountNumber = console.nextInt();           //Step 2
            System.out.println();
     
            System.out.print("Enter the customer type: "
                           + "R or r (Residential), "
                           + "B or b(Business): ");      //Step 3
             customerType = console.next().charAt(0);
     
             System.out.print("Enter the number of "
                               + "premium channels: ");  
                noOfPremChannels = console.nextInt();    
               System.out.println();
     
     
     
            if (customerType == 'R' || customerType == 'r')
            {
              amountDue = residentcust(noOfPremChannels);
     
            }
            else if (customerType == 'B' || customerType == 'b'){
              amountDue = businesscust(noOfPremChannels, noOfBasicServConn);
     
            }
             System.out.println("Account number = "
                                  + accountNumber);  
     
             System.out.printf("Amount due = $%.2f %n",
                                  amountDue); 
        }
     public static double residentcust(int bill)
        {
          int noOfPremChannels;
          double amountDue;
          int accountNumber;
     
     
                amountDue = R_BILL_PROC_FEE +            //Step 5c
                            R_BASIC_SERV_COST +
                            noOfPremChannels *
                            R_COST_PREM_CHANNEL;
     
                return amountDue;
     
     }
     
        public static double businesscust(int premium, int basic)
        {
          int noOfPremChannels;
          double amountDue;
          int accountNumber;
          int noOfBasicServConn;
     
     System.out.print("Enter the number of "
                               + "basic service "
                               + "connections: ");       //Step 6a
                noOfBasicServConn = console.nextInt();   //Step 6b
                System.out.println();     
     
                if (noOfBasicServConn <= 10)             //Step 6e
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
                else
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                (noOfBasicServConn - 10) *
                                B_BASIC_CONN_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
     
              return amountDue;
        }
        }

  13. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: cable company billing..need help with methods

    import java.util.*;
    import java.io.*;
     
    public class CableCompanyBilling
    {
        static Scanner console = new Scanner(System.in);
     
            //Named constants - residential customers
        static final double R_BILL_PROC_FEE = 4.50;
        static final double R_BASIC_SERV_COST = 20.50;
        static final double R_COST_PREM_CHANNEL = 7.50;
     
          //Named constants - business customers
        static final double B_BILL_PROC_FEE = 15.00;
        static final double B_BASIC_SERV_COST = 75.00;
        static final double B_BASIC_CONN_COST = 5.00;
        static final double B_COST_PREM_CHANNEL = 50.00;
     
        public static void main(String[] args)
        {
              //Variable declaration
            int accountNumber = 0;
            char customerType = 't';
            int noOfPremChannels = 0;
            int noOfBasicServConn = 0;
            double amountDue = 0;
     
            System.out.println("This program computes "
                             + "a cable bill.");
     
            System.out.print("Enter the account "
                           + "number: ");                //Step 1
            accountNumber = console.nextInt();           //Step 2
            System.out.println();
     
            System.out.print("Enter the customer type: "
                           + "R or r (Residential), "
                           + "B or b(Business): ");      //Step 3
             customerType = console.next().charAt(0);
     
             System.out.print("Enter the number of "
                               + "premium channels: ");  
                noOfPremChannels = console.nextInt();    
               System.out.println();
     
     
     
            if (customerType == 'R' || customerType == 'r')
            {
              amountDue = residentcust(noOfPremChannels);
     
            }
            else if (customerType == 'B' || customerType == 'b'){
              amountDue = businesscust(noOfPremChannels);
     
            }
             System.out.println("Account number = "
                                  + accountNumber);  
     
             System.out.printf("Amount due = $%.2f %n",
                                  amountDue);
        }
     public static double residentcust(int noOfPremiumChannels)
        {
     
          double amountDue;
       //    int accountNumber;
     
     
                amountDue = R_BILL_PROC_FEE +            //Step 5c
                            R_BASIC_SERV_COST +
                            noOfPremChannels *
                            R_COST_PREM_CHANNEL;
     
                return amountDue;
     
     }
     
        public static double businesscust(int noOfPremiumChannels)
        {
          int noOfPremChannels;
          double amountDue;
          int accountNumber;
          int noOfBasicServConn;
     
     System.out.print("Enter the number of "
                               + "basic service "
                               + "connections: ");       //Step 6a
                noOfBasicServConn = console.nextInt();   //Step 6b
                System.out.println();    
     
                if (noOfBasicServConn <= 10)             //Step 6e
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
                else
                    amountDue = B_BILL_PROC_FEE +
                                B_BASIC_SERV_COST +
                                (noOfBasicServConn - 10) *
                                B_BASIC_CONN_COST +
                                noOfPremChannels *
                                B_COST_PREM_CHANNEL;
     
              return amountDue;
        }
        }

    if you're having the customer enter the number of basic serv inside the method, don't pass that number as a parameter to the method.

    Parameters are usually for setting values.

    Note, the object name you give it in the parameter doesn't have to be the same one as the name you give it int the main method.

    You could have passed the method numberOFPremiumChannels but you'd have to change your noOfPremiumChannels to numberOfPremiumChannels in your computations in those methods.

    And you could pass it noOfPremiumChannels in the main and it wouldn't care that the names are different.

    As it is, the new code above should work.
    Last edited by javapenguin; November 7th, 2010 at 04:21 PM.

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

    printmatic (November 7th, 2010)

  15. #13
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: cable company billing..need help with methods

    ok I think I getting it now, Thank you so much for all your help, seems to actually give me the correct answers now!

    THANKS!

Similar Threads

  1. i am joining in a new company
    By ittoolvardhan in forum Member Introductions
    Replies: 2
    Last Post: September 6th, 2010, 02:54 AM
  2. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  3. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM
  4. Help with GUIs please & methods
    By killerknight141 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 07:12 AM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM