Im writing the demo for my class to demonstrate each method. The class takes a number and assigns it to a month, or is supposed to. I have it working to say month 1 is January, month 2 is February and so on. I want to user to enter a month and set that to a number, how can I do this? Here is my class and driver:
public class Month { private int monthNumber; public Month() { monthNumber = 1; } public Month(int m) { if (m > 0 && m < 13) { monthNumber = 1; } else { monthNumber = m; } } public Month(String name) { if (name.equalsIgnoreCase("january")) monthNumber = 1; else if (name.equalsIgnoreCase("february")) monthNumber = 2; else if (name.equalsIgnoreCase("march")) monthNumber = 3; else if (name.equalsIgnoreCase("april")) monthNumber = 4; else if (name.equalsIgnoreCase("may")) monthNumber = 5; else if (name.equalsIgnoreCase("june")) monthNumber = 6; else if (name.equalsIgnoreCase("july")) monthNumber = 7; else if (name.equalsIgnoreCase("august")) monthNumber = 8; else if (name.equalsIgnoreCase("september")) monthNumber = 9; else if (name.equalsIgnoreCase("october")) monthNumber = 10; else if (name.equalsIgnoreCase("november")) monthNumber = 11; else if (name.equalsIgnoreCase("december")) monthNumber = 12; else monthNumber = 1; } public void setMonthNumber(int a) { if (a > 0 && a < 13) { monthNumber = 1; } else { monthNumber = a; } } public int getMonthNumber() { return monthNumber; } public String getMonthName() { String month; switch (monthNumber) { case 1: month = "January"; break; case 2: month = "February"; break; case 3: month = "March"; break; case 4: month = "April"; break; case 5: month = "May"; break; case 6: month = "June"; break; case 7: month = "July"; break; case 8: month = "August"; break; case 9: month = "September"; break; case 10: month = "October"; break; case 11: month = "November"; break; case 12: month = "December"; break; default: month = ""; } return month; } public String toString() { return getMonthName(); } public boolean equals(Month object2) { boolean status; if (object2.getMonthNumber() == monthNumber) { status = true; } else { status = false; } return status; } public boolean greaterThan(Month object) { boolean status; if (monthNumber > object.getMonthNumber()) { status = true; } else { status = false; } return status; } public boolean lessThan(Month object3) { boolean status; if (monthNumber < object3.getMonthNumber()) { status = true; } else { status = false; } return status; } }
Here is my driver, at the bottom of my driver, I try to get input from the user( a month), so when they type in february, I want it to output February is month 2, or January is month 1, and so on. I cant seem to get that to work though.
Here is my driver so far:
import java.util.Scanner; public class MonthDemo { public static void main(String[] args) { // Use the no-arg constructor. Month object1 = new Month(); System.out.println("Month " + object1.getMonthNumber() + " = " + object1); Month object2 = new Month(2); for (int i = 1; i <= 12; i++) { object2.setMonthNumber(i); System.out.println("Month " + object2.getMonthNumber() + " name is " + object2); } String mName; Scanner keyboard = new Scanner(System.in); Month object5 = new Month(""); System.out.println("Enter a month name: "); mName = keyboard.nextLine(); System.out.println(object5); } }