subject

Write a method so that the main() code below can be replaced by the simpler code that calls method original main(): public class calcmiles { public static void main(string [] args) { double milesperhour; double minutestraveled; double hourstraveled; double milestraveled; milesprhour = scnr. nextdouble(); minutestraveled = scnr. nextdouble(); hourstraveled = minutestraveled / 60.0; milestraveled = hourstraveled * milesperhour; system. out. println("miles: " + milestraveled); } }

Answers

ansver
Answer from: jahnoibenjamin

ANSWER

The JAVA program after simplification is as below.

import java.util.Scanner;

public class CalcMiles {

   

   // variables declaration

   static double milesPerHour;

   static double minutesTraveled;      

   static double hoursTraveled;

   static double milesTraveled;

   

   // method declared static

   public static void mphAndMinutesToMiles(double speed, double mins)

   {

       // computations to calculate distance travelled

       hoursTraveled = mins / 60.0;

       milesTraveled = hoursTraveled * speed;

       

       // result displayed on the screen  

     System.out.println("Miles: " + milesTraveled);

   }

   

   // Scanner object created inside main()

   // user input taken inside main()

   public static void main(String [] args)

   {

       Scanner scnr = new Scanner(System.in);

       System.out.println("Enter miles travelled per hour ");

       milesPerHour = scnr.nextDouble();

       System.out.println("Enter minutes required to travel ");

       minutesTraveled = scnr.nextDouble();

       

       mphAndMinutesToMiles(milesPerHour, minutesTraveled);

       

   }

}

OUTPUT

Enter miles travelled per hour  

2.3

Enter minutes required to travel  

1234

Miles: 47.30333333333333

EXPLANATION

The program is simplified as explained below.

1. User input is taken using the object of Scanner class.

2. This object of Scanner class can only be created inside main() method hence, user input can only be taken inside main().

3. The code to calculate the result is separated in another method, mphAndMinutesToMiles(). This method is declared with return type void since no value is returned.

4. After user input is taken in main() for miles travelled per hour and minutes required to travel, these values are passed as parameters to the mphAndMinutesToMiles() method.

5. These parameters are used in calculation part. The total miles travelled in total hours (obtained from minutes), is calculated. The code to display the result is added to this method.

6. Inside the main method, only the code to create Scanner object, code to take user input for two variables and code to call the mphAndMinutesToMiles() is included.

7. The variables are declared as static and declared at class level.

8. No variable is declared inside any of the two methods.

ansver
Answer from: ashl3yisbored

int main(void)

{

  double milesPerHour = 70.0;

  double minutesTraveled = 100.0;

  double hoursTraveled;

  double milesTraveled;

  hoursTraveled = minutesTraveled / 60.0;

  milesTraveled = hoursTraveled * milesPerHour;

  printf("Miles: %lf\n", milesTraveled);

   return 0;

}

Learn more:

How does coding work on computers?

       

Keywords: milesPerHour, hoursTraveled, printf, return 0

ansver
Answer from: tanviknawale

The simpler code for the given main () code is shown below. For this, calculate the hours and miles before the main function and store them in the separate variables and after this, use these variables in main () code. The below complete code is written in Java which  reduce the given main () code and also make it simple.

Further Explaination:

Code:

import java.util.Scanner;

public class CalcMiles

{

/*Your solution starts here */

//Define the method calcMilesTraveled().

Public double calcMilesTraveled (double mph, double minutesTravelled)

{

//Declare required variables.

Double hoursTravelled = 0.0;

Double milesTravelled = 0.0;

//Calculate the hours travelled and miles travelled.

hoursTravelled = minutesTravelled / 60.0;

milesTravelled = hoursTravelled * mph;

//The total miles travelled in return.

Return milesTravelled;

}

public static void main(String [] args)

{

double milesPerHour = 70.0;

double minsTravelled = 100.0;

CalcMiles tripCalculator = new CalcMiles();

System.out.println("Miles: " + tripCalculator.calcMilesTraveled (milesPerHour, minsTraveled));

}

}

Output:

Run the program, the output will be look like as below:

Miles: 116.66666666666667

Learn more:

1. A company that allows you to license software monthly to use online is an example of ?  

2. How does coding work on computers?   

Answer details:

Grade: College Engineering

Subject: Computer Science and Engineering

Chapter: Java Programming

Keyword:

Java, input, output, programming, statements,  class, double, int, miles, hours, milesPerHour, hoursTraveled, printf, return 0

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 14:30, labrandonanderson00
By default, how many tcp ports are open on a pfsense internal interface
Answers: 2
image
Computers and Technology, 21.06.2019 23:30, jackyrodriguez92
Write a class called book that contains instance data for the title, author, publisher, price, and copyright date. define the book constructor to accept and initialize this data. include setter and getter methods for all instance data. include a tostring method that returns a nicely formatted, multi-line description of the book. write another class called bookshelf, which has name and array of book objects. bookself capacity is maximum of five books. includes method for bookself that adds, removes, isfull( returns boolean), isempty(returns boolean) and tostring method that returns all information about the books in the bookself. create a driver class called testbookshelf, whose main method instantiates five book objects, instantiates one bookshelf, updates the books, and add or remove the books to the bookshelf, isduplicate(book b). make sure to print information of the bookshelf every time you update the books or the bookshelf. add as many api that you think is good to have for the bookshelf class.
Answers: 2
image
Computers and Technology, 22.06.2019 01:00, keasiabradley
Search the web for two examples of digital art that you like. the examples must be from different mediums (example: one digital photo and one computer animation not two computer animations). compose an essay for each example you choose about why the piece appeals to you, and identify the medium used.
Answers: 1
image
Computers and Technology, 22.06.2019 11:30, mmimay3501
What do character formats do for your document's message? a. set the tone b. provide organization c. provide clarity d. set how texts align with documents
Answers: 2
You know the right answer?
Write a method so that the main() code below can be replaced by the simpler code that calls method...

Questions in other subjects:

Questions on the website: 13535424