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

I need help with Java coding Write a Java class named Pencil.java to represent a

ID: 3845242 • Letter: I

Question

I need help with Java coding

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 to String method which returns info about the pencil in the following format: Pencil Color Brand unit Price For example exist0.90 Black BIC Create a class named PencilBox. There are 10 pencils in each PencileBox with two mixed colors (at your choice). The PencilBox has the following fields and methods Pencil type1, Pencil type2, int gty1, 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 exist0.90 and exist0.80 for black ones. Then the toString method return a String as shown below: Brown BIC exist4.50 Black CIS201 exist4.00 Write driver (tester) class named School Bag.java that uses your Pencil and PencilBox class: create two pencil objects create a PencilBox object that uses the pencil object created in step 1 print out th pencilBox as follows

Explanation / Answer

class Pencil
{
private String color;
private String brand;
private double price;
  
public Pencil(String color,String brand,double price)//argument constructor
{
this.color = color;
this.brand = brand;
this.price = price;
}
  
//get and set methods
  
public void setColor(String color)
{
this.color = color;
}
public String getColor()
{
return color;
}
public void setBrand(String brand)
{
this.brand = brand;
}
public String getBrand()
{
return brand;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String toString()
{
return " "+color +" "+brand +" $"+price;
}
}

class PencilBox
{
  
private Pencil type1;
private Pencil type2;
private int qty1;
private int qty2;
  
//argument constructor
public PencilBox(Pencil type1,Pencil type2,int qty1,int qty2)
{
this.type1 = type1;
this.type2 = type2;

if((qty1 + qty2) > 10 || (qty1 +qty2) < 10)//validation for total number of pencils
{
this.qty1 = 5;
this.qty2 = 5;
}
else
{
this.qty1 = qty1;
this.qty2 = qty2;
}
  
}
//set and get methods
public void setType1(Pencil type1)
{
this.type1 = type1;
}
public Pencil getType1()
{
return type1;
}
public void setType2(Pencil type2)
{
this.type2 = type2;
}
public Pencil getType2()
{
return type2;
}
public void setQty1(int qty1)
{
this.qty1 = qty1;
}
public int getQty1()
{
return qty1;
}
public void setQty2(int qty2)
{
this.qty2 = qty2;
}
public int getQty2()
{
return qty2;
}
public double grandTotal()// compute total price of both types
{
return (type1.getPrice() *qty1 + type2.getPrice()*qty2) ;
}
public String toString()
{
return " "+ type1.getColor() + " "+ type1.getBrand() +" "+ type1.getPrice()*qty1 + " "+ type2.getColor() + " "+ type2.getBrand() +" "+ type2.getPrice()*qty2;
}
}
class SchoolBag
{
   public static void main (String[] args)
   {
       Pencil p1 = new Pencil("Brown","BIC",0.9);
       Pencil p2 = new Pencil("Black","CIS201",0.8);
      
       PencilBox PB = new PencilBox(p1,p2,6,8);
      
       System.out.println(PB);
       System.out.println(" Grand Total : $"+PB.grandTotal());
   }
}

Output:

Brown   BIC   4.5
Black   CIS201   4.0

Grand Total : $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