0

I'm doing an assignment for my Computer Science class, but I got stuck in this one part. I am supposed to code the constructor for the given main method and instance variables. The constructor needs to have an ArrayList and an Array as parameters, but I'm not entirely sure how to.

The instructions and main method is as follows:

Use the comments and the code provided to complete the constructor

import java.util.ArrayList;
class RectangleStats
 { 
 //instance variables go 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 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 dblWidth = new ArrayList();

       dblWidth.add(5.2);

       dblWidth.add(9.3);

       double [ ] dblLength = {11.1, 4.7};


       RectangleStats rectStats = new RectangleStats(dblWidth, dblLength);


       rectStats.calcRectangleArea();

       rectStats.printArea();

       }

  }

The constructor I currently have is this: public RectangleStats(double w, double l) { width = w; length = l; area = 0; }

but im not sure if its right or not.. Help?

2
  • where is your constructor ? Commented Mar 2, 2014 at 7:08
  • well thats what I gotta figure out.. I wrote down what I thought the constructor should be but im not so sure.. Commented Mar 2, 2014 at 7:11

2 Answers 2

2

If the actual parameters you pass are an ArrayList and array respectively, then your formal parameters in your method signature should also be the same. That is:

public RectangleStats(ArrayList<Double> arg0, double[] arg1)

You would then assign arg0 and arg1 (or whatever your identifiers are) to your instance variables.

That being said, the entire idea behind the implementation is very poor (eg: using an ArrayList and an array rather than one of either).

Also, when you declare your ArrayList, do this as follows: ArrayList<Double> dblWidth = new ArrayList<Double>();

It's almost always better to specify the class of the objects that you're going to be adding.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer Yeah i guess the implementation of using them isn't all that great, but unfortunately I'm not the one making the questions :/
No problem. I understand this is homework and you can't change your teacher's question, but in case you're interested, it would make more sense to have a Rectangle class with a width and length parameter - then you could simply declare an array holding 2 of them, rather having an object such as the one in your question to hold two rectangle widths and two lengths (which makes much less sense).
2

With a good java book or tutorial you might have easily solved your problem but this is how you'd do it:

ArrayList<Double> w;
double []length;
public RectangleStats(ArrayList<Double> width, double length[])
 { 
     this.width = width;
     this.length = length;

 {

I'm guessing these variables are used outside the constructor hence the initialization.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.