In: Computer Science
Program must be OOP design.
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 } }
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.