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;
}
}
Re: cable company billing..need help with methods
To pass a parameter, say an int, you do this:
public doubt methodName(int anInt)
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);
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)
Re: cable company billing..need help with methods
Also,
amountDue = businesscust(numberOfPremium, numberOfBasic);
Re: cable company billing..need help with methods
Quote:
Originally Posted by
printmatic
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-
Code java:
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.
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.
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?
Re: cable company billing..need help with methods
[highlight=java] put code here [/highlight]
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);
Re: cable company billing..need help with methods
I am trying to input the values of the number of premium/basic through console.
Code java:
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;
}
}
Re: cable company billing..need help with methods
Code java:
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.
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!