Thursday, August 21, 2014

Beverage

Suppose that you work for a beverage company. The company wants to know the optimal cost for a cylindrical container that holds a specified volume. Write a fragment of code that uses an ask-before-iterating loop. During each iteration of the loop, your code will ask the user to enter the volume and the radius of the cylinder. Compute and display the height and cost of the container. Use the following formulas, where V is the volume, r is the radius, h is the height, and C is the cost.
h = V/πr²
C = 2πr(r + h)

__________________________________________________________________


import javax.swing.JOptionPane;
public class LabExercise19 {

    public static void main(String[] args) {

        Double V = 0.0;
        Double pie = 3.14;
        Double r = 0.0;
        Double C = 0.0;
        Double h = 0.0;
        String choice = "";
       
        do{
            V = Double.parseDouble(JOptionPane.showInputDialog("Enter volume: "));
            r = Double.parseDouble(JOptionPane.showInputDialog("Enter radius: "));
       
            h = V/(pie*(r*r));
            C = 2*pie*r*(r+h);
           
            JOptionPane.showMessageDialog(null, "Height: "+h+"\nCost: "+C);
           
            choice = JOptionPane.showInputDialog("Another one? Y/N");
            choice = choice.toLowerCase();
        }while(!choice.equals("n"));
    }

}

No comments:

Post a Comment

advertisement