














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















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); } /**
/**
/**
@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
*/
@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
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
return this.stockPortfolio.iterator();
}
*/
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
*/
*/
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);
}
*/
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: