In: Computer Science
JAVA Please:
Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the lab, all methods will have a void return type and take in an array of integers as a parameter. You’re going to write a program that creates a mini database of numbers that allows the user to reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) – this initializes the array so that each cell is 0 • printSum (int[ ] arr) – this calculates the sum of the elements in the array and prints it • enterNum(int[ ] arr) – this asks the user for a slot number and value – putting the value into the array in the correct slot • printMenu (int[ ] arr) – prints the menu in the sample output (that’s it, nothing more) Note: C++ folks – if you want to pass the size of the array as a second parameter, you can. In main, create an array of 10 integers and immediately call initArray( ). Then, continuously looping, print the menu and ask the user what they want to do – calling the appropriate methods based on the user’s choice. Note that every time you call a method, you must pass the array that was created in main. If it makes it easier, we used a do-while loop and a switch statement in main. In our implementation, main was only 15 lines of code
Sample output #1
Would you like to:
1) Enter a number
2) Print the array
3) Find the sum of the array
4) Reset the array
5) Quit
etc...
CODE:
package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; //main driver class class Main{ //main method public static void main(String[] args)throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //initializing the array int []array = new int[10]; initArray(array); do{ //prints the menu ask user the choice printMenu(); System.out.println("Enter your choice: "); int choice = Integer.parseInt(in.readLine()); //calls the respective function using switch case switch (choice) { case 1 -> enterNum(array); case 2 -> printArray(array); case 3 -> printSum(array); case 4 -> initArray(array); case 5 -> System.exit(1); default -> System.out.println("Error in input!"); } }while (true); } //print function public static void printArray(int[] arr){ //printing all the elements for (int j : arr) { System.out.print(j + " "); } System.out.println(); } //init method public static void initArray(int[] arr){ //making all the values in the array 0 (fill method) Arrays.fill(arr, 0); } //print sum function public static void printSum(int[] arr){ int sum = 0; //traversing the array for (int j : arr) { sum += j; //calculating the sum } //displaying the sum System.out.println(sum); } //asks the user to enter a number at a given position public static void enterNum(int[] arr) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //asking for the position and the number System.out.print("Enter the position to enter the number: "); int position = Integer.parseInt(in.readLine()); System.out.print("Enter the number: "); //storing the number arr[position] = Integer.parseInt(in.readLine()); } //prints the menu public static void printMenu(){ System.out.println("\nWould you like to:"); System.out.println("1) Enter a number"); System.out.println("2) Print the array"); System.out.println("3) Find the sum of the array"); System.out.println("4) Reset the array"); System.out.println("5) Quit"); } }
__________________________________________
CODE IMAGES:
______________________________________________
OUTPUT:
_____________________________________________________
Feel free to ask any questions in the comments section
Thank You!