In: Computer Science
Can you please solve this problem in java
lab 9b: 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.
Page 3 of 4 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 1 Enter the slot: 5 Enter the new value: 76 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 1 Enter the slot: 2 Enter the new value: 33 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 2 |0|0|33|0|0|76|0|0|0|0 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 3 109 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 4 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 2 |0|0|0|0|0|0|0|0|0|0 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 5
Code
import java.util.Scanner;
public class MicroDB {
public static void main(String[] args) {
Scanner scnr=new
Scanner(System.in);
int arr[]=new int[10];
int choice;
initArray(arr);
do
{
printMenu();
choice=scnr.nextInt();
switch (choice)
{
case 1:
enterNum(arr);
break;
case 2:
printArray(arr);
break;
case 3:
printSum(arr);
break;
case 4:
initArray(arr);
break;
case 5:
break;
default:System.out.println("Invalid choice.");
break;
}
}while(choice!=5);
}
private static void printSum(int[] arr) {
int sum=0;
for(int
i=0;i<arr.length;i++)
sum+=arr[i];
System.out.println(sum);
}
private static void printArray(int[] arr) {
System.out.print(arr[0]);
for(int
i=1;i<arr.length;i++)
System.out.print("|"+arr[i]);
System.out.println();
}
private static void enterNum(int[] arr) {
Scanner scnr=new
Scanner(System.in);
int slot;
System.out.print("Enter the slot:
");
slot=scnr.nextInt();
System.out.print("Enter the new
value: ");
arr[slot]=scnr.nextInt();
}
private static void printMenu() {
System.out.println("Would you like
to:\n1) Enter a number\n2) Print the array\n3) Find the sum of the
array\n4) Reset the array\n5) Quit");
}
private static void initArray(int[] arr) {
for(int
i=0;i<arr.length;i++)
arr[i]=0;
}
}
outputs
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.