Java How To Program 9th Edition Paul Deitel and Harvey Deitel ISBN-13: 978013257
ID: 3655287 • Letter: J
Question
Java How To Program 9th Edition Paul Deitel and Harvey Deitel ISBN-13: 9780132575669 Exercise 8.4 Create a class "Rectangle" with attributes "length" and "width", each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle. Exercise 8.6 Create class "SavingsAccount". Use a static variable "annualInterestRate" to store the annual interest rate for all account holders. Each object of the class contains a private instance variable "savingsBalance" indicating the amount the saver currently has on deposit. Provide method "calculateMonthlyInterest" to calculate the monthly interest by multiflying the "savingsBalance" by "annualInterestRate" divided by 12--this interest should be added to "savingsBalance". Provide a static method "modifyInterestRate" that sets the "annualInterestRate" to a new value. Write a program to test class "SavingsAccount". Instantiate two "savingsAccount" objects, "saver1" and "saver2", with balances of $2000.00 and $3000.00, respectively. Set "annualInterestRate" to 4%, then calculate the monthly interest for each of 12 months and print the new balances for both savers. Next set the "annualInterestRate" to 5%, calculate the next month's interest and print the new balances for both savers.Explanation / Answer
/**************Exercise 8.4****************/ class Rectangle { double length; double width; public double getLength() { return length; } public void setLength(double length) { if (length > 0.0 && length < 20.0) { this.length = length; } } public double getWidth() { return width; } public void setWidth(double width) { if (length > 0.0 && length < 20.0) { this.width = width; } } double calculatePerimeter() { return (2 * (getLength() + getWidth())); } double calculateArea() { return (getLength() * getWidth()); } Rectangle() { length = 1.0; width = 1.0; } } public class TestRectangle { public static void main(String[] args) { Rectangle rec = new Rectangle(); rec.setLength(19); rec.setWidth(19); System.out.println("Perimeter of rectangle is :" + rec.calculateArea()); System.out.println("Area of rectangle is :" + rec.calculatePerimeter()); } } /**************Exercise 8.6****************/ class SavingsAccount { static int annualInterestRate; double savingsBalance; double calculateMonthlyInterest() { return ((savingsBalance * annualInterestRate) / 12); } static void modifyInterestRate(int rate) { annualInterestRate = rate; } } public class TestSavingsAccount { public static void main(String[] args) { SavingsAccount saver1 = new SavingsAccount(); SavingsAccount saver2 = new SavingsAccount(); saver1.savingsBalance = 2000.00; saver2.savingsBalance = 3000.00; SavingsAccount.modifyInterestRate(4); System.out.println("Monthly interest for saver 1 is :" + saver1.calculateMonthlyInterest()); System.out.println("Monthly interest for saver 2 is :" + saver2.calculateMonthlyInterest()); System.out.println("12 Month interest for saver 1 is :" + (saver1.calculateMonthlyInterest()) * 12); System.out.println("12 Month interest for saver 2 is :" + (saver2.calculateMonthlyInterest()) * 12); saver1.savingsBalance = saver1.savingsBalance + (saver1.calculateMonthlyInterest() * 12); saver2.savingsBalance = saver2.savingsBalance + (saver2.calculateMonthlyInterest() * 12); System.out.println("New Saving Balance of saver 1 is :" + saver1.savingsBalance); System.out.println("New Saving Balance of saver 2 is :" + saver2.savingsBalance); SavingsAccount.modifyInterestRate(5); System.out.println("Next Month interest for saver 1 is :" + saver1.calculateMonthlyInterest()); System.out.println("Next Month interest for sever 2 is :" + saver2.calculateMonthlyInterest()); saver1.savingsBalance = saver1.savingsBalance + (saver1.calculateMonthlyInterest()); saver2.savingsBalance = saver2.savingsBalance + (saver2.calculateMonthlyInterest()); System.out.println("New Saving Balance of saver 1 is :" + saver1.savingsBalance); System.out.println("New Saving Balance of saver 2 is :" + saver2.savingsBalance); } } /*******You may also download the code from http://www.2shared.com/file/YC-GT3X6/New_Folder.html***************/Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.