The following is about creating a class Ruler and testing it. (i) Create a class Ruler with
Question:
The following is about creating a class Ruler and testing it.
(i) Create a class Ruler 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 Ruler(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 to this part.
(ii) Create another class TestRuler in a separate file with a method main() to test the class Ruler. In main(), create a Ruler object aRuler with brand "Brand A" and length 30. Print the message "A Ruler 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 to this part.
(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 to this part.
(iv) Write a method displayRuler(int index) of the class StationeryShop to produce output similar to the following:
Ruler number 2 brand : Brand A length : 30
where Brand A is the brand and 30 is the length of ruler, and index has the value 2. The parameter index is the index of the array ruler. Copy the content of the method as the answers to this part.
(v) Add a method brandCount(String aBrand) to the class StationeryShop which returns the number of rulers with brand aBrand. Copy the content of the method as the answers to this part.
(vi) Add another method longRulers() to the class StationeryShop which returns an array with 2 elements containing Ruler objects with longest and second longest lengths, which are assumed to be unique. Copy the content of the method as the answers to this part.
(vii) Create another class TestStationeryShop in a separate file with a method main() to perform the following:
create a StationeryShop object myStationeryShop and initialize it with data for 3 rulers: ("Brand A", 30), ("Brand B", 20), ("Brand C", 60);
display the ruler with index 2 using displayRuler();
print the number of rulers with brand "Brand A";
print the brand and length of the rulers with top 2 lengths;
Managerial Economics and Organizational Architecture
ISBN: 978-0073375823
5th edition
Authors: James Brickley, Jerold Zimmerman, Clifford W. Smith Jr