Assignment 2
Be sure to type up your answers so you can submit them electronically on the
BlackBoard Connect system. You may complete this assignment individually.
Make sure all your files have your full name and student number on it.
Type up your answers using Eclipse.
The file format of the Java program you are to submit must be .java.
This assignment is marked out of 33 points.
Due Date
February 15, 2014, 11:59AM (just before noon).
24 Hour Silence Policy
Due to the large class size and the potential volume of questions, we are
implementing a 24-hour silence policy. This means that any questions posted on
the discussion forum in the 24 hours immediately preceding the assignment
deadline may not get answered in time. This does not mean that we will
stop answering questions the moment the day before the deadline arrives, it just means
that we cannot promise all the questions will be answered in time, due to our
prior engagements, such as other classes and meetings we have.
For this reason, please be sure to attempt the assignment early, and try to
complete it as if it were due two days prior to the real deadline.
In any case, we will try to answer all the questions that show up on the
discussion forum. Note that questions sent to emails will not be
answered, as we want all course related material to be viewed for everyone's
benefit.
What You Are Given
There are 2 questions in this assignment. The first question is to practice
polymorphism, and the second is on files.
Question 1 [20 points]
You are going to write a program that calculates the price of clothing with
the appropriate provincial tax. The store will manage clothing from British
Columbia, which has a PST of 7%, and Manitoba, which as a PST of 8%.
Moreover, we will have two types of clothing: children and adult. Note that
children clothing does not get taxed in any province.
In this question, complete the following:
See sample output from the test class.
Save your output into a file called Clothing-Output.txt.
Question 2 [13 points]
Write a simple HTML parser called TagParser that takes an input HTML
file, extracts all the words between the an opening and closing pair of "tt"
tags, and output those words (without the tags) into a plain text file.
An HTML file is a type of file used for websites. Unlike word, there is no
function like boldface or italicize. To specify when a word should be
displayed in a different way, tags are used. For example, to make a phrase
bold, you would type "< b >a bold phrase< / b >" where "b" stands for bold and the
angle brackets denote the tag. (There are no spaces inside the tags when you
use them, they are just here for clarity.) In order for a computer to
understand the difference between when it should start to bold and when it
should stop, an opening tag and a closing tag are used. The closing tag just
has an extra "/" in it. In this question, we will work with the HTML tag for
"teletype text" (tt for short).
In this question, complete the following:
- 3 points:
In the TagParser class, define a constructor that reads from a
file. You should download the file at:
https://people.ok.ubc.ca/bowenhui/121/a2.html
and store it in your Eclipse project folder.
Your program will read this file as input and extract information from it.
In addition, your constructor will create a new file called
a2-tagsOnly.txt which will store all the output later.
Finally, your class will have a String array which will also be
initialized in your constructor. You should initialize all your array
elements to the following default string: "-1".
- 4 points:
Add a method called parseTT() that detects when a line has an
opening tt tag. Recall various String methods you can use here.
When you have such a line, find the index position where the opening tag
ends and the index position where the closing tag starts so you can extract
the words in between. Once you extract the words in between, store that
substring into your String array. When you are done, your array
will have 33 extracted phrases.
- 4 points:
Add a method called storeTT() that goes through each element in
your String array via a loop and outputs it onto a separate line in
the file you created earlier. Don't put any of the default values "-1" into
the file. Be sure to close your file when you are done, otherwise it will
seem like you have not written anything to the file.
- 2 points:
Create a test class that gives the a2.html file to a TagParser
object. Call the two methods you've created to test your program. Be sure to
check your a2-tagsOnly.txt file to see if it has the information you
expect.
Note: Be sure to handle your file I/O exceptions in one way or another.
If you choose to let it propagate, you must still make sure that one of the
caller methods handle it properly, without expecting someone else to handle it
for you, and without letting it crash unexpectedly.
See sample file from the test class.
There is no output from this class.
What to Submit
Be sure to comment all the methods and attributes for clarity.
For this assignment, submit the following:
- Clothing.java
- Children.java
- Adult.java
- BCAdult.java
- MBAdult.java
- Purchases.java
- Clothing-Output.txt
- TagParser.java
- TestParser.java (or test class file you used)
- a2-tagsOnly.txt
- Include your full name and student ID.
- List the full names of the people who helped you in this assignment
(include students and TAs if applicable), and mention what they helped you with
-- failure to do so honestly will result in a lower mark and may be reported
for academic discipline