Question

In: Computer Science

(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 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.

Solutions

Expert Solution

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:


Related Solutions

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...
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...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
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...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
Create a C structure which stores information about a student. Each student should be represented by...
Create a C structure which stores information about a student. Each student should be represented by a student ID (integer), first name, last name, day, month and year of birth, and program code (string). Write a C program which creates an array of 100 of these structures, then prompts the user to enter data from the keyboard to fill the array. If an ID of 0 is entered, data entry should finish, and the list of students should be printed...
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
Create a C structure which stores information about a book. Each book should have the following...
Create a C structure which stores information about a book. Each book should have the following data: Author (string), Title (string), Publisher (string), Year (int), ISBN (int), Genre (string), Price (float). Write a C program which creates an array of 100 of these structures, then prompts the user to enter book information to fill the array. The data entry should stop when the array is full, or when the user enters 0 for the ISBN.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT