In: Computer Science
I need this in JAVA
Lab9B In 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)
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
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
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
static int arr[]=new int[10];
static void initArray ()
{
for(int i=0;i<10;i++)
{
arr[i]=0;
}
}
static void printArray ()
{
for(int i=0;i<10;i++)
{
System.out.println(arr[i]);
}
}
static void printSum ()
{
int sum=0;
for(int i=0;i<10;i++)
{
sum=sum+arr[i];
}
System.out.println(sum);
}
static void enterNum()
{
Scanner sc=new Scanner(System.in);
System.out.println("please enter slot and value");
int s=sc.nextInt();
int v=sc.nextInt();
arr[s]=v;
}
static void printMenu ()
{
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");
}
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
initArray();
while(true)
{
printMenu();
int inp=sc.nextInt();
switch(inp)
{
case 1:enterNum();
break;
case 2:printArray();
break;
case 3:printSum();
break;
case 4:initArray();
break;
case 5:System.exit(0);
default:System.out.println("wrong attempt, try again");
}
}
}
}
Hello, please check the program. Hope i helps you.
Thank you