The main goal of Lab 2 is for the students to complete a simple class, called Currency.
Here is the Currency program in Java:
//Add javadoc with your name as author. The actual author is S. Sahni.
public class Currency
{
// class constants
public static final boolean PLUS = true;
public static final boolean MINUS = false;
// instance data members
private boolean sign;
private long dollars;
private byte cents;
// constructors
public Currency(boolean theSign, long theDollars, byte theCents)
{
...
...
}
public Currency()
{...}
public Currency(double theValue)
{...}
// accessor methods
public boolean getSign()
{...}
public long getDollars()
{...}
public byte getCents()
{...}
// mutator methods
public void setSign(boolean theSign)
{...}
public void setDollars(long theDollars)
{
...
...
}
public void setCents(byte theCents)
{
...
...
}
public void setValue(double theValue)
{
if (theValue < 0)
{
...
...
}
else
sign = PLUS;
... // extract integral part
// get two decimal digits
...
}
public void setValue(Currency x)
{
sign = x.sign;
dollars = x.dollars;
cents = x.cents;
}
public String toString()
{
if (sign == PLUS)
{return "$" + dollars + "." + cents;}
else
{return "-$" + dollars + "." + cents;}
}
public static void main(String [] args)
{
// test constructors
Currency g = new Currency(),
h = new Currency(PLUS, 3L, (byte) 50),
i = new Currency(-2.50),
j = new Currency();
// test toString
System.out.println("The initial values are " + g +
" " + h + " " + i + " " + j);
System.out.println();
// test mutators
// first make g nonzero
g.setDollars(2);
g.setSign(MINUS);
g.setCents((byte) 25);
i.setValue(-6.45);
System.out.println("New values are " + g + " " + i);
System.out.println();
}
}