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

Thread: I am stuck

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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)
    {
    // 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!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I am stuck

    Quote Originally Posted by hawkman4188 View Post
    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!
    Last edited by copeg; June 28th, 2010 at 09:49 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am stuck

    We are not allowed to use arrays for this assignment. Is there another way to do the same thing?

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am stuck

    i sent you something....take a look at it and see if it helps alittle

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  6. #6
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: I am stuck

    Quote Originally Posted by DestinyChick1225 View Post
    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
    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.
    Last edited by Faz; June 29th, 2010 at 12:49 AM.

Similar Threads

  1. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM
  2. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  3. Help I'm stuck on code.
    By GreenM in forum Loops & Control Statements
    Replies: 3
    Last Post: January 29th, 2010, 09:27 PM
  4. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM
  5. stuck on this program can any one help me
    By clive in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2009, 05:54 PM