JAVA In the lawless world of underground fish racing everyon

*********JAVA************

In the lawless world of underground fish racing, everyone looks to use any information they can to gain an advantage. We have recently come across a formula to help us estimate the distance travelled by a fish based on their size and frequency of their tail beat. The formula is: D = (s/4) * (L(3f - 4)) where D is distance travelled by the fish in cm, f is the frequency in tail beats per sec, L is the body length in cm, and s is the number of seconds. Write a Java program that predicts the winner of a race between 3 different fish. First, ask the user to enter the length and frequency of the tail beats of 3 fish. Write a method called fishDistance that given arguments L, f and s, returns the distance travelled by the fish after s seconds. In your main method, use your fishDistance method to print the distances travelled by all the fish at each second for 20 seconds. Check for valid input. The length of the fish must be a positive number and the tail frequency must be greater than 1. If invalid input is entered, repeatedly ask the user to re-enter values until it is valid. Round output to 2 decimal places.

Solution

import java.util.Scanner;

class Fish
{
   private double distance;
   private double size;
   private double frequency;
   public double getDistance() {
       return distance;
   }
   public void setDistance(double distance) {
       this.distance = distance;
   }
   public double getSize() {
       return size;
   }
   public void setSize(double size) {
       this.size = size;
   }
   public double getFrequency() {
       return frequency;
   }
   public void setFrequency(double frequency) {
       this.frequency = frequency;
   }
   void fishDistance(double second)
   {
       setDistance((second/4)*(size*(3*frequency-4)));
   }
}
public class FishDistanceTester {

   public static void main(String[] args) {
     
   System.out.println(\"Enter details of 3 fish for competition\");
   Scanner sc=new Scanner(System.in);
   int i=0;
   Fish f[]=new Fish[3];
   boolean flag=true;
   for(i=0;i<3;i++){
       flag=true;
       System.out.println(\"Enter length\");
       double l=sc.nextDouble();
       System.out.println(\"Enter frequency\");
       double fre=sc.nextDouble();
       if(l<0||fre<1){
           flag=false;
           i--;
       }
       if(flag==true){
       f[i]=new Fish();
       f[i].setSize(l);
       f[i].setFrequency(fre);}
       sc.nextLine();
   }
   System.out.println(\"Display fish distances\");
   for(int j=1;j<=20;j++)
   {
       System.out.println(\"Distance travel in \"+ j +\" seconds\");
       for(int k=0;k<3;k++)
       {
           f[k].fishDistance((double)j);
           System.out.print(f[k].getDistance());
           System.out.print(\" \");
       }
       System.out.println(\"\");
   }

   }

}

Output:

Enter details of 3 fish for competition
Enter length
3
Enter frequency
4
Enter length
1
Enter frequency
3
Enter length
1
Enter frequency
3
Display fish distances
Distance travel in 1 seconds
6.0 1.25 1.25
Distance travel in 2 seconds
12.0 2.5 2.5
Distance travel in 3 seconds
18.0 3.75 3.75
Distance travel in 4 seconds
24.0 5.0 5.0
Distance travel in 5 seconds
30.0 6.25 6.25
Distance travel in 6 seconds
36.0 7.5 7.5
Distance travel in 7 seconds
42.0 8.75 8.75
Distance travel in 8 seconds
48.0 10.0 10.0
Distance travel in 9 seconds
54.0 11.25 11.25
Distance travel in 10 seconds
60.0 12.5 12.5
Distance travel in 11 seconds
66.0 13.75 13.75
Distance travel in 12 seconds
72.0 15.0 15.0
Distance travel in 13 seconds
78.0 16.25 16.25
Distance travel in 14 seconds
84.0 17.5 17.5
Distance travel in 15 seconds
90.0 18.75 18.75
Distance travel in 16 seconds
96.0 20.0 20.0
Distance travel in 17 seconds
102.0 21.25 21.25
Distance travel in 18 seconds
108.0 22.5 22.5
Distance travel in 19 seconds
114.0 23.75 23.75
Distance travel in 20 seconds
120.0 25.0 25.0

*********JAVA************ In the lawless world of underground fish racing, everyone looks to use any information they can to gain an advantage. We have recently
*********JAVA************ In the lawless world of underground fish racing, everyone looks to use any information they can to gain an advantage. We have recently
*********JAVA************ In the lawless world of underground fish racing, everyone looks to use any information they can to gain an advantage. We have recently

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site