Deliverables: (1) Souce code file for class; (2) source code file that creates o
ID: 662672 • Letter: D
Question
Deliverables: (1) Souce code file for class; (2) source code file that creates objects and tests the class.
Goal: Design, implement, and test a user defined class similar to the Person or SavingsAccount classes.
Examples of Classes to Use for your Project: AirplanePassenger, Baby, CatalogItem, DriversLicense, GroceryItem, InsurancePolicy, LibraryBook, LibraryCD, LibraryPatron, Pet, Store, Student.
UML Diagram: Submit a UML diagram for your class. You can include the UML diagram as comments at the top of your class code (use typewriter graphics if you do this), or in a separate file.
Example Classes: Do not use these classes that are posted as examples on the class website or discussed in the textbook: Person, SavingsAccount, Coin, Automobile, BabyDragon.
Design Specifications: For full credit, your class should have
An initialize method.
At least four fields, including at least one read-only field and at least one read/write field.
A to_s method.
At least one method of your choice, in addition to the initialize, to_s methods and the getters and setters.
Code Specifications: Put the class code and test code in separate files.
Write explicit getter and setter methods.
Include source code comments.
Indent your source code properly. Each indentation should be 2 spaces.
Testing Specifications:
Create an array that contains at least three objects.
Call each method of your class to test it.
Call the to_yaml method to convert the array of objects to a YAML string. You don't need to save the YAML string to an output file. To call use the to_yaml method, you need
require 'yaml'
at the top of your test script. If the name of your object array is objects, print the YAML string of the objects like this:
print objects.to_yaml, " "
i am going to post the example of what i need if anyone can help would be great.
UML diagram for SavingsAccount Class using Typewriter graphics
+-------------------------------------------+
| SavingsAccount |
+-------------------------------------------+
| - @account_number: Fixnum |
| - @cust_name: String |
| - @balance: Float |
| - @interest_rate: Float |
+-------------------------------------------+
| - initialize(the_account_number: Fixnum, |
| the_cust_name: String, |
| the_interest_rateL Float) |
| + C new(the_account_number: Fixnum, |
| the_cust_name: String, |
| the_interest_rateL Float) |
| + account_number : Fixnum |
| + cust_name : String |
| + balance : Float |
| + interest_rate : Float |
| + interest_rate=(value: Float) |
| + withdraw(amount: Float) : Float |
| + deposit(amount : Float) : Float |
| + add_interest(the_interest: Float) |
| + to_s : String |
+-------------------------------------------+
Savings Account Class example code.
# SavingsAccount Example
# Source code in /classes/SavingsAccount.rb
class SavingsAccount
# This initialize method is automatically called when the user
# creates a new object using the class new method.
def initialize(the_account_number, the_cust_name, the_interest_rate)
@account_number = the_account_number
@cust_name = the_cust_name
@interest_rate = the_interest_rate
@balance = 0.0
end
# Getter for @account_number
def account_number
return @account_number
end
# Getter for @cust_name
def cust_name
return @cust_name
end
# Getter for @balance
def balance
return @balance
end
# Getter for @interest_rate
def interest_rate
return @interest_rate
end
# Setter for @interest_rate
def interest_rate=(value)
@interest_rate = value
end
# Only withdraw the amount passed in of there is enough
# money in the account to cover it.
def withdraw(the_amount)
if the_amount <= @balance
@balance -= the_amount
return the_amount
else
ret_val = @balance
@balance = 0.0
return ret_val
end
end
# Deposit the indicated amount in the account
def deposit(the_amount)
@balance += the_amount
return the_amount
end
# Compute interest and add to balance.
def add_interest
@balance += @balance * @interest_rate * 0.01
end
# Print out values of all instance variables.
def to_s
return "#{account_number} #{cust_name} #{balance} #{interest_rate}"
end
end
Test code of the saving account class
# Savings Account Example
# Source code in /classes/TestSavingsAccount.rb
require "./savings-account.rb"
acct = SavingsAccount.new(235434, "Steve", 0.75)
print "Account Number: #{acct.account_number} "
print "Customer Name: #{acct.cust_name} "
print "Balance: #{acct.balance} "
print "Interest Rate: #{acct.interest_rate} "
print acct, " "
acct.deposit(5000.0)
print acct.balance, " "
print acct.withdraw(3000.0), " "
print acct.withdraw(3000.0), " "
print acct.balance, " "
acct.deposit(5000.0)
acct.add_interest
print acct.balance, " "
Explanation / Answer
public class SavingAcc
{
private double balance;
private String customer_name;
private final double account_number;
private double interest_rate;
public double getBalance() {
return balance;
}
public String getCustomer_name() {
return customer_name;
}
public double getInterest_rate() {
return interest_rate;
}
public void setInterest_rate(double interest_rate) {
this.interest_rate = interest_rate;
}
public double getAccount_number() {
return account_number;
}
public SavingAcc(double accountNumber, String customerName , double intersetRate)
{
balance = 0;
customer_name = customerName;
account_number = accountNumber;
interest_rate = intersetRate;
}
public double withdraw(double amount)
{
if(amount <= balance )
{
balance = balance - amount;
return amount;
}
else
{
amount =
balance ;
balance =0;
System.
out.println("Low Balance");
return amount;
}
}
public double deposit(double amount)
{
balance = balance + amount;
return amount;
}
public void add_interest()
{
balance = balance * interest_rate * 0.01;
}
public void display()
{
System.
out.println("ACCOUNT INFORMATION");
System.
out.println("____________________");
System.
out.println("Account Number is :" + account_number);
System.
out.println("Customer Name :" + customer_name );
System.
out.println("Balance is :" + balance);
System.
out.println("Interset Rate :" + interest_rate);
}
}
For testing:
public class SavingAccountTest {
public static void main(String[] args)
{
SavingAcc acct =
new SavingAcc(235434,"Steve",0.75);
System.
out.println(acct.getAccount_number());
System.
out.println(acct.getCustomer_name());
System.
out.println(acct.getBalance());
System.
out.println(acct.getInterest_rate());
System.
out.println(" ");
acct.deposit(5000.0);
System.
out.println(acct.getBalance());
acct.withdraw(3000.0);
acct.withdraw(3000.0);
System.
out.println(acct.getBalance());
acct.deposit(5000);
System.
out.println(" ");
acct.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.