Can someone PLEASE help me with this JAVA program i am so lost! Write a class ca
ID: 644068 • Letter: C
Question
Can someone PLEASE help me with this JAVA program i am so lost!
Write a class called calender. The class has four data members, the month name, the day on which the month starts, and the number of days in that month and the year.
Write a program that instantiate an instance of that class and print a calendar based on the given month and year. EAch month has different starting day, so make sure you ask the user on what day the month starts and print the calendar for that month starting on that day.
The class should include constructors, getters and setters for each of the data members. The clas should have a method to read the information from the user and another method to print the month. Assume February is always 28 days.
Explanation / Answer
class main
package com.calender.main;
import java.util.Scanner;
public class Main {
static String month;
static int nod=31, year, day=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the name of the month you want to print : ");
month=scanner.nextLine();
System.out.println("Enter the year");
year=scanner.nextInt();
System.out
.println("Enter the day on which the month starts (use 0 for sunday, 1 for monday, ...etc)");
day=scanner.nextInt();
scanner.close();
if(month.equals("january")||month.equals("march")||month.equals("may")||month.equals("july")||month.equals("august")||month.equals("october")||month.equals("december")){
nod=31;
}else if(month.equals("april")||month.equals("june")||month.equals("september")||month.equals("november")){
nod=30;
}else if(month.equals("february")){
nod=28;
}else {
System.out.println("Not a valid month. 31 days assumed");
nod=31;
}
if(day>6)
{
System.out.println("Invalid option. Calender will start from sunday");
day=0;
}
Cal cal = new Cal(month, day, nod, year);
cal.print();
}
}
class cal
package com.calender.main;
public class Cal {
private String month;
private int day;
private int nod;
private int year;
int i,count=1;
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getNod() {
return nod;
}
public void setNod(int nod) {
this.nod = nod;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public Cal(String month, int day, int nod, int year){
setMonth(month);
setYear(year);
setDay(day);
setNod(nod);
}
public void print(){
System.out.println("Sun Mon Tue Wed thu Fri Sat");
int start=getDay();
int nods=getNod();
for(i=0;i<start;i++)
{
System.out.print(" ");
++count;
}
for(i=1;i<=nods;i++)
{
if(count<=7)
{
System.out.print(" "+i+" ");
++count;
}
if(count>7)
{
System.out.println(" "+" ");
count=1;
}
}
}
}