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

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

ID: 3902795 • Letter: P

Question

Part I: Create a new Java class named Pencil that has the following fields:
color (String), brand(String), price (double)

A pencil can have one of the following colors: Orange, Yellow, Brown, or Blue (all other colors are considered as invalid and default to Brown); The length of a band name is within the range of [3, 20]. The price of a pencil is limited to [$0.01, $200] (for all other prices, set the price to $0.50). The following rules are applied when a client program tries to set brand name: Because the length of a brand name is limited to [3,20], if the length of a brand name is less than 3, you should repeat the brand name until it meets the length requirement. For example, if the brand name is "A", you are going to repeat it three times so it becomes "AAA", which meets the length requirement. If the length exceeds 20, your program should take the first 20 characters as its brand name.
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 Pencil color occupies 10 spaces(left aligned), brand name 20 spaces(right aligned), and unit price 10 spaces (hint: use String.format() method)
For example: Black BIC $0.90
Part II:
Create a class named PencilBox. There are exactly 10 pencils in each PencileBox with two mixed colors (at your choice). The PencilBox has the following method: Constructor(Pencil type1, int qty1, Pencil type 2, int qty2)
a method called totalCost which returns the total cost (total price of the pencils in a pencilBox)
toString method which returns the info about pencils in the box.

For example, there are 5 brown and 5 yellow color pencils in a pencilBox, the unit price for a brown color pencil is $0.90 and $0.80 for yellow ones. Then the toString method return a String as shown below: Brown BIC $4.50 Black CIS201 $4.00
Each pencil box stores exactly 10 pencils. For all invalid quantities, your program should set 5 to each color, so altogether, there are 10 pencils in the pencil box. No getter or setter methods are necessary for this PencilBox class.
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 Part I: Create a new Java class named Pencil that has the following fields:
color (String), brand(String), price (double)

A pencil can have one of the following colors: Orange, Yellow, Brown, or Blue (all other colors are considered as invalid and default to Brown); The length of a band name is within the range of [3, 20]. The price of a pencil is limited to [$0.01, $200] (for all other prices, set the price to $0.50). The following rules are applied when a client program tries to set brand name: Because the length of a brand name is limited to [3,20], if the length of a brand name is less than 3, you should repeat the brand name until it meets the length requirement. For example, if the brand name is "A", you are going to repeat it three times so it becomes "AAA", which meets the length requirement. If the length exceeds 20, your program should take the first 20 characters as its brand name.
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 Pencil color occupies 10 spaces(left aligned), brand name 20 spaces(right aligned), and unit price 10 spaces (hint: use String.format() method)
For example: Black BIC $0.90
Part II:
Create a class named PencilBox. There are exactly 10 pencils in each PencileBox with two mixed colors (at your choice). The PencilBox has the following method: Constructor(Pencil type1, int qty1, Pencil type 2, int qty2)
a method called totalCost which returns the total cost (total price of the pencils in a pencilBox)
toString method which returns the info about pencils in the box.

For example, there are 5 brown and 5 yellow color pencils in a pencilBox, the unit price for a brown color pencil is $0.90 and $0.80 for yellow ones. Then the toString method return a String as shown below: Brown BIC $4.50 Black CIS201 $4.00
Each pencil box stores exactly 10 pencils. For all invalid quantities, your program should set 5 to each color, so altogether, there are 10 pencils in the pencil box. No getter or setter methods are necessary for this PencilBox class.
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 Part I: Create a new Java class named Pencil that has the following fields:
color (String), brand(String), price (double)

A pencil can have one of the following colors: Orange, Yellow, Brown, or Blue (all other colors are considered as invalid and default to Brown); The length of a band name is within the range of [3, 20]. The price of a pencil is limited to [$0.01, $200] (for all other prices, set the price to $0.50). The following rules are applied when a client program tries to set brand name: Because the length of a brand name is limited to [3,20], if the length of a brand name is less than 3, you should repeat the brand name until it meets the length requirement. For example, if the brand name is "A", you are going to repeat it three times so it becomes "AAA", which meets the length requirement. If the length exceeds 20, your program should take the first 20 characters as its brand name.
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 Pencil color occupies 10 spaces(left aligned), brand name 20 spaces(right aligned), and unit price 10 spaces (hint: use String.format() method)
For example: Black BIC $0.90
Part II:
Create a class named PencilBox. There are exactly 10 pencils in each PencileBox with two mixed colors (at your choice). The PencilBox has the following method: Constructor(Pencil type1, int qty1, Pencil type 2, int qty2)
a method called totalCost which returns the total cost (total price of the pencils in a pencilBox)
toString method which returns the info about pencils in the box.

For example, there are 5 brown and 5 yellow color pencils in a pencilBox, the unit price for a brown color pencil is $0.90 and $0.80 for yellow ones. Then the toString method return a String as shown below: Brown BIC $4.50 Black CIS201 $4.00
Each pencil box stores exactly 10 pencils. For all invalid quantities, your program should set 5 to each color, so altogether, there are 10 pencils in the pencil box. No getter or setter methods are necessary for this PencilBox class.
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


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Pencil.java
====
public class Pencil {
private String color;
private String brand;
private double price;


public Pencil(String color1, String brand1, double price1)
{
setColor(color1);
setBrand(brand1);
setPrice(price1);
}


public String getColor() {
return color;
}


public void setColor(String color1) {
this.color = color1;
if(!(color.equals("Orange") || color.equals("Yellow")
|| color.equals("Brown") || color.equals("Blue")))
{
color = "Brown";
}
}


public String getBrand() {
return brand;
}


public void setBrand(String brand1) {
this.brand = brand1;
if(brand.length() < 3)
{
while(brand.length() < 3)
brand = brand + brand1;
}
else if(brand.length() > 20)
brand = brand.substring(0, 20);
}


public double getPrice() {
return price;
}


public void setPrice(double price1) {
this.price = price1;

if(price < 0.01 || price > 200)
price = 0.50;
}

public String toString()
{
String p = String.format("$%.2f", price);
return String.format("%-10s %20s %10s", color, brand, p);
}


}


PencilBox.java
========

public class PencilBox {
private Pencil type1, type2;
private int qty1, qty2;

public PencilBox(Pencil t1, int q1, Pencil t2, int q2)
{
type1 = t1;
type2 = t2;
qty1 = q1;
qty2 = q2;

int total = qty1 + qty2;
if(total < 10 || total > 10)
{
qty1 = 5;
qty2 = 5;
}
}

public double totalCost()
{
return qty1 * type1.getPrice() + qty2 * type2.getPrice();
}

public String toString()
{
String s = "";
String p = String.format("$%.2f", type1.getPrice() * qty1);
s = String.format("%-10s %20s %10s", type1.getColor(), type1.getBrand(), p);

p = String.format("$%.2f", type2.getPrice() * qty2);
s = s + " "+ String.format("%-10s %20s %10s", type2.getColor(), type2.getBrand(), p);
return s;
}

}


SchoolBag.java
=======

public class SchoolBag {
public static void main(String[] args) {
Pencil p1 = new Pencil("Brown", "BIC", 0.90);
Pencil p2 = new Pencil("Blue", "CIS201", 0.80);

PencilBox box = new PencilBox(p1, 5, p2, 5);

System.out.printf("%-10s %20s %10s ", "Color", "Brand", "Subtotal");
System.out.printf("%-10s %20s %10s ", "-----", "-----", "--------");
System.out.println(box.toString());
System.out.println("---------------------------------------------");
System.out.printf("Grand Total: $%.2f ",box.totalCost());

}
}


output
=====
Color Brand Subtotal
----- ----- --------
Brown BIC $4.50
Blue CIS201 $4.00
---------------------------------------------
Grand Total: $8.50

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