Question

In: Computer Science

Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...

Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it.

  1. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements.
    • Add the four fields to the Headphone class. Create a constructor that takes two parameters. The first parameter is a String named manufacturer that is used to initialize the headphone’s manufacturer (i.e. Bose, Sennheiser). The second and final parameter is a String named name that is used to initialize the headphone’s name (i.e. SoundLink, RS 195). The field timesRestocked should be initialized to zero (0). The field quantity should be initialized to a random number between 3 and 12 (inclusive). All fields must be declared private.
    • Write a method named purchase that is used to decrement the quantity by one (1).
    • Write a method named restock. This method is used to restock this particular headphone. This is done by incrementing quantity by three (3) and incrementing timesRestocked by one (1).
    • Write an accessor method to return the value of the field quantity.
    • Write a method named headphoneEquals that takes two (2) parameters and returns a boolean value. The first parameter is a String named manufacturer. The second parameter is a String named name. If the manufacturer and the name passed in to the method are the same as the manufacturer and name of this object, then return true. Otherwise they are not equal, so return false. Note: the case of the letters in the Strings should not make a difference.
    • Write a method named headphoneToString that returns the details of this headphone as a String. This String will include all four fields and needs to be formatted like Example 1 shown below. Hint: \n is the newline character.
  2. The HeadphoneInventory class represents a store headphone inventory. It stores three Headphone objects. You will write a constructor, a sellHeadphone method and a summary method for the class.
    • Add the object three fields that are used by the HeadphoneInventory class. Create a constructor that initializes the three fields with the values: field one (“Bose”, “SoundLink”), field two (“Sennheiser”, “RS 195”), and field three (“Koss”, “BT540i”).
    • Write a method named sellHeadphone. This method takes two parameters. The first parameter is a String that represents the manufacturer of the headphone. The second parameter is a String that represents the name of the headphone.
      • If the manufacturer and name of the headphone is in your inventory (hint - use the headphoneEquals method of the headphone in the if statement condition), use the purchase method of the headphone to sell a headphone. Use the accessor method of the headphone to retrieve the quantity of the headphone and store the value in a local variable called headphoneQuantity.
        • If the number of headphones is less than or equal to three (3), print the following: “You have headphoneQuantity manufacuturer name headphones left in stock. You are running low. Maybe it is time to restock?” Then, use the restock method of the headphone to restock the headphones.
        • Otherwise print the following message: “You have sold a manufacturer name
      • If the headphone is not in your inventory, the sellHeadphone method should print the following message: “Sorry, we do not seem to have the manufacturer name headphones in stock.
    • Write a method called summary that prints the current details of your headphone inventory. This output should be formatted as Example 2, below.

Example 1 (output format for the headphoneToString method of the Headphone class.)

Bose SoundLink

       - You have 6 headphones left.

- This headphone has been restocked 2 times.

Example 2 (output format for the summary method of the HeadphoneInventory class.)

Headphone Summary

-------------------------------------------------------------

Bose SoundLink

- You have 6 headphones left.

- This headphone has been restocked 2 times.

======================================

Sennheiser RS 195

- You have 8 headphones left.

- This headphone has been restocked 0 times.

======================================

Koss BT540i

- You have 5 headphones left.

- This headphone has been restocked 1 times.

======================================

Solutions

Expert Solution

Headphone.java:

package Exam1;

import java.util.Random;

public class Headphone {
    // members
    private String manufacturer;
    private String name;
    private int quantity;
    private int timesRestocked;

    // constructor
    public Headphone(String manufacturer, String name) {
        this.manufacturer = manufacturer;
        this.name = name;
        // generate a number between 0 and 9 then add 3 to make the number
        // between 3 and  12
        quantity = new Random().nextInt(10) + 3;
        timesRestocked = 0;
    }

    // purchase method
    public void purchase() {
            --quantity;
    }

    // restock method
    public void restock() {
        quantity += 3;
        timesRestocked++;
    }

    // accessor for quantity
    public int getQuantity() {
        return quantity;
    }

    // headphoneEquals method
    public boolean headphoneEquals(String manufacturer, String name) {
        if (this.manufacturer.equalsIgnoreCase(manufacturer) && this.name.equalsIgnoreCase(name))
            return true;
        else return false;
    }
    
    // display headphone details
    public String headphoneToString() {
        String temp = "";
        temp += manufacturer + " " + name + "\n";
        temp += "- You have " + quantity + " headphones left.\n";
        temp += "- This heaphone has been restocked " + timesRestocked + " times.\n";
        return temp;
    }
}

HeadphoneInventory.java:

package Exam1;

public class HeadphoneInventory {
    private Headphone[] headphones = new Headphone[3];

    public HeadphoneInventory() {
        headphones[0] = new Headphone("Bose","SoundLink");
        headphones[1] = new Headphone("Sennheiser","RS 195");
        headphones[2] = new Headphone("Koss","BT540i");
    }

    public void sellHeadphone(String manufacturer, String name) {
        int headphoneQuantity;
        boolean flag = false;
        for (int i = 0; i < 3; i++) {
            if (headphones[i].headphoneEquals(manufacturer,name)) {
                flag = true;
                headphones[i].purchase();
                headphoneQuantity = headphones[i].getQuantity();
                if (headphoneQuantity <= 3) {
                    System.out.println("You have " + headphoneQuantity + " " + manufacturer + " " + name  + " headphones left in stock. You are running low. Maybe it is time to restock?" );
                    headphones[i].restock();
                }
                else
                    System.out.println("You have sold a " + manufacturer + " " + name);

            }
        }
        if(!flag)
            System.out.println("Sorry, we do not seem to have the " + manufacturer + " " + name + " headphones in stock.");
    }

    public void summary() {
        System.out.println("\n\nHeadphone Summary");
        System.out.println("------------------------------------------------------------");
        for (Headphone h: headphones) {
            System.out.println(h.headphoneToString());
            System.out.println("=========================================================");
        }
    }

    public static void main(String[] args) {
        HeadphoneInventory hi = new HeadphoneInventory();
        hi.sellHeadphone("bose","soundlink");
        hi.sellHeadphone("kia","alto");
        hi.sellHeadphone("bose","soundlink");
        hi.sellHeadphone("bose","soundlink");
        hi.sellHeadphone("Koss","BT540i");
        hi.sellHeadphone("Koss","BT540i");
        hi.summary();
    }
}

OUTPUT:


Related Solutions

In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as...
In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in the LineSegment class. There is information...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: 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...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT