# I am stuck

• June 28th, 2010, 09:28 PM
hawkman4188
I am stuck
Hey everyone!

I am new to Java programming and am taking a class. I have some homework due on Friday and I am stuck. I'll begin with giving the directions and then give you the code I have already:

The electric company charges according to the following rate schedule:
9 cents per kilowatt-hour (kwh) for the first 300 kwh \$27
8 cents per kwh for the next 300 kwh (up to 600 kwh) \$27 + \$24 = 51
6 cents per kwh for the next 400 kwh (up to 1000 kwh) \$51 + \$24 =\$75
5 cents per kwh for all electricity used over 1000 kwh \$75 + X
Write a program that would repeatedly read in a customer number (an integer) and the usage for that customer in kwh (an integer). The program should include a method to compute the total charge for each customer. Use the following template for the method:
// takes kwh and calculates the total charge for that customer.
public static double findCharge(int kwh)
{
}
The program should terminate when the user enters a sentinel value of -999 for the customer number. The program should print a three-column chart listing the customer number, the kilowatt-hours used, and the total charge for each customer. The program should also compute and print the number of customers, the total kilowatt-hours used, and the total charges. Use a JTextArea component within a JOptionPane dialog box to display the output. You should name the source code file Hw4.java.
Note that you should not use arrays for this assignment.

Here is my code:

Code :

``` import java.text.*; import javax.swing.*;   //ask user for customer # and kwh usages   public class Hw4 {   // takes two int values and // returns the maximum of the two public static double findCharges ( int kwh ) { double total, first3, second3, third4, after1K;   first3 = kwh * .09; second3 = ( kwh - 300 ) * .08 + 27; third4 = ( kwh - 400 ) * .06 + 51; after1K = ( kwh - 1000 ) * .05 + 75;     if ( kwh <= 300 ) return first3 ;   if ( kwh <= 600 ) return second3;   if ( kwh <= 1000 ) return third4;   if ( kwh > 1000 ) return after1K;   return .1; }     public static void main( String[] arg ) {   //declare variables int cust, kwh, count; double findCharges; final int SENT = -99;   String message; //will contain final result   //initialize loop control variable count = 0; //begin count at 0 to keep track of loop     JTextArea area = new JTextArea( 10, 23 );   // add the scroll bars to the text area JScrollPane scroller = new JScrollPane( area );     //prompt the user for the next value cust = Integer.parseInt(JOptionPane.showInputDialog( "Please enter a customer number or " + SENT + " to quit:" )); kwh = Integer.parseInt(JOptionPane.showInputDialog( "Please enter the usage in KWH" ));   findCharges = findCharges ( kwh );   do {     area.setText ( "Customer No. Usage in Kwh. Total Charges\n" ); area.append( "\n" ); area.append( "**************************************************" ); area.append( "\n" ); area.append( cust + " " + kwh + " " + findCharges ); }           while ( cust != SENT ); //loop repetition condition { //update count count = count + 1; //loop control variable   //prompt user for the next value cust = Integer.parseInt(JOptionPane.showInputDialog( "Please enter a customer number or " + SENT + "to quit:" )); kwh = Integer.parseInt(JOptionPane.showInputDialog( "Please enter the usage in KWH" ));   } //end of while       JOptionPane.showMessageDialog( null, scroller );   System.exit( 0 );     }     }```

I really just don't know where to go from here. I'm not sure the best way to get it to remember all the values separately and then print each of them.

• June 28th, 2010, 09:47 PM
copeg
Re: I am stuck
Quote:

Originally Posted by hawkman4188
I really just don't know where to go from here. I'm not sure the best way to get it to remember all the values separately and then print each of them.

If you use an object oriented approach, you can 'remember' the input and calculation values by creating objects for each calculation. For example, you could create a class that has these values, and for each entry/calculation create a new object (an instance of this class you created) and add it to an array or List. When you need to print everything out, just loop through the array/list and print out the necessary values. Looping through the array/list will also allow you to tally and print the required totals. Hope this helps!
• June 28th, 2010, 09:52 PM
hawkman4188
Re: I am stuck
We are not allowed to use arrays for this assignment. Is there another way to do the same thing?
• June 28th, 2010, 09:56 PM
DestinyChick1225
Re: I am stuck
i sent you something....take a look at it and see if it helps alittle
• June 28th, 2010, 10:07 PM
hawkman4188
Re: I am stuck
Thanks so much for the reply! I took a look at it, but it doesn't do what I need it to. Thanks anyway!
• June 29th, 2010, 12:46 AM
Faz
Re: I am stuck
Quote:

Originally Posted by DestinyChick1225
i sent you something....take a look at it and see if it helps alittle

It's a general rule of thumb that these things should be solved in public so people have a chance of finding it without asking the same question later on.

You don't need an array here you just need a variable that increments each time you call the findCharge() method. So say for customers you would have a class variable called numCust(that is an int) then at the end of the method have
Code :

`numCust++//this will increment it by one`

I'll let you figure the other 2 out for yourself.

And I'll take a look at the rest in a while.

However if you could describe what is actually happening up to now I mean is everything working up to the output? Does it even compile? Does it give the wrong numbers? All this helps us in finding the problem quickly.