Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.) boolean leapyear = (year % 4 == 0 && year % 100 != 0) (year % 400 = 0) Use t

ID: 3671794 • Letter: 1

Question

1.) boolean leapyear = (year % 4 == 0 && year % 100 != 0)

(year % 400 = 0)

Use the statements above in your program.

Connect the two statements above with a short-circuit operator, make sure code is correct, and create a program where the user can enter an integer year using the new Scanner (System.in); method and be returned a true if the year is a leap year or false if the year is not a leap year.

Sample Outputs

“Enter Year” // User prompt

2000

“2000 is a leap year? true” // Output

“Enter Year” // User prompt

190

“190 is a leap year? false” // Output

2.) Write a program that prints out the first 20 squared numbers. Starting at x = 1 until x = 20. Do not use java.lang.Math

Create a line counter variable that inputs a blank line every 5 lines.

Display outputs as shown below.

1 squared = 1

2 squared = 4

3 squared = 9

4 squared = 16

// Every 5 lines create a blank line.

5 squared = 25

6 squared = 36

7 squared = 49

Continued…

Explanation / Answer

1).

import java.util.Scanner;

/*
* This program is to test a year as a leap or not
*/
class LeapYearTester{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int year;
       System.out.println("Enter Year");
       year = sc.nextInt(); // user input
       sc.close();
      
       // testing year
   boolean leapyear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? true : false;
         
       System.out.println(year+" is a leap year? "+leapyear);
      
   }
}

/*

OUTPUT:

Enter Year
2000
2000 is a leap year? true

Enter Year
190
190 is a leap year? false

*/

2).

/*
* This program is to find square of first 20 numbers
*/
class SquaredNumber{
   public static void main(String[] args) {
      
       for(int i=1; i<=20; i++) { // iterating for first 20 numbers
           int square = i*i; // finding square
           System.out.println(i+" squared "+square);
       }
   }
}

/*

OUTPUT:

1 squared 1
2 squared 4
3 squared 9
4 squared 16
5 squared 25
6 squared 36
7 squared 49
8 squared 64
9 squared 81
10 squared 100
11 squared 121
12 squared 144
13 squared 169
14 squared 196
15 squared 225
16 squared 256
17 squared 289
18 squared 324
19 squared 361
20 squared 400

*/