Skip to main content

L02 - Soda Can Lab

In this exercise, we're going to implement a class SodaCan. This class has a constructor that accepts the height and radius of the soda can as parameters. We'll supply the methods getVolume and getSurfaceArea. We'll also write a SodaCanTest class to test our SodaCan class.

  1. Use a calculator to generate some test cases. We'll use these to check the program's results:
    Case 1: r = 4   h = 10  Area = _____________ Volume = ______________ 
    Case 2: r = 3.5 h = 7.2 Area = _____________ Volume = ______________
    
  2. Enter the program below that contains a skeleton of the SodaCan class. You must supply the code that fills in the blanks.
    /**
     Constructor and methods for a soda can object 
     @author __________
     @version __________
     */
     public class SodaCan { 
       __________ double r; //soda can radius 
       __________ double h; //soda can height 
       public SodaCan(double _____, double _____) { 
         r = _____; 
         h = _____; 
       } 
       public _______ findSurfaceArea() { 
         return _____; //compute the surface area A=2πrh+2πr^2 
       }
    
       public double findVolume(){
          return _____; //compute the volume V=πr^2h 
       } 
     }
    
  3. Enter the program below that contains a skeleton of the SodaCanTest class. You must supply the code that fills in the blanks.
    import java.util.Scanner;
    /** 
     _______________________________ 
     @author __________ 
     @version __________
     */
     public class ___________ { 
       public static void main(String[] args) { 
         Scanner myScanner = new Scanner(System.in);
    
         System.out.print("Enter the radius: ");
         String answer = myScanner.nextLine();
         double radius = ______; //convert using the parseDouble method
    
         System.out.print("Enter the height: "); 
         answer = myScanner.nextLine(); 
         double height = ______; //convert using the parseDouble method
    
         //instantiate a soda can object using the radius and height above 
         //display the surface area to three decimal digits using printf 
         //display the volume to three decimal digits using printf 
       } 
     } 
    
  4. Use your test cases to check your code.