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;
}
}
Related
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);
}
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!
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;
}
}
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);
}
}
The program I'm writing is to determine whether or not a year is a leap year. This is an assignment, so I am required to use the four methods I wrote inside the program. The program compiles and runs, it asks for user input at the appropriate places, but it doesn't take the input into the program. Also it's saying that the year is a leap year and looping no matter what has been inputted. I've very confused as to where there error is, since this program seems match the examples we were given.
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
boolean repeat;
String computeanother, yes="yes";
Scanner kb=new Scanner(System.in);
int year = -1;
boolean leap;
do
{
displayInstructions();
getYear(year);
leap = isLeap(year);
displayResults(year, leap);
System.out.println("Would you like to compute another year?");
computeanother = kb.nextLine();
if(computeanother.equals(yes))
repeat=true;
else
repeat=false;
} while(repeat=true);
}
public static void displayInstructions()
{
System.out.println("This program is designed to predict whether or not a year is a leap year.");
System.out.println("When prompted please enter a positive number for the year.");
System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
}
public static void getYear(int year)
{
Scanner kb = new Scanner(System.in);
do {
System.out.println("Please enter the year.");
year=kb.nextInt();
} while (year < 0);
}
public static boolean isLeap(int year)
{
boolean leap;
if ((year%4==0 && year%100 != 0) || year%400==0){
leap = true;
return true;
} else {
leap = false;
return false;
}
}
public static void displayResults(int year, boolean leap)
{
if (leap = true) {
System.out.println("The year " +year);
System.out.println("is a leap year.");
} else {
System.out.println("The year " +year);
System.out.println("is not a leap year.");
}
}
}
Thanks for everyone's help! The edited code looks like this:
import java.util.Scanner;
public class LeapYear{
public static void main(String[] args){
boolean repeat;
String computeanother, yes="yes";
Scanner kb=new Scanner(System.in);
int year = -1;
boolean leap;
do
{
displayInstructions();
getYear(year);
leap = isLeap(year);
displayResults(year, leap);
System.out.println("Would you like to compute another year?");
computeanother = kb.nextLine();
repeat = computeanother.equals(yes);
}while(repeat);
}
public static void displayInstructions()
{
System.out.println("This program is designed to predict whether or not a year is a leap year.");
System.out.println("When prompted please enter a positive number for the year.");
System.out.println("Once the program has run completely, it will state the year and whether it is a leap year.");
}
public static int getYear(int year)
{
Scanner kb = new Scanner(System.in);
do{
System.out.println("Please enter the year.");
year=kb.nextInt();
}while (year < 0);
return year;
}
public static boolean isLeap(int year)
{
boolean leap;
year = getYear(year);
if ((year%4==0 && year%100 != 0) || year%400==0){
leap = true;
return true;}
else{
leap = false;
return false;}
}
public static int displayResults(int year, boolean leap)
{
year = getYear(year);
if (leap == true){
System.out.println("The year " +year);
System.out.println("is a leap year.");}
else{
System.out.println("The year " +year);
System.out.println("is not a leap year.");}
return year;
}
}
while(repeat=true);
In the while loop should be:
while(repeat == true);
or
while(repeat);
Aside from this being noted by everyone it can be noted you make this mistake twice:
if (leap = true) {
Should be:
if (leap == true) {
or
if (leap) {
You can also shorten your code:
do{
displayInstructions();
getYear(year);
leap = isLeap(year);
displayResults(year, leap);
System.out.println("Would you like to compute another year?");
computeanother = kb.nextLine();
repeat = computeanother.equals(yes) //this line makes code shorter
} while(repeat);
Indeed, always avoid code redundancy like this famous pattern:
if(expression) return true; else return false;
That becomes: return expression;
Change this:
while(repeat=true);
to
while(repeat==true);
or
while(repeat);
Here while(repeat=true); you are assigning a value, not comparing. while(repeat==true); or while(repeat); this will compare the value. It's always better to test like this while(repeat); instead of obvious while(repeat==true);. I hope it helps.
And you are not getting value for year as -1 because, you are returning from this method getYear(year); but ignoring the value. Change it to:
year = getYear(year);
This should work.