5. Examine the following starter code. Use the comments and the code provided to
ID: 3875920 • Letter: 5
Question
5.
Examine the following starter code. Use the comments and the code provided to complete the constructor and two methods in the RectangleStats class.
import java.util.ArrayList;
class RectangleStats
{
// Private instance variables
// width is an ArrayList which will contain decimal values, length is an array which will contain decimal
// values, and area is an array which will contain integer values.
//code goes here for instance variables goes here
//The constructor for the RectangleStats class takes an ArrayList and an array as parameters for
// width and length, respectively.
// code for constructor goes here
// The calcRectangleArea() method calculates the area of rectangles using the length and width
// assigned to the private instance variables and assigns the results to the area array of type int.
// This method does not return anything.
// code for the calcRectangleArea() method goes here
// The printArea() method prints the values assigned to the area array using the most appropriate
// type of loop. This method does not return anything.
// code for the printArea() method goes here
}
// The RectangleStatsTesterClass assigns the width of two rectangles to an ArrayList and assigns the
// length of two rectangles to an array. The calcRectangleArea() and printArea() methods are invoked to
// calculate and print the area of the two rectangles.
public class RectangleStatsTesterClass
{
public static void main(String[] args)
{
ArrayList<Double> dblWidth = new ArrayList<Double>();
dblWidth.add(5.2);
dblWidth.add(9.3);
double [ ] dblLength = {11.1, 4.7};
RectangleStats rectStats = new RectangleStats(dblWidth, dblLength);
rectStats.calcRectangleArea();
rectStats.printArea();
}
}
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
import java.util.ArrayList;
class RectangleStats
{
// Private instance variables
// width is an ArrayList which will contain decimal values, length is an array which will contain decimal
// values, and area is an array which will contain integer values.
//code goes here for instance variables goes here
private ArrayList<Double> width;
private double[] length;
private int[] area;
//The constructor for the RectangleStats class takes an ArrayList and an array as parameters for
// width and length, respectively.
// code for constructor goes here
public RectangleStats(ArrayList<Double> wid, double[] len)
{
this.width = wid;
this.length = len;
}
// The calcRectangleArea() method calculates the area of rectangles using the length and width
// assigned to the private instance variables and assigns the results to the area array of type int.
// This method does not return anything.
// code for the calcRectangleArea() method goes here
public void calcRectangleArea()
{
area = new int[width.size()];
for(int i = 0; i < area.length; i++)
area[i] =(int) (width.get(i) * length[i]);
}
// The printArea() method prints the values assigned to the area array using the most appropriate
// type of loop. This method does not return anything.
// code for the printArea() method goes here
public void printArea()
{
for(int i = 0; i < area.length; i++)
System.out.println(area[i]);
}
}
output of RectagleStatsTesterClass
===========
57
43
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.