Project 2-1: Calculate time based on distance and speed
The application should print data like this to the console:
Distance: 50 miles
Speed (mph) Time (mins)
55.0 54.54545454545455
65.0 46.15384615384615
70.0 42.85714285714286
Specifications
The application calculates the time to travel 50 miles at speeds of 55 mph, 65 mph,
and 70 mph. It then displays the distance, speed, and corresponding time to the
console.
This application doesn‟t require any input.
The formula for calculating the time is distance traveled in miles / speed in mph.
To convert time in hours to minutes, you multiply the time in hours by 60.
Project 2-2: Convert number grades to letter grades
Operation
The user enters a numerical grade from 0 to 100 and clicks the enter button.
The application calculates the letter grade corresponding to the numerical grade and
displays the current numerical grade and corresponding letter grade. Then, the user
can continue the application or exit.
Specifications
The grading criteria is as follows:
A 88-100
B 80-87
C 67-79
D 60-67
F <60
Assume that the user will enter valid numerical data for the grades so your code
doesn‟t have to check this entry for validity.
Project 2-3: Accumulate test score data
Operation
The user enters a test score and chooses to enter another test score or to view test
score data.
For each entered score, the application adds one to the number of scores, calculates
the average score, and determines what the best score is so far.
Each time the user chooses to view test score data, the number of scores, average
score, and best score is displayed. Then, the user continue the application or exit.
Specifications
The average score is the sum of all scores divided by the number of scores.
Project 3-1: Calculate sales tax and grand total
Operation
The user enters the subtotal amount and presses Enter.
If the user enters an invalid subtotal, a dialog box prompts the user for a valid entry.
Once a valid number is entered, the application calculates and displays the sub total
amount, the sales tax amount and the grand total amount. Then, the user can continue
the application or exit.
If the user presses the Cancel button in any of the dialog boxes, the application
terminates gracefully.
Specifications
The sales tax percentage is 0.0785 (7.85%).
The sales tax calculation is subtotal * sales tax percentage.
The grand total calculation is subtotal + sales tax.
All dollar amounts should be displayed using proper currency format.
The data validation code should be contained in a static method named
parseSubTotal that returns a valid subtotal entry.
Project 3-2: Calculate grade point average
Operation
The user enters the number of grade points earned for the semester. Then, the user
enters the number of credits taken that semester.
If the user makes an invalid entry, a dialog box prompts the user for a valid entry.
Once valid entries are entered, the application calculates the grade point average and
displays the number of grade points earned, the number of credits taken, and the
computed grade point average. Then, the user can continue the application or exit.
If the user presses the Cancel button in any of the dialog boxes, the application
terminates gracefully.
Specifications
The formula for calculating the grade point average is number of credits earned / total
number of credits taken.
The computed grade point average should be displayed to the nearest thousandth
(3 decimal places).
The data validation code should be contained in one method named parseCredits that
returns a valid credit entry.
Project 4-1: Create a class that contains static methods to
validate data
Project 4-1: Create a class that contains static methods to
validate data (continued)
Operation
The user enters an integer in the dialog box. The application validates that the data is
a valid positive integer by calling a static method in the DataValidator class.
Once a valid integer is entered, the application asks the user for a valid double value.
The application validates that the data is a valid positive double value by calling a
static method in the DataValidator class.
Once a valid double value is entered, the user can continue the application or exit.
Specifications
Create a class named DataValidator that contains two static methods named
parseDouble and parseInteger. Each method should use nested while loops and
exception handling to validate that the string is a valid positive number. Each method
should return the number. The declarations for the methods are:
public static int parseInteger(String numberString)
public static double parseDouble(String numberString)
Create a driver class named TestApp that tests the static methods in the
DataValidator class.
Project 4-2: Calculate the circumference and area of a
circle
Operation
The user enters a radius of a circle.
If the user enters an invalid radius, another dialog box appears requesting a valid
radius value.
Once the user enters a valid radius, the application calculates and displays the radius,
circumference, and area of the circle.
When the user presses the OK button, the application terminates gracefully.
Specifications
Create a class named Circle that has instance variables for the radius, circumference,
and area. This class should also contain a constructor that accepts a value for the
radius, then initializes the radius, circumference, and area. In addition, the class
should contain methods that set the circumference and area and get all instance
variables.
The formula for calculating the circumference of a circle is 2 * PI * radius.
The formula for calculating the area of a circle is PI * radius2.
Create a class named CircleApp that gets the user input, creates a Circle object, and
displays the necessary information to the nearest hundredth (2 decimal places).
To validate data, you can use static methods from the DataValidator class created in
Project 4-1. Otherwise, you‟ll have to write the data validation code in the CircleApp
class.
Enhancements
Add a toString method to the Circle class and use it in the CircleApp class.
Project 4-3: Create an address book
Project 4-3: Create an address book (continued)
Operation
In the first five dialog boxes, the user enters name, street, city, state, and zip code.
In the sixth dialog box, the application displays the entry and total number of
addresses. Then, the user can continue the application or exit.
Specifications
Create a class named Address that contains String instance variables for street, city,
state, and zip code. This class should contain a constructor that accepts values to
initialize these variables and methods that return each one. In addition, it should
contain a method that returns a string for the Address object.
Create a class named AddressBookEntry that contains instance variables for name
and address (use the Address class type). It should also contain a static variable that
calculates the number of entries created.
Code at least two constructors in the AddressBookEntry class…one that accepts a
name and Address object and one that accepts a name, city, street, state, and zip code.
Code methods that set and get the address. A programmer that uses this class should
be able to set the address by either sending in an Address object or the string
variables for street, city, state, and zip.
Code a method that returns the string of an object.
Code a static method that returns the number of entries created.
Code a driver class that creates address book entries.
Assume valid data is entered.
Project 5-1: Use inheritance to display a student record
Project 5-1: Use inheritance to display a student record
(continued)
Operation
The user enters a college student‟s identification number followed by the major and
GPA.
The application creates a college student object and displays the object to the screen.
Specifications
First, create a class that defines a student. This class should contain at least two
instance variables for identification number and GPA. It should also contain two
constructors…a default one and one that accepts two values for the instance
variables. In addition to set and get methods, the class should override the toString
method from the Object class.
Next, create a class that defines a college student. To do this, the class should inherit
the above class. In addition to the inherited instance variables, this class should
contain at least one more instance variable that defines the college student‟s major.
This class should also contain a default constructor and one that accepts three values
for the instance variables. The first statement in this constructor should call a
constructor in the superclass. This class should also override the toString method.
Code a driver class that carries out the operation of the project.
Assume valid data is entered.
Project 5-2: Calculate area and perimeter of shapes using
an abstract class
Project 5-2: Calculate area and perimeter of shapes using
an abstract class (continued)
Operation
The user enters a letter to pick a shape to draw.
Then, the user enters the length and width for rectangles or the radius for circles.
Then, the application “draws” the shape by displaying its length, width, area, and
perimeter for rectangles or the radius, area, and circumference for circles.
Once the user clicks OK in the fourth dialog box shown above, the user can use the
next dialog box to continue the application or exit.
Specifications
Create an abstract class that defines any shape. This class should contain at least two
instance variables for the area and perimeter. In addition, it should contain at least
two regular methods that return these instance variables. It should also contain at
least one abstract method named draw that accepts and returns no values. This
method should be defined in the subclasses to display the shape‟s measurements.
Next, create two classes that both inherit the above class. One should define a
rectangle and one should define a circle. The draw method should just use a dialog
box to display the necessary measurements.
Create a driver class that asks the user to choose a shape to draw. The application
should first declare an object of the abstract class and initialize it to null. Then, two if
statements should obtain the necessary dimensions and create the object of the
appropriate subclass. Outside of the if statements, the object should invoke the draw
method.
The formula for calculating the area of a rectangle is length * width. The formula for
calculating the perimeter of a rectangle is 2*length + 2*width.
The formula for calculating the area of a circle is PI * radius2. The formula for
calculating the circumference (perimeter) of a circle is 2 * PI * radius.
Enhancement
Create another class that contains one static method named drawShape that accepts a
Shape reference and calls the draw method. Then, the driver class should use this
method rather than directly calling the draw method.
Project 5-3: View a car rental transaction
Project 5-3: View a car rental transaction (continued)
Operation
The user enters the desired car type to rent. Then, the user enters the number of days
to rent the car.
Then, the application displays the rental transaction, including days, car type, price
per day, and total price.
Then, the user can continue the application or exit.
Specifications
First, create an interface named Printable that contains at least two methods: one
called print and one called printToScreen. Both methods shouldn‟t accept or return
values.
Next, create a class that defines a car rental transaction. This class can exist on its
own or be derived from a class that defines rental transactions. This class should
implement the Printable interface.
The car rental class should contain at least four instance variables for the car type,
daily fee, number of days, and the total price. In addition, it should contain at least
three constants that define the daily fee for the three different types of car (economy,
midsize, and luxury).
The car rental class should also contain two constructors…one default and one that
accepts the number of days and car type. It should also contain methods that set the
daily fee and total price.
The printToScreen method should display the car rental transaction in a dialog box.
The print method should do nothing.
Write a driver class that carries out the operation of the project.
Assume valid data is entered.
Project 5-3: View a car rental transaction (continued)
Enhancement 1
Create another class name PrintData that contains one static method named
printToConsole. This method should accept a Printable reference and return no value.
This method calls the printToScreen method associated with the Printable reference.
Modify the driver class so that it calls this method rather than the printToScreen
method to view the transaction.
Enhancement 2
Create another class that defines a bank account that also implements the Printable
interface. This class should contain at least two instance variables for account number
and balance. It should also contain a default constructor and one that accepts values
for the two instance variables.
Minimally, this class must contain the methods in the Printable interface. The
printToScreen method should display the bank account information in a dialog box.
The print method should do nothing.
Write a driver class that obtains the account number and balance from the user. Then,
the application creates a bank account, and calls printToConsole method in the
PrintData class.
Project 7-1: Calculate a reservation total
Project 7-1: Calculate a reservation total (continued)
Operation
The user enters the month, day, and year for the arrival and departure dates.
Then, the application displays the arrival date, departure date, daily rate, total price,
and number of nights. Then, the user can continue the application or exit.
Specifications
Create a class that defines a reservation. This class should contain instance variables
for the arrival date, departure date, number of nights, and total price. It should also
contain a constant initialized to the nightly rate of $115.00.
This class should contain at least two constructors: a default one and one that accepts
the arrival date and departure date. Within this constructor, the number of nights and
total price should be set. This means that this class should contain two methods that
set both these instance variables. This class should also override the toString method.
Create a driver class that gets the user input, creates a reservation object, and displays
it in a dialog box.
Assume valid data is entered.
Project 8-1: Display a list of squares and cubes
Operation
The user enters a number less than 10 in a dialog box. If the user enters a number
greater than or equal to 10, the application requests another number.
Then, the application calculates and displays the squared and cubed values for all
number within the given range. Then, the user can continue the application or exit.
Specifications
The square of a number is the number times itself (x * x).
The cube of a number is the number times itself times itself (x * x * x).
Assume a positive integer is entered.
Project 8-2: Find the greatest common divisor of two
positive integers
Project 8-2: Find the greatest common divisor of two
positive integers (continued)
Operation
The user enters two positive integers in two different dialog boxes.
Then, the application computes the greatest common divisor of the two numbers and
displays all three values in another dialog box.
Then, the user can continue the application or exit.
Specifications
The formula for finding the greatest common divisor of two positive integers x and y
follows the Euclidean algorithm as follows:
1. Subtract x from y repeatedly until y < x.
2. Swap the values of x and y.
3. Repeat steps 1 and 2 until x = 0.
4. y is the greatest common divisor of the two numbers.
This application can use a while loop for step 1 nested within a do while loop that
checks step 3. To swap values, you can use the following technique:
int temp = a;
a = b;
b = temp;
Code should handle invalid data. If you did Project 4-1, you can use a method in the
DataValidator class to validate integer values.
Project 8-3: Create a simple integer calculator
Project 8-3: Create a simple integer calculator (continued)
Operation
The user enters an integer in the first dialog box. Then, the user enters a character
into the second dialog box followed by another integer in the third dialog box.
Then, the application displays the result of the equation depending on the character
according to the following definition:
+ sum
- difference
* product
/ quotient
% remainder
The user then chooses to quit or continue.
Specifications
Write a class named Calculator that defines a simple calculator. This class should
contain instance variables for the two input values, the result value, and a char value
for the selected operator. The constructor should accept values for the three input
values and initialize the instance variables accordingly. In addition, it should
calculate the result of the equation by calling another method in the same class. This
method should use a switch statement to determine what operator to use.
Write a driver class named CalculatorApp that uses dialog boxes to get the input
from the user and to display the results of the calculation. This class should use the
Calculator class. To get the first char value from a string, you can use the charAt(0)
method of the String class. For more information on this method, you can look it up
in the API documentation.
Assume valid data is entered.
Project 9-1 Calculate change
Operation
The user inputs a number of cents from 0 to 99.
Then, the application calculates and displays the minimum number of pennies,
nickels, dimes, and quarters with the same value.
The user chooses to quit or continue.
Specifications
If the minimum number of coins is equal to 1, the application should display the coin
type singularly.
Use an array to store the four values corresponding to coin types. Also, use an array
to store the number amount of each coin based on the user‟s input.
To calculate the number amount of each coin, use a for loop that divides the
remaining cents by the coin value.
Assume valid data is entered.
Project 9-2 Parse dates
Project 9-2 Parse dates (continued)
Operation
The user enters a birth date in the format MM/DD/YYYY in a dialog box. The user
then enters a projected graduation date in the same format.
Then, the application parses the dates and displays the projected age of the user upon
graduation and the number of days until graduation.
The user chooses to quit or continue.
Specifications
Create a class called DateParser that parses a string in the format MM/DD/YYYY.
To do this, the class should contain instance variables for the date as a String, and the
month, day, and year values as ints. A constructor of this class should accept a String
object, initialize the corresponding instance variable and call a method in the same
class that parses the String. This method should accept and return no values. Inside
this method, use the substring method to retrieve the month, day, and year values. Be
sure to subtract 1 from the month value. This method should set the month, day and
year instance variables. This class should also contain a method that returns the
number of milliseconds corresponding to the object.
Create a driver class that requests two dates from the user. Then, create two
DateParser objects.
The formula for age at graduation is (number of milliseconds of graduation date –
number of milliseconds of birthdate) /1000/60/60/24/365.
The formula for days until graduation is (number of milliseconds of graduation date –
number of milliseconds of current date) / 1000/60/60/24.
Assume valid data is entered.
Project 9-3 Translate English to Pig Latin
Project 9-3 Translate English to Pig Latin (continued)
Operation
The user enters a word into a dialog box.
Then, the application displays the Pig Latin equivalent.
Specifications
If the word starts with a consonant, move the consonants before the first vowel to the
end of the word and add ay. If the word starts with a vowel, just add lay to the end of
the word.
To convert all letters in a string to lowercase, you can use the toLowerCase method
of the String class. To learn more about this method, see the API documentation.
Use the StringBuffer class to manipulate the user‟s string. First, use a switch
statement to determine if the first letter is a vowel. If it is, then add “lay” to the word.
Otherwise, search the StringBuffer object for the first vowel. Each time a consonant
is found, add that letter to the end of the StringBuffer object, then delete the first
character. When a vowel is found, add “ay” to the StringBuffer object. This can all
occur within a switch statement.
Project 9-4 Parse strings
Operation
The user enters a line of entries that contains a last name, account number, and
password in this format “Thomas, 123-45, jtb”.
The application parses the name, account, number, and password from the string and
displays each field separately.
Project 9-5 Convert numbers to words
Operation
The user enters a four digit number in a dialog box.
Then, the application converts the number to words and displays the string in another
dialog box. The user then chooses to quit or continue.
Specifications
First, parse the user‟s input into an array of four String objects. Then, convert that
array to an array of four ints.
You can use arrays of String objects for units, teens, and tens places. For instance, the
teens array may include {“ten”, “eleven”, “twelve”…and so on}. The tens array may
include {“twenty”, “thirty”, “forty”…and so on}.
If the third number is greater than 1, the word will use “twenty”, “thirty”, “forty”, and
so on. Then, the fourth number will be a units digit.
If the third and fourth numbers are 0, only the thousandth and hundredth places are
printed.
If the third number is 0, the fourth number will be a units digit.
If the third number is 1, the last word will be “ten”, “eleven”, “twelve” and so on.
Assume valid data is entered.
Project 9-6 Update a student roster
Project 9-6 Update a student roster (continued)
Operation
The user chooses to add a student to the roster or to delete a student. The user can
also choose to exit the program.
If the user chooses to add a student, the application asks the user to enter a name
followed by a list of grades in this format, “Name,92,93,93”.
If the user chooses to delete a student, the application asks the user to enter a
student‟s name and deletes that record.
Once the application performs the desired operation, it displays the updated roster
and asks the user for another option.
Specifications
Create a Student class that contains at least two instance variables for name and for
an array of int grades. This class should contain a default constructor and one that
accepts values for both instance variables and initializes them. Minimally, the class
should contain methods that return the name of a Student object, return the array of
grade values, and the one that overrides the toString method. To convert a Student
object to a string, you should display the student‟s name followed by each grade in
the array.
The driver application should first create a vector of Student objects. To do this,
create three Student objects based on the following information:
John Jones 88 92 95
Kelly Green 76 83 86
Jeff Smith 92 94 88
Add each of these objects to the Vector object. Then, start a while loop that ends
when the user enters „X‟. The first step inside the while loop should create a String
object of each item in the Vector object. Then, the first dialog box displays this string
and user options.
If the user enters „A‟, the application requests the student name and grades separated
by commas. No spaces should be entered between grades. The application should
parse the string accordingly, create another Student object, and add it to the Vector
object.
Project 9-6 Update a student roster (continued)
If the user enters „D‟, the application requests the student‟s name. Then, the
application searches through each item in the Vector for a matching name. When the
match is found, the application deletes that record from the Vector.
Assume valid data is entered.
Enhancements
Add an option to the program that allows a user to view a specific student‟s highest
grade.
To do this, you need to add a method to the Student class that returns the highest
grade of a Student object.
Project 10-1 Find the factorial of a number
Operation
The user enters a number greater than 0 and less than 10 into a dialog box. If the user
enters an invalid number, the application informs the user of the error. Once the user
clicks OK, the application asks for another number.
Once a valid number is entered, the application displays the factorial of that number.
To quit the application, the user presses the Cancel button on either dialog box or
enters a „X‟ in the second dialog box.
Project 10-1 Find the factorial of a number (continued)
Specifications
This application should use a catch statement that catches the
NumberFormatException that may be thrown from the Integer.parseInt method. This
catch statement should inform the user of the invalid entry. If the user enters an
integer, but not within range, the application should divert code to this catch
statement.
If the user clicks the Cancel button in the first dialog box, the application should
throw a NullPointerException. If the user clicks the Cancel button in the final dialog
box, the application will automatically throw a NullPointerException when it calls
the equalsIgnoreCase method of the String class in the while conditional expression.
The NullPointerException should be caught outside the while loop and terminate the
program.
The showInputDialog method of the JOptionPane class returns “null” if the user
clicks the Cancel button in the dialog box.
The exclamation point is used to identify a factorial number. For example, the
factorial of the number, n, is denoted by n!. Here‟s how you calculate the factorial of
the numbers 1 through 5:
1! = 1 which equals 1
2! = 1 * 2 which equals 2
3! = 1 * 2 * 3 which equals 6
4! = 1 * 2 * 3 * 4 which equals 24
5! = 1 * 2 * 3 * 4 * 5 which equals 120
Project 11-1 Calculate grade point average
Operation
The user must enter the number of credits earned and taken for that semester.
The user can press the Tab key to move the focus from the Credits Earned text box to
the Calculate button.
When the user presses the Calculate button, the application displays the calculated
GPA for those values in the uneditable text field.
When the user closes the frame or presses the Exit button, the application exits.
Specifications
Create a class that sets up the frame. To help with the layout, start with a height of
170 and a width of 200 and adjust accordingly. The frame should be centered on the
screen and shouldn‟t be resized by the user. This class should add a user-defined
panel to the content pane.
The panel class should contain instance variables for three labels, three text fields,
and two buttons. Its constructor should initialize these variables. The text fields
should be 7 character widths wide.
The panel class should act as the event handler class for button events.
This panel class should use the Border layout manager. The labels and text fields can
be placed on one panel, while the buttons are placed on another. The two panels can
then be placed on the main panel.
The actionPerformed method of this class should compute the GPA if the source is
the Calculate button and exit the program if the source is the Exit button.
The formula for calculating GPA is the number of credits earned / total number of
credits taken.
The computed GPA should be displayed to the nearest thousandth (3 decimal places).
Assume valid data is entered.
Enhancements
Add exception handling code to prevent invalid entries. Don‟t forget to prevent
negative entries.
Project 11-2 Accumulate test scores
Operation
The user enters test scores one at a time and then clicks the Enter score button.
The user can press the Tab key to move the focus from the Test Score text field to the
Enter Score button.
For each entered score, the application adds one to the number of scores, calculates
the average score, and determines what the best score is so far. Then, it displays the
number of scores, average score, and best score in the three disabled text fields.
When the user closes the frame or presses the Close button, the application exits.
Specifications
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 200 and a width of 267 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for the four labels, four text fields,
and two buttons. It should also contain instance variables for the sum, best score,
average score, and number of scores.
The panel class should acts as the event handler class for all button events.
The constructor should initialize all instance variables.
The text fields should be 10 character widths wide.
Use the Flow layout with right alignment and do not create any extra panels.
The actionPerformed method should perform necessary operations when a button
event occurs.
The average score is the sum of all scores divided by the number of scores.
Assume valid data is entered.
Enhancements
Add exception handling code to prevent invalid entries. Don‟t forget to prevent
negative entries.
Project 11-3 Convert temperatures
Operation
When the user enters a number into the Fahrenheit text field and clicks the Calculate
button, the application displays the result in the uneditable text field.
The user can press the Tab key to move the focus from the Fahrenheit text field to the
Calculate button.
When the user enters an invalid number, the application displays an appropriate error
message.
When the user closes the frame or presses the Exit button, the application exits.
Specifications
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 170 and a width of 170 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for the two labels, two text fields,
and two buttons. The constructor of this class should initialize all instance variables.
This class should also use the Border layout manager. A panel for the labels and text
fields and one for the buttons should be added to this main panel.
The panel class should act as the event handler class for the button events.
The actionPerformed method should perform necessary operations when a button
event occurs. It should also contain appropriate exception handling to insure a valid
result is entered.
The formula for converting temperatures from Fahrenheit to Celsius is (F-32)*5/9.
Project 11-4 Calculating the circumference and area of a
circle
Operation
When the user enters a valid radius of a circle and clicks the Calculate button, the
application displays the circumference and area for that circle in the uneditable text
fields.
The user can press the Tab key to move the focus from the Radius text box to the
Calculate button.
When the user enters an invalid radius, the application displays an appropriate error
message.
When the user closes the frame or presses the Exit button, the application exits.
Specifications
Create a class that defines a circle. This class should contain a constructor that
accepts a double value for the radius and initializes the circumference and area
instance variables. It should also contain methods that return the instance variable
values.
The formula for calculating the circumference of a circle is 2 * PI * radius.
The formula for calculating the area of a circle is PI * radius2.
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 200 and a width of 200 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for the three labels, three text fields,
and two buttons. The constructor of this class should initialize all instance variables.
This class should use the Border layout manager. A panel for the labels and text
fields that uses the Flow layout with right alignment and a panel for the buttons
should be added to this main panel.
The panel class should act as the event handler class for the button events.
The actionPerformed method should perform necessary operations when a button
event occurs. It should also contain appropriate exception handling to insure a valid
result is entered. This method should use the Circle class to create an object and
return information on that object.
Project 11-5 Update a student roster
Operation
The user clicks on the Next button to view a list of students in the roster. If the user
clicks on the Next button when the last student is displayed, the first student is then
displayed.
The user clicks on the Delete button to delete a student.
The user clicks on the Add button to add a student to the roster. When the Add button
is clicked, the application enables the Update button, disables the Add, Delete, and
Next buttons, clears all text fields, and moves the focus to the name text field. Once
the user enters the necessary information, the user clicks the Update button to store
the new student. When the user clicks on the Update button, the Add, Delete, and
Next buttons are enabled and the Update button is disabled.
When the user closes the frame or presses the Exit button, the application exits.
Specifications
Create a Student class that contains at least two instance variables for name and for
an array of int grades. This class should contain a default constructor and one that
accepts values for both instance variables and initializes them. Minimally, the class
should contain a method that returns the name of a Student object and the array of
grade values.
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 200 and a width of 260 and adjust
accordingly. This class should add a user-defined panel to the content pane.
Project 11-5 Update a student roster (continued)
The panel class should contain instance variables for the four labels, four text fields,
and five buttons. It should also contain instance variables for a vector of students and
an integer value that represents the displayed student‟s index value in that vector.
The panel class should act as the event handler class for the button events.
The constructor of the panel class should initialize all instance variables including the
Vector object. To simulate data in a file, you can create a vector of Student objects.
To do this, create three Student objects based on the following information:
John Jones 88 92 95
Kelly Green 76 83 86
Jeff Smith 92 94 88
Then, add each of these objects to the Vector object.
The panel class should use the Border layout manager. To help with the layout, you
can try the following technique and modify it accordingly: create another panel that
uses the Flow layout with right alignment. Add all labels, text fields, and the Add,
Update, and Delete buttons to it. Then, create another panel just for the Next and Exit
buttons. Add the larger panel to the center of the content pane and the smaller one to
the southern part of the content pane.
In the constructor of the panel class, you should set the text of the text field to the
data for the first Student object in the vector (this corresponds to an index of 0).
The actionPerformed method should perform necessary operations when a button
event occurs. If the user clicks the Next button, the application should first determine
if the displayed student is the last student in the Vector object. To do this, check the
index values. If the displayed student is the last student, the next student should be
the first student. Otherwise, the next student is just the index value of the current
student + 1. Be sure to increment the index instance variable. Get the next student
and display its data in the text fields.
If the user clicks the Delete button, just delete the current student from the vector.
If the user clicks the Add button, clear all fields and enable or disable the buttons
appropriately.
If the user clicks the Update button, retrieve the user‟s values, create a Student object,
and add it to the vector. Since this object will be the last object in the vector, be sure
to update the current index value to the last value in the Vector (vector.size() – 1).
Assume valid data is entered.
Project 12-1 Convert temperatures
Operation
When the user clicks on one of the two radio buttons, the application clears both text
fields, sets the text box that accepts user input to editable, and sets the text box that
displays the result to uneditable.
The user can press the Tab key to move the focus from the enabled text field directly
to the Calculate button. When the user presses the Calculate button, the application
displays the result in the uneditable text field.
When the user closes the frame or presses the Exit button, the application exits.
Specifications
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 200 and a width of 250 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for all components in the frame.
The text fields should be 7 character-widths wide. This class should act as the event
handler class for the button events.
To help with the layout, first add both radio buttons to a panel that uses the Border
Layout manager. Add the top button to the CENTER field and the bottom one to the
SOUTH field. Next, add the Calculate button and Exit button to another panel using
the default Flow Layout manager. Then, lay out the two panels, the labels, and text
fields to use a Grid Bag layout of 5 rows and 2 columns.
The formula for converting the temperature from Fahrenheit to Celsius is (F-32) *
5/9.
The formula for converting the temperature from Celsius to Fahrenheit is
(C*5/9)+32.
Enhancements
Add a focus listener to the text fields to prevent invalid data. When the user enters
invalid data, the application should display an appropriate error message in a dialog
box. When the user responds to the message, focus returns to the text box.
Add a key listener to the text fields so that the user can press Alt+C in either text
field to calculate the result.
Project 12-2 Distance converter
Operation
When the user enters a valid number in the text field and selects a measurement in
the list box, the conversion information is displayed in a label. Each conversion value
is displayed to a maximum of four decimal places.
When the user closes the frame or presses the Close button, the application exits.
Specifications
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 250 and a width of 220 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for all components in the frame. A
static array of strings should be defined to include the following measurements:
fathoms, feet, furlongs, hectometers, kilometers, meters, nautical miles, yards. The
text field should be 7 character-widths wide and initially set to contain the number 1
and the result label should be empty.
The panel class should act as the event handler class for the list selection and button
events.
To layout these components, start by adding the top label and text field to a panel that
uses the default Flow layout manager. Then, layout that panel, the scroll pane, the
bottom label, and the button using a Grid Bag layout of 4 rows and 1 column.
The following conversion information is necessary in the valueChanged method:
1 mile = 880 fathoms
5280 feet
8 furlongs
16.09344 hectometers
1.609344 kilometers
1609.344 meters
0.86898 nautical miles
1760 yards
Assume valid data is entered.
Project 12-2 Distance converter (continued)
Enhancement 1
Add a focus listener to the miles text field to prevent invalid data from being entered.
Enhancement 2
Use a combo box instead of a list box to display the conversion measurements.
Project 12-3 Process lunch orders
Operation
For each order, the user selects one main course item and zero or more of the
associated add-ons.
When the user clicks the Place order button, the application displays the subtotal, tax,
and total due for an order.
When the user closes the frame or presses the Exit button, the application exits.
Add-ons
The three add-ons are $0.25 each and change based on the main course that‟s
selected. In other words, when the user selects a radio button item, the text in the add-
on changes to different values.
For a hamburger, the three items are (1) Lettuce, tomato, and onions, (2)
Mayonnaise, and (3) Mustard.
For a pizza, the three items are (1) Pepperoni, (2) Sausage, and (3) Olives.
For a salad, the three items are (1) Croutons, (2) Bacon bits, and (3) Bread sticks.
Project 12-3 Process lunch orders (continued)
Specifications
Create a class that sets up the frame. The frame should be centered on the screen. To
help with the layout, start with a height of 300 and a width of 400 and adjust
accordingly. This class should add a user-defined panel to the content pane.
The panel class should contain instance variables for all components in the frame.
The text fields should be 7 character-widths wide and the Hamburger radio button
should be initially selected.
The panel class should act as the event handler class for the button events.
To help with the layout, first add the radio buttons to a panel that uses the Border
Layout manager and lay out the radio buttons in the NORTH, CENTER, and SOUTH
fields. Add a border to this panel. Then, create a similar panel for the check box items
and order totals. For the order totals components, you can first add each label and text
field to a separate panel using the Flow Layout manager with right alignment. Then,
add these three panels to one larger panel using the Border Layout manager. Finally,
layout the three panels and two buttons using a Grid Bag layout of 4 rows and 4
columns.
The subtotal is equal to the cost of the main course item plus the cost of the add-ons.
The tax is the subtotal * 0.0785. And the total due is the subtotal + tax.
Project 13-1 Convert temperatures using menus
Operation
When the user clicks on one of the two radio button menu items in the Convert
submenu of the Tools menu, the application clears both text fields, sets the text box
that accepts user input to editable, and sets the text box that displays the result to
uneditable.
When the user clicks on a radio button menu item in the Convert submenu of the
Tools menu, the application clears both text fields and sets the one that displays the
result to uneditable.
The user can press the Tab key to move the focus from the editable text field to the
Calculate button. When the user presses the Calculate button, the application displays
the result to a maximum of two decimal places in the uneditable text field.
The user can select either menu by holding down the Alt key and pressing the first
letter of the menu.
The user can select the Calculate button pressing Alt + C.
The user can allow the frame to be resized by selecting the Resizable check box
menu item in the Tools menu or by using the keyboard accelerator Ctrl + R. Then,
the user can resize the frame by dragging the edge of the frame.
The user can exit the application by closing the frame, pressing the Exit button, by
selecting the Exit item from the File menu, or by using Ctrl + E to select the Exit item
from the File menu.
Specifications
Create a class that defines the frame as shown in Project 11-3. Although you
shouldn‟t have to change the layout or frame size, you should place all code in the
class that defines the frame. Then add the other panels and components directly to the
content pane of the frame. And finally, add the code for the appropriate menus, menu
items, accelerators, keyboard mnemonics, and event handling.
The formula for converting temperatures from Fahrenheit to Celsius is (F-32)*5/9.
The formula for converting the temperature from Celsius to Fahrenheit is
(C*5/9)+32.
Project 13-1 Converting temperatures with menus
(continued)
Enhancement 1
Add a pop-up menu that displays all values as integers. In other words, the resulting
value should be displayed to a maximum of 0 decimal places.
Add an Exit menu item to the pop-up menu.
Display the pop-up menu by using an anonymous class.
Enhancement 2
Display the pop-up menu without an anonymous class.
Project 14-1 Working with shapes and colors
Operation
The user clicks on a button and that color and/or shape is displayed.
When the user closes the frame, the application exits.
Specifications
Create a class that sets up the frame. Create another class that defines the panel to be
placed on the frame. In this panel, draw the appropriate shape using the Java2D API.
Project 14-2 Working with images
Operation
The user clicks on a button and the appropriate image is displayed.
When the user closes the frame, the application exits.
Specifications
Create a frame that is wide enough to fit all three buttons. First, paint the image by
overriding the paintComponent method of the JPanel class.
Then, paint the image by displaying it in a JLabel object.
The three image files are located in the files/ch14 folder of the Student Projects
directory.
Project 15-1 Temperature conversion Swing applet
Operation
The user opens the web page in a web browser that‟s using the Java Plug-in.
When the user enters a valid number into the Fahrenheit text field and clicks the
Calculate button, the application displays the result in the uneditable text field.
When the user enters an invalid number, the application displays an appropriate error
message.
Specifications
Create a Swing applet that is similar to the application presented in Project 11-3.
Write the HTML code and use the HTML Converter to convert the APPLET tag to
the OBJECT and EMBED tags.
Test the applet using the Applet Viewer and using a browser.
The formula for converting temperatures from Fahrenheit to Celsius is (F-32)*5/9.
Project 15-2 Temperature conversion AWT applet
Operation
The user opens a web page in a browser that is Java enabled.
When the user enters a number into the Fahrenheit text field and clicks the Calculate
button, the application displays the result in the uneditable text field.
Specifications
Create an AWT applet that is similar to the application presented in Project 11-3.
Test the applet using the Applet Viewer and using a browser. When testing this
applet in a browser, be sure that the Java Plug-in isn‟t enabled. And since newer Java
features can‟t be used, you‟ll have to make some adjustments to run this applet in a
browser.
The formula for converting temperatures from Fahrenheit to Celsius is (F-32)*5/9.
Assume valid data is entered.