Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
178 views

Programming Assinment Answer

The document provides solutions to two programming exercises: 1. It modifies an Account class to add a withdraw method that withdraws money from an account balance if the amount is less than or equal to the balance. 2. It creates an Invoice class to represent hardware store invoices. The class contains variables for part number, description, quantity, and price. Getters and setters are provided for each variable. A getInvoiceAmount method calculates the total invoice amount.

Uploaded by

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

Programming Assinment Answer

The document provides solutions to two programming exercises: 1. It modifies an Account class to add a withdraw method that withdraws money from an account balance if the amount is less than or equal to the balance. 2. It creates an Invoice class to represent hardware store invoices. The class contains variables for part number, description, quantity, and price. Getters and setters are provided for each variable. A getInvoiceAmount method calculates the total invoice amount.

Uploaded by

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

Chapter 3 Programming Assignment 1

1
Ex 3.11 (Modified Account Class) Modify class Account (Fig. 3.8) to provide a method called withdraw
that withdraws money from an Account. Ensure that the withdrawal amount does not exceed the
Account’s balance. If it does, the balance should be left unchanged and the method should print a
message indicating "Withdrawal amount exceeded account balance." Modify class AccountTest (Fig.
3.9) to test method withdraw.

Solution :

Account Class:

// Fig. 3.8: Account.java


// Account class with a double instance variable balance and a constructor
// and deposit method that perform validation.

public class Account {


private String name; // instance variable
private double balance; // instance variable

// Account constructor that receives two parameters


public Account(String name, double balance) {
this.name = name; // assign name to instance variable name

// validate that the balance is greater than 0.0; if it's not,


// instance variable balance keeps its default initial value of 0.0
if (balance > 0.0) // if the balance is valid
this.balance = balance; // assign it to instance variable balance
}

// method that deposits (adds) only a valid amount to the balance


public void deposit(double depositAmount) {
if (depositAmount > 0.0) // if the depositAmount is valid
balance = balance + depositAmount; // add it to the balance
}

public void withdraw(double withdrawAmount) {

if (withdrawAmount < balance) {


balance = balance - withdrawAmount;
} else {
System.out.println("Withdrawal amount exceeded account
balance.");
}
}

// method returns the account balance


public double getBalance() {
return this.balance;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}}
Account Test Class:

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Chapter 3 Programming Assignment 2

2
// Fig. 3.9: AccountTest.java
// Inputting and outputting floating-point numbers with Account objects.
import java.util.Scanner;

public class AccountTest {


public static void main(String[] args) {

Account account1 = new Account("Jane Green", 50.00);


Account account2 = new Account("John Blue", -7.53);

// display initial balance of each object


System.out.printf("%s balance: $%.2f%n", account1.getName(),
account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n", account2.getName(),
account2.getBalance());

// create a Scanner to obtain input from the command window


Scanner input = new Scanner(System.in);

System.out.print("Enter deposit amount for account1: "); // prompt


double depositAmount = input.nextDouble(); // obtain user input
System.out.printf("%nadding to account1 balance%n%n", depositAmount);
account1.deposit(depositAmount); // add to account1’s balance

// display balances
System.out.printf("%s balance: $%.2f%n", account1.getName(),
account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n", account2.getName(),
account2.getBalance());

System.out.print("Enter deposit amount for account2: "); // prompt


depositAmount = input.nextDouble(); // obtain user input
System.out.printf("%nadding to account2 balance%n%n", depositAmount);
account2.deposit(depositAmount); // add to account2 balance

// display balances
System.out.printf("%s balance: $%.2f%n", account1.getName(),
account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n", account2.getName(),
account2.getBalance());

// Withdraws amount from balance


System.out.print("Enter amount to be withdrawn for account1: "); //
prompt
double withdrawAmount1 = input.nextDouble(); // obtain user input
account1.withdraw(withdrawAmount1);// Account withdrawn from
Account1's

// display balances
System.out.printf("%s balance: $%.2f%n", account1.getName(),
account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n", account2.getName(),
account2.getBalance());

// Withdraws amount from balance


System.out.print("Enter amount to be withdrawn for account2: "); //
prompt
double withdrawAmount2 = input.nextDouble(); // obtain user input

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Chapter 3 Programming Assignment 3

3
account1.withdraw(withdrawAmount2);// Account withdrawn from
Account2's

// display balances
System.out.printf("%s balance: $%.2f%n", account1.getName(),
account1.getBalance());
System.out.printf("%s balance: $%.2f%n%n", account2.getName(),
account2.getBalance());

} // end main
} // end class AccountTest

Ex : 3.12 (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
Write a test app named InvoiceTest that demonstrates class Invoice’s capabilities.

Solution:

Invoice Class :

public class Invoice {

private String partNum;


private String partDescription;
private int itemQuantity;
private double pricePerItem;

public Invoice(String partNum, String partDescription,int itemQuantity, double


pricePerItem) {
this.partNum = partNum;
this.partDescription = partDescription;
this.itemQuantity = itemQuantity;
this.pricePerItem = pricePerItem;
}

public double getInvoiceAmount() {

if (this.pricePerItem < 0) {
return 0.0;
} else if (this.itemQuantity < 0) {
return 0;
} else {
return this.itemQuantity * this.pricePerItem;
}
}

public String getPartNumber() {


return partNum;

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Chapter 3 Programming Assignment 4

4
}

public void setPartNumber(String partNum) {


this.partNum = partNum;
}

public String getPartDescription() {


return partDescription;
}

public void setPartDescription(String partDescription) {


this.partDescription = partDescription;
}

public int getQuantityOfItem() {


return itemQuantity;
}

public void setQuantityOfItem(int itemQuantity) {


this.itemQuantity = itemQuantity;
}

public double getPricePerItem() {


return pricePerItem;
}

public void setPricePerItem(double pricePerItem) {


this.pricePerItem = pricePerItem;
}

Invoice Test class:

public class InvoiceTest {

public static void main(String[] args) {


// TODO Auto-generated method stub

Invoice invoice1 = new Invoice("IN1", "Gold", 100 , 500 );


Invoice invoice2 = new Invoice("IN2", "Silver", -100 , 700 );
Invoice invoice3 = new Invoice("IN3", "Platinum", 200 , -500 );

//Display Invoice Detail


System.out.println("Invoice 1 : \nPart Number : "+
invoice1.getPartNumber() +
"\nPart Description : "+ invoice1.getPartDescription()+
"\nQuantity Purchased : "+ invoice1.getQuantityOfItem()+
"\nPrice Per Item : "+ invoice1.getPricePerItem() +
"\nTotal Cost : " + invoice1.getInvoiceAmount());

System.out.println("\nInvoice 2 : \nPart Number : "+


invoice2.getPartNumber() +
"\nPart Description : "+ invoice2.getPartDescription()+
"\nQuantity Purchased : "+ invoice2.getQuantityOfItem()+

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Chapter 3 Programming Assignment 5

5
"\nPrice Per Item : "+ invoice2.getPricePerItem() +
"\nTotal Cost : " + invoice2.getInvoiceAmount());

System.out.println("\nInvoice 3 : \nPart Number : "+


invoice3.getPartNumber() +
"\nPart Description : "+ invoice3.getPartDescription()+
"\nQuantity Purchased : "+ invoice3.getQuantityOfItem()+
"\nPrice Per Item : "+ invoice3.getPricePerItem() +
"\nTotal Cost : " + invoice3.getInvoiceAmount());

Ex : 3.13 (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 app 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.

Solution:

Employee Class :

public class Employee {

private String firstName;


private String lastName;
private double monthlySalary;

public Employee(String firstName, String lastName, double monthlySalary) {


this.firstName = firstName;
this.lastName = lastName;
if (monthlySalary > 0) {
this.monthlySalary = monthlySalary;
}
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Chapter 3 Programming Assignment 6

public double getMonthlySalary() {


return monthlySalary;
}

public void setMonthlySalary(double monthlySalary) {


if(monthlySalary > 0) {
this.monthlySalary = monthlySalary;
}
}

Employee Test Class:

public class EmployeeTest {

public static void main(String[] args) {


// TODO Auto-generated method stub

Employee employee1 = new Employee("Nick", "Erwin", 300);


Employee employee2 = new Employee("Michael", "Ben", -150);

// Display Detail
System.out.println("\n" + employee1.getFirstName() + " " +
employee1.getLastName() + " Yearly Salary :"
+ (12 * employee1.getMonthlySalary()));
System.out.println("\n" + employee2.getFirstName() + " " +
employee2.getLastName() + " Yearly Salary :"
+ (12 * employee2.getMonthlySalary()));

// Salary hike for 10% yearly


double incrementedSalary1 = employee1.getMonthlySalary() +
employee1.getMonthlySalary() * 0.1;
employee1.setMonthlySalary(incrementedSalary1);

double incrementedSalary2 = employee2.getMonthlySalary()+


employee2.getMonthlySalary() * 0.1;
employee2.setMonthlySalary(incrementedSalary2);

// Display Detail
System.out.println("\n" + employee1.getFirstName() + " " +
employee1.getLastName() + " Yearly Salary :"
+ (12 * employee1.getMonthlySalary()));
System.out.println("\n" + employee2.getFirstName() + " " +
employee2.getLastName() + " Yearly Salary :"
+ (12 * employee2.getMonthlySalary()));
}
}

This study source was downloaded by 100000848206536 from CourseHero.com on 06-17-2022 06:07:48 GMT -05:00

https://www.coursehero.com/file/24520789/Chapter-3-Programming-Assignmentdocx/
Powered by TCPDF (www.tcpdf.org)

You might also like