2019 FRQ 1 A

Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive.

public static int numberOfLeapYears (int year1, int year2){
    //check IF year 1 is leap year or not, if not check year 1 + 1, and so on (for loop + if statement)
    //year 1 should be less than year 2 (condition)
    //then add up all the leap years (count)

    //isLeapYear(year) returns true if year is a leap year and false otherwise. 

    int count = 0; //initialize count, count should be outside of the for loop
    for (int y = year1; y <= year2; y++) {  // for each year between year 1 and year 2
        if (isLeapYear(y)) {  //if y is leap year (using the isLeapYear() method)
            count++; //count plus 1
        }
    }
    return count; //return the total number of leap years
}

2019 FRQ 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.

// 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 6denotes Saturday
// two helper methods: firstDayOfYear(year), dayOfYear(month, day, year) 

public static int dayOfWeek (int month, int day, int year) { //remember to define data type
    int firstDay = firstDayOfYear(year); //return the int value representing the day of the week for the first day of year
    int nthDay = dayOfYear(month, day, year); //return n, where month, day, and year specify the nth day of the year 
    int returnDay = (nthDay + firstDay - 1) % 7; //return based on a 0-indexed array instead of the 1-based list of days, gives us the right day of the week
    //% 7 at the end takes care of keeping the value within the bounds of the day of week index
    return returnDay; //return

}

Quizziz Screenshot

quizziz

Conditional Exercises

2) Write a Java program to solve quadratic equations (use if, else if and else).

import java.util.Scanner;  // import scanner for use input
public class Exercise2 {

    
  public static void main(String[] Strings) { 

        Scanner input = new Scanner(System.in); 

            System.out.print("Input a: ");
            double a = input.nextDouble();
            System.out.print("Input b: ");
            double b = input.nextDouble();
            System.out.print("Input c: ");
            double c = input.nextDouble();

            double result = b * b - 4.0 * a * c; // result the value under the square root

            if (result > 0.0) {  //if it is a positive number under the square root, continue with the rest of quadratic formula
                double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a); //The Math.pow() method returns the value of a base raised to a power. 
                double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a); 
                System.out.println("The roots are " + r1 + " and " + r2);
            } else if (result == 0.0) {  //if 0, -b +or- 0 over 2a
                double r1 = -b / (2.0 * a);
                System.out.println("The root is " + r1);
            } else {  //if negative number under square root, no solution
                System.out.println("The equation has no real roots.");
            }

    }
}

Exercise2.main(null);
Input a: 

4) Write a Java program that reads a floating-point number and prints "zero" if the number is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.

import java.util.Scanner;
public class Exercise4 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input value: ");
        double input = in.nextDouble();

        if (input > 0)
        {
            if (input < 1)
            {
                System.out.println("Positive small number");
            }
            else if (input > 1000000)
            {
                System.out.println("Positive large number");
            }
            else
            {
                System.out.println("Positive number");
            }
        }
        else if (input < 0)
        {
            if (Math.abs(input) < 1)
            {
                System.out.println("Negative small number");
            }
            else if (Math.abs(input) > 1000000)
            {
                System.out.println("Negative large number");
            }
            else
            {
                System.out.println("Negative number");
            }
        }
        else
        {
            System.out.println("Zero");
        }
    }
}

Exercise4.main(null);

6) Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

import java.util.Scanner;
public class Exercise6 {

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

        System.out.print("Input floating-point number: ");
        double x = in.nextDouble();
        System.out.print("Input floating-point another number: ");
        double y = in.nextDouble();

        x = Math.round(x * 1000);
        x = x / 1000;

        y = Math.round(y * 1000);
        y = y / 1000;

        if (x == y)
        {
            System.out.println("They are the same up to three decimal places");
        }
        else
        {
            System.out.println("They are different");
        }
    }
}

Exercise6.main(null);

The conditional exercises are really difficult for me. I would spent time understanding the code above as much as possible.