Docsity

Java Program for Stock Portfolio Management, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 2123 Midterm Examination
This exam is closed book, closed note. You have the entire class period to complete the exam. The
exam must be turned in at the end of the testing period, and there will be severe penalties for delaying
submission past the end of the period.
Your Name:_________________________________
Problem 1, Tester Class: /35
Problem 2, Stock Class: /25
Problem 3, Implement Iterable: /15
Problem 4, Simple Questions: /10
Problem 5, Single Nodes: /7
Problem 6, Doubly-linked Nodes: /8
Your Grade:_______ __/100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Java Program for Stock Portfolio Management and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 2123 Midterm Examination

This exam is closed book, closed note. You have the entire class period to complete the exam. The exam must be turned in at the end of the testing period, and there will be severe penalties for delaying submission past the end of the period.

Your Name:_________________________________

Problem 1, Tester Class: /

Problem 2, Stock Class: /

Problem 3, Implement Iterable: /

Problem 4, Simple Questions: /

Problem 5, Single Nodes: /

Problem 6, Doubly-linked Nodes: /

Your Grade:_______ __/

package stocks;

public final class Tester {

public static void main( final String... args) { final Portfolio myPortfolio = new Portfolio("01137675");

{ System. out .println("adding stocks"); { final Stock stock = new Stock("co. 0", new CurrencyAmount(1.2)); myPortfolio.addStock(stock); } { final Stock stock = new Stock("co. 1", new CurrencyAmount(5.6)); myPortfolio.addStock(stock); } { final Stock stock = new Stock("co. 2", new CurrencyAmount(9.8)); myPortfolio.addStock(stock); } }

System. out .println(myPortfolio); System. out .println("total profit: " + myPortfolio.getTotalProfit()); System. out .println(); { System. out .println("updating current values"); final CurrencyAmount toAdd = new CurrencyAmount(7.3); for ( final Stock temp : myPortfolio) { final CurrencyAmount tempValue = temp.getCurrentValue(); tempValue.add(toAdd); temp.setCurrentValue(tempValue); } } System. out .println(myPortfolio); System. out .println("total profit: " + myPortfolio.getTotalProfit()); System. out .println();

{ System. out .println("removing a stock"); myPortfolio.removeStock("co. 0"); }

System. out .println(myPortfolio);

setCurrentValue(currentValue); } /**

  • convenience constructor
  • uses the same value for the original cost as for the current cost
  • @param stockName the name of this stock
  • @param cost the original cost and the current cost of this stock / public Stock( final String stockName, final CurrencyAmount cost) { this (stockName, cost, cost); } /*
  • @return the current cost of this stock / public final CurrencyAmount getCurrentValue() { return new CurrencyAmount( this .currentValue); } /*
  • @param currentValue sets the current cost of this stock */ public final void setCurrentValue( final CurrencyAmount currentValue) { this .currentValue = new CurrencyAmount(currentValue); }

/**

  • @return the name of this stock */ public final String getStockName() { return this .stockName; }

/**

  • @return the original cost of this stock / public final CurrencyAmount getOriginalCost() { return new CurrencyAmount( this .originalCost); } /*
  • @return the amount of profit that would be gained by this stock if it were sold */ public final CurrencyAmount getProfit() { final CurrencyAmount profit = new CurrencyAmount( this .currentValue); profit.subtract( this .originalCost); return profit; }

@Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("Stock [\n");

builder.append("\tstockName="); builder.append( this .stockName); builder.append("\n");

builder.append("\toriginalCost="); builder.append( this .originalCost); builder.append("\n"); builder.append("\tcurrentValue="); builder.append( this .currentValue); builder.append("\n");

builder.append("]"); return builder.toString(); } }

package stocks;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

  • represents a simple stock portfolio

*/

public final class Portfolio implements Iterable {

  • the identifier for this Portfolio

*/

  • @param toAdd to stock to be added to this Portfolio

  • @throws IllegalArgumentException if there is already a stock by this name

*/

public void addStock(final Stock toAdd) {

{

final String stockName = toAdd.getStockName();

if (containsStock(stockName))

throw new IllegalArgumentException(

"duplicate elements not supported โ€โ€ " + stockName);

}

final Stock temp = new Stock(toAdd);

final boolean added = this.stockPortfolio.add(temp);

assert added;

}

  • @param stockName the name of the stock to find

  • @return whether this Portfolio contains the parameter

*/

public boolean containsStock(final String stockName) {

for (final Stock toTest : this.stockPortfolio)

if (toTest.getStockName().equals(stockName))

return true;

return false;

}

  • @param stockName the name of the stock to remove

  • @return the stock that corresponds to the parameter, or null if the specified stock is not found

*/

public Stock removeStock(final String stockName) {

final Iterator iter = this.stockPortfolio.iterator();

while (iter.hasNext()) {

final Stock temp = iter.next();

if (temp.getStockName().equals(stockName)) {

iter.remove();

return temp;

}

}

// throw new IllegalArgumentException("No such stock โ€โ€ " + stockName);

return null;

}

  • calculates the total amount of profit represented by this Portfolio

public Iterator iterator() {

return this.stockPortfolio.iterator();

}

  • @return the number of stocks contained by this Portfolio

*/

public int size() {

return stockPortfolio.size();

}

@Override

public String toString() {

final StringBuilder builder = new StringBuilder();

builder.append("Portfolio [\n");

builder.append("\taccountNumber=");

builder.append(accountNumber);

builder.append("\n");

builder.append("\tstockPortfolio=");

String temp = stockPortfolio.toString();

temp = temp.replaceAll("\n", "\n\t");

// builder.append ('\t');

builder.append(temp);

builder.append("\n");

builder.append("]");

return builder.toString();

}

}

package stocks;

import java.math.BigDecimal;

import java.math.MathContext;

import java.math.RoundingMode;

import java.text.NumberFormat;

import java.util.Currency;

import java.util.Locale;

  • represents an amount of some currency

*/

  • @param toClone the CurrencyAmount to be cloned

*/

public CurrencyAmount(final CurrencyAmount toClone) {

this.me = toClone.me;

this.mc = toClone.mc;

this.nf = toClone.nf;

this.value = toClone.value;

}

  • standard constructor

  • note that not all currencies have fractional parts

  • @param me the type of currency

  • @param value the amount of currency

*/

public CurrencyAmount(final Currency me, final double value) {

this.me = me;

final RoundingMode roundingMode = RoundingMode.HALF_EVEN;

final int precision = me.getDefaultFractionDigits();

if (precision < 0)

throw new IllegalArgumentException(

"precision must be nonโ€negative โ€โ€ " + precision);

this.mc = new MathContext(precision+1, roundingMode);

}

this.nf = NumberFormat.getCurrencyInstance();

// this.value = BigDecimal.ZERO;

this.value = BigDecimal.valueOf(value);

}

  • convenience constructor

  • uses the default locale to determine which currency type to use

  • @param value the amount of currency

*/

public CurrencyAmount(final double value) {

this(Currency.getInstance(Locale.getDefault()), value);

}

  • @return a symbol to represent the type of currency

*/

  • subtract from this CurrencyAmount the amount of currency represented by the parameter

  • @param subtrahend the amount of currency to subtract

*/

public void subtract(final CurrencyAmount subtrahend) {

this.value = value.subtract(subtrahend.value, mc);

}

  • multiply this CurrencyAmount by the amount of currency represented by the parameter

  • @param multiplicand the amount of currency by which to multiply

*/

public void multiply(final CurrencyAmount multiplicand) {

this.value = this.value.multiply(multiplicand.value, this.mc);

}

  • divide this CurrencyAmount by the amount of currency represented by the parameter

  • @param divisor the amount of currency by which to divide

*/

public void divide(final CurrencyAmount divisor) {

this.value = this.value.divide(divisor.value, this.mc);

}

  • raise this CurrencyAmount to a power

  • @param n the power to which this amount will be raised

*/

public void pow(final int n) {

this.value = this.value.pow(n, this.mc);

}

@Override

public String toString() {

return this.nf.format(this.value);

}

}

Q: complete the following table

type bytes

byte

short

int

long

float

double

boolean

char

Q: what is output by the following code?

final Integer int0 = new Integer (2);

final Integer int1 = new Integer (2);

System.out.println (int0 == int1);

A:

Q: what is output by the following code?

final Integer int0 = new Integer (2);

final Integer int1 = new Integer (2);

System.out.println (int0.equals (int1));

A: