Methods and Control Structures FRQ 1

  • We did the Calendar FRQ and created 5 methods of the class Calendar
  • First was to determine whether or not a year was a Leap Year
  • Second was to determine the first Day of each Year
  • Third was to determine the day of the the week that a date in the year fell on
  • Fourth was to determine the number of Leap Years in between 2 years
  • Fifth was to determine the Day of Week a certain day was on in a year

First:

  • We did this not so complicated use of control structures using if statements and if else statements
public static boolean isLeapYear(int year) {
    // implementation not shown
    if (year % 4 == 0 && year % 100 != 0){
        return true;
    }
    else if (year % 400 == 0){
        return true;
    }
    else {
        return false;
    }
}

Second:

  • as a group we ended up getting really confused because we couldn't get something consistent so we imported java.util and the date package to do it for us :(
  • I actually had a pretty clever code but it only worked for a few years near the year 2022 but I ended up getting rid of it because it wasn't the correct solution but I regret not keeping it somewhere
  • Had to change from private to public
public static int firstDayOfYear(int year) {
    Date currentDate = new Date(year-1900, 0, 1);

    return currentDate.getDay();
}

Third:

  • We collaborated as a group to create a Hashmap for each day and month and then had a total for the day, which we then used our DayOfWeek method to find the specific day of the Week
  • We also had to change from private to public for the API
public static int dayOfYear(int month, int day, int year) {
    int num = 0;
    HashMap<Integer, Integer> map = new HashMap<>();
    map.put(1, 0);
    map.put(2, 31);
    map.put(3, 59);
    map.put(4, 90);
    map.put(5, 120);
    map.put(6, 151);
    map.put(7, 181);
    map.put(8, 212);
    map.put(9, 243);
    map.put(10, 273);
    map.put(11, 304);
    map.put(12, 334);
    if (isLeapYear(year) && month > 2) {
        num = map.get(month) + day + 1;

    } else {
        num = map.get(month) + day;
    }

    return num;
}

Fourth:

  • Number of Leap Years was just simple iteration
public static int numberOfLeapYears(int year1, int year2) {
    // to be implemented in part (a)
    int count = 0;
    for (int i = year1; i <= year2; i++) {
        if (isLeapYear(i)) {
            count++;
        }
    }
    return count;
}

Fifth:

  • DayOfWeek function using java.util and the Date package
public static int dayOfWeek(int month, int day, int year) {
    // to be implemented in part (b)
    Date today = new Date(year-1900, month-1, day);

    return today.getDay();
}

Tester

System.out.println("firstDayOfYear: " + APCalendar.firstDayOfYear(2022));
System.out.println("dayOfYear: " + APCalendar.dayOfYear(1, 1, 2022));
System.out.println("isLeapYear: " + APCalendar.isLeapYear(2022));
System.out.println("numberOfLeapYears: " + APCalendar.numberOfLeapYears(2000, 2022));
System.out.println("dayOfWeek: " + APCalendar.dayOfWeek(1, 1, 2022));

API Endpoints

  • We had to make a Day.java as well as Year.java
@GetMapping("/firstDayOfYear/{year}")
    public ResponseEntity<JsonNode> getfirstDayOfYear(@PathVariable int year) throws JsonMappingException, JsonProcessingException {
      // Backend Year Object
      Year year_obj = new Year();
      year_obj.setYear(year);  

      // Turn Year Object into JSON
      ObjectMapper mapper = new ObjectMapper(); 
      JsonNode json = mapper.readTree(year_obj.firstDayOfYearToString()); // this requires exception handling

      return ResponseEntity.ok(json);  // JSON response, see ExceptionHandlerAdvice for throws
    }
    // add other methods
    @GetMapping("/dayOfYear/{day}/{month}/{year}")
    public ResponseEntity<JsonNode> getDayInfo(@PathVariable int day, @PathVariable int month, @PathVariable int year) throws JsonMappingException, JsonProcessingException, IOException, InterruptedException, ParseException {
      // Backend Day Object
      Day day_obj = new Day(day, month, year);

      // Turn Year Object into JSON
      ObjectMapper mapper = new ObjectMapper(); 
      JsonNode json = mapper.readTree(day_obj.toJSON()); // this requires exception handling

      return ResponseEntity.ok(json);  // JSON response, see ExceptionHandlerAdvice for throws
    }

     /** GET number of leap years from year1 to year 2
     * ObjectMapper throws exceptions on bad JSON
     *  @throws JsonProcessingException
     *  @throws JsonMappingException
     */
    @GetMapping("/numberOfLeapYears/{year1}/{year2}")
    public ResponseEntity<JsonNode> getLeapYears(@PathVariable int year1, @PathVariable int year2) throws JsonMappingException, JsonProcessingException {
      ObjectMapper mapper = new ObjectMapper(); 
      JsonNode json = mapper.readTree("{ \"count\": " + APCalendar.numberOfLeapYears(year1, year2) + " } "); // this requires exception handling

      return ResponseEntity.ok(json);  // JSON response, see ExceptionHandlerAdvice for throws
    }

Testing

  • Used Postman and Spring