Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Info 1521: Java Programming 1 Assignment 8

Assignment 8
Chapter 9, Exercise 9-1, Pages 321 & 322, Create and Work with Interfaces

Set Up

1. You will provide your personal Heading and Date classes, calling on them as appropriate in
the modification instructions below.

2. Our author offers start code and classes for you; however, the output is limited, as shown in
Exhibit A. Following the author’s instruction; which is what you will be performing initially,
you would end up with the following in Exhibit B.

Exhibit A. Author’s Starter Class Output.

Wheeeee!!

Exhibit B. Author’s Solution Class Output. Where you are headed by following the
Author’s instructions; this is not your final coding solution.

[ Continued ]

Exhibit C. Modified output. When in doubt as to an instruction, code to this Exhibit in these
situations. An Exhibit is the equivalent of written instruction(s) and, therefore, gradable in every
aspect.

Wm Bowers Page 1 of 7
Info 1521: Java Programming 1 Assignment 8

Required Additional Modifications

General Formatting

1. Top margin a minimum and maximum of two blank lines. The call to your personal Heading
class’s getHeading(…) method should perform that for you. If it does not, then correct your
personal getHeading(…) method, not this weekly application. You would be in copyright
violation, otherwise.

2. As shown above, Exhibit C, maintain a minimum and maximum left margin of two (2) blank
spaces. The use of blank lines must match mine, as well; too many are careless in their
production and coding and believe that just throwing something in the output does not matter.

Wm Bowers Page 2 of 7
Info 1521: Java Programming 1 Assignment 8

It does matter. You have a client to consider; just because it’s me makes no difference in
expectations.

3. Your overall alignment (columns, margins, spacing, etc.) of displayed data should resemble
mine. Just more practice for you; I know how much you enjoy the detail work. First Name,
Last Name and Department columns are left justified; the Salary $$$ column is right justified
and aligned on the decimal point, with the thousands separator displayed for any value over
999.99. The dollar sign is not used with any of the individual values.

4. Title Lines. Anything other than instructed\shown is considered a misspelling and a loss of 8-
points. You may call it a display difference, you may not consider it a misspelling; but, these
roses by any other name are still a loss of points for not following specific instructions.

a. Replace “Welcome to the Displayable Test application” with “Displayable Interface


Application” Anything else, to include not capitalizing the first letter in each word is
considered an error. It is not a sentence, so there is no punctuation.

b. A nice professional printer’s touch, note the spacing involved with the categories:
E M P L O Y E E S and P R O D U C T S and S E R V I C E S. All caps with a space
in between each letter.

c. Note the Spelling, capitalization of words, column alignment, decimal alignment and
the use of hyphens instead of underscores for the column headings.

d. At the closing, state “End of Report” ahead of the calls to your Date class’s printfDate()
and printfTime() methods. It is a title line, also: watch capitalization, as shown, and it
does not have a period at the end because it is not meant to be a sentence this time.
Why the changes? Because this is programming, and anything goes when it comes to
customer specifications.

Required Additional Modifications by Class

5. Class DepartmentConstants.

a. Add two (2) more constant data members. Being constants and having the examples
set for you in the author’s solutions, as well as on page 283 of your text, add these:
SERVICES whose position will be assigned the value of 4.
PROGRAMMING whose position will be assigned the value of 5.

Note 1. Interface items may exist, but do not have to be necessarily used.
Note 2. I do require you capitalize these words as shown in the text. It is traditional
programming, and we will follow that path.

b. Do not forget to use the correct opening programmer’s comment citations which, as
you know, must be complete, current and correct.

Wm Bowers Page 3 of 7
Info 1521: Java Programming 1 Assignment 8

6. Class Employee. Add more else if statements that test as follows.

a. If the department is SERVICES, display the word “Services”.

b. If the department is PROGRAMMING, display the word “Programming”.

c. Referring to Exhibit C, we are displaying Employee information horizontally, not


vertically as demonstrated on Page 285. We can utilize the String.format( ) method in a
small variety of ways.

text += " " + String.format("%-12s", firstName)


+ String.format("%-13s", lastName)
+ String.format("%,10.2f", salary);
...
text += String.format(" %-12s", dept);
Remember, we are going to align our columns: text left justified, and numeric values
right justified and on the decimal point.

d. Do not forget to use the correct opening programmer’s comment citations which, as
you know, must be complete, current and correct.

7. Class Product.

a. Part of the Author’s Step 4 is to add a getDisplayText( ) method. One may use the
print( ) method found on Page 285 as a guide; however, that is coded to display the
output vertically. As you review Exhibit C, notice we are taking the horizontal route of
displaying information here, too. Two reasons for horizontal: first, it saves screen
space; second, it gives you much-needed conversion practice and source code which is
not in your text.

@Override
public String getDisplayText()
{ // added Step 4
String text = "";
text += " " + String.format("%-12s", getCode())
+ String.format("%-23s", getDescription())
+ String.format("%,14.2f", price)+ "\n";

// Original Author's solution code on the next three lines


//text += "Code:\t\t" + getCode() + "\n";
//text += "Description:\t" + getDescription() + "\n";
//text += "Price:\t\t" + getFormattedPrice() + "\n";

return text;
}

b. Do not forget to use the correct opening programmer’s comment citations which, as
you know, must be complete, current and correct.

Wm Bowers Page 4 of 7
Info 1521: Java Programming 1 Assignment 8

8 Our own modification addition: create a class named Service. You have three choices: you
can create one from scratch, or you can model it on the Employee class, or you can model it
on the Product class. Look over the prospects of required output in Exhibit C. The
Employee class has four fields to display: first name, last name salary and department. The
Product class has three fields to display: code, title and price each. The Services class has
four fields to display: services, description, per hour and flat rate. I chose to model it on the
Product class to show you how to make more complicated (time consuming) changes. Do
what you wish, but here are my steps.

a. Make a copy of the Product class.


Select (click once on) the Product class, right-mouse, copy option, click once on the
Source Packages folder, right-mouse, paste option, Refactor copy … option, which
will produce a Copy Class dialog box. Change the name in the New Name line from
Product1 to Service, and click on the Refactor button.

b. Using the new Service class.


1) To the global data member list, add a private double flatRate.
2) To the default constructor, add and initialize to 0.0, the flatRate variable.
3) To the three argument constructor, add the double flatRate to the end of the
current argument list to make it a four argument constructor.

c. Add a complete public void setFlatRate(double flatRate) method.

d. Add a complete public double getFlatRate( ) method.

e. You already have a getFormattedPrice( ) method, don’t change it. But do add a
complete public String getFormattedFlatRate( ) method to handle the flatRate
variable.

f. Your getDisplayText( ) method needs to accommodate four elements instead of three,


and here’s a helpful hint. If you need to adjust it to meet Exhibit C’s requirements, do
so.

@Override
public String getDisplayText()
{ // added Step 4
String text = "";
text += " " + String.format("%-12s", getCode())
+ String.format("%-21s", getDescription())
+ String.format("%,8.2f", price)
+ String.format("%,12.2f", flatRate) + "\n";
return text;
}

9. Class Displayable.

a. No changes need to be made from the author’s instructions.

Wm Bowers Page 5 of 7
Info 1521: Java Programming 1 Assignment 8

However …

b. Do not forget to use the correct opening programmer’s comment citations which, as
you know, must be complete, current and correct.

10. Class DisplayableTestApp. Review Exhibit C!

a. At the class level; that is, ahead of main(…), declare and instantiate your personal
Heading class and personal Date class. We’ve done this before, and we will practice
some more. Mine, for example:

BowersHeading bh = new BowersHeading( );


BowersDate bd = new BowersDate( );

As a reminder, from this point forward, I can only use the bh and bd as needed; I
cannot (or should not, traditionally) use the words BowersHeading and BowersDate.
This is applicable to all object oriented programming, regardless of the language.

b. Inside of main(…), open with a call to your personal heading class, passing the String
“Assignment Eight – Working With Interfaces”; no more, and no less than shown in the
quotes. There is a space on each side of the hyphen, and watch capitalization and the
lack of any other punctuation. Yep, the word Eight instead of number 8.

c. As shown, use the title, “Displayable Interface Application”. No welcomes, no


lowercase first letters of each word.

Handling Employees

d. An E M P L O Y E E S title line, as shown, followed by the column headings and


hyphenated lines, as shown.

e. You need to display\call for display, a minimum of five (5) employees; one from each
of the categories. Instantiate them as follows, and in this order. Order in this part is
important because I want to show you that order can be changeable.

Example Displayable e1 = new Employee(1, "Smith", "John", 50000);

Instance Four argument values to pass in the parenthesis


e1 1, “Smith”, “John”, 50000
e2 2, “Smyth”, “Jahn”, 5000.0
e3 3, “Smythe”, “Jon”, 50000.05
e4 4, “YourLastName”, “YourFirstName”, 555.55
e5 5, “APresidentsLastName”, “APresidentsFirstName”, 5.05
Note 1. e4 needs to be your real first name and your real last name.
Note 2. e5, pick your favorite non-living U.S. President; careful though, if you
switch their names out of order, you lose points. Must be non-living!

Wm Bowers Page 6 of 7
Info 1521: Java Programming 1 Assignment 8

Note 3. Observe I entered values and decimal places differently. That is to


demonstrate the final output will be consistently aligned on the decimal place anyway,
and display two decimal places when run. Learning, learning, learning.

f. When you actually display them, do so in this order because someone came along with
more stripes on their arm than you and wanted to change the final output without
changing the original input. Order of display: e5, e1, e2, e3 and e4; with you being
the last line showing. Refer to Exhibit C, which is what I did to put President Lincoln
on top; albeit, he was last on the instantiation\declaration list.

Handling Products

g. The author instantiated a single product, Murach’s Beginning Java. We will instantiate
a second product, of your choosing, as long as it follows the existing format of a 4-
letter or 4-digit product code, a title and a price each.

Handling Services

h. Reviewing Exhibit C, Make your columnar headings as shown in the Exhibit. Add four
services with four descriptions a column for Per Hour charges and a column for Flat
Rate charges.
1) Per Hour charges must decimal-align.
2) Flat rate charges must decimal align and, as shown, accommodate a thousand’s
separator for any value above 999.99. To make sure you are doing so, send such
a value to your Service class via the four argument Service class constructor.

Elvis Wants to Leave the Building

i. At the end of main(…)

1) On its own line, exactly as shown in Exhibit C, state the following: “End of
Report”. Capitalize the first letters as shown, and do not use a period or any
other punctuation.

2) On their own (one, single, solo) line, use your personal Date class instance to call
your printfDate( ) and printfTime( ) methods.

ji. Do not forget to use the correct opening programmer’s comment citations which, as
you know, must be complete, current and correct.

k. If I forgot anything, you fill in the necessaries. If any instruction is confusing, default
to the finished output examples provided in Exhibit C.

Wm Bowers Page 7 of 7

You might also like