In: Computer Science
Please use java to answer the below question.
(i) Create a class Pencil with the attributes brand (which is a string) and length (an integer) which are used to store the brand and length of the ruler respectively. Write a constructor Pencil (String brand, int length) which assigns the brand and length appropriately. Write also the getter/setter methods of the two attributes. Save the content under an appropriate file name. Copy the content of the file as the answers.
(ii) Create another class TestPencil in a separate file with a method main() to test the class Pencil. In main(), create a Pencil object aPencil with brand "Brand A" and length 30. Print the message "A Pencil object has been created: brand is Brand A, length is 30", in which the data "Brand A" and "30" are obtained from the corresponding getter method of the attribute. Run the program. Copy the content of the file and the output showing the message as the answers.
(iii) Create the class StationeryShop which contains an attribute ruler which is a Ruler array. In the constructor StationeryShop(Ruler[] ruler), initialize the attribute ruler using the parameter. Also write the getter/setter method of the attribute ruler. Copy the content of the file as the answers.
(iv) Write a method displayPencil(int index) of the class StationeryShop to produce output similar to the following:
Pencil number 2 brand :
Brand A
length : 30
where Brand A is the brand and 30 is the length of pencil, and index has the value 2. The parameter index is the index of the array pencil. Copy the content of the method as the answers.
(v) Add a method brandCount(String aBrand) to the class StationeryShop which returns the number of pencils with brand aBrand. Copy the content of the method as the answers.
(vi) Add another method longRulers() to the class StationeryShop which returns an array with 2 elements containing Pencil objects with longest and second longest lengths, which are assumed to be unique. Copy the content of the method as the answers.
(vii) Create another class TestStationeryShop in a separate file with a method main() to perform the following: 1. create a StationeryShop object myStationeryShop and initialize it with data for 3 rulers: ("Brand A", 30), ("Brand B", 20), ("Brand C", 60); 2. display the ruler with index 2 using displayRuler(); 3. print the number of rulers with brand "Brand A"; 4. print the brand and length of the rulers with top 2 lengths; Run the program. Copy the content of the class and the output as the answers.
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You ! Note: The question is having some mistakes, instead of Ruler it should be Pencil. =========================================================================== public class Pencil { private String brand; private int length; public Pencil(String brand, int length) { this.brand = brand; this.length = length; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } }
===============================================================
public class TestPencil { public static void main(String[] args) { Pencil aPencil = new Pencil("Brand A",30); System.out.println("A Pencil object has been created "+aPencil.getBrand() +" and length "+aPencil.getLength()); } }
===============================================================
public class StationeryShop { private Pencil pencils[]; public StationeryShop(Pencil[] pencils) { this.pencils = pencils; } public Pencil[] getPencils() { return pencils; } public void setPencils(Pencil[] pencils) { this.pencils = pencils; } public void displayPencil(int index) { if (0 <= index && index < pencils.length) { System.out.println("Pencil number " + index + " brand: "); System.out.println("Brand " + pencils[index].getBrand()); System.out.println("Length: " + pencils[index].getLength()); } } public int brandCount(String aBrand) { int count = 0; for (int i = 0; i < pencils.length; i++) { if (pencils[i] != null && pencils[i].getBrand().equals(aBrand)) { count += 1; } } return count; } public Pencil[] longPencils() { Pencil longestPencil = pencils[0]; for (int i = 0; i < pencils.length; i++) { if (pencils[i] != null && longestPencil.getLength() < pencils[i].getLength()) { longestPencil = pencils[i]; } } Pencil secondLongest = pencils[0]; for (int i = 0; i < pencils.length; i++) { if (pencils[i] != null && longestPencil.getLength() < pencils[i].getLength() && !secondLongest.getBrand().equals(longestPencil.getBrand())) { secondLongest = pencils[i]; } } return new Pencil[]{longestPencil, secondLongest}; } }
===============================================================
public class TestStationeryShop { public static void main(String[] args) { StationeryShop myStationeryShop = new StationeryShop( new Pencil[]{ new Pencil("Brand A",30), new Pencil("Brand B",20), new Pencil("Brand C",60) } ); myStationeryShop.displayPencil(2); System.out.println("Number of pencils with Brand A: "+myStationeryShop.brandCount("Brand A")); Pencil[] longest = myStationeryShop.longPencils(); System.out.println("First Longest Pencil: "+longest[0].getBrand()+", Length: "+longest[0].getLength()); System.out.println("Second Longest Pencil: "+longest[1].getBrand()+", Length: "+longest[1].getLength()); } }
===============================================================