Question

Hi, I need this program written in Java for my intro CS class. Thank you in...

Hi, I need this program written in Java for my intro CS class. Thank you in advance. (also here is the description given in class for the code)

Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period.

- You should use the method isLeap which returns "true" if the year is a leap year, otherwise it should return false.

- In your main(), run a loop with year with iterating from 101-2100 and just invoke the isLeap() method that you wrote.

- For the printing, use System.out.print(year+" "); to print each year separated by a white space.

- Use a counter to keep track of the number of leap years you found inside, if this number is divisible by 10, then print a blank line. This will ensure 20 numbers are printed on each line.

-Finally after the loop ends, print the counter.

Homework Answers

Answer #1

The explanation is done in the code itself. if any queries write a comment. If it is helpful or understood upvote Thank you.

SOLUTION:

public class HelloWorld{
  
static boolean isLeap(int year){
// a year which is divisible by 400 is leap year so return true
if(year % 400 == 0)
{
return true;
}
//a year which is divisible 100 but not with 400 is not leap year since it is not entered in the previous condition that divisible by 400 and it divisible 100 so it is not a leap year
else if (year % 100 == 0)
{
return false;
}
//normal one a year divisible by 4 is leap year
else if(year % 4 == 0)
{
return true;
}
//if not divisible by 4 and 400 is not a leap year
else
{
return false;
}
}

public static void main(String []args){
//declare counter to store the leap years details
int counter=0;
//iterate through the years
for(int i=101;i<=2100;i++){
//check fot leap year if it is true
if(isLeap(i)){
//increase counter and print number and if mod 10 is 0 print a new line
counter++;
System.out.print(i+" ");
//for new line after 10 numbers
if(counter%10==0){
System.out.println("");
}
}
}
//print the total count
System.out.println("\nTotal leap years are "+counter);
}
}

CODE AND OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in java Write a program that displays all the leap years, eight per line, from 101...
in java Write a program that displays all the leap years, eight per line, from 101 to 2500, separated by one space. Meanwhile display the number of leap years in this period of year.
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Write a java program that calculates the number of duplicate words in a sentence. You may...
Write a java program that calculates the number of duplicate words in a sentence. You may assume that the input consists only 52 English characters with a single white-space as the separator between words (not punctuations in the inputted sentence). You need consider uppercase and lowercase letters the same. You may get the input from the keyboard. (You need to use Set and/or Hashset). What I have so far import java.util.HashSet; import java.util.Scanner; public class Duplicate { int counter; int...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
For this part, you will write a PostfixCalculator class that has methods for processing each possible...
For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If...
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with...
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for the various delicious...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...