import java.util.Scanner; public class Calendar { private int [][] table; privat
ID: 3622881 • Letter: I
Question
import java.util.Scanner;
public class Calendar {
private int [][] table;
private int max, start;
public Calendar ( ) {
table = new int [6][7];
max = start = 0;
}
public static void main ( String [] args ) {
Calendar program = new Calendar ( );
program.GetNumberOfDays ( );
program.GetStartDay ( );
program.SetCalendar ( );
program.PrintCalendar ( );
}
public void GetNumberOfDays ( ) {
Scanner console = new Scanner ( System.in);
do {
System.out.print ( " Enter the number of days in the month -> " );
max = console.nextInt ( );
} while ( max < 28 || max > 31 );
}
public void GetStartDay ( ) {
Scanner console = new Scanner (System.in );
do {
System.out.print ( " 1: Sunday, 2: Monday, " );
System.out.print ( " 3: Tuesday, 4: Wednesday, " );
System.out.print ( " 5: Thursday, 6: Friday, 7: Saturday " );
System.out.print ( " Enter the day to start the month (1 - 7) -> " );
start = console.nextInt ( );
} while ( start < 1 || start > 7 );
}
public void SetCalendar ( ) {
// I need help here
}
public void PrintCalendar ( ) {
// I need help here
}
}
NOTE: I need help on SetCalendar() and PrintCalendar()
Explanation / Answer
import java.util.Scanner;
public class Main {
private int [][] table;
private int max, start;
public Main ( ) {
table = new int [6][7];
max = start = 0;
}
public static void main ( String [] args ) {
Main program = new Main ( );
program.GetNumberOfDays ( );
program.GetStartDay ( );
program.SetCalendar ( );
program.PrintCalendar ( );
}
public void GetNumberOfDays ( ) {
Scanner console = new Scanner ( System.in);
do {
System.out.print ( " Enter the number of days in the month -> " );
max = console.nextInt ( );
} while ( max < 28 || max > 31 );
}
public void GetStartDay ( ) {
Scanner console = new Scanner (System.in );
do {
System.out.print ( " 1: Sunday, 2: Monday, " );
System.out.print ( " 3: Tuesday, 4: Wednesday, " );
System.out.print ( " 5: Thursday, 6: Friday, 7: Saturday " );
System.out.print ( " Enter the day to start the month (1 - 7) -> " );
start = console.nextInt ( );
} while ( start < 1 || start > 7 );
}
public void SetCalendar ( ) {
int temp=0;
int temp2=1;
for(int i=0;i<6;i++)
{
if(i==0)
{
temp=start-1;
}
else
{
temp=0;
}
for(int j=temp;j<7;j++,temp2++)
{
table[i][j]= temp2;
if(temp2==max)
{
break;
}
}
}
}
public void PrintCalendar ( ) {
// I need help here
System.out.println("SUN MON TUE WED THU FRI SAT");
for(int i=0;i<6;i++)
{
for(int j=0;j<7;j++)
{
if(table[i][j]==0)
{
System.out.print(" ");
}
else
{
System.out.print(table[i][j]+" ");
}
}
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.