Question

In: Computer Science

I need this in JAVA Lab9B In each method returned an integer. In this part of...

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

Solutions

Expert Solution



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


Related Solutions

JAVA Please: Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the...
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...
JAVA) I need to get 10 integer numbers from the user. Then I need to find...
JAVA) I need to get 10 integer numbers from the user. Then I need to find sum of odd numbers, sum of even numbers, the lowest number of all numbers, the highest number of all numbers, and the average of all numbers( use double, with the two digit decimal) process; loopCount = 1 While LoopCount <= 10 Read number from the keyboard If odd, add to the total of all odd numbers If even, add to the total of all...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Can you please solve this problem in java lab 9b: MicroDB. In Lab9A, each method returned...
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...
In Java I need a Flowchart and Code. Write the following method that tests whether the...
In Java I need a Flowchart and Code. Write the following method that tests whether the array has four consecutive numbers with the same value: public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series.
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending...
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT