simplest
simplest

Reputation: 217

Java working with Objects and Constructors

I have been given an assignment by my professor to create a Monthnum class with multiple constuctors to take all argument, as we are just learning Object Oriented Programming. I needed to create a new constructor that accepts user input both as an int value and another constructor that accepts it as a string value for the month of the year. Ex: 1 = January and January = 1. I know that I can create a scanner in my main method, but I'm unsure of how to get this number to be accepted and printed out. A step in the right direct would be extremely useful!

import java.util.Scanner;

public class learningObjectsAndClasses {
    public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    int monthNumber = input.nextInt();
    String monthName = input.nextLine();
    Monthnum inputMonthNumber = new Monthnum(monthNumber);
    Monthnum inputMonthName = new Monthnum(monthName);
    System.out.println("Please enter the month name or number: " 
        + inputMonthNumber);    

    }
}
class Monthnum{

    int Monthnum;
    String monthName;


    Monthnum(){
        Monthnum = 1;
    }
    Monthnum(int whichMonth){
        Monthnum = whichMonth;
        if (whichMonth == 1){
            System.out.println("January");
        }
        else if (whichMonth == 2){
            System.out.println("February");
        }
        else if (whichMonth == 3){
            System.out.println("March");
        }
        else if (whichMonth == 4){
            System.out.println("April");
        }
        else if (whichMonth == 5){
            System.out.println("May");
        }
        else if (whichMonth == 6){
            System.out.println("June");
        }
        else if (whichMonth == 7){
            System.out.println("July");
        }
        else if (whichMonth == 8){
            System.out.println("August");
        }
        else if (whichMonth == 9){
            System.out.println("September");
        }
        else if (whichMonth == 10){
            System.out.println("October");
        }
        else if (whichMonth == 11){
            System.out.println("November");
        }
        else if (whichMonth == 12){
            System.out.println("December");
        }
        else
            System.out.println("Invalid input");

        }
    Monthnum(String whichMonth){
        if (whichMonth == "January"){
            Monthnum = 1;
        }
        else if (whichMonth == "February"){
            Monthnum = 2;
        }
        else if (whichMonth == "March"){
            Monthnum = 3;
        }
        else if (whichMonth == "April"){
            Monthnum = 3;
        }
        else if (whichMonth == "May"){
            Monthnum = 4;
        }
        else if (whichMonth == "June"){
            Monthnum = 5;
        }
        else if (whichMonth == "July"){
            Monthnum = 6;
        }
        else if (whichMonth == "August"){
            Monthnum = 7;
        }
        else if (whichMonth == "September"){
            Monthnum = 8;
        }
        else if (whichMonth == "October"){
            Monthnum = 9;
        }
        else if (whichMonth == "November"){
            Monthnum = 10;
        }
        else if (whichMonth == "December"){
            Monthnum = 11;
        }
        else if (whichMonth == "March"){
            Monthnum = 12;
        }
        else
            System.out.println("Invalid input");
    }
}

Upvotes: 1

Views: 88

Answers (1)

Faraz
Faraz

Reputation: 6285

So after you setup the scanner and everything. You want to read the value that a user would input. You would read that with Scanner's .nextInt() method.

Scanner input = new Scanner(System.in);
int num;   
num = input.nextInt();

Then after reading it and saving it in a variable num, You can instantiate an object of Monthnum with a given parameter that you got from user.

Monthnum m = new Monthnum(num);

And if you want to read the String from user, you can user Scanner's nextLine() method like this:

Scanner input = new Scanner(System.in); //Same scanner from above 
                                        //no need to initialize it again
String s;   
s = input.nextLine();

Now you would declare and initialize an object of Monthnum with String parameter (which you haven't made yet)

Monthnum p = new Monthnum(s);

Now here is a hint for making that constructor. You use those same if else that you already have. Just change them to like this:

if (whichMonth.equals("January"){
        System.out.println("a");
}
....

Do ask me questions if you don't understand something! thanks

EDIT: So now your code should look like this:

package tst;

import java.util.Scanner;

public class learningObjectsAndClasses {
public static void main(String args[]){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter Month Name ");
    String monthName = input.next();
    Monthnum inputMonthNumber = new Monthnum(monthName);

    System.out.println("Enter Number ");
    int monthNumber = input.nextInt();
    Monthnum inputMonthName = new Monthnum(monthNumber);

}
public static class Monthnum{

    public int Monthnum;
    public String monthName;


    public Monthnum(){
        Monthnum = 1;
        monthName = "January";
    }
    public Monthnum(int whichMonth){
        Monthnum = whichMonth;
        if (whichMonth == 1){
            monthName = "January";
            System.out.println("January");
        }
        else if (whichMonth == 2){
            monthName = "February";
            System.out.println("February");
        }
        else if (whichMonth == 3){
            monthName = "March";
            System.out.println("March");
        }
        else if (whichMonth == 4){
            monthName = "April";
            System.out.println("April");
        }
        else if (whichMonth == 5){
            monthName = "May";
            System.out.println("May");
        }
        else if (whichMonth == 6){
            monthName = "June";
            System.out.println("June");
        }
        else if (whichMonth == 7){
            monthName = "July";
            System.out.println("July");
        }
        else if (whichMonth == 8){
            monthName = "August";
            System.out.println("August");
        }
        else if (whichMonth == 9){
            monthName = "September";
            System.out.println("September");
        }
        else if (whichMonth == 10){
            monthName = "October";
            System.out.println("October");
        }
        else if (whichMonth == 11){
            monthName = "November";
            System.out.println("November");
        }
        else if (whichMonth == 12){
            monthName = "December";
            System.out.println("December");
        }
        else {
            System.out.println("Invalid input from int cons");
        }

    } //end of cons

    public Monthnum(String whichMonth){
        if (whichMonth.equals("January")){
            Monthnum = 1;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("February")){
            Monthnum = 2;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("March")){
            Monthnum = 3;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("April")){
            Monthnum = 4;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("May")){
            Monthnum = 5;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("June")){
            Monthnum = 6;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("July")){
            Monthnum = 7;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("August")){
            Monthnum = 8;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("September")){
            Monthnum = 9;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("October")){
            Monthnum = 10;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("November")){
            Monthnum = 11;
            System.out.println(Monthnum);
        }
        else if (whichMonth.equals("December")){
            Monthnum = 12;
            System.out.println(Monthnum );
        }
        else
            System.out.println("Invalid input");
    }
  }
}

Upvotes: 1

Related Questions