Quizz Completion

Conditionals Exercises #1-20 Even

//exercise 2
import java.util.Scanner;

    
  
          Scanner input = new Scanner(System.in);
  
              System.out.print("Input a: ");
              double a = input.nextDouble();
              System.out.print(a);
              System.out.print("Input b: ");
              double b = input.nextDouble();
              System.out.print(b);
              System.out.print("Input c: ");
              double c = input.nextDouble();
              System.out.print(c);
  
              double result = b * b - 4.0 * a * c;
  
              if (result > 0.0) {
                  double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
                  double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
                  System.out.println("Roots are" + r1 + " and " + r2);
              } else if (result == 0.0) {
                  double r1 = -b / (2.0 * a);
                  System.out.println("Root is " + r1);
              } else {
                  System.out.println("No real roots.");
              }
Input a: 1.0Input b: 5.0Input c: 2.0Roots are-0.4384471871911697 and -4.561552812808831
//exercise 4
Scanner in = new Scanner(System.in);
System.out.print("Input: ");
double input = in.nextDouble();
System.out.print(input);
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");
        }
Input value: 30.0Positive number
// exercise 6
 
    Scanner in = new Scanner(System.in);

    System.out.print("Double 1: ");
    double num1 = in.nextDouble();
    System.out.print(num1);
    System.out.print("Double 2: ");
    double num2 = in.nextDouble();
    System.out.print(num2); 

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

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

    if (num1 == num2)
    {
        System.out.println("Same up to three decimal places");
    }
    else
    {
        System.out.println("Different");
    }
Double 1: 25.586Double 2: 25.589Different
// exercise 8

        Scanner in = new Scanner(System.in);

        System.out.print("Type a letter: ");
        String input = in.next().toLowerCase();
        System.out.println(input);

        boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
        boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
        boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
                || input.equals("o") || input.equals("u");

        if (input.length() > 1)
        {
            System.out.println("Error. Not a single character.");
        }
        else if (!(uppercase || lowercase))
        {
            System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
        }
        else if (vowels)
        {
            System.out.println("Input letter is Vowel");
        }
        else
        {
            System.out.println("Input letter is Consonant");
        }
Type a letter: a
Input letter is Vowel
// exercise 10

int i;
System.out.println ("First 10 natural numbers:");
for (i = 1;i <= 10;i++)
{      
    System.out.println(i);
}
System.out.println ("\n");
First 10 natural numbers:
1
2
3
4
5
6
7
8
9
10


2019 FRQ 1

// part a
public static int numberOfLeapYears(int year1, int year2)
{  
   int numLeapYears = 0;
   for (int i = year1; i <= year2; i++) {
       if (isLeapYear(i)) {
           numLeapYears ++;
       }
   }
   return numLeapYears; 

}
// part b
public static int dayOfWeek(int month, int day, int year)
 {  
    int firstDay = firstDayOfYear(year);
    int theDate = dayOfYear(month, day year);
    int theDay = (firstDay + theDate - 1) % 7;
    return theDay;
 }
public class APCalendar
{
 /** Returns true if year is a leap year and false otherwise. */
 private static boolean isLeapYear(int year)
 { /* implementation not shown */ }
 /** 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) */ 
    int numLeapYears = 0;
    for (int i = year1; i <= year2; i++) {
        if (isLeapYear(i)) {
            numLeapYears ++;
        }
    }
    return numLeapYears; 

}
 /** 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)
 { /* implementation not shown */ }

 /** 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)
 { /* implementation not shown */ }

 /** 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)
 {  
    int firstDay = firstDayOfYear(year);
    int theDate = dayOfYear(month, day year);
    int theDay = (firstDay + theDate - 1) % 7;
    return theDay;
 }
 // There may be instance variables, constructors, and other methods not shown.
}