Java_CH3_Exercise
Java_CH3_Exercise
1. (Invoice Class) Create a class called Invoice that a hardware store might use to represent
an invoice for an item sold at the store. An Invoice should include four pieces of information as
instance variables—a part number (type String), a part description (type String), a quantity of the
item being purchased (type int) and a price per item (double). Your class should have a constructor
that initializes the four instance variables. Provide a set and a get method for each instance variable.
In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e.,
multiplies the quantity by the price per item), then returns the amount as a double value. If the
quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.
Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
Program:
//Invoice.java
package CH_3;
public class Invoice{
private int partNumber;
private String partDescription;
private int quantity;
private double unitPrice;
//InvoiceTest.java
package CH_3;
public class InvoiceTest{
public static void main(String[] args){
Invoice saw = new Invoice(123, "Saw", 7, 32.5);
Invoice hammer = new Invoice(124, "Hammer", 18, 12.0);
saw.displayInfo();
hammer.displayInfo();
}
}
Output:
Invoice
Part Number: 123
Part Description: Saw
Quantity: 7
Unit Price: $32.50
Invoice Amount: $227.50
Invoice
Part Number: 124
2
Part Description: Hammer
Quantity: 18
Unit Price: $12.00
Invoice Amount: $216.00
2. (Employee Class) Create a class called Employee that includes three instance variables—a first
name (type String), a last name (type String) and a monthly salary (double). Provide a constructor
that initializes the three instance variables. Provide a set and a get method for each instance variable.
If the monthly salary is not positive, do not set its value. Write a test application named
EmployeeTest
that demonstrates class Employee’s capabilities. Create two Employee objects and display each
object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly
salary again.
Program:
//Employee.java
package CH_3;
public class Employee{
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String fName, String lName, double monthlySalary){
setFirstName(fName);
setLastName(lName);
setMonthlySalary(monthlySalary);
}
public void setFirstName(String fName){
firstName = fName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lName){
lastName = lName;
}
public String getLastName(){
return lastName;
}
public void setMonthlySalary(double salary){
if(salary > 0)
monthlySalary = salary;
}
public double getMonthlySalary(){
return monthlySalary;
}
public void setRaise(double percentage){
double increasedAmount = (monthlySalary / 100) * percentage;
3
double newSalary = monthlySalary + increasedAmount;
setMonthlySalary(newSalary);
}
//EmployeeTest.java
package CH_3;
public class EmployeeTest{
public static void main(String[] args){
employee1.displayInfo();
employee2.displayInfo();
}
}
Output:
Employee Info
First Name: Frank
Last Name: Freddy
Monthly Salary: $1000.00
Yearly Salary: $12000.00
Employee Info
First Name: Jack
4
Last Name: Jackson
Monthly Salary: $768.00
Yearly Salary: $9216.00
Employee Info
First Name: Frank
Last Name: Freddy
Monthly Salary: $1100.00
Yearly Salary: $13200.00
Employee Info
First Name: Jack
Last Name: Jackson
Monthly Salary: $844.80
Yearly Salary: $10137.60
3. (Date Class) Create a class called Date that includes three instance variables—a month (type
int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance
variables and assumes that the values provided are correct. Provide a set and a get method for each
instance variable. Provide a method displayDate that displays the month, day and year separated by
forward slashes (/). Write a test application named DateTest that demonstrates class Date’s
capabilities.
Program:
//Date.java
package CH_3;
public class Date{
private int month;
private int day;
private int year;
//DateTest.java
package CH_3;
import java.util.Scanner;
}
}
Output:
Enter Day: 29
Enter Month: 12
Enter Year: 2023
Date: 29/12/2023