Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java - need help modifying this program - Add a method (i.e. keep the original conversion method), called cupsToGallons , which accepts the number of

Java - need help modifying this program

- Add a method (i.e. keep the original conversion method), called cupsToGallons, which accepts the number of cups as an argument and then returns the equivalent number of gallons as a double. There are 16 cups in a gallon. Call the new method from main() and display the results. You may modify the existing display method or add a new method to display the gallons. The program should display the conversions to both ounces (the original program) and gallons (the part you add.)

The program to fix:

import javax.swing.JOptionPane;

/**

This program converts cups to ounces.

*/

public class CupConverter

{

public static void main(String[] args)

{

double cups; //The number of cups

double ounces; //The number of ounces

// Get the number of cups.

cups = getCups();

// Convert the cups to ounces.

ounces = cupsToOunces(cups);

// Display the results.

displayConversion(cups, ounces);

System.exit(0);

}

/**

The getCups method promtps the user to enter a number of cups.

@return The number of cups entered by the user.

*/

public static double getCups()

{

String input; // To hold input.

double numCups; // To hold cups.

// Get the number of cups from the user.

input = JOptionPane.showInputDialog("This program converts

measurements " + "in cups to ounces. " +

"For reference the formula is: " + "1 cup = 8 fluid

ounces " + "Enter the number of cups.");

// Convert the input to a double.

numCups = Double.parseDouble(input);

// Return the number of cups.

return numCups;

}

/**

The cupsToOunces method converts a number of cups to ounces, using

the formula

1. cup = 8 ounces.

@param numCups, The number of cups to convert.

@return The number of ounces.

*/

public static double cupsToOunces(double numCups)

{

return numCups * 8.0;

}

/**

The displayConversion method displays a message with the results

from converting.

@param cups, A number of cups

@param ounces, A number of ounces.

*/

public static void displayConversion(double cups, double ounces)

{

// Display the number of ounces.

JOptionPane.showMessageDialog(null, cups + " cups equals " + ounces

+ " ounces.");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the rate of return associated with this project

Answered: 1 week ago

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago