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

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...
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...
// 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...
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
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 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...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a...
Both in Java Question 1: Write a method, bunnyEars that takes in a integer for a number of bunnies and return another integer for the total number of ears that the group of bunnies has. (Assume ear bunny has exactly 2 ears).. Write a method countX, that when given a string counts the number of lowercase 'x' chars in the string. countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 Write a method copies that, when given a string and...
in java please: Given any integer, print an English phrase that describes the integer (e.g. “One...
in java please: Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList must be used in your program.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT