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)  ...
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++)...
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...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class HashTable implements IHash { // Method of hashing private HashFunction hasher; // Hash table : an ArrayList of "buckets", which store the actual strings private ArrayList<List<String>> hashTable; /** * Number of Elements */ private int numberOfElements; private int numberOfBuckets; /** * Initializes a new instance of HashTable. * <p> * Initialize the instance variables. <br> * Note: when initializing the hashTable, make sure to...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the constructor for the class. Instantiate the testArrayList *and call the fillArrayList method to add objects to the testArrayList. * @param numElements The number of elements in the ArrayList */ public TestArrayLists(int numElements) { } /** * Complete the fillArrayList method. It should fill the testArrayList with * TestArrays objects consisting of 10 element int Array of random numbers. * @param numElements The number of...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT