In: Computer Science
This needs to be in Java
implementing Insertion Sort and displaying the floats sorted.
Sample Output: (green is user input)
Please select one of the following: 1: Initialize a default array 2: To specify the max size of the array 3: Add value to the array 4: Display values in the array 5: Display the average of the values 6: Enter multiple values 7: Read values from file 8: Save values to file 9: Sort the array 10: To Exit
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;
class Numbers {
private float[] numbers;
private int numItems = 0, size = 1;
public Numbers() {
numbers = new float[size];// default size }
public Numbers(int size) {
this.size = size;
numbers = new float[size]; }
public void addValue(Scanner scanner, boolean fromKeyboard) {
if (fromKeyboard)
System.out.print("Enter value: ");
float value = scanner.nextFloat();
numbers[numItems++] = value;
if (numItems >= size) {
float[] temp = new float[size + 5];
for (int i = 0; i < numItems; i++) {
temp[i] = numbers[i]; }
numbers = temp; } }
public float calcAverage() {
float sum = 0;
for (int i = 0; i < numItems; i++) {
sum += numbers[i];// calculating sum of the snumbers in the array }
return (float) sum / numItems;// calculating average }
@Override
public String toString() {
String data = "";
for (int i = 0; i < numItems; i++) {
data += numbers[i] + "\n"; }
return data; }
public void addMultiple(Scanner scanner, boolean fromKeyboard) {
if (fromKeyboard) {
System.out.print("How many values do you wish to add? "); }
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
addValue(scanner, fromKeyboard);
} }
public void loadData(Scanner scanner) {
System.out.println("Name of the file to read from:");
String name = scanner.next();
try {
Scanner fileScanner = new Scanner(new File(name));
addMultiple(fileScanner, false);
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (Exception e) {
System.out.println("Invalid file format!");
}
}
public void saveData(Scanner scanner) {
System.out.println("Name of the file to save to:");
String name = scanner.next();
try {
PrintWriter writer = new PrintWriter(new File(name));
writer.println(numItems);
for (int i = 0; i < size; i++) {
writer.println(numbers[i]);
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println("File cant be opened!");
} }}
public class Test {// driver class
public static void main(String[] args) {
Numbers obj = new Numbers();
Scanner sc = new Scanner(System.in);
while (true) {// infinte loop
System.out.print("Please select one of the following:\r\n"
+ "1: Initialize a default array\r\n"+ "2: To specify the max size of the array\r\n" + "3: Add value to the array\r\n" + "4: Display values in the array\r\n" + "5: Display the average of the values\r\n" + "6: Enter multiple values\r\n" + "7: Read values from file\r\n" + "8: Save values to file\r\n" + "9: To Exit\r\n" + "> ");
int choice = sc.nextInt();// taking user choice
sc.nextLine();
switch (choice) {
case 1: System.out.println("New array initialized"); obj = new Numbers(); break;
case 2: System.out.print("Enter new size of the array: ");int size = sc.nextInt(); sc.nextLine(); obj = new Numbers(size); break;
case 3: obj.addValue(sc, true); break;
case 4: System.out.println("Numbers are:\n" + obj); break;
case 5: System.out.println("Average is: " + obj.calcAverage()); break;
case 6: // adding multiple values from keyboard obj.addMultiple(sc, true);break;
case 7:// loading values from fileobj.loadData(sc);break;
case 8:// saving values to fileobj.saveData(sc); break;
case 9: System.out.println("Good Bye"); System.exit(0);// exiting the program
default: System.out.println("Invalid choice!"); break; } } }}
If you have any problem with the code feel free to comment. I have highlighted the changes in the code
Program
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class Numbers {
private float[] numbers;
private int numItems = 0, size = 1;
public Numbers() {
numbers = new float[size];//
default size
}
public Numbers(int size) {
this.size = size;
numbers = new float[size];
}
public void addValue(Scanner scanner, boolean fromKeyboard) {
if (fromKeyboard)
System.out.print("Enter value: ");
float value = scanner.nextFloat();
numbers[numItems++] =
value;
validateArraySize();
}
public void validateArraySize() {
if (numItems >= size)
{
float[] temp = new float[size + 5];
for (int i = 0; i < numItems; i++) {
temp[i] = numbers[i];
}
numbers = temp;
}
}
public float calcAverage() {
float sum = 0;
for (int i = 0; i < numItems; i++)
sum += numbers[i];// calculating sum of the snumbers in the array }
return (float) sum / numItems;
// calculating average }
}
@Override
public String toString() {
String data = "";
for (int i = 0; i < numItems; i++) {
data +=
numbers[i] + "\n";
}
return data;
}
public void addMultiple(Scanner scanner, boolean fromKeyboard) {
if (fromKeyboard) {
System.out.print("How many values do you wish to add? ");
}
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
addValue(scanner, fromKeyboard);
}
}
public void loadData(Scanner scanner) {
System.out.println("Name of the file to read from:");
String name = scanner.nextLine();
try {
Scanner fileScanner = new Scanner(new File(name));
while
(fileScanner.hasNext()) {
float value =
Float.parseFloat(fileScanner.nextLine());
numbers[numItems++] = value;
validateArraySize();
}
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (Exception e) {
System.out.println("Invalid file format!");
}
}
public void saveData(Scanner scanner) {
System.out.println("Name of the file to save to:");
String name = scanner.next();
try {
PrintWriter writer = new PrintWriter(new File(name));
writer.println(numItems);
for (int i = 0; i < size; i++) {
writer.println(numbers[i]);
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println("File cant be opened!");
}
}
public void sortArray() {
//performing insertion sort
for (int i = 1; i < numItems;
++i) {
float toInsert =
numbers[i];
int j = i -
1;
while (j >= 0
&& numbers[j] > toInsert) {
numbers[j + 1] = numbers[j];
j = j - 1;
}
numbers[j + 1] =
toInsert;
}
}
}
public class Test {// driver class
public static void main(String[] args) {
Numbers obj = new Numbers();
Scanner sc = new Scanner(System.in);
while (true) {// infinte loop
System.out.print("Please select one of the following:\r\n"
+ "1: Initialize a default
array\r\n" + "2: To specify the max size of the array\r\n"
+ "3: Add value to the
array\r\n" + "4: Display values in the array\r\n"
+ "5: Display the average of
the values\r\n" + "6: Enter multiple values\r\n"
+ "7: Read values from
file\r\n" + "8: Save values to file\r\n" + "9: Sort the
array\r\n"
+ "10: To Exit\r\n" + ">
");
int choice = sc.nextInt();// taking user choice
sc.nextLine();
switch (choice) {
case 1:
System.out.println("New array
initialized");
obj = new Numbers();
break;
case 2:
System.out.print("Enter new size of the array:
");
int size = sc.nextInt();
sc.nextLine();
obj = new Numbers(size);
break;
case 3:
obj.addValue(sc, true);
break;
case 4:
System.out.println("Numbers are:\n" +
obj);
break;
case 5:
System.out.println("Average is: " +
obj.calcAverage());
break;
case 6:
obj.addMultiple(sc, true);
break;
case 7:
obj.loadData(sc);
break;
case 8:
obj.saveData(sc);
break;
case
9:
obj.sortArray();
break;
case
10:
System.out.println("Good Bye");
System.exit(0);// exiting the
program
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
Output
Input