So this is what i have so far sorry about format on here imp

So this is what i have so far (sorry about format on here):

import java.util.Scanner;

public class ContainerCalculator

{

   public static void main(String[] args)

   {

       //Welcomes user to program

       System.out.println(\"Welcome to the Container Calculator!\");

       System.out.println(\"====================================\");

       //Prompts user to enter diameter

       System.out.print(\"Enter the diameter of a cylinder \"

               + \"(in centimeters): \");

       double diameter = 0;

       Scanner scnr = new Scanner(System.in);

       while (!scnr.hasNextInt()) { //first loop for diameter

           System.out.print(\"Please enter an integer value \"

                   + \"(less than 2,147,483,648) as decimal digits: \");

           scnr.nextLine();  

       }

       diameter = scnr.nextInt();

       scnr.nextLine();

       while (diameter <= 0) { //second loop for diameter

           System.out.print(\"Please enter a positive integer value: \");          

           diameter = scnr.nextInt();

           scnr.nextLine();

       }  

       //Prompts user to enter height

       System.out.print(\"Enter the height of a cylinder (in centimeters): \");

       double height = 0;

       while (!scnr.hasNextInt()) { //first loop for height

           System.out.print(\"Please enter an integer value \"

                   + \"(less than 2,147,483,648) as decimal digits: \");

           scnr.nextLine();  

       }

       height = scnr.nextInt();

       scnr.nextLine();

       while (height <= 0) { //second loop for height

           System.out.print(\"Please enter a positive integer value: \");

           height = scnr.nextInt();

           scnr.nextLine();

       }

       scnr.close();

       System.out.print(\"\ \");

       //Starts answer

       System.out.printf(\"A can with a diameter of %.0f and \"

               + \"a height of %.0f has \ \", diameter, height);

       //Sets up equation for volume

       double radius = diameter/2;

       double volume = Math.PI * radius * radius * height;

       System.out.print(\"\\ta volume of \");

       System.out.printf(\"%.2f\",volume);

       System.out.println(\",\");

       //Sets up equation for surface area

       double surfaceArea = (Math.PI * radius * height * 2)

               + (Math.PI * radius * radius * 2);

       System.out.print(\"\\tand a surface area of \");

       System.out.printf(\"%.2f\", surfaceArea);

       System.out.println(\".\ \");

       //Milestone 3

       System.out.println(\"*** Surface Area Optimizer ***\");

      

       //Closing message for the program

       System.out.println(\"=============================================\");

       System.out.println(\"Thank you for using the Container Calculator.\");

   }

}

And this is the question right now:

Since the surface area of a cylinder corresponds to the amount of metal needed to fabricate an aluminum soda can (which largely impacts its price), it can be useful to find dimensions that minimize the surface area of a can without shrinking its volume. In this final milestone, we will compute the volume and surface area of cans with many different sizes to find the “best”. Remember to avoid using arrays, which are not allowed for this assignment and would only lead to an unnecessarily wasteful use of memory here. If you have mathematical experience finding closed form solutions for optimization problems like this, please only use them to check your work. If your program does not follow the algorithm described below, it is likely to compute results that our automated grading tests consider incorrect.

Here are some tips and requirements for implementing this final milestone. All of this will happen after your program has read in an input diameter and height from the user, and output the corresponding cylinder’s volume (referred to as the “original volume”) and surface area.

Consider cylinders with every possible combination of 1) diameters between 1 and the “original volume”, and 2) heights between 1 and the original volume. For example, if the “original volume” was 3, you would need to consider nine cylinders with the following (diameter,height) dimensions: (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3).

For each of these cylinder’s, check whether its volume is greater than or equal to the “original volume”. If it is, then it is a candidate for the new “best” cylinder that has been found so far. You’ll just need to confirm that this can’s surface area is smaller than that of the “best” can found so far. If it is, then your program should remember this can’s dimensions as the new best.

After searching through all of the possible cylinders and keeping track of the best that was found (the can with minimal surface area among those that hold at least as much volume as the “original can”), your program should report those optimal can dimensions to the user as demonstrated below.

Note that in step 2 you were asked to duplicate the code that you had written in the previous step. Although this is OK for the purposes of this assignment, it is generally undesirable to maintain code that includes this kind of duplication where each copy is only slightly different from (or identical to) the others. We usually prefer to generalize our code so that it can handle small variations in the computation that it performs (without duplication). Although you are encouraged to think about how this could be done for your current code, you are not required to refactor it for the purposes of this assignment. In fact, throughout the rest of this semester we will be learning about new ways of better organizing code like this.

Again, sorry about the format, help if you can!

GRAMS 0.0355834 x (2Trh 3Tr2)

Solution

Hi Friend, I have implemented first part.

please repost second part separately.

Please let me know in case of any issue.

import java.util.Scanner;

public class ContainerCalculator

{

   public static void main(String[] args)

   {

       //Welcomes user to program

       System.out.println(\"Welcome to the Container Calculator!\");

       System.out.println(\"====================================\");

       //Prompts user to enter diameter

       System.out.print(\"Enter the diameter of a cylinder \"

               + \"(in centimeters): \");

       double diameter = 0;

       Scanner scnr = new Scanner(System.in);

       while (!scnr.hasNextInt()) { //first loop for diameter

           System.out.print(\"Please enter an integer value \"

                   + \"(less than 2,147,483,648) as decimal digits: \");

           scnr.nextLine();

       }

       diameter = scnr.nextInt();

       scnr.nextLine();

       while (diameter <= 0) { //second loop for diameter

           System.out.print(\"Please enter a positive integer value: \");

           diameter = scnr.nextInt();

           scnr.nextLine();

       }

       //Prompts user to enter height

       System.out.print(\"Enter the height of a cylinder (in centimeters): \");

       double height = 0;

       while (!scnr.hasNextInt()) { //first loop for height

           System.out.print(\"Please enter an integer value \"

                   + \"(less than 2,147,483,648) as decimal digits: \");

           scnr.nextLine();

       }

       height = scnr.nextInt();

       scnr.nextLine();

       while (height <= 0) { //second loop for height

           System.out.print(\"Please enter a positive integer value: \");

           height = scnr.nextInt();

           scnr.nextLine();

       }

       scnr.close();

       System.out.print(\"\ \");

       //Starts answer

       System.out.printf(\"A can with a diameter of %.0f and \"

               + \"a height of %.0f has \ \", diameter, height);

       //Sets up equation for volume

       double radius = diameter/2;

       double volume = Math.PI * radius * radius * height;

       System.out.print(\"\\ta volume of \");

       System.out.printf(\"%.2f\",volume);

       System.out.println(\",\");

       //Sets up equation for surface area

       double surfaceArea = (Math.PI * radius * height * 2)

               + (Math.PI * radius * radius * 2);

       System.out.print(\"\\tand a surface area of \");

       System.out.printf(\"%.2f\", surfaceArea);

       System.out.println(\".\ \");

       //Milestone 3

       System.out.println(\"*** Surface Area Optimizer ***\");

      

       double new_surface_area = 0;

       double new_volume = 0;

       double new_d = diameter, new_h = height;

       for(int d=1; d<volume; d++){ // diameter iteration

          

           for(int h=1; h<volume; h++){ // height iteration

              

               double v = Math.PI * d/2 * d/2 * h;

               double sa = (Math.PI * d/2 * h * 2)

                       + (Math.PI * d/2 * d/2 * 2);

               if(v > volume){

                   if(new_surface_area == 0 || sa < new_surface_area){

                       new_d = d;

                       new_h = h;

                       new_surface_area = sa;

                       new_volume = v;

                   }

               }

           }

       }

       System.out.printf(\"A can with a diameter of %.0f and \"

               + \"a height of %.0f has \ \", new_d, new_h);

       System.out.print(\"\\ta volume of \");

       System.out.printf(\"%.2f\",new_volume);

       System.out.println(\",\");

       System.out.print(\"\\tand a surface area of \");

       System.out.printf(\"%.2f\", new_surface_area);

       System.out.println(\".\ \");

       //Closing message for the program

       System.out.println(\"=============================================\");

       System.out.println(\"Thank you for using the Container Calculator.\");

   }

}

/*

Sample run:

Welcome to the Container Calculator!

====================================

Enter the diameter of a cylinder (in centimeters): 7

Enter the height of a cylinder (in centimeters): 2

A can with a diameter of 7 and a height of 2 has

   a volume of 76.97,

   and a surface area of 120.95.

*** Surface Area Optimizer ***

A can with a diameter of 5 and a height of 4 has

   a volume of 78.54,

   and a surface area of 102.10.

=============================================

Thank you for using the Container Calculator.

Welcome to the Container Calculator!

====================================

Enter the diameter of a cylinder (in centimeters): 2

Enter the height of a cylinder (in centimeters): 33

A can with a diameter of 2 and a height of 33 has

   a volume of 103.67,

   and a surface area of 213.63.

*** Surface Area Optimizer ***

A can with a diameter of 6 and a height of 4 has

   a volume of 113.10,

   and a surface area of 131.95.

=============================================

Thank you for using the Container Calculator.

*/

So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)
So this is what i have so far (sorry about format on here): import java.util.Scanner; public class ContainerCalculator { public static void main(String[] args)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site