how to design a class that stores in a distance field the distance, in feet, tra
ID: 3634514 • Letter: H
Question
how to design a class that stores in a distance field the distance, 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:getSpeeAir: this method should return the number of seconds in 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 diance in air: Time = distance/1100
getSpeedInWater: this method should return the numbe of seconds it would take a sound wave to travel, in water, the distance stored in the distance field. the formulaa 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 methof 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 air is: Time = distance/16400
Explanation / Answer
import java.util.Scanner; public class distanceDriver { public static void main(String[] args) {Scanner in = new Scanner (System.in); int choice,d; double t; System.out.println("Enter the required media:"); do{ System.out.println("1. Water 2.Air 3. Steel"); choice=in.nextInt(); if(choice3) System.out.println("Invalid choice"); }while(choice3); System.out.println("Enter a distance: "); d=in.nextInt(); Distance dist = new Distance(d); if(choice==1) t=dist.getSpeedinWater(); else if(choice==2) t=dist.getSpeedinAir(); else t=dist.getSpeedinSteel(); System.out.println("The time it will take is "+t+"seconds"); } } ------------------------------------------------------- class Distance {private int distance; public Distance() {distance=0;} public Distance(int d) {distance=d;} public void setdistance(int d) {distance=d; } public int getdistance() {return distance; } public double getSpeedinWater() {return distance/4900.; } public double getSpeedinSteel() {return distance/16400.; } public double getSpeedinAir() {return distance/1100.; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.