Question

In: Computer Science

(JAVA) Create a program that creates a mini database of numbers that allows the user to:...

(JAVA)

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

It should behave like the sample output below: (user input = bold)

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


import java.util.*;
public class Main
{   
    static int arr[]=new int[10];
    static void printArray(int arr[])
    {
    for(int i=0;i<10;i++)
     System.out.print("|"+arr[i]);
     
     System.out.println();
    }

    static void initArray(int arr[])
    { 
     for(int i=0;i<10;i++)
      arr[i]=0;
    
    }
    static void printSum(int arr[])
    {   int Sum=0;
        for(int i=0;i<10;i++)
          Sum=Sum+arr[i];
        System.out.println(Sum);
    }
    static void enterNum(int arr[])
    {   int pos,val;
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the slot: ");
        pos=s.nextInt();
        System.out.println();
        if(pos>9 || pos<0)
        {System.out.println("Incorrect slot");
         return;}
        System.out.print("Enter the new Value: ");
        val=s.nextInt();
        System.out.println();
        arr[pos]=val;
        }
    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");
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        while(a!=5)
        {
            switch (a)
            {
                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("Please enter correct input");
                    
            }
         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");

            a=sc.nextInt();
        }
    }
     
        public static void main(String[] args) {
            initArray(arr);
            printMenu();
                
        }
}

The above program stimulates mini database program in Java Programming language as mentioned in the question. All the methods are defined using the same name as the question. Output is attached below. Please ask your queries in the comment section.Thanks.


Related Solutions

in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a GUI that allows the user to do the following: Create a new Patient Database...
Write a GUI that allows the user to do the following: Create a new Patient Database if it doesn’t exist yet by the click of a button. Create a second button that populates the database with the appropriate Asset table if it does not exist yet, and fill the table with at least 10 patients. Connect to the Patient Database and display all current patients in a List by default. You will have to create a Patient Class. This class...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
Design the logic for a program that allows a user to enter 20 numbers, then displays...
Design the logic for a program that allows a user to enter 20 numbers, then displays them in the reverse order of entry. Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the numeric average of the numbers entered. The program is C++. I need a Pseudocode
Design the logic for a program that allows a user to enter 10 numbers, stores the...
Design the logic for a program that allows a user to enter 10 numbers, stores the numbers in an array, then displays all of the numbers, the largest number, and the smallest.  create a solution algorithm using pseudocode  create a flowchart
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT