Lab 5

advertisement
Lab 5:
Due April 22 (100 pt lab)
You can choose to work with a partner or you can choose to work alone. If you choose to work with a
partner, be aware that this is the partner you will be working on the first project with. Make sure
both your name and your partner’s name are on the lab when you submit it, but only one person
needs to submit.
Problem 1: Creating a web page (18 pts)
Download JackBeanstalk.txt from my web site. You are going to read in this text file and convert it to a
web page. To do this, you must both read in the text file and write to an html file (You must create two
new File objects, one for the file you’re reading from, and one for the file you’re writing to).
To the html file, you must add at the top the following html tags:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Web Page</title>
</head>
<body>
Then for each line of the file you read in, you want to convert it to a paragraph by adding the <p>… and
</p> tags around the line. So, for instance, if the line you read in is:
ONCE upon a time there was a poor widow who lived in a little cottage with her only son Jack.
You want to write it to the html file as:
<p> ONCE upon a time there was a poor widow who lived in a little cottage with her only son Jack.</p>
There are, however, certain lines in the file that should not be paragraphs, but instead should be
headers.
The first line, for instance, should have header 1 tags ( <h1> </h1> ) surrounding it.
Also, when an entire line consists of all capital letters, it should have header 2 tags ( <h2> </h2> )
surrounding it., e.g.,
<h2> WONDERFUL GROWTH OF THE BEANSTALK </h2>
When you’re done reading in the beanstalk text file and writing it out as an html file, you’ll need to add
two tags to the bottom of the html file to finish it. Those two tags are:
</body>
</html>
Finally, you’ll want to launch your newly created web page in the browser. So you’ll have to close the
printstream. Once it’s closed, you then want to launch the file object (not the printstream object) in the
browser using :
try {
Desktop.getDesktop().browse(f2.toURI());
}
catch(IOException e) {
System.out.println(e.getMessage());
}
Where f2 is the File object used to create the html file. You’ll have to import at the top of your class
definition:
import java.awt.Desktop;
When you run your program, you should have the web page automatically launched in a browser.
You’ve just converted a text file to a web page! (Note: I didn’t use any arrays reading in or writing the
web page).
Problem 2: Reading and sorting data (25 pts)
For this problem you will need to download the classlist.txt file from my web site. You will be reading in
the data from the classlist file. Each line will become a student object. You will then ask the user what
field they want to sort on and use the console (a Scanner object) to read in the user’s choice. Then you
will create a new file printing out the data you’ve read in, only sorted on the field the user chose. So, for
instance, if the original file is:
Class Size: 10
Smither 42 98.2 21
Jones 99 88.42 64
Wilmur 87 42.32 39
Buggles 24 48.12 95
Wuzbump 76 75.5 28
Glorpiey 38 93.4 78
Blingy 87 33.3 24
Muglug 66 57.4 65
Jluzzy 78 78.9 39
Yarrington 100 99.99 100
And the user chooses to sort on the lab field, the resulting file should be:
Yarrington
Jones
Wilmur
Blingy
Jluzzy
Wuzbump
Muglug
100
99
87
87
78
76
66
99.99
88.42
42.32
33.3
78.9
75.5
57.4
100
64
39
24
39
28
65
99.99
78.326
49.59
39.39
58.77
51.85
62.92
Smither
Glorpiey
Buggles
More details:
42
38
24
98.2 21
93.4 78
48.12 95
48.36
74.62
66.73
You’ll need a class for each student, consisting of fields for the last name, the lab score, the project
score, and the exam score. It will have a fifth field, the total, calculated as labscore * .2 plus the
projectscore * .3 plus the examscore * .5.
You will need a second class for reading in the text file (classlist.txt) and creating an array of students.
The array of student objects is created by reading the data from the file. Note that the very first line of
the file tells you the number of students in the class. Other than the constructor, my reading class has
one other method that reads in the file and creates an array of students.
And you will need a third class for sorting and printing out the array of student data. This class will
clearly need a field that is an object of the second class. In this constructor, I
1. Created the field by calling the constructor of the secondclass with the name of the file to be
opened and read as the parameter.
2. Created a new Scanner object for reading from the console (Note: for this particular case, don’t
use the dialog box. Use a scanner object to read data from the console. I want to make sure
you know how to get input this way).
3. Ask the user whether they want to sort on last name, lab scores, project scores, exam scores, or
total score.
4. Read what the user enters into a string.
5. Call a printfile method with that string.
This third class also has a printfile method that first calls the appropriate sorting method (based on what
the user chose to sort on and then prints out the array of student objects in the sorted order, with tabs
between the values. You can call the output file anything you want, as long as it ends in .txt.
And finally, the sorting methods… Remember sorting? You’re going to have to sort your array of
students, based on whatever field the user chose. So when you’re finished, the array will be sorted from
largest to smallest in terms of the chosen field. I don’t care which sorting algorithm you use. Note that
to switch two values in array, you’ll need a temporary pointer to a value, e.g.,
Student temp;
temp = data.students[k];
data.students[k] = data.students[loc];
data.students[loc] = temp;
Otherwise data.students[k] becomes garbage, with no way of being accessed, as soon as you say
data.students[k] = data.students[loc];
Problem 3: Using this to call constructors: (10 pts)
Write a class for a time object. The time object has the following fields:




an hour (assuming 1-12, not military),
a minute,
a second,
and an am field
So 4 fields, 3 of which are numbers and one of which is a Boolean value. The Time class should have:

a constructor that sets each one of these fields using input parameters.
It should have 3 other constructors:



one that has an input parameter for the hour field, and uses that input parameter to call the
first constructor with this (setting the minutes to 0, the seconds to 0, and the am field to true as
the default values).
Another constructor should have input parameters for the hour and the minutes fields and
should use them to call the first constructor using this (setting the seconds field to 0 and the am
field to true).
The final constructor should have input parameters for the hour field, the minute field, and the
second field, and should use this to call the first constructor (setting the amfield to true).
Now write a toString method that returns a string representing the time object.
Finally, write a main method that creates 4 different time objects using each of the different
constructors. Make sure you print out each of the different time objects.
Problem 4: Inheritance 1: (17 pts)
You’re going to write a class hierarchy for different customerBills at a pet food store. So start by writing
a class for a customer. The customer class has fields for




the current bags of regular food (an int),
the current bags of premium food (an int),
and the current bags of raw food (an int).
It also has a field for the total number of bags of regular food bought (an int).
It should have 3 different constructors:



one in which there are input parameters for the amount of regular food, the amount of
premium food, and the amount of raw food,
one in which there are input parameters for regular food, which calls the first constructor with 0
and 0 for the amount of premium food and the amount of raw food,
and one in which there are input parameters for regular food and premium food, with a call to
the first constructor with 0 for the amount of raw food).
Each constructor should increase the total number of regular bags of food based on the number input
into the constructor.
This class should also have three methods:



calculateTotal: a method that calculates the total bill, using $10 for each bag of regular, $15for
each bag of premium, and $25 for each bag of raw. If there are over 10 bags of regular and the
user is buying regular, the user gets a bag for free and total bags of regular gets reset to 0.
totalBags: A method that returns the total number of regular bags bought so far
And there should be a toString method that returns a string representing a bill (including total
cost).
Now you should create a PremiumCustomerBill, which is a subclass of the customerBill. This method
should have constructors that use the superclass’s constructors (so you need to have 3 constructors).
It should also have its own

calculateTotal method, that calculates the total in the same way, but then gives a 12% discount
on the total.
It should have its own

toString method that returns a string representing the bill and the total cost, but mentions that
the user is a premium member.
Next, you want to write a class for employeeBill, which is also a subclass of customerBills and which
must also have 3 constructors that use the superclass’s constructors.
This must also have a

calculateTotal method that calculates the total by giving a 50% discount on both the regular
food and the premium food, but the raw food is still full-price.
And it should have a

toString method that returns a string representing the bill and total cost, mentioning that the
user got an employee discount.
Now write a main method , in which you create a customersBill object, in which the user buys different
amounts of each food. Call the totalBags method to see how many more bags the customer needs to
get his/her free bag. Print out the customersBill. You should also create a premiumCustomerBill object,
in which you create a premium customer bill with different amounts of the different foods. Again, call
the totalBags method to find out how many more totalBags the customer needs to get a free bag.
Which totalBags method are you using? And, of course, Print out the premium customer’s bill. Which
calculateTotal method are you using? Finally, create an employeeBill object, find out how many more
totalBags the customer needs, and print out the employee’s bill. Make sure you’re clear on which
methods are being used and why.
Problem 5: Insects and inheritance: (30 pts)
We’re going to create a hierarchy of Bugs.
In order to do that you will need to start by creating a class for Insects. This is your superclass. Each
insect should have:

an armor field, indicating the amount of armor it has (an int),





and fields for the x and y coordinates of where the insect is located.
Clearly your insect class needs a constructor that sets the armor amount and the original x and y
coordinates.
It will also need a method that reduces the armor by an amount (an int). If the armor is less
than 0, the x and y coordinates should both be set to a negative number (-1).
It will need a toString method that prints out the type (Insect), the amount of armor the insect
has, and the insect’s x and y coordinates.
Finally it will need a method that returns a Boolean value. This function will make more sense
when we make other subclasses. But this method should be named isAnt() and right now it
should return false. That’s the whole function.
Next you want to create a class for Ants. This class is a subclass of Insects. Thus its constructor should
set the armor and the x and y coordinates of the Insect. This class will also have fields (whose values
you should set with passed in parameters) for:



the amount of damage it can inflict (an int),
the amount of food it uses (an int),
and a name field (a String).

In addition, this class should also have a method isAnt() that returns a Boolean value, and, in this
case, the Boolean value should be true.
Now you want to create a class for a Bee. This class is a subclass of Insects. Thus its constructor should
set the armor and the x and y coordinates of the Insect. This class will also have fields for:





The name (“Bee”) (a String)
(Note that you’ll need to call the Insect constructor with the appropriate values, and these
values should be passed in as parameters to the bee’s constructor).
There should also be a sting method that takes as an input parameter something of type Ant.
The method should reduce the armor of the ant by 1 (using the reduceArmor method from the
Insect class).
There should be a method that moves the bee to a new location (within one square of where it
is currently, using its x and y coordinates. So, for instance, it can move 1 square directly up, one
square down, one square to the left, one square to the right, or one square in any of the 4
diagonal directions. This method should generate random numbers to move the bee, and then
reset the x and y coordinates.
And it should have a toString method that returns the word “bee” plus the amount of armor the
bee has.
Now you want to create some specialized ants. You’ll want to create a class for Harvester Ant, which is a
subclass of the Ant class. The Harvester Ant should call the Ant’s constructor, setting the amount of
armor, the x and y coordinates, and the name of the insect (“Harvester”).
The harvester Ant needs a toString method that prints out the word “Harvester” and the amount of
armor the ant has.
And finally, you’ll want to create a ThrowerAnt, which is a subclass of Ant. The ThrowerAnt needs to set
the armor, the x and y coordinates using parameters passed into the ThrowerAnt’s constructor, along
with the name (using “Thrower”). In the constructor, the damage field should be set to 1.


The ThrowerAnt needs a method thowsAt, which takes as an input parameter a Bee. It should
reduce the bee’s armor by whatever the ThrowerAnt’s damage amount is. (You’re using the
reduceArmor method in the Insect class.
And, of course, the ThrowerAnt needs a toString method that returns a string with “Thrower”
and the amount of armor the thrower ant has.
Now write a main method. In it create at least one Bee, one Harvester And, and one Thrower Ant. Test
the different methods in all the classes. Make sure you test the isAnt method with both the bee and the
ants. Make sure you have the bee sting each ant and then print out each Ant. Use the moveTo method
to move the bee and print it out. Make sure you have the throwerAnt attack the bee using the throwsAt
method, and then print out the bee.
(This is all part of my master plan).
Download