Assignment 1
Part -1:
Design a Payroll class with the following fields:
- name : a String containing the employee's name
- idNumber: an int representing the employee's ID number
- rate: a double containing the employee's hourly pay rate
- hours: an int representing the number of hours this employee has worked
The class should also have the following methods :
- Constructor : takes the employee's name and ID number as arguments
- Accessors: allow access to all of the fields of the Payroll class
- Mutators: let the user assign values to the fields of the Payroll class
- grossPay: returns the employee's gross pay, which is calculated as the number of hours worked times the hourly pay rate.
Write another program that demonstrates the class by creating a Payroll object, then asking the user to enter the data for an employee in the order: name , ID number, rate, hours.
The program should then print out a statement in the following format (for example, if you had an employee named Chris Jacobsen with ID number 11111, who works for 5 hours at $10/hr):
Chris Jacobsen, employee number 11111, made $50.00 in gross pay.
Using text forming so that the gross pay is rounded to two decimal places.
Output:
Enter-employee's-name:Enter-employee's-ID-number:Enter-hourly-rate:Enter-number-of-hours-worked:Hermione-Granger,-employee-number-107548,-made-$4522.50-in-gross-pay.
Part -2:
Write a class named Car that has the following fields:
- yearModel: The yearModel field is an int that holds the car's year model.
- make: The make field is a String object that holds the make of the car.
- speed: The speed field is an int that holds the car's current speed.
In addition, the class should have the following methods :
- Constructor : The constructor should accept the car's year model and make as arguments. These values should be assigned to the object 's yearModel and make fields. The constructor should also assign 0 to the speed field.
- Accessor: The appropriate accessor methods should be implemented to access the values stored in the object 's yearModel, make, and speed fields.
- accelerate: The accelerate method should add 5 to the speed field when it is called.
- brake: The brake method should subtract 5 from the speed field each time it is called.
Demonstrate the class in a program that contains a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and print it on a separate line. Then, call the brake method five times, each time printing the current speed of the car on a separate line.
Part -3:
Area Class:
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes:
- circles
- rectangles
- cylinders
Here are the formulas for calculating the area of the shapes.
Area of a circle: Area = πr2
where p is Math.PI and r is the circle's radius
Area of a rectangle: Area = Width x Length
Area of a cylinder: Area = π r2 h
where p is Math.PI, r is the radius of the cylinder's base, and
h is the cylinder's height
Because the three methods are to be overloaded, they should each have the same name , but different parameter lists. Demonstrate the class in a complete program .
Output:
==-Area-Calculator-===
Enter-radius-to-calculate-circle-area:2.0
-Enter-width-to-calculate-rectangle-area:3.5
-Enter-length-to-calculate-rectangle-area:4.0
-Enter-base-radius-to-calculate-cylinder-area:8.0
-Enter-height-to-calculate-cylinder-area:5.7
-
--------
Results:
--------
The-area-of-the-circle-is:-12.57
The-area-of-the-rectangle-is:-14.00
The-area-of-the-cylinder-is:-1146.05
Part -4:
Make a LandTract class with the following fields:
- length - an int containing the tract's length
- width - an int containing the tract's width
The class should also have the following methods :
- area - returns an int representing the tract's area
- equals - takes another LandTract object as a parameter and returns a boolean saying
whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if the length of the first is the same as the width of the other and vice versa, that counts as having equal dimensions.)
- toString - returns a String with details about the LandTract object in the format:
LandTract object with length 30 and width 40
(If, for example, the LandTract object had a length of 30 and a width of 40.)
Write a separate program that asks the user to enter the dimensions for the two tracts of land (in the order length of the first, width of the first, length of the second, width of the second). The program should print the output of two tracts' toString methods followed by a sentence stating whether or not the tracts have equal dimensions. (If the tracts have the same dimensions, print, "The two tracts have the same size." Otherwise, print, "The two tracts do not have the same size.") Print all threestatements on separate lines.
SAMPLE RUN #1: java LandTract
Part -5:
Write a RainFall class that has the following field:
- an array of doubles that stores the rainfall for each of the 12 months of the year (where the first index corresponds with January, the second with February, etc.)
The class should also have the following methods :
- a method that returns the total rainfall for the entire year
- a method that returns the average monthly rainfall for the year
- a method that returns the month with the most rain as a string
- a method that returns the month with the least rain as a string
Demonstrate the class in a program that takes 12 doubles from the user (take the doubles in the order of the months of the year, the first corresponding to the rainfall in January, etc.). Do input validation: if the user inputs a negative number, ignore it and continue asking them for input until you have 12 nonnegative doubles .
Once the user has given you all 12 doubles , create an instance of the RainFall class and call its methods , printing out the total rainfall, the average monthly rainfall, the month with the most rain, and the month with the least rain, each on a separate line.
SAMPLE RUN #1: java RainFall
Part - 6:
In a program , write a method that accepts two arguments : an array of integers and a number n. The method should print all of the numbers in the array that are greater than the number n (in the order that they appear in the array , each on their own line).
In the same file, create a main method and call your function using the following data sets:
The array {1, 5, 10, 2, 4, -3, 6} and the number 3.
The array {10, 12, 15, 24} and the number 12.
SAMPLE RUN #0: java LargerThanN
Part - 7:
Design a class named Person with fields for holding a person's name, address, and telephone number (all as Strings ). Write a constructor that initializes all of these values , and mutator and accessor methods for every field.
Next, design a class named Customer, which inherits from the Person class . The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these values and the appropriate mutator and accessor methods for the class 's fields.
Demonstrate the Customer class in a program that prompts the user to enter values for the customer's name , address, phone number, and customer number, and then asks the user whether or not the customer wants to recieve mail. Use this information to create a customer object and then print its information.
Put all of your classes in the same file. To do this, do not declare them public.
Instead, simply write:
class Person { ... }
class Customer { ... }
SAMPLE RUN #1: java Driver
Part - 8:
A retail store has a preferred customer plan where customers can earn discounts on all their purchases. The amount of a customer's discount is determined by the amount of the customer's cumulative purchases in the store , as follows:
When a preferred customer spends $500, they get a 5% discount on all future purchases.
- When a preferred customer spends $1,000, they get a 6% discount on all future purchases.
- When a preferred customer spends $1,5000, they get a 7% discount on all future purchases.
- When a preferred customer spends $2,000, they get a 10% discount on all future purchases.
Design a class named PreferredCustomer, which inherits from the Customer class you created in Exercise 7. The preferredCustomer class should have int fields for the amount of the customer's purchases and the customer's discount level.
Write a constructor that initializes these fields as well as accessor and mutator methods.
Desmonstrate the class in a program that prompts the user to input the customer's name , address, phone number, customer number, whether they want to recieve mail, and the amount they've spent, and then uses that information to create a PreferredCustomer object and print its information. (Use string formatting to print the amount spent.)
Create all of the classes in the same file by not delcaring any of them public.
SAMPLE RUN #1: java Driver
Assignment 2
Amusement Park Programming Project
Project Outcomes
1. Use the Java selection constructs (if and if else).
2. Use the Java iteration constructs (while, do, for).
3. Use Boolean variables and expressions to control iterations.
4. Use arrays or ArrayList for storing objects.
5. Proper design techniques.
Project Requirements
Your job is to implement a simple amusement park information system that keeps track of admission tickets and merchandise in the gift shop. The information system consists of three classes including a class to model tickets, a class to model gift shop merchandise, the amusement park, and the amusement park tester. The gift shop supports access to specific merchandise in the park's gift shop and to purchase the merchandise or to order new merchandise for the gift shop. The UML diagram for each class (except the tester class) is given below.
1) Develop a simple class that models admission tickets. Each admission is described by several instance fields:
a. A ticket number as a long integer to identify the unique ticket,
b. A ticket category represented as a String to store the category of the ticket (i.e. adult, child, senior),
c. A ticket holder represented as a String to store the name of the person who purchased the ticket,
d. A date represented as a Date to store the admission date for the ticket,
e. A price represented as a double to store the price of the ticket,
f. A purchase status represented as a boolean to indicate if the ticket has been purchased (or is reserved).
Ticket
|
-number : long
-category : String
-holder : String
-date : Date
-price : double
|
In addition to these fields, the class has the following constructors and methods:
a. A parameterized constructor that initializes the attributes of a ticket.
b. setPrice(double price) to change the price of a textbook.
c. changePurchaseStatus(boolean newStatus) to change the purchase status of the ticket.
d. Accessor methods for all instance fields.
e. toString() to return a neatly formatted string that contains all the information stored in the instance fields.
2) Develop a simple class that models merchandise available in the gift shop such as t-shirts, sweatshirts, and stuffed animals. The class has several instance fields:
a. An ID as a long integer to identify the specific merchandise item,
b. A category as a String to store the specific type of merchandise,
c. A description as a String to store the description of the merchandise,
d. A price represented as a double to store the price of the merchandise,
e. An instock as a boolean to indicate if the merchandise is instock or on- order.
Valid values for category include "T-Shirt", "Sweatshirt", and "Stuffed Animal", as well as any additional category you choose to support. If invalid values are entered, an error message must be printed and the category instance field must be set to "UNKNOWN".
In addition to these attributes, the class has the following constructors and methods:
f. A parameterized constructor that initializes the attributes of a merchandise item.
g. setPrice(double price) to change the price of the merchandise.
h. setInstock(boolean newStatus) to change the status of the merchandise item.
i. Accessor methods for all instance fields.
j. toString() to return a neatly formatted string that contains all the information stored in the instance fields.
Merchandise
|
-id : long
-category : String
-description : String
-price : double
-inStock : boolean
|
+Merchandise(String, String, String, double, boolean)
+setPrice(double)
+setInstock(boolean)
+getId() : String
+getCategory() : String
+getDescription() : String
+getPrice() : double
+getInstock() : boolean
+toString() : String
|
3) Develop class AmusementPark that keeps track of tickets and gift shop inventory. The AmusementPark uses two ArrayLists to store Ticket and Merchandise objects. The AmusementPark provides several methods to add merchandise to the gift shop and to access merchandise. The following UML diagram describes the class, the constructor, and the methods:
AmusementPark
|
-tickets : ArrayList
-merchandise : ArrayList
-name : String
|
+AmusementPark(String)
+getName() : String
+getTicketDates() : ArrayList
+getTickets(Date date) : int
+getTicket(long id) : Ticket
+getMerchandise() : ArrayList
+getMerchandise(String category) : ArrayList
+getMerchandise(long id) : Merchandise
+addTicket(Ticket)
+addMerchandise(Merchandise)
+buyMerchandise(String id)
+buyTicket(String id)
|
a. The class has three instance fields:
a. name, the name of the bookstore
b. tickets, an ArrayList storing Ticket objects
c. merchandise, an ArrayList storing
Merchandise objects
b. getName() returns the name of the bookstore.
c. getTicketDates() returns an ArrayList of all the dates for which tickets are still available. If there are no tickets available, an empty list is returned.
d. getTickets (Date date) returns an integer indicating the number of tickets available for the specified date.
e. getTicket(long id) returns the Ticket that matches the specified id. If there is no Ticket matching the given id, null is returned.
f. getMerchandise()returns an ArrayList of all the inventory (in-stock and ordered). This method must create a separate copy of the ArrayList before it returns the list. If there are no merchandise items in the AmusementPark, an empty list is returned.
g. getMerchandise(String category) returns a list of Merchandise objects whose category matches the specified category. For example, if called with "T-shirt" the method returns all Merchandise objects with the category "T-shirt" as a new list. This method must create a new copy of an ArrayList that stores all the matched Merchandise objects. If no items in the AmusementPark match the given name, an empty list is returned.
h. getMerchandise(long id) returns the merchandise item that matches the specified id. If there is no merchandise item matching the given id, null is returned.
i. addTicket(Ticket) adds a new Ticket to the inventory of the
AmusementPark.
j. addMerchandise(Merchandise) adds a new Merchandise to the inventory of the AmusementPark.
k. buyMerchandise(String id) removes a Merchandise object from the list of merchandise of the AmusementPark. If the id does not match any Merchandise object in the list, an exception is thrown.
l. buyTicket(String id) removes a Ticket object from the list of ticket items of the AmusementPark. If the id does not match any Ticket object in the list, an exception is thrown.
4) Design a tester class called AmusementParkTester. The tester class has a main() method and tests the functionality of the class AmusementPark as follows:
a. Create AmusementPark and name it "Walden Amusement Park".
b. Create a minimum of three Ticket objects and add them to the bookstore.
c. Create Apparel objects, at least two of each category, and add them to the AmusementPark.
d. Set up a loop to:
i. Display a short menu that allows a user to perform different actions in the gift shop such as looking up tickets or merchandise or purchasing items. Use all of the accessor methods in the AmusementPark to access specific items. Use the given methods to make purchases.
ii. Prompt the user for a specific action.
iii. Depending on the specific action prompt the user for additional input such as the id of a ticket or merchandise category, etc. You might want to use static methods in main() to handle each menu item separately.
iv. Perform the action and display results such as the list of merchandise that the user has requested. Use the toString() method to display AmusementPark items on the screen.
v. Prompt the user for continued access to the AmusementPark or to end the program.
Your program should handle input errors gracefully. For example, if a particular ticket is searched and not found, the program should display a message such as "Selected ticket not found." Feel free to experiment with the tester program in order to develop a more useful program.Retail Transaction Programming Project
Project Requirements:
1. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing.
The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the Transaction class.
2. Design and build a LineItem class. This class will have three instance variables. There will be an itemName variable that will hold the identification of the line item (such as, "Colgate Toothpaste"); a quantity variable that will hold the quantity of the item being purchased; and a price variable that will hold the retail price of the item. The LineItem class should have a constructor, accessors for the instance variables, a method to compute the total price for the line item, a method to update the quantity, and a method to convert the state of the object to a string. Using Unified Modeling Language (UML), the class diagram looks like this:
LineItem
|
- itemName : String
|
- quantity : int
|
- price : double
|
+ LineItem( String, int, double )
|
+ getName( ) : String
|
+ getQuantity( ) : int
|
+ getPrice( ) : double
|
+ getTotalPrice( ) : double
|
+ setQuantity( int )
|
+ setPrice( double )
|
+ toString( ) : String
|
a. The constructor will assign the first parameter to the instance variable itemName, the second parameter to the instance variable quantity, and the third parameter to the instance variable price.
b. The class will have three accessor methods-getName( ), getQuantity( ), and getPrice( )-that will return the value of each respective instance variable.
c. The class will have two mutator methods, setQuantity( int ) and setPrice( double ), that will update the quantity and price, respectively, of the item associated with the line of the transaction.
d. The method getTotalPrice( ) handles the conversion of the quantity and price into a total price for the line item.
e. The method toString( ) allows access to the state of the object in a printable or readable form. It converts the variables to a single string that is neatly formatted.
Note: Refer to the textbook for a discussion of escape sequences. These are characters that can be inserted into strings and, when printed, will format the display neatly. An escape sequence for the tab character can be inserted to get a tabular form when printing. This tab character is "\t".
The LineItem class will have a toString( ) method that concatenates itemName, quantity, price, and total price-separated by tab characters-and returns this new string. When printing an object, the toString( ) method will be implicitly called, which in this case, will print a string that will look something like:
Colgate Toothpaste qty 2 @ $2.99 $5.98
3. Build a Transaction class that will store information about the items being purchased in a single transaction. It should include a customerID and customerName. It should also include an ArrayList to hold information about each item that the customer is purchasing as part of the transaction.
Note: You must use an ArrayList, not an array.
4. Build a TransactionTest class to test the application. The test class should not require any interaction with the user. It should verify the correct operation of the constructor and all methods in the Transaction class.
Specific Requirements for the Transaction Class
1. The Transaction class should have a constructor with two parameters. The first is an integer containing the customer's ID and the second is a String containing the customer's name.
2. There should be a method to allow the addition of a line item to the transcript. The three parameters for the addLineItem method will be (1) the item name, (2) the quantity, and (3) the single item price.
3. There should be a method to allow the updating of a line item already in the transaction. Notice that updating an item means changing the quantity or price (or both). The parameters for the updateItem method are also (1) the item name,
(2) the quantity, and (3) the single item price. Notice that the updating of a
specific line item requires a search through the ArrayList to find the desired item. Anytime a search is done, the possibility exists that the search will be unsuccessful. It is often difficult to decide what action should be taken when such an "exception" occurs. Since exception handling is not covered until later in this textbook, make some arbitrary decisions for this project. If the item to be updated is not found, take the simplest action possible and do nothing. Do not print an error message to the screen. Simply leave the transaction unchanged.
4. The transaction class needs a method called getTotalPrice to return the total price of the transaction.
5. There should also be a method to return information about a specific line item. It should return a single String object in the same format described for the LineItem class:
Colgate Toothpaste qty 2 @ $2.99 $5.98
Again, the possibility exists that the search for a specific line item will fail. In this instance, you should return a string containing a message similar to this:
Colgate Toothpaste not found.
6. The final method needed is a toString method. It should return the transaction information in a single String object. It should use the following format:
Customer ID : 12345 Customer Name : John
Doe
Colgate Toothpaste qty 2 @ $2.99 $5.98
Bounty Paper Towels qty 1 @ $1.49 $1.49
Kleenex Tissue qty 1 @ $2.49 $2.49
Transaction Total $9.96
Notice that a newline character "\n" can be inserted into the middle of a string. Ex.
int age = 30;
String temp = "John Doe \n is " + age + "\n" + " years old";
The output would be:
John Doe is 30 years old
Notice also that "\n" is a single character and could actually go inside single or double quotes, depending on the circumstances.
Here is a UML diagram for the Transaction class as described above. Notice that private instance variables and methods may be added, as needed. For all public methods use exactly the name given below.
Transaction
|
- lineItems : ArrayList
|
- customerID : int
|
- customerName : String
|
+ Transaction( int, String )
|
+ addLineItem( String, int, double )
|
+ updateItem( String, int, double )
|
+ getTotalPrice( ) : double
|
+ getLineItem( String ) : String
|
+ toString( ) : String
|
I am expecting a better deal this time around.