PLEASE USE JAVA FOR THE FOLLOWING 1. (MyInteger class) Design a class named MyIn
ID: 664368 • Letter: P
Question
PLEASE USE JAVA FOR THE FOLLOWING
1. (MyInteger class)
Design a class named MyInteger. The class contains:
- An int data field named value that stores the int value represented by this object.
- A constructor that creates a MyInteger object for the specified int value.
- A get method that returns the int value.
- Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively.
- Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
- Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
- Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
- A static method parseInt(String) that converts a string to an int value. Implement the method without using Integer.parseInt(x).
Draw the UML diagram for the class. Implement the class. Write a client program that tests all methods in the class.
2. (The Account, CheckingAccount and SavingsAccount classes)
Design a class named Account that contains:
- A private int data field named id for the account (default 0).
- A private double data field named balance for the account (default 0).
- A private double data field named annualInterestRate that stores the current interest rate (default 0).
- A private java.util.Date data field named dateCreated that stores the date when the account was created.
- A no-arg constructor that creates a default account.
- A constructor that creates an account with a specified id and initial balance.
- The accessor and mutator methods for id, balance, and annualInterestRate.
- The accessor method for dateCreated.
- A method named getMonthlyInterestRate() that returns the monthly interest rate (annualInterestRate/12).
- A method named withDraw that withdraws a specified amount from the account. If there are not enough funds to withdraw an amount of money, print a statement saying that there are not enough money and leave the same balance in the account.
- A method named deposit that deposits a specified amount to the account.
Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.
Draw the UML diagram for these classes. Implement these classes. Write a test program that creates a CheckingAccount object with an account ID of 1122, a balance of 20000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2500, use the deposit method to deposit $3000, and print the balance, the monthly interest, and the date when this account was created.
Note: the SavingsAccount, and CheckingAccount subclasses must override the toString method of the superclass Account.
Explanation / Answer
Program : Client program to test all methods in the class
public class TestMyInteger {
public static void main(String[] args) {
MyInteger n1 = new MyInteger(5);
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("15 is prime? " + MyInteger.isPrime(15));
char[] chars = {'3', '5', '3', '9'};
System.out.println(MyInteger.parseInt(chars));
String s = "3539";
System.out.println(MyInteger.parseInt(s));
MyInteger n2 = new MyInteger(24);
System.out.println("Check n2 is odd? " + n2.isOdd());
System.out.println("Check 45 is odd? " + MyInteger.isOdd(45));
System.out.println("Check n1 is equal to n2? " + n1.equals(n2));
System.out.println("Check n1 is equal to 5? " + n1.equals(5));
}
}
class MyInteger {
int value;
MyInteger(int newValue) {
value = newValue;
}
public int getValue() {
return value;
}
public static boolean isEven(int n) {
return (n % 2 == 0);
}
public static boolean isOdd(int n) {
return !isEven(n);
}
public static boolean isPrime(int n) {
for (int f = 2; f < n / 2; f++) {
if (n % f == 0) {
return false;
}
}
return true;
}
public static boolean isEven(MyInteger n) {
return n.isEven();
}
public static boolean isOdd(MyInteger n) {
return n.isOdd();
}
public static boolean isPrime(MyInteger n) {
return n.isPrime();
}
public boolean isEven() {
return isEven(value);
}
public boolean isOdd() {
return isOdd(value);
}
public boolean isPrime() {
return isPrime(value);
}
public boolean equals(int n) {
return (value == n);
}
public boolean equals(MyInteger n) {
return equals(n.getValue());
}
public static int parseInt(String s) {
return Integer.parseInt(s);
}
public static int parseInt(char[] s) {
return parseInt(new String(s));
}
}
Program and UML diagram for the second question …. (The Account, Checking Account and Savings Account classes)
import java.util.Date;
public class AccProblem
{public static void main(String[] args)
{
Account acc1 = new Account(1122, 20000, .045);
acc1.withdraw(2500);
acc1.deposit(3000);
java.util.Date dateCreated = new java.util.Date()
System.out.println("Date Created:" + dateCreated);
System.out.println("Account ID:" + acc1.id);
System.out.println("Balance:" + acc1.getBalance());
System.out.println("Interest Rate:" + acc1.getAnnualInterestRate());
System.out.println("Balance after withdraw of 2500:" + acc1.getAnnualInterestRate());
System.out.println("Balance after deposit of 3000:" + acc1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + acc1.id);
System.out.println("Process completed.");
}
class Account {
//define variables
private int id;
private double balance; // balance for account
private double annualInterestRate; //stores the current interest rate
private Date dateCreated; //stores the date account created
//no arg construtor
Account () {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specific id and initial balance
Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor/mutator methods for id, balance, and annualInterestRate
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
//accessor method for dateCreated
public void setDateCreated(Date newDateCreated) {
dateCreated = newDateCreated;
}
//define method getMonthlyInterestRate
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//define method withdraw
double withdraw(double amount) {
return balance -= amount;
}
//define method deposit
double deposit(double amount) {
return balance += amount;
} } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.