In: Computer Science
(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 the class as the answers to this part.
(b) Create a class ComputerShop which stores the webcam information in a map webcamMap, whose key is the concatenation of the brand and model number, separated by ": " (a colon and a space). The value is a Webcam object. Write a method addWebcam(Webcam oneWebcam) which adds oneWebcam to webcamMap. 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 webcam with brand "Logitech", model number "B525" and price 450.0. Add the second webcam with brand "Microsoft", model number "HD-3000" and price 235.0. Copy the content of the class as the answers to this part.
(d) Write a method showWebcam() of ComputerShop which loops through the keys of webcamMap using the enhanced for-loop and directly prints each webcam 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 webcam 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 webcams in a set. You should loop through the values of webcamMap 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 webcams in a list. You should loop through the values of webcamMap using the enhanced for-loop and collect the prices of the webcams. 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.
public class Webcam {
private String brand;
private String modelNumber;
private double price;
public Webcam() {
this.brand = "";
this.modelNumber = "";
this.price = 0;
}
public Webcam(String brand, String modelNumber,
double price) {
this.brand = brand;
this.modelNumber =
modelNumber;
this.price = price;
}
public String getBrand() {
return brand;
}
public String getModelNumber() {
return modelNumber;
}
public double getPrice() {
return price;
}
public String toString() {
return "brand:" + this.brand + ",
model number:" + this.modelNumber + ", price:" + this.price;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ComputerShop {
private HashMap<String, Webcam> webcamMap=new
HashMap<>();
public void addWebcam(Webcam oneWebcam) {
this.webcamMap.put(oneWebcam.getBrand() + ": " +
oneWebcam.getModelNumber(), oneWebcam);
}
public void showWebcam(){
for(String
key:webcamMap.keySet()){
System.out.println(webcamMap.get(key));
}
}
public Set<String> modelNumberSet(){
Set<String> models=new
HashSet<>();
for(Webcam
obj:webcamMap.values()){
models.add(obj.getModelNumber());
}
return models;
}
public List<Double> priceList(){
List<Double> prices=new
ArrayList<Double>();
for(Webcam
obj:webcamMap.values()){
prices.add(obj.getPrice());
}
return prices;
}
}
public class TestComputerShop {
public static void main(String[] args) {
ComputerShop shop=new
ComputerShop();
shop.addWebcam(new
Webcam("Logitech","B525", 450.0));
shop.addWebcam(new
Webcam("Microsoft","HD-3000",235.0));
shop.showWebcam();
System.out.println(shop.modelNumberSet());
System.out.println(shop.priceList());
}
}
Expected output: