Question: Add a static variable named totalInstances. Each time you construct a class, this variable should be incremented by 1. Adjust your toString method to also
Add a static variable named totalInstances. Each time you construct a class, this variable should be incremented by 1. Adjust your toString method to also print the total number of times the class has been created on a different line than the Month name.
Then Create a test class to test the code
public class Month {
private int monthNumber;
private String monthName;
private static String[] names = {"Undecided", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
public Month() {
setMonthNumber(1); }
public Month(int month) { setMonthNumber(month); }
public Month(String stringIn) { Boolean isSet = false; for(int i = 0; i < names.length; i++) { if(stringIn.equalsIgnoreCase(names[i])) { setMonthNumber(i); isSet = true; break; } }
if(!isSet) { setMonthNumber(1); } }
public void setMonthNumber(int intIn) { if(intIn < 1 || intIn > 12) { monthNumber = 1; setMonthName(1); }
else
{ monthNumber = intIn; setMonthName(intIn); } }
private void setMonthName(int intIn) { monthName = names[intIn]; }
public int getMonthNumber() { return monthNumber; }
public String getMonthName() { return monthName; }
public String toString() { String monthOut = "The month is " + getMonthName(); return monthOut; } public Boolean equals(Month objIn) { Boolean isEqual; if(this.getMonthNumber() == objIn.getMonthNumber())
{ isEqual = true; }
else
{ isEqual = false; }
return isEqual;
}
public Boolean greaterThan(Month objIn)
{ Boolean isGreater;
if(this.getMonthNumber() > objIn.getMonthNumber())
{ isGreater = true; }
else
{ isGreater = false; }
return isGreater; }
public Boolean lessThan(Month objIn) { Boolean isLess; if(this.getMonthNumber() < objIn.getMonthNumber()) { isLess = true; }
else
{ isLess = false; } return isLess; } }
Create a Test class that tests everything in the code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
