0% found this document useful (0 votes)
18 views

Java_CH3_Exercise

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java_CH3_Exercise

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter 3

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;

public Invoice(int partNumber, String partDescription, int quantity, double unitPrice){


setPartNumber(partNumber);
setDescription(partDescription);
setQuantity(quantity);
setUnitPrice(unitPrice);
}
public void setPartNumber(int num){
partNumber = num;
}
public void setDescription(String des){
partDescription = des;
}
public void setQuantity(int count){
if(count > 0){
quantity = count;
}else{
quantity = 0;
}
}
public void setUnitPrice(double price){
if(price > 0){
unitPrice = price;
}else{
unitPrice = 0;
}
}
public int getPartNumber(){
1
return partNumber;
}
public String getDescription(){
return partDescription;
}

public int getQuantity(){


return quantity;
}
public double getUnitPrice(){
return unitPrice;
}
public double getInvoiceAmount(){
return getQuantity() * getUnitPrice();
}
public void displayInfo() {
System.out.println("Invoice");
System.out.printf("Part Number: %d \n",partNumber);
System.out.printf("Part Description: %s \n",partDescription);
System.out.printf("Quantity: %d \n",quantity);
System.out.printf("Unit Price: $%.2f \n",unitPrice);
System.out.printf("Invoice Amount: $%.2f \n\n",getInvoiceAmount());
}
}

//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);
}

public double getYearlySalary(){


return getMonthlySalary() * 12;
}
public void displayInfo(){
System.out.println("Employee Info");
System.out.printf("First Name: %s \n",firstName);
System.out.printf("Last Name: %s \n",lastName);
System.out.printf("Monthly Salary: $%.2f \n",monthlySalary);
System.out.printf("Yearly Salary: $%.2f \n\n",getYearlySalary());
}

//EmployeeTest.java
package CH_3;
public class EmployeeTest{
public static void main(String[] args){

Employee employee1 = new Employee("Frank", "Freddy", 1000);


employee1.displayInfo();

Employee employee2 = new Employee("Jack", "Jackson", 768);


employee2.displayInfo();

System.out.println("\nAfter 10% raises:\n");


employee1.setRaise(10);
employee2.setRaise(10);

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

After 10% raises:

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;

public Date(int month, int day, int year){


setMonth(month);
setDay(day);
setYear(year);
}
public void setMonth(int value){
month = value;
}
public int getMonth(){
return month;
}

public void setDay(int value){


day = value;
5
}

public int getDay(){


return day;
}
public void setYear(int value){
year = value;
}
public int getYear(){
return year;
}
// display date
public void displayDate(){
System.out.printf("\nDate: %d/%d/%d", getMonth(), getDay(), getYear());
}
}

//DateTest.java
package CH_3;
import java.util.Scanner;

public class DateTest{


public static void main(String[] args){
Scanner input = new Scanner(System.in);

System.out.print("Enter Day: ");


int day = input.nextInt();
System.out.print("Enter Month: ");
int month = input.nextInt();
System.out.print("Enter Year: ");
int year = input.nextInt();

Date date = new Date(day,month,year);


date.displayDate();

}
}

Output:
Enter Day: 29
Enter Month: 12
Enter Year: 2023
Date: 29/12/2023

You might also like