0

Hi everyone. I am new to java programming and taking a class in college. I got to a problem which I can do but i don't know the correct way to do it. It says to use mutator and accesor . can you please explain what should i change? I also got an error running this program on my computer it says cannot find or locate the class. Thank you

write a program that should display a menu allowing the user to select air, water, or steel. Once the user has made a selection, he or she should be asked to enter the distance a sound wave will travel in the selected medium. The program will then display the amount of time it will take. Check that the user had selected on of the the available choices from the menu.

Design a class that stores in a distance field, in feet, traveled by a sound wave. The class should have the appropriate accessor and mutator methods for this field. In addition, the class should have the following methods:

getSpeedInAir. This method should return the number of seconds it would take a sound wave to travel, in air, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in air is:

Time= distance/1100

getSpeedInWater. This method should return the number of seconds it would take a sound wave to travel, in water, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in water is:

Time= distance/4900

getSpeedInSteel.This method should return the number of seconds it would take a sound wave to travel, in steel, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in steel is:

Time= distance/16400

import java.util.Scanner;  // imports the scanner class

public class SSTheSpeedOfSound

{ // begin class



	    public static void main(String[] args)

	      {
			    System.out.printf("Medium\t\t\t Speed");

			    System.out.printf("Air\t\t\t 1100 feet/sec");

			    System.out.printf("Water\t\t\t 4900 feet/sec");

			    System.out.printf("Steel\t\t\t 16,400 feet/sec");


	 			Scanner keyboard = new Scanner(System.in);

	 			System.out.print("Enter air, water, or steel: ");

	            String input;

	            input = keyboard.nextLine();


	            System.out.print("Enter distance: ");

	            double distance;

	            distance = keyboard.nextDouble();

	            double time;

	            if (input.equals("air"))

	                {

	                time = (distance / 1100);

	                System.out.println("The total time traveled is " + time + ".");

	                }



	                else if (input.equals("water"))

	                {

	                time = (distance / 4900);

	                System.out.println("The total time traveled is " + time + ".");

	                }



	                else if (input.equals("steel"))

	                {

	                time = (distance / 16400);

	                System.out.println("The total time traveled is " + time + ".");

	                }

	    }

	}
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by dononelson
0

Accessors and mutators are methods that retrieve or change the value of a class's instance variables. The naming convention is set<VariableName> or get<VariableName>. Thus, if I had a class with with an instance variable

private String cat

, I would create an accessor

public String getCat() {...}

and a mutator

public void setCat(String value) {...}

Notice the form of the get and set methods - the variable name always follows "set" or "get", and it is always first letter capitalized. This is important for certain tools that use introspection (also known as reflection) to retrieve or change the value of instance variables programatically.

- don

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.