Question

In: Computer Science

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 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...

Solutions

Expert Solution

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!


Related Solutions

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 Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer,...
In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer, It seems an overzealous programmer tried to create a Fibonacci slider puzzle from our old code. This brought up the fact there is a data integrity issue in our SlidingSquarePuzzle class. It makes sense because the class’s data only consists of an int[]. Since, you are new this is a good opportunity to get your feet wet. I want you to change the offending...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and...
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and determines if the vector v3 is the intersection or union of vectors v1 and v2. Example 1: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {3, 5}, then:               intersect_or_union_fcn(v1, v2, v3) will print:                              v3 is the intersection of v1 and v2 Example 2: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5}...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT