In: Computer Science
(JAVA)
Create 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)
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.
It should behave like the sample output below: (user input = bold)
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
import java.util.*;
public class Main
{
static int arr[]=new int[10];
static void printArray(int arr[])
{
for(int i=0;i<10;i++)
System.out.print("|"+arr[i]);
System.out.println();
}
static void initArray(int arr[])
{
for(int i=0;i<10;i++)
arr[i]=0;
}
static void printSum(int arr[])
{ int Sum=0;
for(int i=0;i<10;i++)
Sum=Sum+arr[i];
System.out.println(Sum);
}
static void enterNum(int arr[])
{ int pos,val;
Scanner s=new Scanner(System.in);
System.out.print("Enter the slot: ");
pos=s.nextInt();
System.out.println();
if(pos>9 || pos<0)
{System.out.println("Incorrect slot");
return;}
System.out.print("Enter the new Value: ");
val=s.nextInt();
System.out.println();
arr[pos]=val;
}
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");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
while(a!=5)
{
switch (a)
{
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("Please enter correct input");
}
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");
a=sc.nextInt();
}
}
public static void main(String[] args) {
initArray(arr);
printMenu();
}
}
The above program stimulates mini database program in Java
Programming language as mentioned in the question. All the methods
are defined using the same name as the question. Output is attached
below. Please ask your queries in the comment
section.Thanks.