Question

In: Computer Science

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...

Convert this java code from hashmap into arraylist.
import java.io.*;
import java.util.*;

public class Solution {

        public static void main(String[] args) throws IOException {

                Scanner sc = new Scanner(System.in);
                HashMap labs = new HashMap();

                while (true) {
                        System.out.println("Choose operation : ");
                        System.out.println("1. Create a Lab");
                        System.out.println("2. Modify a Lab");
                        System.out.println("3. Delete a Lab");
                        System.out.println("4. Assign a pc to a Lab");
                        System.out.println("5. Remove a pc from a Lab");
                        System.out.println("6. Quit");

                        int choice = sc.nextInt();
                        String name=sc.nextLine();
                        switch (choice) {
                        case 1:
                                System.out.println("Enter Lab name (i.e. LAB A or LAB B) : ");
                                name = sc.nextLine();
                                labs.put(name, new Lab(name));
                                break;
                        case 2:
                                System.out.println("Enter name of lab to modify : ");
                                name = sc.nextLine();
                                
                                if (labs.containsKey(name)) {
                                        System.out.println("Enter new name to modify : ");
                                        String name2 = sc.nextLine();
                                        Lab lab = labs.get(name);
                                        labs.remove(name);
                                        labs.put(name2, lab);
                                } else {
                                        System.out.println("Invalid name");
                                }

                                break;
                        case 3:
                                System.out.println("Enter name of lab to delete : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        Lab lab = labs.get(name);
                                        System.out.println("Total pc in this lab are : " + lab.pc_count);
                                        labs.remove(name);
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 4:
                                System.out.println("Enter name of lab to assign a pc : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        if (labs.get(name).pc_count >= 50) {
                                                System.out.println("cannot assign more pc");
                                        } else {
                                                labs.get(name).pc_count++;
                                        }
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 5:
                                System.out.println("Enter name of lab to assign a pc : ");
                                name = sc.nextLine();
                                if (labs.containsKey(name)) {
                                        if (labs.get(name).pc_count <= 0) {
                                                System.out.println("cannot remove pc");
                                        } else {
                                                labs.get(name).pc_count--;
                                        }
                                } else {
                                        System.out.println("Invalid name");
                                }
                                break;
                        case 6:
                                return;
                        default:
                                System.out.println("Invalid choice choose again.");
                                break;
                        }
                }

        }

        static class Lab {
                String name;
                int pc_count;

                Lab(String name) {
                        this.name = name;
                        pc_count = 0;
                }
        }
}

Solutions

Expert Solution

THE CODE IS:


import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Solution {

    // In stead of hashmap we will use two arraylist.
    // One arraylist of type String to act as the Key arraylist
    // Second arraylist to store the Lab to act as the Value arraylist
    static ArrayList<String> key = new ArrayList<>();
    static ArrayList<Lab> value = new ArrayList<>();

    public static void put(String name, Lab lab) {
        // if key is already there in hashmap then replace the previous lab value
        if (key.contains(name)) {
            int index = key.indexOf(name);
            value.set(index, lab);
        }// else if key is not present then add the key-value pair to the hashmap
        else {
            key.add(name);
            value.add(lab);
        }
    }

    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("Choose operation : ");
            System.out.println("1. Create a Lab");
            System.out.println("2. Modify a Lab");
            System.out.println("3. Delete a Lab");
            System.out.println("4. Assign a pc to a Lab");
            System.out.println("5. Remove a pc from a Lab");
            System.out.println("6. Quit");

            int choice = sc.nextInt();
            String name = sc.nextLine();
            switch (choice) {
                case 1:
                    System.out.println("Enter Lab name (i.e. LAB A or LAB B) : ");
                    name = sc.nextLine();
                    put(name, new Lab(name));
                    break;
                case 2:
                    System.out.println("Enter name of lab to modify : ");
                    name = sc.nextLine();

                    if (key.contains(name)) {
                        System.out.println("Enter new name to modify : ");
                        String name2 = sc.nextLine();
                        // getting index of current position of key and updating the name
                        int index = key.indexOf(name);
                        key.set(index, name2);
                    } else {
                        System.out.println("Invalid name");
                    }

                    break;
                case 3:
                    System.out.println("Enter name of lab to delete : ");
                    name = sc.nextLine();
                    if (key.contains(name)) {
                        // getting lab
                        int index = key.indexOf(name);
                        Lab lab = value.get(index);
                        System.out.println("Total pc in this lab are : " + lab.pc_count);
                        // removing the lab from the hashmap
                        key.remove(index);
                        value.remove(index);
                    } else {
                        System.out.println("Invalid name");
                    }
                    break;
                case 4:
                    System.out.println("Enter name of lab to assign a pc : ");
                    name = sc.nextLine();
                    if (key.contains(name)) {
                        // checking if number of pc in the lab exceeds 50
                        if (value.get(key.indexOf(name)).pc_count >= 50) {
                            System.out.println("cannot assign more pc");
                        }// if it doesn't then increment the value of pc in the lab
                        else {
                            value.get(key.indexOf(name)).pc_count++;
                        }
                    } else {
                        System.out.println("Invalid name");
                    }
                    break;
                case 5:
                    System.out.println("Enter name of lab to remove a pc : ");
                    name = sc.nextLine();
                    if (key.contains(name)) {
                        if (value.get(key.indexOf(name)).pc_count <= 0) {
                            System.out.println("cannot remove pc");
                        } else {
                            value.get(key.indexOf(name)).pc_count--;
                        }
                    } else {
                        System.out.println("Invalid name");
                    }
                    break;
                case 6:
                    return;
                default:
                    System.out.println("Invalid choice choose again.");
                    break;
            }
        }

    }

    static class Lab {

        String name;
        int pc_count;

        Lab(String name) {
            this.name = name;
            pc_count = 0;
        }
    }
}


Related Solutions

Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT