FRQ 1 - 2019 Exam

  1. The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class. #### (A)
  • Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive. In order to calculate this value, a helper method is provided for you.
  • isLeapYear(year) returns true if year is a leap year and false otherwise.
  • Complete method numberOfLeapYears below. You must use isLeapYear appropriately to receive full credit.
public static int numberOfLeapYears(int year1, int year2){
    int numOfLeapYears = 0; // initialize final count variable
    for(int i = year1; i <= year2; i++){ // for loop that starts at the year1 number and goes until year 2 including it
        if(isLeapYear(i)){  // if the year is a leap year, add 1 to the count
            numOfLeapYears += 1;
        }
    }
    return numOfLeapYears; // return the count
}

Scoring (5/5)

  • Initialize numeric variable (count) 1/1
  • Loop through each necessary year in range (for loop, with condition in code block) 1/1
  • Calls isLeapYear on valid year in range 1/1
  • Update count based on result of isLeapYear 1/1
  • Return count of leap years 1/1

(B)

  • Write the static method dayOfWeek, which returns the integer 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.
  • For example, 2019 began on a Tuesday, and January 5 is the fifth day of 2019. As a result, January 5, 2019, fell on a Saturday, and the method call dayOfWeek(1, 5, 2019) returns 6.
public static int dayOfWeek(int month, int day, int year){
    int date = dayOfYear(month, day, year); // gets current day number
    int first = firstDayofYear(year); // gets starting day of year
    int calculated = ((week + start)-1) % 7; // adds starting and current and subtracts one to account for the offset, and divides by 7. remainder determines the day of week
    return calculated;
}

Scoring (4/4)

  • Call firstDayofYear 1/1
  • Calls dayOfYear 1/1
  • Calculate value representing day of week (-1, incorrect calculation) 1/1
  • Return calculated value 1/1

Excercises 1-20 Even Only

Problem 2

import java.util.Scanner;
public class Prob2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("coeff a: ");
        double a = input.nextDouble();
        System.out.println("coeff b: ");
        double b = input.nextDouble();
        System.out.println("coeff c: ");
        double c = input.nextDouble();

        double root1 = (-b + (b * b - 4 * a * c)/(2*a));
        double root2 = (-b - (b * b - 4 * a * c)/(2*a));
        System.out.println("The roots are " + root1 + " and " + root2);
        }
    }

Prob2.main(null);
coeff a: 
coeff b: 
coeff c: 
The roots are -139.625 and 109.625

Problem 4

public class Prob4 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("num: ");
        double number = input.nextDouble();
        System.out.println(number);

        if (number > 0) {
            System.out.println("pos");
        } else if (number == 0) {
            System.out.println("zero");
        } else {
            System.out.println("neg");
        }

        if (number < 1) {
            System.out.println("neg");
        } else if (number > 1000000) {
            System.out.println("too big ");
        }

    }
}

Prob4.main(null);
num: 1356.0
pos

Problem 6

public class Prob6 {

    static double truncate(double n, int decimalPlace) {
        n = n*Math.pow(10, decimalPlace);
        n = Math.floor(n);
        n = n/Math.pow(10, decimalPlace);

        return n;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("num 1: ");
        double number1 = input.nextDouble();
        double num1Trun = truncate(number1, 3);
        System.out.println(number1);

        System.out.print("num 2: ");
        double number2 = input.nextDouble();
        double num2Trun = truncate(number2, 3);
        System.out.println(number2);

        if (num1Trun == num2Trun) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}

Prob6.main(null);
num 1: 125.6667765
num 2: 125.6660940951
true

Problem 8

public class Prob8 {

    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("enter letter: ");
        String letter = input.nextLine();
        System.out.println(letter);

        String[] vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"};

        if(letter.length() > 1) {
            System.out.println("not one letter");
        } else if (!letter.matches("[a-zA-Z]+")) {
            System.out.println("not a letter");
        } else if (vowels.contains(letter)) {
            System.out.println("vowel");
        } else {
            System.out.println("consonant");
        }


        input.close();

        
    }
}

Prob8.main(null);
enter letter: e
vowel

Problem 10

public class Prob10 {

    public static void main(String[] args) {

    int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int num : n) {
            System.out.println(num);
        }
    }
}

Prob10.main(null);
1
2
3
4
5
6
7
8
9
10

Problem 12

public class Prob12 {

    
    public static void main(String[] args) {
        int sum = 0;
        double average;

        System.out.println("Input nums:");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            int num = input.nextInt();
            System.out.println(num);
            sum += num;
        }
        average = sum/5;

        System.out.println("sum: " + sum);
        System.out.println("avg: " + average);
        
    }
}

Prob12.main(null);
Input nums:
9248357
934867
1342345
257
4
sum: 11525830
avg: 2305166.0

Problem 14

public class Prob14 {

    public static void main(String[] args) {

        System.out.println("num: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();

        System.out.println("terms: ");
        int terms = input.nextInt();

        for (int i = 0; i <= terms; i++) {
            System.out.println(num + " x " + i + " = " + num*i);
        }        
    }
}

Prob14.main(null);
num: 
terms: 
4 x 0 = 0
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28

Problem 16

public class Prob16 {

    public static void main(String[] args) {
        
        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Prob16.main(null);
rows: 6
1
12
123
1234
12345
123456

Prob 18

public class Prob18 {

    
    public static void main(String[] args) {
        

        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        int count = 1;

        for (int i = 1; i <= num; i++) {
            
            for (int j = 1; j <= i; j++) {
                System.out.print(count + "\t");
                count++;
            }
            System.out.println();
        }
    }
}

Prob18.main(null);
rows: 5
1	
2	3	
4	5	6	
7	8	9	10	
11	12	13	14	15	

Problem 20

public class Prob20 {

    
    public static void main(String[] args) {
        

        System.out.print("rows: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println(num);

        int count = 1;

        for (int i = 1; i <= num; i++) {
            
            for (int j = 1; j <= i; j++) {
                System.out.print(count + "\t");
                count++;
            }
            System.out.println();
        }
    }
}

Prob20.main(null);
rows: 5
1	
2	3	
4	5	6	
7	8	9	10	
11	12	13	14	15