Question

In: Computer Science

Java (a) Create a class Router which stores the information of a router. It includes the...

Java

(a) Create a class Router which stores the information of a router. It includes the brand, the model number (String) and the price (double, in dollars). Write a constructor of the class to so that the information mentioned is initialized when a Router object is created. Also write the getter methods for those variables. Finally add a method toString() to return the router information in the following string form.

 "brand: Linksys, model number: RVS4000, price: 1080.0"

Copy the content of the class as the answers to this part.

  1. (b) Create a class ComputerShop which stores the router information in a map routerMap, whose key is the concatenation of the brand and model number, separated by ": " (a colon and a space). Write a method addRouter(Router oneRouter) which adds oneRouter to routerMap. Copy the content of the class, which any include import statement(s) required, as the answers to this part.

  2. (c) Create a class TestComputerShop with a main() method which creates a ComputerShop object aShop and add the first router with brand "Linksys", model number "RVS4000" and price 1080. Add the second router with brand "Planet", model number "VRT-311S" and price 510. Copy the content of the class as the answers to this part.

  3. (d) Write a method showRouter() of ComputerShop which loops through the keys of routerMap using the enhanced for-loop and directly prints each router object stored using System.out.println(). (Loop through the values is simpler but using the keys is required in this part.) This should show suitable information since the method toString() has been written in (a). Add a statement in TestComputerShop to display all the router information of aShop. Copy the content of the method, line(s) added and execution output as the answers to this part.

  4. (e) Write a method modelNumberSet() of ComputerShop which returns model numbers of the routers in a set. You should loop through the values of routerMap using the enhanced for-loop and collect the model numbers. Add a statement in TestComputerShop to display the set using System.out.println(). Copy the content of the method, line(s) added and new execution output as the answers to this part.

  5. (f) Write a method priceList() of ComputerShop which returns the prices of the routers in a list. You should loop through the values of routerMap using the enhanced for-loop and collect the prices of the routers. Add a statement in TestComputerShop to display the list using System.out.println(). Copy the content of the method, line(s) added and new execution output as the answers to this part.

Solutions

Expert Solution

Following is the code required for this answer. Have created total three classes: Router.java, ComputerShop.java, and TestComputerShop.java. Please refer to the screenshots for indentation.

Also here is the output this code produces (answer to part (d)). Refer to this output section after looking at the code for better understanding.

Output (part (d)):

E:\Folder\Java>java TestComputerShop
brand:Planet, model number:VRT-311S, price:510.0
brand:Linksys, model number:RVS4000, price:1080.0

Code:

(a)

/* The router class as asked in the part (a).

* This class stores the information about

* the brand, model number, and price.

*/

public class Router {

    // The fields which store router information

    private String brand;

    private String modelNum;

    private Double price;

    /* The constructor that initializes the new object

     * with values of all three fields. This constructor

     * assigns the values passed while invocation to the

     * newly created object.

     */

    public Router(String brand, String modelNum, Double price) {

        this.brand = brand;

        this.modelNum = modelNum;

        this.price = price;

    }

    // The three getter methods as asked.

    public String getBrand() {

        return brand;

    }

    public String getModelNum() {

        return modelNum;

    }

    public Double getPrice() {

        return price;

    }

    /* The toString methods which is used to convert the

     * router object to be represented in the required

     * string format.

     */

    @Override

    public String toString() {

        return "brand:" + brand + ", model number:" + modelNum + ", price:" + price;

    }

    

}

(b and d)

import java.util.HashMap;

// Class created as an answer to part (b)

public class ComputerShop {

    // The map to store the information of all the routers

    private HashMap<String, Router> routerMap = new HashMap<String, Router>();

    /* The addRouter method which adds the routers to the map.

     * This method extracts the information from the router

     * object provided to build the key as asked.

     */

    public void addRouter(Router oneRouter) {

        String key = "";

        if(oneRouter != null) {

            key = oneRouter.getBrand() + ": " + oneRouter.getModelNum();

            this.routerMap.put(key, oneRouter);

        }

    }

    /* The showRouter method added as answer of part (d).

     * This method prints the information using Router's

     * toString method to the console.

     * Enhanced for loop has been used and iteration has

     * been performed on the key set.

     */

    public void showRouter() {

        for(String entryKey : routerMap.keySet()) {

            Router router = routerMap.get(entryKey);

            System.out.println(router.toString());

        }

    }

}

(c)

public class TestComputerShop {

    // The test class with main method

    public static void main(String[] args) {

        // Computer shop object to access its methods

        ComputerShop aShop = new ComputerShop();

        // Creating two router objects to be added to the map

        Router router1 = new Router("Linksys", "RVS4000", 1080D);

        Router router2 = new Router("Planet", "VRT-311S", 510D);

        // Adding the objects to the map

        aShop.addRouter(router1);

        aShop.addRouter(router2);

        /* Invoking the showRouter method to

         * print the contents of the map to console.

         */

        aShop.showRouter();

    }

}


Related Solutions

(a) Create a class Webcam which stores the information of a webcam. It includes the brand,...
(a) Create a class Webcam which stores the information of a webcam. It includes the brand, the model number (String) and the price (double, in dollars). Write a constructor of the class to so that the information mentioned is initialized when a Webcam object is created. Also write the getter methods for those variables. Finally add a method toString() to return the webcam information in the following string form. "brand: Logitech, model number: B525, price: 450.0" Copy the content of...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
Java Create a class named Billing that includes three overloaded computeBill() methods for a photo book...
Java Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main()....
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter,...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter, FileReader and BufferedReader to write a program that has functional menu: Menu ------------------------------------------------- Add a list of Students and save to File Loading list of Students from a File Display the list of Students descending by Name Display the list of Students descending by Mark Exit Your choice: _ + Save to File: input information of several students and write that information into a...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT