Question

In: Computer Science

Write a Java program that accepts a sequence of commands and print out the number of...

Write a Java program that accepts a sequence of commands and print out the number of successful insertions, the number of successful deletions, the number of successful searches (through the “find” command), the number of items remaining in the list after executing all commands and the final contents of the list.

Three commands that will be taken as inputs are “insert”, “delete” and “find”.

Input

Line 1: The number of transactions m on the list, where 1  m 200.

Line 2 to m+1: A string command (“insert”, “delete”, “find”) followed by an integer n (separated by a space).

  • “insert” means insert n into the list
  • “delete” means delete n from the list
  • “find” means find n in the list

Output

Line 1: Display 4 integers (each number is separated by a space) which are:

  • the number of successful insertions,
  • the number of successful deletions,
  • the number of successful search , and
  • the number of items remaining in the list

Line 2: The final contents of the list after executing all commands.

Sample Input

Sample Output

3

insert 1

delete 5

find 2

1 0 0 1

[ 1 ]

8

find 10

insert 3

insert 2

insert 1

delete 4

delete 3

insert 1

find 2

4 1 1 3

[ 1 1 2 ]

Solutions

Expert Solution

Code

Output

Code to copy

import java.util.*;

class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
      
        ArrayList<Integer> list = new ArrayList<>();
        int insertion = 0, deletion = 0, search = 0;
        while(m > 0){
            String input = sc.nextLine();
            String[] splited = input.split("\\s+");
            if(splited.length < 2){
                continue;
            }
          
            String command = splited[0];
            int n = Integer.parseInt(splited[1]);
          
            if(command.equals("insert")){
              
                insertion++;
                list.add(n);
          
            } else if(command.equals("delete")){
                int flag = 0;
                for(int i = 0; i < list.size(); i++){
                    if(list.get(i) == n){
                        flag = 1;
                        n = i;
                    }
                }
                if(flag == 1){
                    list.remove(n);
                    deletion++;
                }
              
            } else if(command.equals("find")){
                int flag = 0;
                for(int i = 0; i < list.size(); i++){
                    if(list.get(i) == n){
                        flag = 1;
                    }
                }
                if(flag == 1){
                    search++;
                }
              
            } else {
                System.out.println("Invalid Input");
            }
            m--;
        }
      
        System.out.println(insertion + " " + deletion + " " + search + " " + list.size());
        System.out.println(list);
    }
}

Thank you


Related Solutions

Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
In java coding, write a program that will print out a representation of a “W” using...
In java coding, write a program that will print out a representation of a “W” using a character that a user provides.
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
JAVA Write nested while loop that will print out this pattern, based upon a number entered...
JAVA Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT