Programming Assignment 02 Overview We are building on the start made in PA01 to
ID: 3706429 • Letter: P
Question
Programming Assignment 02
Overview
We are building on the start made in PA01 to continue building the reporting system for our investment management company. In this assignment, you will build a basic framework for the securities management portion of the system.
Startup Materials
The following have been provided to you:
• These instructions
• UML Class Diagrams for:
o Security
o MutualFund
o Stock
o CostBasis (interface)
• A test harness for use in testing your code: Test_SecurityHierarchy.java
• Sample expected output results
Requirements
CostBasis - Create the CostBasis interface according to the requirements in the UML. The interface will be used in the classes of the Security hierarchy to calculate the cost basis for a given investment, according to the instructions provided in the individual class diagrams.
Security - Create the Security class according to the requirements in the UML Class Diagram. Note that the UML Class Diagram provided does not include required standard methods – these should be included at all times as defaults. Naming for such methods MUST follow standard naming conventions. All classes in the Security hierarchy must implement the CostBasis interface.
MutualFund - Create the MutualFund class according to the requirements in the UML Class Diagram. Note that the UML Class Diagram provided does not include required standard methods – these should be included at all times as defaults. Naming for such methods MUST follow standard naming conventions. The MutualFund class must provide concrete instructions for implementation of the CostBasis interface.
Stock - Create the Stock class according to the requirements in the UML Class Diagram. Note that the UML Class Diagram provided does not include required standard methods – these should be included at all times as defaults. Naming for such methods MUST follow standard naming conventions. The Stock class must provide concrete instructions for implementation of the CostBasis interface.
/**
* This is a SIMPLE test harness for the Security class.
*
* Keyboard input is accepted from the user and then used to instantiate a Security object.
* Once an object has been instantiated, it will be stored in an array.
* An enhanced for loop will then be used to output information about all objects in the array.
*/
import java.util.Scanner; // to accept keboard input
public class Test_SecurityHierarchy
{
private static int limit = 8;
private static Security[] custArray = new Security[ limit ]; // declared globally for simplicity
public static void main(String[] args)
{
loadArray( );
outputSecuritys( );
} // end main
private static void loadArray( )
{
/************************************************************************************************************
* The first part of this method is simply 'set up' for the real work to be done later.
* We will:
* > Instantiate the Scanner here to accept keyboard input
* > Create a 'holding' space for the Security object(s) to be isntantiated
* > Declare variables to hold the user responses
* > Declare a variable to use in controlling the input loop
************************************************************************************************************/
Scanner input = new Scanner( System.in ); // instantiate Scanner for use; tie to keyboard
Security newSecurity; // "holding reference" for the Security object
// Both Stock AND MutualFund objects 'fit' here
/*
* Create prompts for use in input loop
*/
// Aks what type of object
String askObjType = "Do you wish to enter a Stock (1) or a MutualFund (2) ?: ";
// Security-level prompts (for SUPERclass instance values)
String askCustNumber = "Enter a Customer Number as 7 digits: ";
String askPurchDt = "Enter a purchase date in yyyyddd (Julian) format: ";
String askPurchPrc = "Enter the purchase price per share: ";
String askShares = "Enter the number of shares purchased: ";
String askSymbol = "Enter the symbol for the Security: ";
// MutualFund prompts (for SUBclass instance values)
String askType = "What type of Mutual Fund is this?: ";
String askAdmin = "What is the administrative cost cap? Enter as cents per dollar: ";
String askRptPeriod = "What is the reporting Period? Enter A (annual), Q (quarterly), or M (monthly): ";
String askMgmt = "Is this fund actively (true) or passively (false) managed?: ";
// Stock prompts (for SUBclass instance values)
String askExchg = "On which exchange is this Stock traded?: ";
String askDividends = "Does this Stock pay dividends? (true or false): ";
String askDivDate = "In which quarter are dividends paid? Enter 0 if no dividends: ";
String askDivAmount = "What is the expectged dividend per share? Enter 0 if no dividends: ";
/*
* Declare variables to hold the user responses
*/
// to hold SUPERclass instance values
String custNumber;
int purchDt;
double purchPrc;
double shares;
String symbol;
//==================
// to hold SUBclass instance values
String type;
double admin;
char rptPeriod;
boolean mgmt;
//==================
// to hold SUBclass instance values
String exchange;
boolean dividends;
int divDate;
double divAmount;
// Declare a variable to test type of object
int objType = 0;
// Declare a loop control variable
int countObjects = 0;
/************************************************************************************************************
* The data input loop will continue until the array has been filled.
*
* Each prompt is offered in turn; the response from the user is stored in the appropriate variable
************************************************************************************************************/
while( countObjects < limit )
{
// Collect the basic Security level information
System.out.printf( "%s%n", askCustNumber );
custNumber = input.next( );
System.out.printf( "%s%n", askPurchDt );
purchDt = input.nextInt( ); input.nextLine( );
System.out.printf( "%s%n", askPurchPrc );
purchPrc = input.nextDouble( ); input.nextLine( );
System.out.printf( "%s%n", askShares );
shares = input.nextDouble( ); input.nextLine( );
System.out.printf( "%s%n", askSymbol );
symbol = input.next( );
// Ask what type of object
System.out.printf( "%s%n", askObjType );
objType = input.nextInt( ); input.nextLine( );
if( objType == 1)
{
System.out.printf( "%s%n", askExchg );
exchange = input.next( );
System.out.printf( "%s%n", askDividends );
dividends = input.nextBoolean( );
System.out.printf( "%s%n", askDivDate);
divDate = input.nextInt( ); input.nextLine( );
System.out.printf( "%s%n", askDivAmount );
divAmount = input.nextDouble( ); input.nextLine( );
newSecurity = new Stock( custNumber,
purchDt,
purchPrc,
shares,
symbol,
exchange,
dividends,
divDate,
divAmount );
} // end Stock
else
{
System.out.printf( "%s%n", askType );
type = input.next( );
System.out.printf( "%s%n", askAdmin );
admin = input.nextDouble( ); input.nextLine( );
System.out.printf( "%s%n", askRptPeriod );
rptPeriod = input.next( ).charAt( 0 );
System.out.printf( "%s%n", askMgmt );
mgmt = input.nextBoolean( );
newSecurity = new MutualFund( custNumber,
purchDt,
purchPrc,
shares,
symbol,
type,
admin,
rptPeriod,
mgmt );
} // end MutualFund
/*
* Add newly instantiated object (Stock OR MutualFund) to the array
*/
custArray[ countObjects ] = newSecurity;
countObjects += 1;
} // end of loop
// Reflect to user that loop has ended
System.out.printf( "%nData input complete. Thank you.%n" );
} // end loadArray
private static void outputSecuritys( )
{
/************************************************************************************************************
* An ENHANCED FOR LOOP is used to process each object in the array.
* The toString method of each object is called; the return value is then output to the console.
************************************************************************************************************/
for( Security someone: custArray )
{
System.out.printf( "%s",
someone.toString( ) );
System.out.printf( "The cost basis for this investment is: $%,.2f%n",
someone.calcCost( ) );
}
} // end outputSecuritys
} // end Test_SecurityHierarchy
Explanation / Answer
Please find below the four implemented classes:
CostBasis.java
package april;
public interface CostBasis {
public double calcCost();
}
Security.java
package april;
public abstract class Security implements CostBasis{
private String custNumber;
private int punchDt;
private double purchPrc;
private double shares;
private String symbol;
/**
* @param custNumber
* @param punchDt
* @param purchPrc
* @param shares
* @param symbol
*/
public Security(String nbr, int date, double price, double qty, String sym) {
this.custNumber = nbr;
this.punchDt = date;
this.purchPrc = price;
this.shares = qty;
this.symbol = sym;
}
/**
* getters & setters
*/
public String getCustNumber() {
return custNumber;
}
public void setCustNumber(String custNumber) {
this.custNumber = custNumber;
}
public int getPunchDt() {
return punchDt;
}
public void setPunchDt(int punchDt) {
this.punchDt = punchDt;
}
public double getPurchPrc() {
return purchPrc;
}
public void setPurchPrc(double purchPrc) {
this.purchPrc = purchPrc;
}
public double getShares() {
return shares;
}
public void setShares(double shares) {
this.shares = shares;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
@Override
public String toString() {
return "The security belongs to Customer# " + custNumber + "; " + shares + " shares of " +
symbol + " were purchased on " + punchDt + " for $" + purchPrc + " per share. ";
}
}
MutualFund.java
package april;
public class MutualFund extends Security {
private String type;
private double admin;
private char rptPeriod;
private boolean mgmt;
/**
* @param nbr
* @param date
* @param price
* @param qty
* @param sym
* @param type
* @param admin
* @param rptPeriod
* @param mgmt
*/
public MutualFund(String nbr, int date, double price, double qty, String sym, String sort, double cost, char rpt, boolean style) {
super(nbr, date, price, qty, sym);
this.type = sort;
this.admin = cost;
this.rptPeriod = rpt;
this.mgmt = style;
}
/**
* getters & setters
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getAdmin() {
return admin;
}
public void setAdmin(double admin) {
this.admin = admin;
}
public char getRptPeriod() {
return rptPeriod;
}
public void setRptPeriod(char rptPeriod) {
this.rptPeriod = rptPeriod;
}
public boolean isMgmt() {
return mgmt;
}
public void setMgmt(boolean mgmt) {
this.mgmt = mgmt;
}
@Override
public String toString() {
String cycle = "";
if (rptPeriod == 'A')
cycle = "Annual";
else if (rptPeriod == 'Q')
cycle = "Quaterly";
else if (rptPeriod == 'M')
cycle = "Monthly";
return super.toString() +
"This is a " + type + " fund." + " " +
" Admin costs are capped at " + admin + ". "+
" Reporting cycle is " + cycle + "value. "+
" The fund is managed " + (mgmt ? "actively " : "passively ");
}
@Override
public double calcCost() {
return (super.getShares() * super.getPurchPrc()) * (1 + this.admin);
}
}
Stock.java
package april;
public class Stock extends Security {
private String exchange;
private boolean dividends;
private int divDate;
private double divAmount;
/**
* @param nbr
* @param date
* @param price
* @param qty
* @param sym
* @param exchange
* @param dividends
* @param divDate
* @param divAmount
*/
public Stock(String nbr, int date, double price, double qty, String sym, String xchg, boolean div, int divDt, double amt) {
super(nbr, date, price, qty, sym);
this.exchange = xchg;
this.dividends = div;
this.divDate = divDt;
this.divAmount = amt;
}
/**
* getters & setters
*/
public String getExchange() {
return exchange;
}
public void setExchange(String exchange) {
this.exchange = exchange;
}
public boolean isDividends() {
return dividends;
}
public void setDividends(boolean dividends) {
this.dividends = dividends;
}
public int getDivDate() {
return divDate;
}
public void setDivDate(int divDate) {
this.divDate = divDate;
}
public double getDivAmount() {
return divAmount;
}
public void setDivAmount(double divAmount) {
this.divAmount = divAmount;
}
@Override
public String toString() {
return "This Stock is traded on the " + exchange +" " +
(dividends ? " This stock pay a dividend. " : " This stock does not pay dividend. ") +
"A dividend of $" + divAmount + " will be payed in the " + divDate ;
}
@Override
public double calcCost() {
return (super.getShares() * (super.getPurchPrc() - this.divAmount));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.