Here is what I have so far. The code is complete & fully functioning except that arr2 fills
Here is what I have so far. The code is complete & fully functioning except that arr2 fills
in with random number instead of “1, 2, 3” like I’m trying to make it do.
How do I make arr2 = 1, 2, 3 instead of random numbers?
/*————————————————————————-
// AUTHOR: Andrew Mays
// FILENAME: Arrays.java, lab8.java
// SPECIFICATION: Performs arithmetic on arrays
// FOR: CSE 110- Lab #8
// TIME SPENT: 4 hours and 30 minutes
//———————————————————–*/
public class Lab8
{
public static void main(String[] args)
{
// Arrays object using the first constructor
Arrays arr1 = new Arrays(5);
// Print the contents of the array in arr1
System.out.println(arr1);
// Call findMin, findMax, and calcAverage on arr1 and print their values
System.out.println(“Min: ” + arr1.findMin());
System.out.println(“Max: ” + arr1.findMax());
System.out.println(“Average: ” + arr1.calcAverage());
System.out.println();
// Arrays object using the first constructor
Arrays arr2 = new Arrays(3);
// Print the contents of the array in arr2
System.out.println(arr2);
// Call findMin, findMax, and calcAverage on arr2 and print their values
System.out.println(“Min: ” + arr2.findMin());
System.out.println(“Max: ” + arr2.findMax());
System.out.println(“Average: ” + arr2.calcAverage());
System.out.println();
}
}
/*————————————————————————-
// AUTHOR: Andrew Mays
// FILENAME: Arrays.java, lab8.java
// SPECIFICATION: Performs arithmetic on arrays
// FOR: CSE 110- Lab #8
// TIME SPENT: 4 hours and 30 minutes
//———————————————————–*/
import java.util.Random;
public class Arrays
{
// Instance Variables
private int[] array;
private int count;
private int[] arr1;
private int[] arr2 = {1, 2, 3};
// Constructors
public Arrays(int size)
{
array = new int[size];
count = size;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
array[i] = (rand.nextInt(10));
}
}
public Arrays(int[] arr)
{
array = arr1;
array = arr2;
count = array.length;
}
// findMin
public int findMin()
{
int min = array[0]; // Set min to the first element
for (int i = 1; i < count; i++)
{
// Reassign min if there is a smaller element
if (array[i] < min)
{
min = array[i];
}
}
return min; // Return the smallest element
}
// findMax
public int findMax()
{
int max = array[0]; // Set max to the first element
for (int i = 1; i < count; i++)
{
// Reassign max if there is a larger element
if (array[i] > max)
{
max = array[i];
}
}
return max; // Return the largest element
}
// calcSum
private int calcSum()
{
int sum = 0;
for (int i = 0; i < count; i++)
{
sum += array[i];
}
return sum;
}
// calcAverage
public double calcAverage()
{
return (double) calcSum() / count;
}
// toString
public String toString()
{
String output = “[ “;
for (int i = 0; i < count; i++)
{
output += array[i];
if (i != count – 1)
{
output += “, “;
}
}
return output + ” ]”;
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
