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)
{
// put your code here
}
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:
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.
Thanks for your help!