Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

Rulerjava public class Ruler member variables private String brand private int length constructor public RulerString brand int length thisbrand brand ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Document Format ( 2 attachments)

PDF file Icon
606ad56861902_48469.pdf

180 KBs PDF File

Word file Icon
606ad56861902_48469.docx

120 KBs Word File

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managerial Economics and Organizational Architecture

Authors: James Brickley, Jerold Zimmerman, Clifford W. Smith Jr

5th edition

73375829, 978-0073375823

More Books

Students also viewed these Programming questions

Question

How many bytes are used to store a character in PHP?

Answered: 1 week ago

Question

16. How is the variance of the total project computed in PERT?

Answered: 1 week ago

Question

What costs do incentive conflicts in corporations generate?

Answered: 1 week ago