Leap year - Main is set wrong - java

Error: Duplicate local variable yearb
I been asked to put the month, year , day 3 separate variables.
You will send these values to a convertdays method that will return the date in the form of one long integer using the following formula: your long year will be the sum of the year multiplied by 10000, the month multiplied by 100, and the day. This is a formula used to put the date in a form that can be sorted (we will not be sorting dates, just putting it into a form that can be sorted). Back in main you will print this labeled long integer.
/* rayane seridj
* 5-10
* */
import java.util.Scanner;
public class hw2222
{
public static void main (String [] args)
{
int month, day, year,dates,yearb;
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Date in The Format mm/dd/yyyy : ");
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();
boolean yearb;
yearb = leap(year);
if (yearb == true)
System.out.println (year + " is a leap year ");
else
System.out.println (year + " is not a leap year");
}
public static int convertdays (int month ,int day, int year)
{
int yy,dd,mm,dates;
yy =10000 *year;
mm = 100 *month;
dd= day;
dates =yy + mm +dd;
return dates;
}
public static boolean leap(int year)
{
if (year % 400 == 0 || (year % 4 ==0 && year %100 !=0))
return true;
else
return false;
}
}

You declared variable yearb twice:
at int month, day, year,dates,yearb;
at boolean yearb;
A variable can only be declared once in a particular context (method, global, etc).
Try the code below (note a new variable named isLeap):
/* rayane seridj
* 5-10
* */
import java.util.Scanner;
public class hw2222
{
public static void main (String [] args)
{
int month, day, year,dates,yearb;
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Date in The Format mm/dd/yyyy : ");
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();
boolean isLeap;
isLeap = leap(year);
if (isLeap)
System.out.println (year + " is a leap year ");
else
System.out.println (year + " is not a leap year");
}
public static int convertdays (int month ,int day, int year)
{
int yy,dd,mm,dates;
yy =10000 *year;
mm = 100 *month;
dd= day;
dates =yy + mm +dd;
return dates;
}
public static boolean leap(int year)
{
if (year % 400 == 0 || (year % 4 ==0 && year %100 !=0))
return true;
else
return false;
}
}

Related

Evaluate Multiple Years to Find a Leap Year Using a Boolean Utility Method

Good afternoon everyone,
I am having an issue calculating multiple leap years from a boolean utility method. I've been told to use a loop, but have a hard time figuring out how and where to put it.
import java.util.Scanner;
public class LeapYear {
private static boolean isLeapYear(int year) {
if (year % 400 == 0 && year % 100 == 0) {
return true;
}
if (year % 100 == 0) {
return false;
}
if (year % 4 == 0) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
int quitter = 0;
int negative = 0;
int year = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a year or -1 to quit: ");
year = scan.nextInt();
while (year != -1 && year < 1582) {
System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
year = scan.nextInt();
}
if(year == -1) {
negative += year;
quitter++;
}
System.out.println(isLeapYear(year));
}
}
Just use a do-while loop:
public static void main(String[] args) {
int year = 0;
Scanner scan = new Scanner(System.in);
do{
System.out.println("Enter a year or -1 to quit: ");
year = scan.nextInt();
while (year < 1582) {
System.out.print("The Gregorian calendar was not adopted until 1582, please enter a year after 1582: ");
year = scan.nextInt();
}
if(year != -1) {
System.out.println(isLeapYear(year));
}
}while(year!=-1);
}

I can't get my program to print out a Leap year

The last method I was trying to get it to print out my program can't seem to get it to do it. I don't know what to do. And I'm in an online class so there is no one I could really ask. My teacher takes three days to reply.
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
displayInstructions();
int year = getYear();
isLeap(year);
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(boolean year) {
boolean isLeapYear = false;
if (year % 4 == 0 && year != 100) {
isLeap = true;
}
else {
isLeap false;
}
}
public static void displayResuls( boolean isLeap, boolean year) {
if (isLeap)
{
System.out.println("Year" +year+" is a Leap Year.");
}
else {
System.out.println("Year" +year+" is not a Leap Year");
}
}
}
You never call displayResuls(isLeap(year), year).
Edit: also your declaration of displayResults is wrong, it should be int year instead of boolean year.
Your isLeap function should be like:
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
isLeapYear = true;
} else {
isLeapYear = false;
}
return isLeapYear;
}
Main mistake I think in condition. It is more complicated than yours.
Correct one:
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
For example, 2000 - leap, but 2100 - not.
This is your code rewritten. Just like others said, you never called displayResuls and there was some minor typos. Keep coding ;)
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
displayInstructions();
int year = getYear();
boolean ans = isLeap(year);
displayResuls(ans, year);
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if (((year % 4) == 0) && (year != 100)) {
isLeapYear = true;
} else {
isLeapYear = false;
}
return isLeapYear;
}
public static void displayResuls(boolean isLeap, int year) {
if (isLeap) {
System.out.println("Year " + year + " is a Leap Year.");
} else {
System.out.println("Year " + year + " is not a Leap Year");
}
}
}
Made few changes to your program and it works fine
public static void main(String[] args) {
displayInstructions();
int year = getYear();
System.out.println("Entered Year is "+isLeap(year));
}
public static void displayInstructions() {
System.out.println("This program asks you to enter a year as a 4 digit number. "
+ "The output will indicate whether the year you entered is a leap year.");
}
public static int getYear() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = reader.nextInt();
return year;
}
public static boolean isLeap(int year) {
boolean isLeapYear = false;
if (year%4 == 0 && year != 100) {
return isLeapYear = true;
}
else {
return isLeapYear = false;
}
}

Leap Year Calculations using Method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
import java.util.Scanner;
public class LeapYearTester{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a year");
int year_input = input.nextInt();
// now make a Year object
Year theYear = new Year(year_input);
// now check to see if it's a leap year
if (theYear.isLeapYear())
System.out.println("That's a leap year!");
else
System.out.println("That's not a leap year!");
}
}
public class Year
{
// declare your instance variables here
private int theYear;
private int Year;
// write your constructor here
public Year(int y)
{
y=Year;
}
static boolean isLeapYear(final int Year) {
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
//(((Year%4==0) || (Year%400)) && ((!Year%100==0) || (Year%400=0)))
}
}
The above is the Main and cannot be changed. Having trouble with class.
Not much modification of your original attempt other than adding a check to see if the user entered a valid year in this case between 1000 and 2999 inclusive using regex (I will leave it as a task for you to modify this code for what you consider as a valid year):
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LeapYearTester {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a year: ");
String yearInput = "";
String pattern = "^[12][0-9]{3}$";
Pattern r = Pattern.compile(pattern);
//checking is valid year
while(input.hasNext()){
yearInput = input.nextLine();
Matcher m = r.matcher(yearInput);
if(m.find()) {
System.out.println("You entered the valid year: " + m.group(0));
break;
} else {
System.out.println("Error: Not a valid year");
System.out.print("Please enter a year between 1000 and 2999 inclusive: ");
}
}
Year theYear = new Year(Integer.valueOf(yearInput));
if(theYear.isLeapYear()) {
System.out.println("That's a leap year!");
} else {
System.out.println("That's not a leap year!");
}
}
public static class Year {
private int year;
private Year(int y) {
year = y;
}
private boolean isLeapYear() {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
}
}
Example usage: Testing a leap year
Please enter a year: 2016
You entered the valid year: 2016
That's a leap year!
Example usage: Invalid input and testing a non leap year
Please enter a year: 3000
Error: Not a valid year
Please enter a year between 1000 and 2999 inclusive: 2015
You entered the valid year: 2015
That's not a leap year!
Try it here!

I cant quite figure out how to get these public integers to work

PUBLIC CLASS
public class MonthDays {
private int month;
private int year;
public int Month(int x){
month = x;
return x;
}
public int Year(int y){
year = y;
return y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 || //determines if the the month entered is a month with 31 days or not
month == 7 || month == 8 ||month == 10 || month == 12){
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
DRIVER CLASS
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
Month month = new Month(reader.nextInt());
System.out.println("Enter year: ");
Year year = new Year(reader.nextLine());
}
}
im not too sure where i went wrong, but from my understanding this should be working. but im also pretty certain its a dumb error somewhere.
The class you're testing is MonthDays. You have methods Month and Year (which should probably be called setMonth and setYear).
MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.Month(reader.nextInt());
System.out.println("Enter year: ");
md.Year(reader.nextInt());
or, if you rename the methods,
MonthDays md = new MonthDays();
System.out.println("Enter month: ");
md.setMonth(reader.nextInt());
System.out.println("Enter year: ");
md.setYear(reader.nextInt());
or, add a constructor to MonthDays that takes month and year.
public MonthDays(int month, int year) {
this.month = month;
this.year = year;
}
and call it like
System.out.println("Enter month: ");
int month = reader.nextInt();
System.out.println("Enter year: ");
int year = reader.nextInt();
MonthDays md = new MonthDays(month, year);
Finally, you can call getNumberOfDays once you have a MonthDays instance. Like
System.out.println(md.getNumberOfDays());
This should get your code working. Please read about constructors in Object-Oriented Programming.
public class
public class MonthDays {
private int month;
private int year;
public MonthDays(int x, int y){
month = x;
year = y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 month == 7 || month == 8 ||month == 10 || month == 12){ //determines if the the month entered is a month with 31 days or not
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
driver class
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
int days;
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
x = reader.nextInt();
System.out.println("Enter year: ");
y = reader.nextInt();
MonthDays md = new MonthDays(x, y);
days = md.getNumberOfDays();
System.out.println("Number of days= " + days);
}
}
Hai You have made some basic mistakes in your code .
1) Month is a method ,not a class.You should have needed an instance of MonthDays to call method Month .
As per java coding standard you should have written that as setMonth() or setYear()
2) Method Month return an int value.You should have assigned that to int value.
3) Argument of Year method also a int value.You have specified that a String value now
public class MonthDays
{
private int month;
private int year;
public int setMonth(int x){
month = x;
return x;
}
public int setYear(int y){
year = y;
return y;
}
public int getNumberOfDays(){
if (month == 1 || month == 3 || month == 5 || //determines if the the month entered is a month with 31 days or not
month == 7 || month == 8 ||month == 10 || month == 12){
return 31;
}
if (year % 4 == 0 && month == 2){ //determines if the month is february on a leap year
return 29;
}
if (year % 4 != 0 && month ==2){ //determines if the month is february on a non leap year
return 28;
}
else{ //any other condition is a month with 30 days
return 30;
}
}
}
import java.util.Scanner;
public class MonthDaysDriver {
public static void main(String[] args) {
int x;
int y;
MonthDays MD=new MonthDays();
Scanner reader = new Scanner(System.in);
System.out.println("Enter month: ");
int month = MD.setMonth(reader.nextInt());
System.out.println("Enter year: ");
int year = MD.setYear(reader.nextInt());
int numberOfDays=MD.getNumberOfDays();
System.out.println("Number oF days in that month :"+numberOfDays);
}
}

Need help method calling program - day number in year

basically I am in the middle of my program that involves asking a user to enter a day,month, and year, and based on that entry I am supposed to create a method that calculates the number of days in the year, but of course I need a method that defines how many days are in each month, and a method that determines whether or not it is a leap year or not. I have established my methods, user input, but now I am having method/return issue. Here is what I have so far:
import java.util.Scanner;
public class DayNumber{
public static void main(String[] args){
int year;
int month;
int day;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the date's year (0001 - 9999): ");
year = keyboard.nextInt();
System.out.print("Enter the date's month (1 - 12): ");
month = keyboard.nextInt();
System.out.print("Enter the date's day (1 - 31): ");
day = keyboard.nextInt();
}
public static int numberOfDays(int day, int month, int year){
int numberOfDays;
return numberOfDays;
}
public static int daysInMonth(int month, int year){
int daysInMonth;
if (month ==1 || month==3 || month==5 || month==7 || month==8 ||
month==10 || month==12){
month = 31;}
if (month ==4 || month ==6 || month==9|| month==11){
month = 30;}
else if (month ==2){
if (){
}
}
return daysInMonth;
}
public static boolean isLeapYear(int year){
if (((year%4 == 0) && (year%100 != 0)) || (year%400 ==0)){
return true;
}
else{
return false;
}
}
}
I would appreciate any advice or tips. I am really new to Java so go ahead and critique away! Thanks.
There are several different ways. Yours looks fine to me.
But my favorite is this tiny little line :
daysInMonth = (month === 2) ? (28 + isLeapYear) : 31 - (month - 1) % 7 % 2;
If you are asking yourself : Why % 7 % 2 ?
Well, you have noticed that starting from august, the pattern is reverted, that's what we are doing here. We say that between 0 - 6 it's normal then if it's 7 (7 % 7 = 0) we are going back from the beggining). Then the % 2 is to alternate between 0 - 1.
Hope I made myself clear
Although your question is not clear. I tried to understand and here is my understanding:
Input date
Display number of days in month
Display whether year is leap year or not
Here is what I will do
Class MyDateHomework {
public static boolean isLeapYear(int year) {
//put logic to find leapyear
}
public static int daysInMonth(int month, int year) {
if(month == 2) {
return isLeapYear(year) ? 28 : 29;
}
//put logic to find days in month
}
public static void main(String[] args) {
//take input and call method to do homework
}
}

Categories