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

Thread: Looking for help on two codes:

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Looking for help on two codes:

    Hello everyone, I am new to the forum, nice to meet you!

    I am new to java programming and I am still learning the ropes. I joined here hopefully to get good feedback.

    What I have here are two codes I need help with. They are pretty much done except for a small error in each one. I was hoping
    someone could help me understand where I am missing what. Please understand I am new to this and don't yet understand all
    the terminology yet.

    Any help would be appreciated!


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    After the first time through the loop, the mpgtank and totalmpg should be different, for some reason I can't get it to change:

    import java.util.Scanner;
    public class Lab5a {
    public static void main( String args[] ) {
     
    Scanner input = new Scanner( System.in );
     
    double milesdriven = 1.1; 
    double gallonsused = 1.1; 
    double mpgtank = 1.1; 
    double totalmpg = 1.1; 
     
     
    System.out.print("Enter miles since tank was full (-999 to quit) "); 
    milesdriven = input.nextDouble();
    while(milesdriven!=-999) 
    {
     
    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();
     
    mpgtank  = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;
     
    System.out.printf( "MPG this tankful: %1.2f ",  mpgtank );
     
    System.out.println();
     
    System.out.printf("Total MPG: %1.2f", totalmpg);
     
    System.out.println();
     
    System.out.print("Enter miles since tank was full (-999 to quit) "); 
    milesdriven = input.nextDouble();
    {
     
    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();
     
    mpgtank  = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;
     
    System.out.printf( "MPG this tankful: %1.2f ",  mpgtank );
     
    System.out.println();
     
    System.out.printf("Total MPG: %1.2f", totalmpg);
     
    System.out.println();
     
    System.out.print("Enter miles since tank was full (-999 to quit) "); 
    milesdriven = input.nextDouble();
    while(milesdriven!=-999) 
    {
     
    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();
     
    mpgtank  = milesdriven / gallonsused;
    totalmpg = milesdriven / gallonsused;
     
    System.out.printf( "MPG this tankful: %1.2f ",  mpgtank );
     
    System.out.println();
     
    System.out.printf("Total MPG: %1.2f", totalmpg);
     
    System.out.println();
     
    System.out.println("Press any key to continue . . .");
     
    }
    }}}}

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    This second code is an Easter Egg hunt game. The user is supposed to have the ability to enter Q to quit the game, I do no understand how to put this in and where:


    import java.util.Scanner;
     
    public class EggHunt{
     
    	public static void main(String args[]){
     
    		Scanner input = new Scanner( System.in );
     
    String gender; 
    String type;
    int countboys=0; 
    int countgirls=0; 
    int point=0;
     
    System.out.println("Enter gender of competitor: ");
    gender = input.next();
     
    System.out.println("Enter egg type: A, B, or C"); 
    type = input.next();
     
    if(type.equalsIgnoreCase("A"))
    point=5;
    else if(type.equalsIgnoreCase("B"))
    point=10;
    else
    point=30;
     
    if(gender.equalsIgnoreCase("M"))
    countboys+=point; 
    else 
    countgirls+=point;
     
    System.out.println("Enter gender of competitor: ");
    gender = input.next();
     
    System.out.println("Enter egg type: A, B, or C"); 
    type = input.next();
     
    if(type.equalsIgnoreCase("A"))
    point=5;
    else if(type.equalsIgnoreCase("B"))
    point=10;
    else
    point=30;
     
    if(gender.equalsIgnoreCase("G"))
    countgirls+=point; 
    else 
    countboys+=point;
     
    System.out.println();
     
    System.out.println("The boys scored "+countboys+" and girls scored "+ countgirls);
    if(countboys>countgirls)
    System.out.println("Boys are the champions");
    else if(countgirls>countboys)
    System.out.println("Girls are the champions");
    else
    System.out.println("There is a tie between girls and boys");
    } }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Looking for help on two codes:

    Not sure I follow your code, but shouldn't you be totalling your miles driven somewhere in there?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:


  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    I believe is just supposed to be "miles so far" this is what the assignment says

    "Develop a Java application that will allow input of the miles driven and gallons used for each thankful. The program should calculate and display the miles per gallons obtained for each tankful and print the combined miles per gallon obtained for all tankfuls so far, each time a miles driven and gallons pair is entered."

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    sorry I did post this question on another forum yesterday, I found this one this morning and liked the layout a little better. Sorry for not making
    clear. Just trying learn java programming, and I looked for different sources.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    Can you post the output from the first program and explain what is wrong with it?

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    This is the output from Eclipse:

    Enter miles since tank was full (-999 to quit) 200.7
    Enter gallons used in refilling tank:16.3
    MPG this tankful: 12.31
    Total MPG: 12.31
    Enter miles since tank was full (-999 to quit) 120
    Enter gallons used in refilling tank:5
    MPG this tankful: 24.00
    Total MPG: 24.00
    Enter miles since tank was full (-999 to quit)

    After the first time through the loop, the mpgtank and totalmpg should be different. I may not have the loop in place at all.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    After the first time through the loop, the mpgtank and totalmpg should be different.
    Where in your code do you change the values of the variables?
    Do you forget to reset one of them?
    Or do you reset both of them?

    What are the items that go into computing each of them?

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    the user is supposed to enter a new set of numbers and have them recalculated based on the old "mpg this tank full" and "total mpg"

    The first set is correct, but those two outputs are supposed to change when u enter in a new set of numbers

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    What are the items/numbers that go into computing the values of each of them?

  13. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    i am not sure what u are asking me here, i'm sorry

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    An example: to compute how fast a car is traveling you need the distance traveled and how long it took to travel that distance.
    What values/numbers do you need to compute each of the following:
    mpgtank
    totalmpg

  15. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    should be total miles and total gallons

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    total miles and total gallons
    Then you have what you need to compute the values.

    What is the difference between the computations for the two values to display? Do they both use the same values? Or are there different values for each?

  17. #17
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    there will be different values for each, but whenever i try to put that in the code, eclipse will not take it. I just can't figure out where i would put
    totalmpg = totalgallons/totalmiles
    totaltank = totalgallons/totalmiles

    where does this go in my current code??

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    The equations can not use the same variables if you want to compute different values. You need to have separate variables for the tank and for the total.

  19. #19
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    could u give me an example of what you mean? I am sorry i am just not up on the terms yet

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    Just write in English what the steps are you want the program to do. I'll start it:

    begin loop
    Ask user to enter miles driven this tank
    read milesdriven
    ask user to enter gallons to fill the tank
    read gallons
    ....
    end loop

    Now fill in the steps where the ... to get the two numbers you need to print out?

  21. #21
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    ok so i shouldn't have to repeat this code:

    System.out.print("Enter miles since tank was full (-999 to quit) "); 
    milesdriven = input.nextDouble();
    while(milesdriven!=-999) 
    {
     
    System.out.print( "Enter gallons used in refilling tank:" );
    gallonsused = input.nextDouble();
     
    mpgtank  = milesdriven / gallonsused;
    totalmpg = totalmiles / totalgallons;
     
    System.out.printf( "MPG this tankful: %1.2f ",  mpgtank );
     
    System.out.println();
     
    System.out.printf("Total MPG: %1.2f", totalmpg);
     
    System.out.println();

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    Yes, you should be able to do what you need in one loop.

  23. The Following User Says Thank You to Norm For This Useful Post:

    captianjaax (September 19th, 2011)

  24. #23
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    thank you very much for all your help, i will keep working at it

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for help on two codes:

    Ok. Come on back if you have more problems/questions.

  26. #25
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Looking for help on two codes:

    thank you again for your help norm, i got a 100% on that assignment based on all your help! sending more thank yous!

Similar Threads

  1. [SOLVED] Help with understanding the following commented codes!
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2011, 04:26 PM
  2. Codes skip some lines??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:39 AM
  3. Please Help What's Wrong with my Codes!
    By bertzki10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 11:03 AM
  4. help with text format codes
    By vanchan09 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 04:12 PM
  5. help us in eclipse basic codes
    By bil_Imma in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2009, 06:02 PM