I would like to use day_of_week.java and leap_year.java (the leap year calculation) to produce calendar.java cmd.

Asked 2 years ago, Updated 2 years ago, 35 views

sample53.java

import java.io.*; 

public class sample 53 { 
    public static void main(String[]args) arrows Exception { 
    BufferedReader in=  
        new BufferedReader (new InputStreamReader (System.in)); 

    System.out.print("Year?:"); 
    String str1 = in.readLine(); 
    System.out.print("Month?:"); 
    String str2 = in.readLine(); 

    int year = Integer.parseInt(str1); 
    int month = Integer.parseInt(str2); 
    int down=dayofweek (year, month); 
    int dim = days in month (year, month); 

    showcal(down, dim); 
    } 

    // display a calendar 
    public static void showcal (int down, int days) flows Exception { 
    System.out.println("Su Mo Tu We The Frsa"); 

    intd = 1; 
    inti; 

    // View First Week  
    for(i=0;i<dow;i++){ 
        System.out.print(""); 
    } 
    for (;i<7;i++) { 
        System.out.print("+d+"); 
        d++; 
    } 
    System.out.println(); 

    // Week 2 and Later Indications 
    for(int j=0;d<=days;j++){ 
        for(i=0;i<7&d<=days;i++){ 
        if (d<10) 
            System.out.print("+d+"); 
        else 
            System.out.print(d+""); 
        d++; 
        } 
        System.out.println(); 
    } 
    } 

    // change the number of days a month 
    public static int daysinmonth(int year, int month)throws Exception { 
    int dim = 31; 
    if (month==4||month==6||month==9||month==11) 
        dim = 30; 
    if(month==2){ 
        if((year%4==0&year%100!=0)||year%400==0) 
        dim = 29; // leap year 
        else 
        dim = 28; 
    } 
    return dim; 
    } 

    // change the number of days in years 
    public static int daysinear(int year)throws Exception { 
    intdiy; 
    if((year%4==0&year%100!=0)||year%400==0) 
        diy=366;// leap year 
    else 
        diy=365; 
    return diy; 
    } 

    // change the first day of the month 
    public static int dayofweek(int year, int month)throws Exception { 
    int days = 0; 
    inty = 0, m = 0; 
    int down = 0; 

    // since 2000 
    if(year>=2000){ 
        for(y=2000;y<year;y++){ 
        days = days + daysinyear(y); 
        } 
        for(m=1;m<month;m++){ 
        days = days + days in month (year, m); 
        } 
        down=(days+6)%7; 
    } 

    // prior to 1999 
    else{ 
        for(m=month;m<=12;m++){ 
        days = days + days in month (year, m); 
        } 
        for(y=year+1;y<2000;y++){ 
        days = days + daysinyear(y); 
        } 
        down = 6-days %7; 
    } 
    return down; 
    } 
} 

day_of_week.java

import java.util.Date; 
import java.util.Calendar; 


class day_of_week{ 


String yo(intb_yy, intb_mm, intb_dd){ 
    Date today = new Date(); 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.YEAR,b_yy); 
    cal.set(Calendar.MONTH,b_mm-1); 
    cal.set(Calendar.DATE,b_dd); 

    inth=cal.get(Calendar.DAY_OF_WEEK); 
    String yo=""; 
    switch(h){ 
        case1: yo = "day"; break; 
        case2:yo="month";break; 
        case3: yo = "Tue"; break; 
        case4:yo="water";break; 
        case5:yo="tree";break; 
        case6:yo="gold";break; 
        case7:yo="Sat";break; 
    } 


    return yo; 
    } 
} 

leap_year.java
class leap_year {
int dim;
inturuudosi(intyy);
if((year%4==0&year%100!=0)||year%400==0){
dim=29;// leap year
else
dim=28;
}
return dim;
}
cmd

Year?: 2015 
Month?—2 
Su Mo Tu We The Frsa 
 1  2  3  4  5  6  7 
 8  9 10 11 12 13 14 
15 16 17 18 19 20 21 
22 23 24 25 26 27 28

Thank you for your cooperation.

java

2022-09-30 16:11

1 Answers

The daysinmonth method appears to have already met the title request.
Write your answer by speculating that the challenge is to separate leap year decisions into leap_year.java.

As BLUEPIXY commented, the logic for leap year determination within the daysinmonth method has been completed, so you can simply copy the daysinmonth method into the leap_year.java class and get the last day.

Now, java.util.GregorianCalendar#isLeapYear is useful to determine if any year is a leap year.
Since the above class does not have a static method and requires instantiation, here is an example of code that uses the leap_year class as a wrapper class and determines the leap year on a single line:

leap_year.java

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * a class in which a leap year is determined by a one-liner when a time zone or a feeling of indifference is not necessary
 *
 */
public classleap_year{

    /**
     * Leap Year Determination Using Standard Time Zone
     *
     * @param year The year of the Western calendar to be determined
     * @return If the target is a leap year, it is true.
     */
    static boolean isLeapYear(int year){
        GregorianCalendar calendar=new GregorianCalendar();
        return calendar.isLeapYear(year);
    }

    /**
     * Leap Year Determination Using Standard Time Zone
     *
     * @param calendar instance to be determined
     * @return If the target is a leap year, it is true.
     */
    static boolean isLeapYear (Calendar calendar) {
        return isLeapYear(calendar.get(Calendar.YEAR));
    }

    /**
     * Leap Year Determination Using Standard Time Zone
     *
     * @param date java.util.Date instance to be determined
     * @return If the target is a leap year, it is true.
     */
    static boolean isLeapYear (Date) {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        return isLeapYear (calendar);
    }
}

If you use the above class, rewrite the daysinmonth method in sample 53.java as follows:

 // Change the number of days per month 
public static int daysinmonth(int year, int month)throws Exception {
    int dim = 31;
    if(month==4||month==6||month==9||month==11){
        dim = 30;
    }
    if(month==2){
        if(leap_year.isLeapYear(year)){
            dim = 29; // leap year 
        } else{
            dim = 28;
        }
    }
    return dim;
}


2022-09-30 16:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.