Jump to content

I need to store all of the data from the array in the cunstructor, but i'm not sure how. My instructor specified that I do it that way.  

 

 
 
 
 
import java.util.*;
public class RandomArray 
{
  public int size;
public int value, result;
 
private int[] num = new int
 
    public RandomArray(int ... x ) 
    {
   
   
    Scanner scan = new Scanner(System.in);
    Random gen = new Random();
   
    System.out.print("Enter how many numbers you would like to Generate: ");
    size = scan.nextInt();
   
    for (int ran = 0; ran < num.length; ran++ )
    num[ran] = gen.nextInt(size) + 1;
   
    for (int value : num)
   
   
    }
    
   public double getAverage(int ... num )
   {
    double avg = 0.0; 
    if (num.length != 0)
    {
    int sum = 0;
    for (int list : num)
    sum += list;
    avg = (double)sum / num.length;
    }
   
    return avg;
   }
   
   
  
  public String toString()
  {
  String result = Integer.toString(value);
 
  return result;
  }
}
Link to comment
https://linustechtips.com/topic/281660-need-help-in-a-java-program/
Share on other sites

Link to post
Share on other sites

What is the constructors parameter for? I don't see it being used for anything.

public RandomArray(int ... x ) 

Besides that your constructor looks right to me (minus the extra for loop at the end). What problem are you having?

 

edit: It's probably that you're initializing num before you've defined size and size will have the default value of zero.

Link to post
Share on other sites

What is the constructors parameter for? I don't see it being used for anything.

public RandomArray(int ... x ) 

Besides that your constructor looks right to me (minus the extra for loop at the end). What problem are you having?

 

edit: It's probably that you're initializing num before you've defined size and size will have the default value of zero.

Ok I changed it to 

 

 
   public double getAverage(int ... result)
   {
    double avg = 0; 
    if (result.length != 0)
    {
    int sum = 0;
    for (int list : result)
    sum = sum + list;
    avg = (double)sum / size;
    }
   
    return avg;
 
For the constructor the last loop is to run through the array i think, but when I run getAverage() the output is just whatever I initialize sum to be. 
Link to post
Share on other sites

You could do it this way:

import java.util.Random;import java.util.Scanner;import java.util.*;public class RandomArray extends ArrayList<Double> {	private static final long serialVersionUID = 1L;	public RandomArray() {		super();	}	public static RandomArray getInstance(int size) {		Random randomNumberGenerator = new Random();		RandomArray randomArray = new RandomArray();		for (int i = 0; i < size; i++) {			randomArray.add(randomNumberGenerator.nextDouble());		}		return randomArray;	}	public Double getAverage() {		if (this.size() == 0) {			return 0d;		}		Double sum = 0d;		for (Double element : this) {			sum = sum + element;		}		return sum / this.size();	}	public static void main(String[] args) {		Scanner scan = new Scanner(System.in);		System.out.print("Enter how many numbers you would like to Generate: ");		RandomArray randomArray = RandomArray.getInstance(scan.nextInt());		System.out.println(randomArray);		System.out.println(randomArray.getAverage());	}}
Link to post
Share on other sites

 

You could do it this way:

import java.util.Random;import java.util.Scanner;import java.util.*;public class RandomArray extends ArrayList<Double> {	private static final long serialVersionUID = 1L;	public RandomArray() {		super();	}	public static RandomArray getInstance(int size) {		Random randomNumberGenerator = new Random();		RandomArray randomArray = new RandomArray();		for (int i = 0; i < size; i++) {			randomArray.add(randomNumberGenerator.nextDouble());		}		return randomArray;	}	public Double getAverage() {		if (this.size() == 0) {			return 0d;		}		Double sum = 0d;		for (Double element : this) {			sum = sum + element;		}		return sum / this.size();	}	public static void main(String[] args) {		Scanner scan = new Scanner(System.in);		System.out.print("Enter how many numbers you would like to Generate: ");		RandomArray randomArray = RandomArray.getInstance(scan.nextInt());		System.out.println(randomArray);		System.out.println(randomArray.getAverage());	}}

hmm, yea I want to do it that way, but my teacher's instructions are: 

  1. Implement a class called RandomArray, which has an integer array. The constructor receives the size of the array to be allocated, then populates the array with random numbers from the range 0 through the size of the array. Methods are required that return the minimum value, maximum value, average value, and a string representation of the array values. Include the output for several different test runs that shows the array with its minimum, maximum, and average values.
Link to post
Share on other sites

 

hmm, yea I want to do it that way, but my teacher's instructions are: 

  1. Implement a class called RandomArray, which has an integer array. The constructor receives the size of the array to be allocated, then populates the array with random numbers from the range 0 through the size of the array. Methods are required that return the minimum value, maximum value, average value, and a string representation of the array values. Include the output for several different test runs that shows the array with its minimum, maximum, and average values.

 

Ok so is there anyway to put the Getinstance in the constructor?

Link to post
Share on other sites

 

Ok I changed it to 

 

 
   public double getAverage(int ... result)
   {
    double avg = 0; 
    if (result.length != 0)
    {
    int sum = 0;
    for (int list : result)
    sum = sum + list;
    avg = (double)sum / size;
    }
   
    return avg;
 
For the constructor the last loop is to run through the array i think, but when I run getAverage() the output is just whatever I initialize sum to be. 

 

 

The functions are supposed to be run on the internal array not an array being passed to it. The min, max, and average functions wont need parameters.

Link to post
Share on other sites

You should do your homework by yourself ;).

 

Just move the the logic from getInstance into a constructor with parameter size. 

It may be interesting to use SortedList instead of ArrayList to implement the min/max functions.

It's probably beyond what he's learnt in class.

 

@apostlegamer Just use a simple loop for the min/max functions. And if you don't understand someone's code, it's best not to use it. If your teacher thinks it's too advanced for you and you can't explain it, they might punish you for cheating.

Link to post
Share on other sites

It's probably beyond what he's learnt in class.

 

@apostlegamer Just use a simple loop for the min/max functions. And if you don't understand someone's code, it's best not to use it. If your teacher thinks it's too advanced for you and you can't explain it, they might punish you for cheating.

yea i don't plan on using that code. just confused me more, I think I missed something in the teachings :P. I would be asking my instructor but he's on holidays which sucks.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×