Question

In: Computer Science

In-class exercise 2 Objective and Overview: The exercises in this document is on Lecture 3 Exercise...

In-class exercise 2

Objective and Overview:

The exercises in this document is on Lecture 3

Exercise 1:

(The Account class) Design a class named Account that contains:

1. A private int data field named id for the account (default 0).

2. A private double data field named balance for the account (default 0).

3. A private static double data field named annualInterestRate that stores the current interest rate (default 0). Assume that all accounts have the same interest rate.

4. A private static int data field named transactions, that stores the number of transactions for all accounts.

5. A no-arg (default) constructor that creates a default account.

6. An initialization constructor that creates an account with the specified id and initial balance.

7. A copy constructor that creates an Account and initializes its attribute to the values of another account.

8. The accessor and mutator methods for id, balance, annualInterestRate, and transactions. 9. A method named getMonthlyInterestRate() that returns the monthly interest rate.

10. A method named getMonthlyInterest() that returns the monthly interest.

11. A method named withdraw that withdraws a specified amount from the account.

12. A method named deposit that deposits a specified amount to the account.

13. a toString() method to display all account details and the number of transactions.

Draw the UML diagram for the class then implement the class.

(Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate.

monthlyInterestRate is annualInterestRate / 12.

Note annualInterestRate is a percentage, for example 4.5%. You need to divide it by 100.)

Solutions

Expert Solution

Account.java

public class Account {
private int id;
private double balance;
private static double annualInterestRate;
private static int nTransactions;
  
public Account()
{
this.id = 0;
this.balance = 0;
Account.annualInterestRate = 0;
}

public Account(int id, double balance, double annualInterestRate)
{
this.id = id;
this.balance = balance;
Account.annualInterestRate = annualInterestRate;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getAnnualInterestRate() {
return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}

public static int getnTransactions() {
return nTransactions;
}

public static void setnTransactions(int nTransactions) {
Account.nTransactions = nTransactions;
}
  
public Account(Account account)
{
this.id = account.getId();
this.balance = account.getBalance();
Account.annualInterestRate = account.getAnnualInterestRate();
}
  
public double getMonthlyInterest()
{
return((getAnnualInterestRate() / 1200) * this.balance);
}
  
public void deposit(double amount)
{
if(amount > 0)
{
setBalance(getBalance() + amount);
nTransactions++;
}
}
  
public void withdraw(double amount)
{
if(amount > getBalance() || (getBalance() - amount) < 0)
System.out.println("Transaction failed due to insuffiecient funds!");
else
{
setBalance(getBalance() - amount);
nTransactions++;
}
}
  
@Override
public String toString()
{
return("ID: " + getId() + "\n"
+ "Interest Rate: " + getAnnualInterestRate() + "%\n"
+ "Balance: $" + String.format("%.2f", getBalance()) + "\n"
+ "Monthly Interest: $" + String.format("%.2f", getMonthlyInterest()) + "\n"
+ "Number of transactions so far: " + nTransactions);
}
}

AccountTest.java (Driver class)

public class AccountTest {
  
public static void main(String[] args)
{
Account acc = new Account(389, 5000, 4.5);
  
System.out.println("A new account created with Id: " + acc.getId() + "...\n" + acc.toString());
  
System.out.println("\nDeposited $100...");
acc.deposit(100);
System.out.println(acc.toString());
  
System.out.println("\nWithdraw $6100...");
acc.withdraw(6100);
System.out.println(acc.toString());
  
System.out.println("\nWithdraw $4100...");
acc.withdraw(4100);
System.out.println(acc.toString());
  
System.out.println("\nDeposited $1500...");
acc.deposit(1500);
System.out.println(acc.toString());
}
}

********************************************************************* SCREENSHOT ******************************************************

UML Diagram:


Related Solutions

Exercise 1: Write an XML document describing the exercises in this document: the root element is...
Exercise 1: Write an XML document describing the exercises in this document: the root element is <exercises>. The root has an attribute number that has value 1. The root element has three child elements; <date> that contains as text the date of the exercise, and two <item> elements for the first two exercises (1--2). Write some text in the <item> elements. Exercise 2: Write an XML document describing a person: name, occupation, address and hobbies. Please do not use your...
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic...
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic and the gates and circuits that embody it. A thorough understanding of boolean logic is extremely useful for almost all programming tasks and necessary to correctly implement complex systems. Additionally, greater familiarity with boolean logic should improve one’s ability to think critically in general as it forms the basis for all human reasoning. Technical Description and Instructions: 1. Consider the logical expression ( !x...
Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing)...
Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class to model books and the author that wrote it. Note the relationship that a book has an author. At this point, you should put all of your classes and other code in the same file. Don't worry about separating it out into separate modules. Instructions Write...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
      Design an algorithm for each of the exercises and type it into a word document....
      Design an algorithm for each of the exercises and type it into a word document. Upload the document to PE1 assignment folder. The roots of quadratic equation: Design an algorithm to find the real roots of a quadratic equation of the form ax2+bx+c=0, where a, b, c are all real numbers and a is nonzero
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment...
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2) 5. ssign the name of the web browser and operating system of the...
research the Caesar Cipher, and document your research in 2-3 pages in a Word document with...
research the Caesar Cipher, and document your research in 2-3 pages in a Word document with illustrations in your document and provide an example of how the Caesar Cipher works while encrypting and decrypting a sample message. Also, add its history and its internal workings. Summary of the areas to complete: - Research the Caesar Cipher. - Show its Encryption Method in details. - Show its Decryption Method in details. - Describe its History and what it was used for....
Objective The objective of this lab exercise will be for the student to gain experience conducting...
Objective The objective of this lab exercise will be for the student to gain experience conducting a wireless networking site survey. The objective is for each group to propose a wireless networking solution for a given space (such as a classroom, office rooms, home, etc.). Verify that you can establish link (by pinging) with another laptop in your network. (Also, you may demonstrate this by downloading the shared file on another computer in the same network). Required Each individual/group will...
Provide the history, overview and objective of the standard that provides guidance on the measurement and...
Provide the history, overview and objective of the standard that provides guidance on the measurement and recognition of inventory.
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT