Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
github-classroom[bot] authored Apr 29, 2024
0 parents commit ad6f8c5
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
.gradle/
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 2019 AP Computer Science A FRQ #1 APCalendar

Instructions: [https://apstudents.collegeboard.org/ap/pdf/ap19-frq-computer-science-a.pdf](https://apstudents.collegeboard.org/ap/pdf/ap19-frq-computer-science-a.pdf)

Quick Reference Guide: [https://apstudents.collegeboard.org/ap/pdf/ap-computer-science-a-java-quick-reference.pdf](https://apstudents.collegeboard.org/ap/pdf/ap-computer-science-a-java-quick-reference.pdf)

The correct answer outputs should be as follows.



```
1999 Feb 5 5 Nov 15 1
2000 Feb 5 6 Nov 15 3
2001 Feb 5 1 Nov 15 4
2002 Feb 5 2 Nov 15 5
2003 Feb 5 3 Nov 15 6
2004 Feb 5 4 Nov 15 1
2005 Feb 5 6 Nov 15 2
2006 Feb 5 0 Nov 15 3
2007 Feb 5 1 Nov 15 4
```
18 changes: 18 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
56 changes: 56 additions & 0 deletions src/main/java/APCalendar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public class APCalendar
{
/** Returns true if year is a leap year and false otherwise. */
private static boolean isLeapYear(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}


/** Returns the number of leap years between year1 and year2, inclusive.
* Precondition: 0 <= year1 <= year2
*/
public static int numberOfLeapYears(int year1, int year2)
{
/* to be implemented in part (a) */

}

/** Returns the value representing the day of the week for the first day of year,
* where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.
*/
private static int firstDayOfYear(int year)
{
/* January 1, 1980 was a Tuesday */
return (2 + 365*(year - 1980) + numberOfLeapYears(1980, year-1)) % 7;
}

/** Returns n, where month, day, and year specify the nth day of the year.
* Returns 1 for January 1 (month = 1, day = 1) of any year.
* Precondition: The date represented by month, day, year is a valid date.
*/
private static int dayOfYear(int month, int day, int year)
{
final int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n = day;
int mth = 1;
while (mth < month)
{
n += daysInMonth[mth - 1];
mth++;
}
if (mth > 2 && isLeapYear(year))
n++;
return n;
}

/** Returns the value representing the day of the week for the given date
* (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
* and 6 denotes Saturday.
* Precondition: The date represented by month, day, year is a valid date.
*/
public static int dayOfWeek(int month, int day, int year)
{
/* to be implemented in part (b) */
}
}
20 changes: 20 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Main {
public static void main(String[] args)
{
for (int year = 1999; year <= 2007; year++)
System.out.println(year + " Feb 5 " + APCalendar.dayOfWeek(2, 5, year) +
" Nov 15 " + APCalendar.dayOfWeek(11, 15, year));

/*
1999 Feb 5 5 Nov 15 1
2000 Feb 5 6 Nov 15 3
2001 Feb 5 1 Nov 15 4
2002 Feb 5 2 Nov 15 5
2003 Feb 5 3 Nov 15 6
2004 Feb 5 4 Nov 15 1
2005 Feb 5 6 Nov 15 2
2006 Feb 5 0 Nov 15 3
2007 Feb 5 1 Nov 15 4
*/
}
}
53 changes: 53 additions & 0 deletions src/test/java/Tester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@


import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.*;

public class Tester {

@Test
public void partA () {
int result = APCalendar.numberOfLeapYears(0,2000);
assertEquals(486,result);
result = APCalendar.numberOfLeapYears(1950,1960);
assertEquals(3,result);
result = APCalendar.numberOfLeapYears(1900,2020);
assertEquals(30,result);
result = APCalendar.numberOfLeapYears(2000,2021);
assertEquals(6,result);
}
@Test
public void partB(){
/*
1999 Feb 5 5 Nov 15 1 2000 Feb 5 6 Nov 15 3
2001 Feb 5 1 Nov 15 4 2002 Feb 5 2 Nov 15 5
2003 Feb 5 3 Nov 15 6 2004 Feb 5 4 Nov 15 1
2005 Feb 5 6 Nov 15 2 2006 Feb 5 0 Nov 15 3
2007 Feb 5 1 Nov 15 4
*/
int result = APCalendar.dayOfWeek(2,5,1999);
assertEquals(5,result);
result = APCalendar.dayOfWeek(11,15,1999);
assertEquals(1,result);
result = APCalendar.dayOfWeek(2,5,2000);
assertEquals(6,result);
result = APCalendar.dayOfWeek(11,15,2000);
assertEquals(3,result);
result = APCalendar.dayOfWeek(2,5,2001);
assertEquals(1,result);
result = APCalendar.dayOfWeek(11,15,2001);
assertEquals(4,result);
result = APCalendar.dayOfWeek(2,5,2002);
assertEquals(2,result);
result = APCalendar.dayOfWeek(11,15,2002);
assertEquals(5,result);
}


}

0 comments on commit ad6f8c5

Please sign in to comment.