Question

In: Computer Science

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

Page 3 of 4 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 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

Solutions

Expert Solution

Code

import java.util.Scanner;

public class MicroDB {

   public static void main(String[] args) {
       Scanner scnr=new Scanner(System.in);
       int arr[]=new int[10];
       int choice;
       initArray(arr);
       do
       {
           printMenu();
           choice=scnr.nextInt();
           switch (choice) {
           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("Invalid choice.");
               break;
           }
       }while(choice!=5);
   }

   private static void printSum(int[] arr) {
       int sum=0;
       for(int i=0;i<arr.length;i++)
           sum+=arr[i];
       System.out.println(sum);
   }

   private static void printArray(int[] arr) {
       System.out.print(arr[0]);
       for(int i=1;i<arr.length;i++)
           System.out.print("|"+arr[i]);
       System.out.println();
   }

   private static void enterNum(int[] arr) {
       Scanner scnr=new Scanner(System.in);
       int slot;
       System.out.print("Enter the slot: ");
       slot=scnr.nextInt();
       System.out.print("Enter the new value: ");
       arr[slot]=scnr.nextInt();
   }

   private 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");
      
   }

   private static void initArray(int[] arr) {
       for(int i=0;i<arr.length;i++)
           arr[i]=0;
   }

}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. 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...
How can I solve this problem? Can you please show step by step how to solve...
How can I solve this problem? Can you please show step by step how to solve this? Qd = 2,000 − 10P MC = 0.1Q
Please formulate and solve each of the following problems. For each problem, you should include the...
Please formulate and solve each of the following problems. For each problem, you should include the final SOLVER printout (either your final spreadsheet or an answer report), as well as (1) clear and precise definitions for all decision variable; (2) your objective function indicating whether it is to be maximized and minimized; (3) all constraints, including non-negativity and integrality (if necessary); and (4) what the optimal decision is (in words) and what outcome will be produced. A manufacturer of stereos...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
can you please solve this for me. please write a paragraph for each question and explain...
can you please solve this for me. please write a paragraph for each question and explain your reasoning. 1) assume that you are a full-time worker earning $10/hour, $80/day, $400/week, $20000/year. would you be willing to quit your jobs or keep working if the tax rate was 10%? 20%? 30%? 40%? what do you think is the best tax rate? what do you think is the best tax rate? 2)the state of California has decided to increase funding for public...
Can you please solve this problem. The correct answer that should be found is below. Thank...
Can you please solve this problem. The correct answer that should be found is below. Thank You Early in 2015, Logan Corporation engaged Reese, Inc. to design and construct a complete modernization of Logan's manufacturing facility. Construction was begun on January 1, 2015 and was completed on December 31, 2015. Logan made the following payments to Reese, Inc. during 2015: Date Payment June 1, 2015 $2,400,000 August 31, 2015 3,600,000 December 31, 2015 3,000,000 In order to help finance the...
Solve the following boundary value problem by Laplace Transform. (If you solve with another method you...
Solve the following boundary value problem by Laplace Transform. (If you solve with another method you will NOT get credit. There is nothing wrong about the conditions.) d2y dt2 + y = cos(2t); y0(0) = 0; y0( 2 ) = ?1:
Create an original idea of a problem that you want to solve and the method you...
Create an original idea of a problem that you want to solve and the method you will go about to obtain the data to solve this problem. Use the ANOVA testing method to determine if the means are all equal or if at least one is not the same. As a reminder, here is ANOVA: H0:μ1=μ2=…=μmH0:μ1=μ2=…=μm H1:H1: At least one of the means is different from the rest.
Solve problem 2.3 (a, b) using Lagrangian Method. Hint: if you use the Lagrangian method, problem...
Solve problem 2.3 (a, b) using Lagrangian Method. Hint: if you use the Lagrangian method, problem 2.3 will be as follows. If X denotes the number of CDs and Y denotes the number of DVDs: Using the Lagrangian multiplier method, determine the values of X and Y that maximize the value of the function   U=(X^0.5)(Y^0.5)   subject to 200=5X+20Y Thus, problem II asks to solve followings : Max U=(X^0.5)(Y^0.5) s.t.    200= 5X +20Y
explain why dating can be considered a method to solve the adverse selection problem.
explain why dating can be considered a method to solve the adverse selection problem.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT