Units conversion program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minterman
    New Member
    • Jan 2007
    • 7

    Units conversion program

    1. the program needs to convert btu to jules
    2. Convert calories to joules
    3. Convert joules to joules
    4 exit the program.
    if the user type anything other than 1-4 the program should print a error message.

    here is my code.
    Code:
    import java.util.Scanner;
     
    public class joules {
    	public static void main (String [] args) {
    		final double BTUs = 1056;
    		final double calories = 4.184;
    		final double joule = 1;
    		Scanner stdin = new Scanner(System.in);
    		System.out.println ("1. Convert BTUs to joules");
    		System.out.println ("2. Convert Calories to joules");
    		System.out.println ("3. Convert joules to joules");
    		System.out.println ("4. Exit program");
    		int choice = stdin.nextInt();
    		while (choice != 4)
    			if (choice < 1 || choice > 3)
    			//converting to joules using constants
    			if (choice = 1) {
    			double amount = stdin.nextDouble();
    			double joules = amount * BTUs;
    		System.out.println (amount + " BTUs is " + joules  "joules");
    				}
    		if (choice = 1) {double amount = stdin.nextDouble();
    			double joules = amount * calories;
    		System.out.println (amount + " Calories is " + joules + "joules");
    				}
    			if (choice = 1) {
    			double amount = stdin.nextDouble();
    			double joules = amount * joule;
    			System.out.println (amount + " joules is " + joules + "joules");
    				}
    			//statement for wrong entries
    			else {
    		System.out.println ("That is not a valid entry.  Try again");
    			}
    Last edited by horace1; Feb 28 '07, 08:12 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    what is your problem?

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      You do, of course realise that you only ever check for option 1 (and doesn't java require ==)

      Comment

      • minterman
        New Member
        • Jan 2007
        • 7

        #4
        Originally posted by horace1
        what is your problem?
        I started writing the code to convert btu to joules. This code is not working for me

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by minterman
          1. the program needs to convert btu to jules
          2. Convert calories to joules
          3. Convert joules to joules
          4 exit the program.
          if the user type anything other than 1-4 the program should print a error message.

          here is my code.
          Code:
          import java.util.Scanner;
           
          public class joules {
          	public static void main (String [] args) {
          		final double BTUs = 1056;
          		final double calories = 4.184;
          		final double joule = 1;
          		Scanner stdin = new Scanner(System.in);
          		System.out.println ("1. Convert BTUs to joules");
          		System.out.println ("2. Convert Calories to joules");
          		System.out.println ("3. Convert joules to joules");
          		System.out.println ("4. Exit program");
          		int choice = stdin.nextInt();
          		while (choice != 4)
          			if (choice < 1 || choice > 3)
          			//converting to joules using constants
          			if (choice = 1) {
          			double amount = stdin.nextDouble();
          			double joules = amount * BTUs;
          		System.out.println (amount + " BTUs is " + joules "joules");
          				}
          		if (choice = 1) {double amount = stdin.nextDouble();
          			double joules = amount * calories;
          		System.out.println (amount + " Calories is " + joules + "joules");
          				}
          			if (choice = 1) {
          			double amount = stdin.nextDouble();
          			double joules = amount * joule;
          			System.out.println (amount + " joules is " + joules + "joules");
          				}
          			//statement for wrong entries
          			else {
          		System.out.println ("That is not a valid entry. Try again");
          			}
          You should be using a switch for this.

          Code:
           int choice = stdin.nextInt(); 
          switch(choice) {
          	   case 1: {
           
          			   break;
          		}
          		case 2: {
           
          			   break;
          		}
          		.
          		.
          		.
          	   default : {
          		//wrong input 
          	   
          	   }
          What error did you get?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            your logic in the following statement where you test if choice is between 1 and 3 inclusive is incorrect
            Code:
            		while (choice != 4)
            			if (choice < 1 || choice > 3)
            you test if choice is less than 1 or greater than 3
            you need to test if choice is greater than or equal to 1 AND less than or equal to 3, e.g.
            Code:
                            while (choice != 4)
                                    if (choice >= 1 && choice <= 3)
            also as DeMan indicated statements such as
            Code:
            			if (choice = 1) {
            should use ==
            Code:
            			if (choice == 1) {
            however, as indicated by r035198x, it would be better to use a switch statement

            Comment

            • minterman
              New Member
              • Jan 2007
              • 7

              #7
              Help with Homework What am I doing wrong with this code

              1. the program needs to convert btu to jules
              2. Convert calories to joules
              3. Convert joules to joules
              4 exit the program.
              if the user type anything other than 1-4 the program should print a error message.


              I can not get this code to execute Please hellp
              here is my code.
              Code:

              import java.util.*;
              public class Project_2 {
              // main(): application entry point
              public static void main (String [] args) {
              final double BTUs =1056;
              final double calories =4.184;
              final double joule =1;
              int choice;
              // set up input stream
              Scanner stdin = new Scanner(System. in);
              System.out.prin tln ("1. Convert BTUs to Joules");
              System.out.prin tln ("2. Convert Calories to Joules");
              System.out.prin tln ("3. Convert Joules to Joules");
              System.out.prin tln ("4. Exit program");
              int choice=stdin.ne xtInt();
              //converting BTUS to Joules
              switch (choice){
              case 1:
              double amount = stdin.nextDoubl e();
              double joules = amount * BTUs;
              System.out.prin tln (amount + "BTUs is " + joules + "joules");
              break;
              // converting Calories to Joules
              case 2:
              double joules = amount * calories;
              System.out.prin tln (amount + "Calories is " + joules + "joules");
              break;
              // converting Joules to Joules
              case 3:
              double joules = amount * joule;
              System.out.prin tln (amount + "joules is " + joules + "joules");
              break;
              // exit program
              case 4:
              System.exit(0)
              break;
              default:
              System.out.prin tln ("You have entered an incorrect number, Please try again.");
              }
              }
              }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by minterman
                1. the program needs to convert btu to jules
                2. Convert calories to joules
                3. Convert joules to joules
                4 exit the program.
                if the user type anything other than 1-4 the program should print a error message.


                I can not get this code to execute Please hellp
                here is my code.
                Code:

                import java.util.*;
                public class Project_2 {
                // main(): application entry point
                public static void main (String [] args) {
                final double BTUs =1056;
                final double calories =4.184;
                final double joule =1;
                int choice;
                // set up input stream
                Scanner stdin = new Scanner(System. in);
                System.out.prin tln ("1. Convert BTUs to Joules");
                System.out.prin tln ("2. Convert Calories to Joules");
                System.out.prin tln ("3. Convert Joules to Joules");
                System.out.prin tln ("4. Exit program");
                int choice=stdin.ne xtInt();
                //converting BTUS to Joules
                switch (choice){
                case 1:
                double amount = stdin.nextDoubl e();
                double joules = amount * BTUs;
                System.out.prin tln (amount + "BTUs is " + joules + "joules");
                break;
                // converting Calories to Joules
                case 2:
                double joules = amount * calories;
                System.out.prin tln (amount + "Calories is " + joules + "joules");
                break;
                // converting Joules to Joules
                case 3:
                double joules = amount * joule;
                System.out.prin tln (amount + "joules is " + joules + "joules");
                break;
                // exit program
                case 4:
                System.exit(0)
                break;
                default:
                System.out.prin tln ("You have entered an incorrect number, Please try again.");
                }
                }
                }


                Code:
                 
                import java.util.*;
                public class Project_2 {
                // main(): application entry point
                 public static void main (String [] args) {
                  final double BTUs =1056;
                  final double calories =4.184;
                  final double joule =1;
                  // set up input stream
                  Scanner stdin = new Scanner(System.in);
                  System.out.println ("1. Convert BTUs to Joules");
                  System.out.println ("2. Convert Calories to Joules");
                  System.out.println ("3. Convert Joules to Joules");
                  System.out.println ("4. Exit program");
                  int choice=stdin.nextInt();
                  //converting BTUS to Joules
                  switch (choice){
                   case 1: {
                	System.out.println ("Enter the BTUs");
                	double amount = stdin.nextDouble();
                	double joules = amount * BTUs;
                	System.out.println (amount + "BTUs is " + joules + "joules");
                	break;
                   }
                   // converting Calories to Joules
                   case 2: {
                	System.out.println ("Enter the Calories");
                	double amount = stdin.nextDouble();
                	double joules = amount * calories;
                	System.out.println (amount + "Calories is " + joules + "joules");
                	break;
                   }
                   // converting Joules to Joules
                   case 3: {
                	System.out.println ("Enter the Joules");
                	double amount = stdin.nextDouble();
                	double joules = amount * joule;
                	System.out.println (amount + "joules is " + joules + "joules");
                	break;
                   }
                   // exit program
                   case 4: {
                	System.exit(0);
                   }
                   default: {
                	System.out.println ("You have entered an incorrect number, Please try again.");
                   }
                  }
                 }
                }
                You were missing declarations for amount in the other case statements. Now make it possible to make many conversions before the program exits. Currently you can do only one conversion and then the program exits. You will need to use a while loop.

                Comment

                • minterman
                  New Member
                  • Jan 2007
                  • 7

                  #9
                  Thanks a Lot this got me on the right track. I added a while (true){
                  to get the program to keep looping but I want to make it pause after each calulations. How do I do that.

                  Thanks
                  Minterman

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by minterman
                    Thanks a Lot this got me on the right track. I added a while (true){
                    to get the program to keep looping but I want to make it pause after each calulations. How do I do that.

                    Thanks
                    Minterman
                    How do you want it to pause? Pause until the user does something or pause for a specific period of time?

                    Comment

                    • minterman
                      New Member
                      • Jan 2007
                      • 7

                      #11
                      Originally posted by r035198x
                      How do you want it to pause? Pause until the user does something or pause for a specific period of time?
                      Thanks. When I ran the program in DOS mode it work fine. It just ran fast when I ran the program in JGRASP. Now I just have to create a user guide and a test program.. Thanks a Lot...

                      Minterman

                      Comment

                      Working...