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

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;

Step by Step Solution

3.37 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To complete the tasks outlined in the question we need to create several classes and methods as specified Lets tackle each part step by step Part i Create a class Ruler Rulerjava java public class Rul... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

606ad56861902_48469.pdf

180 KBs PDF File

Word file Icon

606ad56861902_48469.docx

120 KBs Word File

Students Have Also Explored These Related Programming Questions!