Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Class Numbers:

Java
public class Numbers {
private Float [] numbers;
private int default_size = 10;

public Numbers() {
    ///  write code here to intialize a "default" array since this is the default constructor
    numbers = new Float[default_size];
    for(int i = 0; i < default_size; i++)
    numbers [i] = (float) 0.0;
}

public Numbers (int size) {
    ///  write code here to initialize an "initial" array given the parameter size as this is an initial constructor
    numbers = new Float[size];
}

public void initValuesInArray() {
    /// write code here to intialize the values in the array
    Scanner scan = new Scanner(System.in);
    for (int i = 0; i < numbers.length; i++) {
        System.out.println("Enter Value : ");
        numbers[i] = scan.nextFloat();
    }
}

public String toString() {
    // write code to return the values in the array in a list format - one value per line
    for (int i = 0; i < numbers.length; i++)
    System.out.println(numbers[i]);
    return " ";
}

public float calcAverage() {
    // write code to return the average of the values in the array
    double sum = 0.0;
    for (int i = 0; i < numbers.length; i++)
        sum += numbers[i];
    double average = sum / numbers.length;
    System.out.println(average);
    return (float) average;
}
}

Class Main:

public class Main {
public static void main (String [] args) {

    // write the code here to implement the menu as specified in Lab 1

    boolean menuLoop = true;
    while(true) {
    System.out.println("Enter 1 to initialize a default array");
    System.out.println("Enter 2 to initialize an array of input size");
    System.out.println("Enter 3 fill array with values");
    System.out.println("Enter 4 display values in array");
    System.out.println("Enter 5 to display average of the values in the array");
    System.out.println("6 to quit\n");

    Numbers obj = new Numbers();
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();

    switch (i) {

    case 1:
        obj = new Numbers ();
        break;

    case 2:
        System.out.println("Enter the new size of array: ");
        int x = scan.nextInt();
        Numbers [] numbers = new Numbers[x];
        break;

    case 3:
        System.out.println("Enter the float numbers as values in the array: ");
        obj.initValuesInArray();
        break;

    case 4:
        obj.toString();
        break;

    case 5:
        obj.calcAverage();
        break;

    case 6:
        System.exit(0);
        break;
    default :
        System.out.println("Invalid Entry...");
        break;
        }

    } 
}
}

It needs to be able to change the size of the array based on user input and thereafter the user can enter the values into the array.

What I have tried:

I am trying to initialize an array based on user input so it can be of any custom size. By default i have initialized array to 10 with all 0.0 values but when I try call the method that initializes the array nothing happens and the values neither the size or the values in the array change
Posted
Updated 24-Jan-19 17:26pm
v2

1 solution

I would recommend couple of changes

1. on case 2, re initialize the object using Numbers (int size) method

Java
case 2:
        System.out.println("Enter the new size of array: ");
        int x = scan.nextInt();
        obj = new Numbers(x);
        break;

2. move Numbers obj = new Numbers(); outside the while loop to prevent it from being overwritten each time user make a selection

3. You night also need to update Numbers (int size) method to include code to initialize the array

Example: Online Java Compiler - Online Java Editor - Java Code Online - Online Java Runner[^] - make sure to use interactive mode to run the example.
 
Share this answer
 
Comments
JustinMac 25-Jan-19 9:57am    
This worked!!! Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900