Given a class:
public class Money {
int wholeNumber;
int decimalPart;
boolean positive;
char currencySymbol;
}
Write a constructor for setting the 4 instance variables (in the order specified above). The constructor must validate the input throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met:
Write an accessor method for each of the 4 instance variables. The constructor from the previous question is already declared, you DO NOT need to provide it again.
Write a mutator method for each of the 4 instance variables. The constructor and accessor methods from previous questions are already declared, you DO NOT need to write them again. However, the mutators must still validate input they receive. Throwing an IllegalArgumentException (you can specify any error message you see fit) if any of the following conditions are not met:
Ok so the required class is written below.
If you got any doubt feel free to drop that in comment section.
Code:
public class Money{
int wholeNumber;
int decimalPart;
boolean positive;
char currencySymbol;
public Money(int wholeNumber,int decimalPart ,boolean
positive ,char currencySymbol) {
try {
if(wholeNumber<0 || decimalPart<0 || decimalPart>99
||
(currencySymbol!='$'
&& currencySymbol!='€' && currencySymbol!='£'))
{
throw new
IllegalArgumentException("Invalid Value");
}
else {
setCurrencySymbol(currencySymbol);
setDecimalPart(decimalPart);
setPositive(positive);
setWholeNumber(wholeNumber);
}
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
//accessors
public char getCurrencySymbol() {
return currencySymbol;
}
public int getDecimalPart() {
return decimalPart;
}
public int getWholeNumber() {
return wholeNumber;
}
public boolean getPositive() {
return positive;
}
//mutators
public void setCurrencySymbol(char currencySymbol)
{
this.currencySymbol =
currencySymbol;
}
public void setDecimalPart(int decimalPart) {
this.decimalPart =
decimalPart;
}
public void setPositive(boolean positive) {
this.positive = positive;
}
public void setWholeNumber(int wholeNumber) {
this.wholeNumber =
wholeNumber;
}
}
Get Answers For Free
Most questions answered within 1 hours.