Question
SodaCanTester.java /** A class to calculate the area of several SodaCans */ public class SodaCanTester { /** Calculates the average area in an array list
SodaCanTester.java
/**
A class to calculate the area of several SodaCans
*/
public class SodaCanTester
{
/**
Calculates the average area in an array list of SodaCans
@param SodaCanArray the SodaCans
@return the average area of SodaCanArray
*/
private static double averageArea(Measurable[] sodaCanArray)
{
//-----------Start below here. To do: approximate lines of code = 4
// get the measure of area from all sodaCan objects in the array, add them up and
// compute the average. return the average
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
SodaCan[] sodaCans = new SodaCan[4];
sodaCans[0] = new SodaCan(2, 3);
sodaCans[1] = new SodaCan(3, 3);
sodaCans[2] = new SodaCan(5, 7);
sodaCans[3] = new SodaCan(1, 1);
System.out.printf("The average area is: %10.2f", averageArea(sodaCans));
System.out.println("Expected:The average area is: 186.92");
}
}
SodaCan.java
/**
Class to get surface area and volume of a soda can.
*/
public class SodaCan implements Measurable
{
private double height;
private double radius;
/**
Initializes a can with given height and radius.
@param height the height
@param radius the radius
*/
public SodaCan(double height, double radius)
{
this.height = height;
this.radius = radius;
}
/**
Calculates the surface area of the soda can.
@return the surface area of the soda can
*/
public double getSurfaceArea()
{
return 2 * Math.PI * radius * (radius + height);
}
/**
Calculates the volume of the soda can.
@return the volume of the soda can
*/
public double getVolume()
{
return Math.PI * radius * radius * height;
}
/**
Returns the area of the soda can.
@return the area of the soda can
*/
//-----------Start below here. To do: approximate lines of code = 2
// write a method that correctly implements the Measurable interface
// The method should return the surface area of the sodaCan object
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
Measurable.java
public interface Measurable
{
double getMeasure();
/**
Computes the smallest of the measures of the given objects.
@param objects an array of Measurable objects
@return the object with the smallest measure, null if array is empty
*/
static Measurable smallest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable min = objects[0];
for (Measurable obj : objects)
{
if (obj.getMeasure() < min.getMeasure())
{
min = obj;
}
}
return min;
}
/**
Computes the largest of the measures of the given objects.
@param objects an array of Measurable objects
@return the object with the largest measure, null if array is empty
*/
static Measurable largest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable max = objects[0];
for (Measurable obj : objects)
{
if (obj.getMeasure() > max.getMeasure())
{
max = obj;
}
}
return max;
}
}
P.s: please besides the code, also include a screenshot of code, and the output to confirm code doesn't get edited out and the output satisfies the problem!!
Purpose: practice implementing an interface (Measurable) and writing a general method that computes the average area of an array of SodaCan objects See the following files: * SodaCanTester.java (has todo) * SodaCan.java (has todo) * Measurable.java Approximate total lines of code required: 6
Step by Step Solution
3.45 Rating (145 Votes )
There are 3 Steps involved in it
Step: 1
SodaCanTesterjava A class to calculate the area of several SodaCans public class SodaCanTester Calculates the average area in an array list of SodaCan...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started