Unit 3 Homework
Unit 3 Homework
- FRQ 1 - 2019 Exam
- Excercises 1-20 Even Only
- Problem 2
- Problem 4
- Problem 6
- Problem 8
- Problem 10
- Problem 12
- Problem 14
- Problem 16
- Prob 18
- Problem 20
FRQ 1 - 2019 Exam
- 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
}
(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;
}
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);