9 Replies - 456 Views - Last Post: 06 April 2011 - 09:29 PM Rate Topic: -----

#1 emerson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-April 11

Splitting a program into a seperate class

Posted 06 April 2011 - 06:16 PM

I am doing the same problem statement as explained in this thread.

Only thing different is I have to have a class that stores all of the information, then a demo program to get the users input and run everything. Not just all in one.

This is what I got so far, but obviously I make things much more complicated then what they need to be. This is the sound class that will be accesed by the soundDemo program. I wrote this one from scratch. I have to have mutator and accessor methods.

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;
	}
}



This is my demo program. I have repeating information because I am not sure what needs to be where. I used the else if portion from the solution in the thread above.

	import java.util.Scanner
	import java.util.DecimalFormat

	public class soundDemo
{
	public static void main(String[] args)

	{

		double choice

		System.out.print("Please choose 1. Air 2. Water 3. Steel 4. Quit ");
		choice = keyboard.nextDouble();
		
	   if (input.equals("1"))
			{
	     		time = (distance / 1100);
	   			System.out.println("The total time traveled is " + time + ".");
			}
		
		else if (input.equals("2"))
			{
				time = (distance / 4900);
				System.out.println("The total time traveled is " + time + ".");
			}
		
		else if (input.equals("3"))
			{
				time = (distance / 16400);
				System.out.println("The total time traveled is " + time + ".");
			}
		else
			{
				System.out.println("Invalid input!");
			}
		
		System.out.print("What distance would you like to know? ");
		distance = keyboard.nextDouble();



I just need nudged in the right direction, because I'm pretty sure I have all the information already coded. It's just in the wrong place. Any tips would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting a program into a seperate class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 06:44 PM

You don't get define or assign anything to an input variable, nor do I see a Scanner. One thing to note is that you are missing a semi-colon here:
double choice  // <-- missing semi-colon



Also, you do not declare distance in your soundDemo class.

What you really want to do is:
-Get input for distance
-Create a sound object, passing distance when you instantiate it
-Get input for choice
-Based on the choice, use the sound object to get the appropriate value and output it
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 06:46 PM

A lot of errors in the soundDemo class.
when declaring a vraiable, end the statement with ";"
double choice;//<here

what is keyboard, time? you never declare these variables.
if choice is double, why do you treat it as a String:
if (input.equals("1")) 

what is input?

why get a double value if you expect int values (1,2,3,4)?

Please post your real code, currently it just seems that you copy\paste some code blocks from different snippets.

Note, it is common that in Java class' name is declared with a capital first letter:
Sound instead of sound
SoundDemo instead of soundDemo
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 06:55 PM

And respect Java coding conventions
Class name starts with an Uppercase letter
Was This Post Helpful? 0
  • +
  • -

#5 emerson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-April 11

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 08:34 PM

Fixed the above errors. Just not sure if the else if statements should go in my Sound class or SoundDemo.

View Postjapanir, on 06 April 2011 - 06:46 PM, said:

Please post your real code, currently it just seems that you copy\paste some code blocks from different snippets.

The Sound class I wrote myself, the SoundDemo is a combination of example programs and the else if which was in the above thread. This is my real code.

Input should have been choice.
Time I forgot to declare in the demo. Keyboard I don't need to.

This post has been edited by emerson1: 06 April 2011 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 08:40 PM

They belong in your SoundDemo class. Use them to determine which method you need to invoke from your Sound object.
Was This Post Helpful? 0
  • +
  • -

#7 emerson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-April 11

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 08:53 PM

Not sure if I am any closer or just going in circles. The sound class at least has no syntax errors. I have a lot of stuff repeating.

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;
	}
}




	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!");
			}

}
}


This post has been edited by emerson1: 06 April 2011 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 09:19 PM

if (choice.equals("1"))

choice is a double not a String
and you cannot use the equals() method on a basic datatype as double is
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 09:24 PM

Also, why not just read in an int for choice? It's not like you can have choice 2.718. And compare the input to ints:
if(choice == 1){}
else if(choice == 2){}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Splitting a program into a seperate class

Posted 06 April 2011 - 09:29 PM

Actually comparing double with == is a very bad idea

When you read 2 from the keyboard it will actually store 1.999999998 (or something like that) in the double

Follow macosxnerd101 and make choice an int
or a char

String line = scanner.nextLine();
char choice = line.charAt(0);
then you can switch on '1', '2', ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1