100% found this document useful (1 vote)
56 views4 pages

Exercises Java PartII 1

The document describes creating an Invoice class with instance variables for part number, description, quantity, and price. It includes a constructor to initialize the variables, getter and setter methods for each variable, and a method to calculate the invoice amount by multiplying quantity by price. A test application called InvoiceTest is to demonstrate the class functionality by creating Invoice objects and printing invoice details.

Uploaded by

Gurvinder Chahal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
56 views4 pages

Exercises Java PartII 1

The document describes creating an Invoice class with instance variables for part number, description, quantity, and price. It includes a constructor to initialize the variables, getter and setter methods for each variable, and a method to calculate the invoice amount by multiplying quantity by price. A test application called InvoiceTest is to demonstrate the class functionality by creating Invoice objects and printing invoice details.

Uploaded by

Gurvinder Chahal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercises#1

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 getInvoice Amount 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.0. Write a test application
named InvoiceTest that demonstrates class Invoice’s capabilities
ANS:-
public class Invoice{
private String partNumber;
private String partDescription;
private int quantity;
private double unitPrice;

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

setPartNumber(partNumber);
setDescription(partDescription);
setQuantity(quantity);
setUnitPrice(unitPrice);
}
// setters
public void setPartNumber(String partNumber){
this.partNumber = partNumber;
}
public void setDescription(String partDescription){
this.partDescription = partDescription;
}
public void setQuantity(int quantity){
this.quantity = (quantity < 0) ? 0 : quantity;
}
public void setUnitPrice(double unitPrice){
this.unitPrice = (unitPrice < 0.0) ? 0.0 : unitPrice;
}
// getters
public String getPartNumber(){
return partNumber;
}
public String getDescription(){
return partDescription;
}
public int getQuantity(){
return quantity;
}
public double getUnitPrice(){
return unitPrice;
}
public double getInvoiceAmount(){
return getQuantity() * getUnitPrice();
}
}

Exercises#2
Create a class called Employee that includes three pieces of information as
instance variables—a first name (typeString), a last name (typeString) and a
monthly salary (double). Your class should have 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, set it to 0.0. 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.

ANS:-
public class InvoiceTest{
public static void main(String[] args){
// testing positive values
Invoice spanner = new Invoice("123", "A Spanner", 15, 12.5);

printInvoice(spanner.getPartNumber(), spanner.getDescription(),
spanner.getQuantity(), spanner.getUnitPrice(),
spanner.getInvoiceAmount());
// testing negative values
Invoice hammer = new Invoice("124", "A Hammer", -1, -15.0);

printInvoice(hammer.getPartNumber(), hammer.getDescription(),
hammer.getQuantity(), hammer.getUnitPrice(),
hammer.getInvoiceAmount());

}
// print invoice
private static void printInvoice(String num, String desc,
int quan, double price, double total){
System.out.printf("%s: %s - %d * %.2f = %.2f\n", num, desc, quan, price,
total);
}
}

You might also like