anyone know java?

Collapse

Recommended Videos

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SuperBowlNachos
    All Star
    • Jul 2004
    • 10218

    #1

    anyone know java?

    I need help on my java again. I suck at this stuff.

    I have to make a program that lets a user select between 4 options and then choose a distance of their choosing. It's trying to figure out how fast sound travels through certain objects. I have to have a demo program and a separate class.

    As you will see I have a lot of repeating calculations. I'm just not sure what needs to be where, and I am not sure of what the output statement should be.

    Code:
    public class Sound
    
    {
    	private final double air = 1100;
    	private final double water = 4900;
    	private final double steel = 16400;
    	double distance;
    	double time;
    
    	// no arg constructor
    
    	public Sound()
    	{
    		distance = 0.0;
    	}
    
    	//parameterized constructor
    
    	public Sound(double d)
    	{
    		distance = d;
    	}
    
    	//mutator methods
    
    	public void setDistance(double d)
    	{
    		distance = d;
    	}
    
    	//accessor methods
    
    	public double getSpeedInAir()
    	{
    		return distance / 1100;
    	}
    
    	public double getSpeedInWater()
    	{
    		return distance / 4900;
    	}
    
    	public double getSpeedInSteel()
    	{
    		return distance / 16400;
    	}
    }

    Code:
    	import java.util.Scanner;
    	import java.text.DecimalFormat;
    
    	public class SoundDemo
    {
    	public static void main(String[] args)
    
    	{
    
    		double choice;
    		double time;
    		double distance;
    
    		Scanner keyboard = new Scanner(System.in);
    
    
    		System.out.print("Please choose 1. Air 2. Water 3. Steel 4. Quit ");
    		choice = keyboard.nextDouble();
    
    		System.out.print("What distance would you like to know? ");
    		distance = keyboard.nextDouble();
    
    
    	   if (choice.equals("1"))
    			{
    	     		time = (distance / 1100);
    	   			System.out.println("The total time traveled is " + time + ".");
    			}
    
    		else if (choice.equals("2"))
    			{
    				time = (distance / 4900);
    				System.out.println("The total time traveled is " + time + ".");
    			}
    
    		else if (choice.equals("3"))
    			{
    				time = (distance / 16400);
    				System.out.println("The total time traveled is " + time + ".");
    			}
    		else if (choice.equals(" "))
    			{
    				System.out.println("Invalid input!");
    			}
    
    
    }
    }
  • ChampN252
    Rookie
    • Mar 2009
    • 440

    #2
    Re: anyone know java?

    I'm no expert, but I'm trying to get this running and can't. I made my little java script file and tried running this thru FF and I'm having to luck. What should this look like in the file and what are you using to run this?

    Comment

    • ChampN252
      Rookie
      • Mar 2009
      • 440

      #3
      Re: anyone know java?

      I see I had confused myself...maybe I should leave to for an actually "pro". GL

      Comment

      • SuperBowlNachos
        All Star
        • Jul 2004
        • 10218

        #4
        Re: anyone know java?

        Textpad. You have to save them both to the same folder. First one should have no syntax errors, the second isn't complete. It won't run as a whole yet.

        Comment

        • stizz
          MVP
          • Sep 2006
          • 1718

          #5
          Re: anyone know java?

          Your biggest problem is that you're trying to compare a double to a string. To compare a double you should have if (choice == 1) { do stuff }.

          Another thing you could do as well is make the very last else if statement just an else statement. That way if they type anything other than 1,2,3 or 4 it'll print out your error message.

          I also noticed that you haven't created a Sound object yet either.

          You don't use your final variables either. In your Sound class, you should use the final variables instead of the numbers you type in.

          I don't think you should have those computing methods inside your main either. Those should be calculated in the Sound class then you should call the methods on the Sound object you create.
          Last edited by stizz; 04-07-2011, 03:44 AM.

          Comment

          • The GIGGAS
            Timbers - Jags - Hokies
            • Mar 2003
            • 28474

            #6
            Re: anyone know java?

            Originally posted by stizz
            Your biggest problem is that you're trying to compare a double to a string. To compare a double you should have if (choice == 1) { do stuff }.

            Another thing you could do as well is make the very last else if statement just an else statement. That way if they type anything other than 1,2,3 or 4 it'll print out your error message.

            I also noticed that you haven't created a Sound object yet either.

            You don't use your final variables either. In your Sound class, you should use the final variables instead of the numbers you type in.

            I don't think you should have those computing methods inside your main either. Those should be calculated in the Sound class then you should call the methods on the Sound object you create.
            This is all good stuff.
            Rose City 'Til I Die
            Duuuuuuuvvvvaaaaaaaal
            Hokie Hokie Hokie Hy

            Member: OS Uni Snob Assoc.
            OS OT Post Champ '11

            Twitter: @TheGIGGAS_OS
            Xbox Live: TheGIGGAS
            3DS: 1349-7755-3870

            Comment

            • SuperBowlNachos
              All Star
              • Jul 2004
              • 10218

              #7
              Re: anyone know java?

              Well apparently we are supposed to use switches. So this is where I am now at. Had a buddy helping me, but he had to go. He said I needed to add something to the class statements in the driver program, can't remember what though.

              Code:
              public class Sound
              
              {
              
              	double distance;
              
              
              	// no arg constructor
              
              	public Sound()
              	{
              		distance = 0.0;
              	}
              
              	//parameterized constructor
              
              	public Sound(double d)
              	{
              		distance = d;
              	}
              
              	//mutator methods
              
              	public void setDistance(double d)
              	{
              		distance = d;
              	}
              
              
              	//accessor methods
              
              	public double getSpeedInAir()
              	{
              		return distance / 1100;
              	}
              
              	public double getSpeedInWater()
              	{
              		return distance / 4900;
              	}
              
              	public double getSpeedInSteel()
              	{
              		return distance / 16400;
              	}
              }

              Code:
              	import java.util.Scanner;
              	import java.text.DecimalFormat;
              
              	public class SoundDemo
              {
              	public static void main(String[] args)
              
              	{
              
              		Int choice;
              		double distance;
              
              		Scanner keyboard = new Scanner(System.in);
              
              
              		System.out.print("Please choose 1. Air 2. Water 3. Steel 4. Quit ");
              		choice = keyboard.nextDouble();
              
              		if (choice != 1 && choice != 2 && choice != 3 && choice !=4)
              		System.out.print("Please choose 1. Air 2. Water 3. Steel 4. Quit ");
              		choice = keyboard.nextDouble();
              
              		if(choice ==4)
              		{
              		System.exit(choice)
              	}
              
              		System.out.print("What distance would you like to know? ");
              		distance = keyboard.nextDouble();
              
              		switch(choice)
              
              		class 1:
              
                	 			System.out.println("The total time traveled is " + time + ".");
              
              		class 2:
              
              				System.out.println("The total time traveled is " + time + ".");
              
              		class 3:
              
              				System.out.println("The total time traveled is " + time + ".");
              
              
              }

              Comment

              • The GIGGAS
                Timbers - Jags - Hokies
                • Mar 2003
                • 28474

                #8
                Re: anyone know java?

                Originally posted by olliethebum85
                Well apparently we are supposed to use switches. So this is where I am now at. Had a buddy helping me, but he had to go. He said I needed to add something to the class statements in the driver program, can't remember what though.
                Your syntax for the switch statement is a bit off:

                It should look like this:

                Code:
                   switch (choice)
                   {
                      case 1:
                         sop...;
                         break;
                      case 2:
                         sop...;
                         break;
                      case 3:
                         sop...;
                         break;
                   }
                Rose City 'Til I Die
                Duuuuuuuvvvvaaaaaaaal
                Hokie Hokie Hokie Hy

                Member: OS Uni Snob Assoc.
                OS OT Post Champ '11

                Twitter: @TheGIGGAS_OS
                Xbox Live: TheGIGGAS
                3DS: 1349-7755-3870

                Comment

                • SuperBowlNachos
                  All Star
                  • Jul 2004
                  • 10218

                  #9
                  Re: anyone know java?

                  What does the break mean?

                  Comment

                  • p_rushing
                    Hall Of Fame
                    • Feb 2004
                    • 14514

                    #10
                    Re: anyone know java?

                    Originally posted by olliethebum85
                    What does the break mean?
                    It breaks out of the switch case. It ends that current case statement and then exits the switch so you don't check the other cases, saving processing time. You also need to add a default case so that action is taken if their is a case that doesn't match anything.
                    Last edited by p_rushing; 04-07-2011, 12:10 PM.

                    Comment

                    • Blzer
                      Resident film pundit
                      • Mar 2004
                      • 42514

                      #11
                      Re: anyone know java?

                      Originally posted by p_rushing
                      It breaks out of the switch case. It ends that current case statement and then exits the switch so you don't check the other cases, saving processing time. You also need to add a default case so that action is taken if their is a case that doesn't match anything.
                      To append to what he said, "switch" is essentially another way of using a bunch of if-else statements. The default case is your "else" in this scenario.
                      Samsung PN60F8500 PDP / Anthem MRX 720 / Klipsch RC-62 II / Klipsch RF-82 II (x2) / Insignia NS-B2111 (x2) / SVS PC13-Ultra / SVS SB-2000 / Sony MDR-7506 Professional / Audio-Technica ATH-R70x / Sony PS3 & PS4 / DirecTV HR44-500 / DarbeeVision DVP-5000 / Panamax M5400-PM / Elgato HD60

                      Comment

                      • stizz
                        MVP
                        • Sep 2006
                        • 1718

                        #12
                        Re: anyone know java?

                        http://www.java2s.com/Tutorial/Java/CatalogJava.htm

                        This website has good tutorials for Java.

                        Comment

                        • SuperBowlNachos
                          All Star
                          • Jul 2004
                          • 10218

                          #13
                          Re: anyone know java?

                          I ended up getting it. I'll post it next week when I am in class again.

                          Comment

                          Working...