Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS2370.03 Java Programming Spring 2005
Dr. Zhizhang Shen
Lab 8: Work with Classes (V)
Background
In this lab, we will try to become more experienced with class related concepts such as
interface and inner class, and GUI. In particular, we will play with buttons, and dialog
boxes.
This part consists of three lab assignments. You should be able to complete all by the
end of this lab session.
For each of the assignment, hand in the source code, together with some non-trivial output
sample.
1 Band Booster
In this exercise, you will write a class that models a band booster and use your class to
update sales of band candy.
More specifically, you will go through the following steps:
1. Write the BandBooster class, assuming a band booster object is described by two
pieces of instance data: name, a String, and boxesSold, an integer that represents
the number of boxes of band candy the booster has sold in the band fundraiser. The
class should have the following methods:
• A constructor that has one parameter-a string containing the name of the band
booster. The constructor should set boxesSold to 0.
• A method getName that returns the name of the band booster (it has no param-
eters).
• A method updateSales that takes a single integer parameter representing the num-
ber of additional boxes of candy sold. The method should add this number to
boxesSold.
• A toString method that returns a string containing the name of the band booster
and the number of boxes of candy sold in a format similar to the following:
Joe: 16 boxes
2. Write a program that uses BandBooster objects as you just declared to track the sales
of three band boosters over several weeks. Your program should do the following:
(a) Read in the names of the three band boosters and construct an object for each.
1
(b) Read in the number of weeks for the current fundraising campaign.
(c) Have a count controlled loop that for each week gets the number of boxes of
candy sold by each booster. Your prompts should include the booster’s name.
For example,
Enter the number of boxes sold by Joe this week:
For each member, after reading in the weekly sales, invoke the updateSales method
to update the total sales by that member.
(d) After the loop print the name and total sales for each member (you will implicitly
use the toString method here).
2 Is it even or odd, and more?
File EvenOdd.java contains the dialog box example in Listing 5.12 of the text. We already
played with it in our very first lab.
1. Compile and run the program to see how it works.
2. Write a similar class named SquareRoots.java (you may modify EvenOdd) that com-
putes and displays the square root of the integer that a user entered.
3 Voting with buttons
The file VoteCounter.java contains a slightly modified version of PushCounter.java in
Listing 5.13 of the text. As in the text the program counts the number of times the button
is pushed; however, it assumes (”pretends”) each push is a vote for Joe so the button and
variables have been renamed appropriately.
1. Compile the program, then use the browser to see how it works (you have to write an
.html file.).
2. Modify the program so that there are two candidates to vote for: Joe and Sam. To do
this you need to do the following:
• Add variables for Sam: a vote counter, a button, and a label.
• Add a new inner class named SamButtonListener to listen for clicks on the button
for Sam. Instantiate an instance of the class when adding the ActionListener
method to the button for Sam.
• Add the button and label for Sam to the Container object.
3. Compile and run the program.
2