I’m trying to get my program to print each of the 4 variables (wholeNumber, decimalPart, positive, currencySymbol) defined in the program below using an accessor for each, however when I go to run the program instead of getting this
Expected Output:
1 2 true $
I instead get this. Could someone explain what error is causing this?
CODE
public class Q4 { int wholeNumber; int decimalPart; boolean positive; char currencySymbol; public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){ } public static void main(String[] args) { Q4 m = new Q4(1,2,true,'$'); System.out.println(m.getWholeNumber()); System.out.println(m.getDecimalPart()); System.out.println(m.isPositive()); System.out.println(m.getCurrencySymbol()); } // Accessor public int getWholeNumber(){ return(wholeNumber); } // Accessor public int getDecimalPart(){ return(decimalPart); } // Accessor public boolean isPositive(){ return(positive); } // Accessor public char getCurrencySymbol(){ return(currencySymbol); } }
Advertisement
Answer
You forgot to set the member variables in the constructor, so they are left at their default values.
public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){ this.wholeNumber = wholeNumber; this.decimalPart = decimalPart; this.positive = positive; this.currencySymbol = currencySymbol; }