Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I: Write a Java class named Pencil.java to represent a pencil type Create a

ID: 3845271 • Letter: P

Question

Part I:

Write a Java class named Pencil.java to represent a pencil type

Create a new Java class named Pencil that has the following fields:

color, brand, price (use appropriate data type)

In addition, the Pencil class should have the following methods. Constructor - The constructor should accept the color, brand, price as arguments These values should be used to initialize the pencil color, brand, and price Getter Methods and setter methods for all instance variables toString method which returns info about the pencil in the following format: PencilColor Brand unitPrice

For example:

Black BIC $0.90

Part II:

Create a class named PencilBox. There are 10 pencils in each PencileBox with two mixed colors (at your choice). NOTE: if a client program tries to place more than or less than 10 pencils, your program should set the quantity of each type of pencils to 5.

The PencilBox has the following fields and methods Pencil type1, Pencil type2, int qty1, int qty2

Constructor(Pencil type1, int qty1, Pencil type 2, int qty2) getter and setter methods

a method: public double grandTotal(), which returns grand total cost (total price of the pencils in a pencilBox)

overwrite the toString method which returns the info about pencils in the box. For example, there are 5 brown and 5 black color pencils in a pencilBox, the unit price for a brown color pencil is $0.90 and $0.80 for black ones. Then the toString method return a String as shown below:

Brown BIC $4.50

Black CIS201 $4.00

Part III:

Write driver (tester) class named SchoolBag.java that uses your Pencil and PencilBox class:

1. create two pencil objects

2. create a PencilBox object that uses the pencil object created in step 1

3. printout th pencilBox as follows Color Brand Subtotal

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

Brown BIC $4.50

Black CIS201 $4.00

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

Grand Total: $8.50

Explanation / Answer

Find the program and output below:

Pencil.java

public class Pencil {

   private String color;
   private String brand;
   private double price;
   public Pencil(String color, String brand, double price) {
       super();
       this.color = color;
       this.brand = brand;
       this.price = price;
   }
  
   public String getColor() {
       return color;
   }
   public void setColor(String color) {
       this.color = color;
   }
   public String getBrand() {
       return brand;
   }
   public void setBrand(String brand) {
       this.brand = brand;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }

   @Override
   public String toString() {
       return color + " " + brand + " $" + price ;
   }
  
}

PencilBox.java

package com.chegg.serial;

public class PencilBox {
   Pencil type1;
   Pencil type2;
   int qty1;
   int qty2;
  
   public PencilBox(Pencil type1, Pencil type2, int qty1, int qty2) {
       super();
       this.type1 = type1;
       this.type2 = type2;
       this.qty1 = qty1;
       this.qty2 = qty2;
   }
   public Pencil getType1() {
       return type1;
   }
   public void setType1(Pencil type1) {
       this.type1 = type1;
   }
   public Pencil getType2() {
       return type2;
   }
   public void setType2(Pencil type2) {
       this.type2 = type2;
   }
   public int getQty1() {
       return qty1;
   }
   public void setQty1(int qty1) {
       this.qty1 = qty1;
   }
   public int getQty2() {
       return qty2;
   }
   public void setQty2(int qty2) {
       this.qty2 = qty2;
   }
  
   public double grandTotal(){
       return qty1*type1.getPrice() + qty2*type2.getPrice();
   }

   @Override
   public String toString() {
       return type1.getColor() + " " + type1.getBrand() + " $" + (type1.getPrice()*qty1) + " " + type2.getColor() + " " + type2.getBrand() + " $" + (type2.getPrice()*qty2) + " $" + grandTotal();
   }
  
}

SchoolBag.java

package com.chegg.serial;

public class SchoolBag {

   public static void main(String[] args) {

       Pencil p1 = new Pencil("Brown", "BIC", 4.50);
       Pencil p2 = new Pencil("Black", "CIS201", 4.00);
      
       PencilBox box = new PencilBox(p1, p2, 1, 1);
       System.out.println(box.toString());
   }

}

OUTPUT:

Brown BIC $4.5
Black CIS201 $4.0
$8.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote