Question

In: Computer Science

Program must be OOP design.   Prompt the user to enter 10 doubles and fill an array...

Program must be OOP design.  

  1. Prompt the user to enter 10 doubles and fill an array
  2. Print the array
  3. Fill an array with 10 random numbers and print the array
  4. Sort the array -- then print
  5. Delete index[ 3] of the array and re-print the array

JAVA

This is my program so far. I am having trouble with my delete element method.



import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;


public class ArrayPlay {

    Random rand = new Random();//assign random number variable
    private double[] tenNumArray = new double[10];//initiate array containing 10 doubles.

    Scanner input = new Scanner(System.in);//create new scanner


    public Random getRand() {
        return this.rand;
    }

    public void setRand(Random rand) {
        this.rand = rand;
    }

    public double[] getTenNumArray() {
        return this.tenNumArray;
    }

    public void setTenNumArray(double[] tenNumArray) {
        this.tenNumArray = tenNumArray;
    } //end getters//setters

    //begin method
    public void fillArrayAndDisplay() {//begin prompt User method

        // #1 Prompt the user to enter 10 doubles and fill an array
        System.out.println("Enter ten elements to fill the array");
        for (int i = 0; i < this.tenNumArray.length; i++) {//for index equals 1 count plus 1 to 10
            this.tenNumArray[i] = input.nextDouble();//placing values index values into the array
        }//end for loop

        // #2 Print array
        System.out.println("The Numbers you entered into the array are");
        System.out.printf(Arrays.toString(this.tenNumArray)); // displays user filled array
    }//ends prompt User and print method


    // #3 Fill an array with 10 random numbers and print the array
    public void randomArrayAndDisplay() {   //begins random numbers and display array
        for (int i = 0; i < this.tenNumArray.length; i++) {
            this.tenNumArray[i] = rand.nextInt(10);//create the random numbers using Random class object. set to 10
        }
        System.out.println("\n The Randomly generated numbers are; ");
        System.out.printf(Arrays.toString(this.tenNumArray)); // displays random numbers
    }//ends random array and display method

    //  4. Sort the array -- then print
    public void sortNumberAndDisplay() {

        Arrays.sort(this.tenNumArray); //replaces your whole sort function
        System.out.println("\n The random numbers sorted are: ");
        System.out.printf(Arrays.toString(this.tenNumArray)); // replaces your display line
       }// end sort array method

    // #5. Delete index[ 3] of the array and re-print the array
    //begin delete element method
    public void deleteArrayElements(int index) {
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (this.tenNumArray == null
                || index >= this.tenNumArray.length || index < 0) {
            System.out.println("\n\n No deletion operation can be performed\n\n");

        } else {
            double[] tempArray = new double[this.tenNumArray.length - 1];

            // Copy the elements except the index
            // from original array to the other array
            for (int i = 0, k = 0; i < this.tenNumArray.length; i++) {

                // if the index is
                // the removal element index
                if (i == index) {
                    continue;
                }

                // if the index is not
                // the removal element index
                tempArray[k++] = this.tenNumArray[i];
            }

            // return the resultant array
            System.out.println(tempArray[4] + " \n delete num " + " ");

        }


    }

    public void loop(){
        fillArrayAndDisplay();//runs prompt user
        randomArrayAndDisplay();
        sortNumberAndDisplay();
        deleteArrayElements(3);//delete index 3
    }

}















Solutions

Expert Solution

Here is the solution,

I have done the required modification in the deleteArrayElements Method. Also generating values from 0 to 100

for better understanding whether the right element it getting deleted or not.

Added the main function for testing purpose

// your code starts here
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;


public class ArrayPlay {

Random rand = new Random();//assign random number variable
private double[] tenNumArray = new double[10];//initiate array containing 10 doubles.

Scanner input = new Scanner(System.in);//create new scanner


public Random getRand() {
return this.rand;
}

public void setRand(Random rand) {
this.rand = rand;
}

public double[] getTenNumArray() {
return this.tenNumArray;
}

public void setTenNumArray(double[] tenNumArray) {
this.tenNumArray = tenNumArray;
} //end getters//setters

//begin method
public void fillArrayAndDisplay() {//begin prompt User method

// #1 Prompt the user to enter 10 doubles and fill an array
System.out.println("\nEnter ten elements to fill the array");
for (int i = 0; i < this.tenNumArray.length; i++) {//for index equals 1 count plus 1 to 10
this.tenNumArray[i] = input.nextDouble();//placing values index values into the array
}//end for loop

// #2 Print array
System.out.println("\nThe Numbers you entered into the array are");
System.out.printf("\n" + Arrays.toString(this.tenNumArray)); // displays user filled array
}//ends prompt User and print method


// #3 Fill an array with 10 random numbers and print the array
public void randomArrayAndDisplay() { //begins random numbers and display array
for (int i = 0; i < this.tenNumArray.length; i++) {
this.tenNumArray[i] = rand.nextInt(100);//create the random numbers using Random class object. set to 10
}
System.out.println("\n\n The Randomly generated numbers are; ");
System.out.printf("\n" + Arrays.toString(this.tenNumArray)); // displays random numbers
}//ends random array and display method

// 4. Sort the array -- then print
public void sortNumberAndDisplay() {

Arrays.sort(this.tenNumArray); //replaces your whole sort function
System.out.println("\n\n The random numbers sorted are: ");
System.out.printf("\n" + Arrays.toString(this.tenNumArray)); // replaces your display line
}// end sort array method

// #5. Delete index[ 3] of the array and re-print the array
//begin delete element method
public void deleteArrayElements(int index) {
// If the array is empty
// or the index is not in array range
// return the original array
if (this.tenNumArray == null
|| index >= this.tenNumArray.length || index < 0) {
System.out.println("\n\n No deletion operation can be performed\n\n");

} else {
double[] tempArray = new double[this.tenNumArray.length - 1];

// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < this.tenNumArray.length; i++) {

// for deleting the element with index value 3
// if i == index then we will skip it from copying it to temp
if (i == index) {
continue;
}

//copying all the other elements other than index 3 in temp array
tempArray[k++] = this.tenNumArray[i];
}
  
System.out.println("\n Element deleted : " + this.tenNumArray[index] + " at index value 3");
// make the tenNumArray to point to the new generated array i.e tempArray
this.tenNumArray = tempArray;
// return the resultant array
System.out.println("\n" + Arrays.toString(this.tenNumArray));

}


}

public void loop(){
fillArrayAndDisplay();//runs prompt user
randomArrayAndDisplay();
sortNumberAndDisplay();
deleteArrayElements(3);//delete index 3
}
// for testing i have added the code in the main function
public static void main(String[] args) {
ArrayPlay a = new ArrayPlay();
a.loop();
}

}
//code ends here

here is the sample output:-

Thank You.


Related Solutions

Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Design the logic for a program that allows a user to enter 10 numbers, stores the...
Design the logic for a program that allows a user to enter 10 numbers, stores the numbers in an array, then displays all of the numbers, the largest number, and the smallest.  create a solution algorithm using pseudocode  create a flowchart
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT