1) Write a class called SodaCan that contains instance data for the height and d
ID: 3808940 • Letter: 1
Question
1) Write a class called SodaCan that contains instance data for the height and diameter of the soda can. (70 points)
Define the SodaCan constructor to accept and initialize the height and diameter of the soda can.
Overload the Sodacan constructor such that the initial value of instance data (height and diameter) are 0.
Include getter and setter method for all instance data.
Provide a method getVolume to find the volume of the soda can.
Provide a method getSurfaceArea to find the surface area of the soda can.
2) Create a diver class SodaCanTest with main method. (30 points)
Driver class instantiate one soda can object and find it’s volume and surface area.
Driver class create another soda can object and read its height and diameter value, then output this soda can object’s volume and surface area.
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
########### SodaCane.java #########
public class SodaCan {
private double height;
private double diameter;
public SodaCan() {
height = 0;
diameter = 0;
}
public SodaCan(double height, double diameter) {
this.height = height;
this.diameter = diameter;
}
public double getHeight() {
return height;
}
public double getDiameter() {
return diameter;
}
public void setHeight(double height) {
this.height = height;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
public double getVolume(){
return Math.PI*diameter*diameter*height/4.0;
}
public double getSurfaceArea(){
return 2*Math.PI*diameter*(diameter/2 + height)/2;
}
}
#############################
import java.util.Scanner;
public class SodaCanTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter height of soda cane: ");
double height = sc.nextDouble();
System.out.print("Enter diameter of soda cane: ");
double diameter = sc.nextDouble();
// creatign object
SodaCan soda = new SodaCan(height, diameter);
System.out.println("Volume: "+soda.getVolume());
System.out.println("Surface Area: "+soda.getSurfaceArea());
}
}
/*
Sample run:
Enter height of soda cane: 5.6
Enter diameter of soda cane: 4
Volume: 70.37167544041137
Surface Area: 95.50441666912971
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.