0

I am trying to solve the following question:

Implement a class called TriangleArray which has the following: No other instance variables or constants allowed.

  1. An instance variable called list which is an array of Triangle objects.
  2. No other instance variables or constants allowed.
  3. Only one constructor which has two integer parameters. The first parameter called number is the length of list. The second parameter called maxSize is the maximum size allowed for the
  4. The constructor initializes list to an array of Triangle objects.
  5. The sides of each Triangle object are randomly generated integers in the range from 1 to maxSize.
  6. Getter and setter methods for list.
  7. A largest method with no parameters. The method returns the largest Triangle in list.
  8. A toString method which returns a description of each Triangle in list.

How can I populate the list full of random objects without using other instance variable?

Here is the code that I have so far:

import java.util.Random;
public class TriangleArray
{
  private Triangle[ ] list;
  // list is an array of "number" Triangle objects.   
  // The maximum size of each Triangle edge is "maxSize".
  public TriangleArray (int number,  int maxSize);
  {
    list = new Triangle[number];
Community
  • 1
  • 1
tacoofdoomk
  • 53
  • 1
  • 7

4 Answers4

1

You can do this with the local variables in functions.

public TriangleArray (int number,  int maxSize) {
    list = new Triangle[number];
    Random random = new Random();
    for(int i = 0; i < number; list[i] = makeRandomTriangle(random, maxSize);
}

private function makeRandomTriangle(Random random, int maxSize) {
    int a = random.nextInt(maxSize) + 1,
        b = random.nextInt(maxSize) + 1;

    // |a - b| <= c <= (a + b) because of the triangle inequality
    int min = Math.max(a - b, b - a),
        max = Math.min(maxSize, a + b);
    int c = rand.nextInt((max - min) + 1) + min;
    new Triangle(a, b, x);
}
John Bupit
  • 10,406
  • 8
  • 39
  • 75
0

An instance variable, or field, is not the same as a local variable.

See: What is the difference between a local variable, an instance field, an input parameter, and a class field?

You can use local variables just fine, like:

public TriangleArray (int number,  int maxSize) {
  list = new Triangle[number];
  for(int i = 0; i < number; i++) {
    list[i] = makeTriangle(maxSize);
   }
}

private makeTriangle(maxSize) {
  Random r = new Random();

  do {
    int first = r.nextInt(maxSize - 1) + 1;
    int second = r.nextInt(maxSize - 1) + 1;
    int third = r.nextInt(maxSize - 1) + 1;

    int max = Math.max(first, Math.max(second, third));
  } while ((2 * max) - first - second - third) > 0);

  return new Triangle(first, second, third);
}

In this code you never create a new instance variable.

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
0

You do not need to use instance variables to fill in random numbers. You could use your java.util.Random or Math.random:

list = new Triangle[number];
for (int i = 0; i < list.length; i++) {
    int a = (int)(Math.random() * (maxSize + 1));
    int b = (int)(Math.random() * (maxSize + 1));
    int c = (int)(Math.random() * (maxSize + 1));

    //handle the triangle inequality:
    while (c > a + b || c < Math.abs(a - b)) {
        c = (int)(Math.random() * (maxSize + 1));
    }

    list[i] = new Triangle(a, b, c);
}
durron597
  • 31,968
  • 17
  • 99
  • 158
L3R5
  • 198
  • 1
  • 8
0

Assuming a Triangle object's constructor looks like:

public Triangle(int a, int b, int c) { ...

We initialize the array with:

Random r = new Random(); // random number gen
for ( int i = 0; i < number; i++ ) {
  int a = r.nextInt(maxSize-1) + 1; // randomly generate each side
  int b = r.nextInt(maxSize-1) + 1;
  int c = r.nextInt(maxSize-1) + 1; // this won't necessarily make a proper triangle
  list[i] = new Triangle(a, b, c);
}

We have to create a random number generator, then we have to iterate through the entire array of uncreated Triangles and create them. While iterating, we randomly generate each side a, b, and c, to a number from 1 to maxSize, then create the Triangle object at the current index.

The list is your instance variable, meanwhile the variables a, b, c, and r are all local variables to the constructor or whatever function you put them in.

elrobe
  • 555
  • 2
  • 16