In: Computer Science
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.
(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.
(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.
(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.
(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.
(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.
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();
}
}