Define a class called Month. Your class will have one member variable of type in
ID: 3669709 • Letter: D
Question
Define a class called Month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). The complete class will include all the following member functions:
- a constructor to set the month using the first three letters in the name of the month.
- a constructor to set the month using an integer argument (1 for January, 2 for February, and so forth),
- a default constructor,
- an input function that reads the month as an integer,
- an input function that reads the month as the first three letters in the name of the month,
- an output function that outputs the month as an integer,
- an output function that outputs the first three letters in the name of the month,
- a member function that returns the next month as a value of type Month.
Embed the class definition in a test program.
Explanation / Answer
import java.util.*;
class Month
{
int m;
String sm;
Month(String s)
{
sm=s;
}
Month(int mo)
{
m=mo;
}
Month()
{
m=0;
}
void setinp()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Month in 3 Letters");
sm=scan.nextLine();
}
void setinp1()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter Month in Integer");
m=scan.nextInt();
}
int get()
{
return m;
}
String get1()
{
return sm;
}
}
class TestMonth
{
public static void main(String args[])
{
Month o=new Month(3);
Month o1=new Month("Mar");
o.setinp1();
int aa=o.get();
System.out.println("Integer Month is "+aa);
o1.setinp();
String ss=o1.get1();
System.out.println("String Month is "+ss);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.