88
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 - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 2-1: Calculate time based on distance and speedThe application should print data like this to the console:

Distance: 50 miles

Speed (mph) Time (mins)55.0 54.5454545454545565.0 46.1538461538461570.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.

Page 2: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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-100B 80-87C 67-79D 60-67F <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.

Page 3: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 4: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 5: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 6: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 4-1: Create a class that contains static methods to validate data

Page 7: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 8: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 9: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 4-3: Create an address book

Page 10: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 11: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 5-1: Use inheritance to display a student record

Page 12: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 13: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 5-2: Calculate area and perimeter of shapes using an abstract class

Page 14: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 15: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 5-3: View a car rental transaction

Page 16: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 17: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 18: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 7-1: Calculate a reservation total

Page 19: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 20: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 21: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 8-2: Find the greatest common divisor of two positive integers

Page 22: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 23: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 8-3: Create a simple integer calculator

Page 24: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 25: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 26: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 9-2 Parse dates

Page 27: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 28: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 9-3 Translate English to Pig Latin

Page 29: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 30: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 31: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 32: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 9-6 Update a student roster

Page 33: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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 95Kelly Green 76 83 86Jeff 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.

Page 34: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 35: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 36: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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 12! = 1 * 2 which equals 23! = 1 * 2 * 3 which equals 64! = 1 * 2 * 3 * 4 which equals 245! = 1 * 2 * 3 * 4 * 5 which equals 120

Page 37: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 11-1 Calculate grade point average

Operation The user must enter the number of credits earned and taken for that semester.

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.

Page 38: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 11-2 Accumulate test scores

Operation The user enters test scores one at a time and then clicks 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.

Page 39: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

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.

Page 40: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

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.

Page 41: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 42: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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 95Kelly Green 76 83 86Jeff 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.

Page 43: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

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.

Page 44: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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 fathoms5280 feet8 furlongs16.09344 hectometers1.609344 kilometers1609.344 meters0.86898 nautical miles1760 yards

Assume valid data is entered.

Page 45: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 46: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 47: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 48: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

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.

Page 49: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 50: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 51: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 52: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 53: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

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.

Page 54: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 17-1 Writing data to a text file

Operation Create an application that writes the information shown below to a delimited text file.

Specifications The text file should contain the following data:

Raul Menendez|2|[email protected]

Kim Mathews|4|[email protected]

Mary Owens|1|[email protected]

Enhancements

Create a class that displays the user interface shown above.

Create a class named Downloader that accepts the data that’s input into the user interface shown above.

Create a static method called addRecord in a class named DownloaderIO that accepts a Downloader object and writes its data to the text file.

When the user clicks the Add button, the application should create a Downloader object from the user’s entries. Then, the application should use the addRecord method of the DownloaderIO class to write the data stored in the object to the delimited text file in the same format shown above.

Page 55: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 17-2 Reading a delimited text file

Specifications Create an application that reads the name, number of downloads, and email address

from the delimited text file created in Project 17-1. Then, print the data to the console as shown above.

Use the StringTokenizer class to parse the fields from each line in the file.

Page 56: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 18-1 Write data to a binary file

Specifications Create an application that writes the information shown below to a binary file.

Each record should contain 62 bytes as follows:

item number: 10 bytes (5 characters)

item description: 40 bytes (20 characters)

quantity: 4 bytes (1 integer)

unit cost: 8 bytes (1 double)

To insure that all strings are written the same length, use the StringHelper class from the CD-ROM included with the book.

The following data should be written to the binary file:

Item number Item description Quantity Unit cost ($)

10112 MICROPROCESSOR 2325 12.5211202 RIBBON_CART 6520 6.8514003 VIDEO_DISPLAY 240 385.5015663 CHARACTER_PRINT 15 2355.7419888 DISK_DRIVE 1562 1985.00

Enhancements

Create a class that displays the user interface shown above. Use any layout that works (the GridBagLayout is used in the user interface shown here).

Create a class named Item that defines an item. This class should contain methods that return the values of each instance variable.

Create a static method named addRecord in a class named ItemIO that accepts an Item object and writes its data to the end of the binary file.

When the user clicks the Add button, the application should create an Item object from the user’s entries. Then, the application should use the addRecord method of the ItemIO class to write the data that’s stored in the object to the binary file in the same format shown above.

Page 57: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 18-2 Read a binary file

Specifications Create an application that reads the item number, description, quantity, and unit cost

from the binary file written in Project 18-1. Then, print the data to the console as shown above.

Use a for loop to read in each record. To determine the number of records in the file, use the available method of the DataInputStream class and the size of each record.

To read fixed-length strings, you can use the StringHelper class from the CD-ROM included with the book.

Page 58: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 18-3 Maintain inventory records

Operation To navigate through the items, the user clicks on one of the buttons in the bottom

row.

To modify the quantity or unit cost of an item, the user makes a change to one of these fields. At that point, the Update button is enabled and the Add, Delete, and navigation buttons are disabled. Then, to save the changes to the file, the user clicks on the Update button. At this point, the Update button is disabled and the Add, Delete, and navigation buttons are enabled.

To add a record to the file, the user clicks on the Add button. This clears the text fields, enables the Update button, and disables the Add, Delete, and navigation buttons. Then, the user enters the item number, description, quantity and price (with or without dollar sign) for a new item and clicks on the Update button to save the new record to the file. This adds the record to the end of the file, disables the Update button, and enables the Add, Delete, and navigation buttons.

To delete a record, the user navigates to that record and clicks the Delete button. After it deletes the record from the file, the program displays the data for the next record (or the new last record if the deleted record was the last record).

To exit the program, the user clicks the Exit button or closes the frame.

Specifications Create the user interface using any layout that works (the GridBagLayout was used

for the user interface shown here).

Model this project on the Book Maintenance application described throughout the book. To do this, you can create an Item class that defines an item. Plus, create an ItemIO class that performs necessary I/O operations. This class can use the StringHelper class from the book to assist in writing and reading fixed-length strings.

To start, you can use the binary file that was created in Project 18-1.

Assume valid data is entered.

Page 59: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 18-3 Maintain inventory records (continued)

Enhancements

Add a Find button to the frame that allows a user to find an item by entering an item number.

When the user clicks on the Find button, a dialog box requests an item number. When a valid number is entered, the dialog box closes and the frame displays all the data for that record. If an invalid number is entered, the user is notified and asked to enter another number.

Create a method named find in the ItemIO class that accepts an item number as a parameter and returns the matching Item object.

Modify the code for the actionPerformed method to handle the event caused by the Find button.

Page 60: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 19-1 Connecting to a database

Specifications Write an application that connects to the Inventory database and displays the first

record in the Items table to the console as shown above.

If a connection can’t be made due to driver problems, an appropriate error should be displayed. If the database can’t be found, a different error message should be displayed.

Create an ODBC data source named Inventory for the database named Inventory that’s in the Student Projects\files\ch19 folder. If your system doesn’t support the Access 2000 format, try creating an ODBC data source for the database named Inventory97, but still use Inventory as the name of the data source.

To print the first record to the console, you should create an Item object from the data that’s stored in the database. This means you’ll need to create a class named Item that defines an item.

The records in the Items table consist of the following field names and data types:

ItemNumber ItemDescription Quantity UnitCost(String) (String) (Integer) (Double)

Enhancement

Modify the program so it displays the original record, uses an UPDATE statement to change the UnitCost value for the first record to $14.00, and then displays updated record.

Page 61: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 19-2 Maintain an inventory database

Operation To navigate through the inventory list, the user clicks on one of the buttons in the

bottom row.

To modify the quantity or unit cost of an item, the user makes a change to one of these fields. At that point, the Update button is enabled and the Add, Delete, and navigation buttons are disabled. Then, to save the changes to the database, the user clicks on the Update button.

To add a record to the database, the user clicks on the Add button. This clears the text fields, enables the Update button, and disables the Add, Delete, and navigation buttons. Then, the user enters the item number, description, quantity and price (with or without dollar sign) for a new item and clicks on the Update button to add the new record to the database.

To delete a record, the user navigates to that record and clicks the Delete button.

To exit from the program, the user clicks on the Exit button or closes the frame.

Specifications Model this project much like the Book Maintenance application described in chapter

19 of the book. To do this, you can create an Item class that defines an item. Plus, create an ItemDB class that performs all necessary database operations.

Create the frame using any layout that works (the GridBagLayout was used for the GUI shown here). If you did Project 18-3, you can use the same frame class, but modify it to use the ItemDB class rather than the ItemIO class.

Use the Inventory database located in the Student Projects\files\ch19 directory. If you didn’t do Project 19-1, you’ll need to create an ODBC data source named Inventory for the Inventory database. If your system doesn’t support the Access 2000 format, you can create an ODBC data source for the database named Inventory97, but still use Inventory as the name of the data source.

Page 62: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 19-2 Maintain an inventory database (continued) Since the currency format may use commas to separate larger numbers, you’ll have

to add code that parses a currency format to just numbers before an update can be made. To help with this, you can use the following code in the actionPerformed method for the Update button to remove dollar signs and commas:if (costString.charAt(0) == '$') costString = costString.substring(1);StringBuffer costSB = new StringBuffer(costString);for (int i = 0; i<costSB.length(); i++){ if (costSB.charAt(i) == ',') costSB.deleteCharAt(i); }costString = costSB.toString();double cost = Double.parseDouble(costString);

Enhancements Use prepared statements to modify, add, and delete records.

Page 63: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 20-1 Find a random number

Operation The application creates a random integer between 0 and 1000. Then, the application

creates four threads (thread0, thread1, thread2, and thread3) that search for the random number.

When the random number is found, the thread and the random number are printed to the console and the application terminates.

Specifications Each thread is to search a specified set of values as follows: thread0, 0-250; thread1,

250-500; thread2, 500-750; and thread3, 750-1000.

Create a class that implements the Runnable interface. This class is responsible for searching through a specified range of values. This means that this class should contain three instance variables for the specified number, the low range value, and the high range value. A constructor should accept three int values and initialize these instance variables.

The run method should use a for loop that checks each value in a range to match the specified value. If a match is made, a statement is printed to the console as shown above and the application terminates. Be sure that if a value isn’t found, the run method makes the current thread yield for others.

Page 64: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 20-2 Scrolling text in an applet

Operation A string is scrolled from right to left in an applet. When the string moves off the

screen completely, it starts scrolling again from right to left.

Specifications Extend the Applet class so users don’t have to download the Java Plug-in to run the

applet. Since the applet will contain a thread, it should implement the Runnable interface.

The applet class should contain instance variables for the x position of the string and the thread used to scroll the string. In addition, it may contain a few static variables for the string itself and the size of the applet.

The init method should set the background color to white. Then, the x and y positions of the string should be initialized. Since the string should start offscreen, you can start with an x position equal to the width of the applet + 10.

The start method of the applet class should include code that creates and starts the thread. The stop method of the applet class should stop the thread from executing.

The run method should repaint the screen every 75 milliseconds. To repaint the screen, you can override the paint method for the applet. Inside the paint method, set the font and color of the string. Decrement the x position about 3 pixels each time the paint method is called (every 75 milliseconds). If the x position is less than the negative of the string width, it should be reset to the width of the applet + 1. Use the FontMetrics class to determine the width of the static string.

The HTML page should include the heading “Scrolling Text” as shown in the screenshot above. Use a width of 240 and a height of 70 for the applet.

Enhancements Add a mouse listener to the applet that stops the applet when the mouse button is

pressed and the starts the applet when the mouse button is released.

Page 65: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 20-3 Create a timer with a warning signal

Operation The user enters a valid integer value for the number of minutes that the timer should

be set.

The application displays a warning dialog box when one minute is left.

The application displays a dialog box when time is up.

Specifications Use a dialog box to obtain the number of minutes. First, convert the string to an

integer value. Then, convert the integer value to the number of milliseconds as a long value. The formula for converting minutes to milliseconds is the number of minutes * 60 * 1000.

Create a class that contains two java.util.Timer instance variables for the warning timer and the end timer. A constructor of this class should accept a long value that represents the number of milliseconds that the end timer should be set to.

To determine the number of milliseconds for the warning timer, subtract 60 *1000 from the end time.

Page 66: Project - Tripodjclasses.tripod.com/java/Projects.doc  · Web viewThe application should print data like this to the console: Distance: 50 miles. Speed (mph) Time (mins) 55.0 54.54545454545455

Project 20-4 An animated Swing applet

Operation The applet displays numbers starting with 0 as shown above. Even numbers are

displayed in red and odd numbers are displayed in blue.

The applet stops executing when the user presses the mouse button and starts running again when the button is released.

Specifications Create an applet that extends the JApplet class. This class should contain instance

variables for a javax.swing.Timer and a JLabel object. It should also contain an instance variable that keeps track of the current number displayed.

The init method of the JApplet should initialize the label with a specified font (try SanSerif, bold, 24 pt) and color. A mouse listener should be added to the label to handle the mouse events. You can use an adapter class similar to the one on page 449 of the text, but override both the mouseReleased and mousePressed methods. These two methods should start and stop the timer.

Be sure to add the label to the content pane.

The actionPerformed method sets the text of the label accordingly.

The HTML page should be converted by the Java Plug-in HTML converter so it can be viewed within a browser.