I need help writing this program
In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Write a program that asks the user how many years they want to check.
For that number of times the program accepts as data a year. The program will then determine if the year is a leap year or not. The program will keep doing this until the correct number of years is checked.
Display the percentage of input years that are leap years, as well as the percentage that are not leap years. Confirm these add to 100.
Use a sentinel instead of asking how much data you have
using formatting as described in the text, make a table of your output. Real numbers should have 2 decimal places, and decimal points should line up
can you put the comments on each code for what they do. Thank you if you can help me
Re: I need help writing this program
Hello kev2000, welcome to the forums.
Have you started any of this assignment yet? Please post what you have so far and we can guide you in the right direction.
Re: I need help writing this program
I haven't started yet, I don't know where to start. It will be nice if you can help me understand.
Re: I need help writing this program
Quote:
Originally Posted by
kev2000
I haven't started yet, I don't know where to start. It will be nice if you can help me understand.
Here is some code to help you get started.
It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...
Code :
import java.util.Scanner;
public class Kev2000 {
/**
* JavaProgrammingForums.com
*/
private static int year;
private static boolean isLeapYear = false;
public void divisibleBy4() {
String myYear = Integer.toString(year);
myYear = myYear.substring(2, 4);
int myYearInt = Integer.parseInt(myYear);
for (int a = 0; a < myYearInt; a++) {
int calc = (4 * a);
if (calc == myYearInt) {
System.out.println(year + " is divisible by 4");
//Next part of the calculations needed here
break;
}
}
isLeapYear = false;
}
public static void main(String[] args) {
Kev2000 k = new Kev2000();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
year = sc.nextInt();
k.divisibleBy4();
}
}
Re: I need help writing this program
Quote:
Originally Posted by
JavaPF
Here is some code to help you get started.
It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...
Code :
import java.util.Scanner;
public class Kev2000 {
/**
* JavaProgrammingForums.com
*/
private static int year;
private static boolean isLeapYear = false;
public void divisibleBy4() {
String myYear = Integer.toString(year);
myYear = myYear.substring(2, 4);
int myYearInt = Integer.parseInt(myYear);
for (int a = 0; a < myYearInt; a++) {
int calc = (4 * a);
if (calc == myYearInt) {
System.out.println(year + " is divisible by 4");
//Next part of the calculations needed here
break;
}
}
isLeapYear = false;
}
public static void main(String[] args) {
Kev2000 k = new Kev2000();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a year: ");
year = sc.nextInt();
k.divisibleBy4();
}
}
A quick suggestion: wouldn't it be simplier to use mod function?
boolean divBy4 = myYearInt % 4 == 0
Re: I need help writing this program
Quote:
Originally Posted by Dalisra
A quick suggestion: wouldn't it be simplier to use mod function?
boolean divBy4 = myYearInt % 4 == 0
It would indeed be a lot simpler to use that function! Thank you Dalisra. :-B