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.


I have the rest of the program written, I just need help figuring out how to do the part of the program where I check to make sure that the length of the fish is a positive numver and the tail frequency is greater than 1.
import java.util.Scanner; import java.io. public class Hw4pr2 public static double fishdistance(double s, double f, double L) double D; D (s/4)s(L* (3*f-4 ) ) ; return D; public static void main(String[l args) double L, f, L2, f2,L3, f3 //L must be a positive number //f must be greater than 1 //if invalid input is entered, repeatedly ask the user to re-enter until it is valid Scanner keyboard new Scanner(System.in); System.out.print(\"Enter the length of fishl: \"); keyboard.nextDouble(); System.out.print(\"Enter the tail beat frequency of fishl: \"); keyboard.nextDouble(); System.out.print(\"Enter the length of fish2: \"); 2- keyboard.nextDouble(); System.out.print(\"Enter the tail beat frequency of fish2: \"); f2-keyboard.nextDouble); System.out.print(\"Enter the length of fish3: \"); L3 = keyboard, nextDouble(); System.out.print(\"Enter the tail beat frequency of fish3: \"); f3-keyboard.nextDouble); int s; double Df; double Df2;

Solution

import java.util.Scanner;
import java.io.*;
public class HW4pr2
{
   public static double fishDistance(double s, double f, double L){
       double D;
       D=(s/4)*(L*(3*f-4));
       return D;
   }
   public static void main(String[] args){
       double L,f,L2,f2,L3,f3;
       int ch;
       Scanner keyboard=new Scanner(System.in);
       do{
       System.out.println(\"Enter Length of fish 1:\");
       L=keyboard.nextDouble();
       if (L<=0)
       {
           System.out.println(\"Please enter a positive number for length of fish 1\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);
       do{
       System.out.println(\"Enter tail beat frequency of fish 1:\");
       f=keyboard.nextDouble();
       if (f<=1)
       {
           System.out.println(\"Please enter a number >1 for tail beat frequency of fish 1\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);
       do{
       System.out.println(\"Enter Length of fish 2:\");
       L2=keyboard.nextDouble();      
       if (L2<=0)
       {
           System.out.println(\"Please enter a positive number for length of fish 2\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);
       do{
       System.out.println(\"Enter tail beat frequency of fish 2:\");
       f2=keyboard.nextDouble();
       if (f2<=1)
       {
           System.out.println(\"Please enter a number >1 for tail beat frequency of fish 2\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);
       do{
       System.out.println(\"Enter Length of fish 3:\");
       L3=keyboard.nextDouble();      
       if (L3<=0)
       {
           System.out.println(\"Please enter a positive number for length of fish 3\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);
       do{
       System.out.println(\"Enter tail beat frequency of fish 3:\");
       f3=keyboard.nextDouble();
       if (f3<=1)
       {
           System.out.println(\"Please enter a number >1 for tail beat frequency of fish 3\");
           ch=1;
       }
       else
           ch=0;
       }while(ch==1);

  
       for (int s=1;s<=20 ;s++ )
       {
       double df=fishDistance(s,f,L);
double df2=fishDistance(s,f2,L2);
           double df3=fishDistance(s,f3,L3);
           System.out.printf(\"After %d Seconds fish1 swim %.2f cm,\ \\t fish2 swim %.2f cm,\ \\t fish3 swim %.2f cm\ \",s,df,df2,df3);
       }
   }
}

*********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