Programming Assinment Answer
Programming Assinment Answer
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:
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;
// 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());
// 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());
// 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());
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 :
if (this.pricePerItem < 0) {
return 0.0;
} else if (this.itemQuantity < 0) {
return 0;
} else {
return this.itemQuantity * this.pricePerItem;
}
}
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
}
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());
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 :
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
// 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()));
// 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)