Off Topic: The Flood
This topic has moved here: Subject: Computer science help!
  • Subject: Computer science help!
  • Pages:
  • 1
  • 2
  • of 2
Subject: Computer science help!

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"

Java

Write a Java method that determines whether a given year is a leap year. Your method should have the
following header:
public static boolean isLeap (int year)

A year is a leap year if it is either:
a. evenly divisible by 400, OR
b. evenly divisible by 4 AND NOT evenly divisible by 100


this boolean stuff confuses me, help!


EDIT:

okay this i what I've got so far

public static boolean isLeap (int year)

{

if( year%400==0||(year%4==0&&year%100!=0))
year = 366;

return year;
}


its telling me its wrong because you can't convert from "int to boolean"

[Edited on 12.09.2012 10:53 PM PST]

  • 12.09.2012 10:28 PM PDT

Ba-BOMB!

  • 12.09.2012 10:32 PM PDT
  • gamertag: [none]
  • user homepage:

I don't know the java syntax and have not taken java but boolean is a true/false. So I would assume set a boolean variable to false then check the conditions. So if Either a or b is true then you know it is a leap year but if both fail then you set the boolean to true.

  • 12.09.2012 10:34 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: Hylebos
Ba-BOMB!


could you perhaps explain a bit further? in english?

  • 12.09.2012 10:34 PM PDT


Posted by: KUZOKU85

Posted by: Hylebos
Ba-BOMB!


could you perhaps explain a bit further? in english?
You check if the remainder after dividing is 0.

[Edited on 12.09.2012 10:38 PM PST]

  • 12.09.2012 10:37 PM PDT

Posted by: KUZOKU85
Posted by: Hylebos
Ba-BOMB!


could you perhaps explain a bit further? in english?
Modulus is your friend. He's the little % operator that gives you the remainder of dividing two numbers.

4 % 3 gives you 1, because three goes into 4 one time and theres one remainder. In your case, figuring out if it's a leap year, if Years % 400 = 0, then it's cleanly divisible.

  • 12.09.2012 10:37 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: wallawalla1992

Posted by: KUZOKU85

Posted by: Hylebos
Ba-BOMB!


could you perhaps explain a bit further? in english?
You check if the remainder after dividing is 0.


How would I write that?

(I haven't been a very good student this semester)

  • 12.09.2012 10:38 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: Hylebos
Posted by: KUZOKU85
Posted by: Hylebos
Ba-BOMB!


could you perhaps explain a bit further? in english?
Modulus is your friend. He's the little % operator that gives you the remainder of dividing two numbers.

4 % 3 gives you 1, because three goes into 4 one time and theres one remainder. In your case, figuring out if it's a leap year, if Years % 400 = 0, then it's cleanly divisible.


oh ok, got it, thanks, I'll see what I can do

  • 12.09.2012 10:41 PM PDT

http://ideone.com/MzSsny

  • 12.09.2012 10:41 PM PDT

Posted by: dazarobbo
http://ideone.com/MzSsny
That's cheating >_>

  • 12.09.2012 10:42 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: dazarobbo
http://ideone.com/MzSsny


That looks way more complicated than anything I've been doing

  • 12.09.2012 10:47 PM PDT

Posted by: KUZOKU85
Posted by: dazarobbo
http://ideone.com/MzSsny


That looks way more complicated than anything I've been doing
It's not super complicated, his is just a clever solution that is unfortunately entirely unhelpful to you as your professor / teacher is likely expecting you to get your answer with the Modulus route.

Here, daz is using a built in java class for calender related purposes, and creates an instance of the calender for the entered year. He then uses a method called getActualMaximum to return the total number of days in that particular year, and compares that to the usual number of days in a year to see if that year is a leap year or not.

[Edited on 12.09.2012 10:52 PM PST]

  • 12.09.2012 10:51 PM PDT

Posted by: KUZOKU85
That looks way more complicated than anything I've been doing
I'll explain the three lines.

java.util.Calendar c = java.util.Calendar.getInstance();
This creates a new Calendar object by firstly declaring an object (c) of type Calendar and assigning it the object returned from Calendar.getInstance(), which is a static method (ie. you don't need to instantiate a Calendar object to call the getInstance method).

Calendar is defined within java.util, which is why I've prefixed it. If you import the packages/namespaces beforehand, you don't have to prefix. For instance.

import java.util;

//...

Calendar c = Calendar.getInstance();


c.set(java.util.Calendar.YEAR, year);Here we modify the Calendar instance (c) with the set method and tell it we're setting the year to the variable named year.

return c.getActualMaximum(java.util.Calendar.DAY_OF_YEAR) > 365;In the final line three things happen in the following order:

1) getActualMaximum is called to return the number of days in the year in the Calendar instance (c).
2) Evaluating the result of 1) and whether that number is greater than 365. If it is, a boolean true is the result.
3) The result from 2) is returned back to the caller who invoked isLeap (in this case the main method).


There is quite a bit of terminology here you might not be familiar with yet.

  • 12.09.2012 11:00 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: dazarobbo

There is quite a bit of terminology here you might not be familiar with yet.


Indeed, can't really use it anyways because I'll get accused of cheating since we haven't learned it


thank you though

[Edited on 12.09.2012 11:06 PM PST]

  • 12.09.2012 11:06 PM PDT

Key

Don't worry, OP. daza will probably accidentally fall onto his keyboard and spit out the code you actually need. He's good like that.

(+1 for finding a reason to post in a thread about stuff I don't understand.)

©

  • 12.09.2012 11:10 PM PDT

Posted by: KUZOKU85
its telling me its wrong because you can't convert from "int to boolean"
That's because you are returning an integer (year) when you should be returning a boolean.

  • 12.09.2012 11:11 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: Hylebos
Posted by: KUZOKU85
its telling me its wrong because you can't convert from "int to boolean"
That's because you are returning an integer (year) when you should be returning a boolean.


how?

  • 12.09.2012 11:15 PM PDT

Ohai, I'm Loscocco (pronounced Loss-cocoa). I'm a college student (computer science major), 3D animator, and long-time Halo player.

public static boolean isLeap (int year)
{

if(year%400 == 0)
{
return true;
}

else if(year%4 == 0 && year%100 != 0)
{
return true;
}

else
{
return false;
}

}




Posted by: KUZOKU85

its telling me its wrong because you can't convert from "int to boolean"


The return type is a boolean (the constructor saying "public static boolean ...."). This means that when you call the method, it is expecting a boolean. By returning year in your method, you are practically doing the same thing as this:

boolean example = 8;

That doesn't make any sense. If you want your method to return year, then the constructor header should be something like public static int instead. If your instructions say that it should be a public static boolean, then you should be returning booleans.

  • 12.09.2012 11:20 PM PDT

return true; //returns true
return false; //returns false
return 1 == 1; //returns true
return 1 != 1; //returns false
return 1 > 2; //returns false
return 1 < 2; returns true

etc...

Those are some ways you can return a boolean value.

[Edited on 12.09.2012 11:23 PM PST]

  • 12.09.2012 11:20 PM PDT

Posted by: KUZOKU85
Posted by: Hylebos
Posted by: KUZOKU85
its telling me its wrong because you can't convert from "int to boolean"
That's because you are returning an integer (year) when you should be returning a boolean.

how?
So, Booleans are either true or false. There are two ways you could either do this.

As I don't want to write your code for you, let me pose a hypothetical situation where we are writing code for the function doIHaveMoreThan10Apples?

This is pseudo code and I'm not too familliar with the exact specifics of Java, but it could look like this:

boolean doIHaveMoreThan10Apples(int Apples)
{
if (Apples > 10):
{
return true;
}
else:
return false;
}

As you can see here, if the result of the if statement is true, then I return true, if the result of the if statement is false, I return false. Or we could simply shortcut this by writing:

boolean doIHaveMoreThan10Apples(int Apples)
{
return (Apples > 10);
}

Because the interiors of those if statements are boolean algebra which will be either be false or true.

Typing computer help while League of Legending is hard.

[Edited on 12.09.2012 11:24 PM PST]

  • 12.09.2012 11:21 PM PDT

"Your eyes are full of hate, forty-one. That's good. Hate keeps a man alive. It gives him strength"


Posted by: Mikelp_1


That helped, thanks

  • 12.09.2012 11:23 PM PDT

Isn't the year 100 (one of many examples I have) a leap year? yet your conditions say it isn't?

Or am I misinterpreting?

  • 12.09.2012 11:25 PM PDT

Posted by: SRQ baller24
Isn't the year 100 (one of many examples I have) a leap year? yet your conditions say it isn't?

Or am I misinterpreting?
Per his conditions it wouldn't be.

  • 12.09.2012 11:27 PM PDT


Posted by: Hylebos
Posted by: SRQ baller24
Isn't the year 100 (one of many examples I have) a leap year? yet your conditions say it isn't?

Or am I misinterpreting?
Per his conditions it wouldn't be.

I just thought his conditions were supposed to be representative of real life.

  • 12.09.2012 11:28 PM PDT
  • gamertag: Hank13
  • user homepage:

You're trying to return an integer value, when your function (Or 'method', I think that's what they call them in Java, right?) is expecting you to return a value of true or false (IE, 1 for true, 0 for false).

So, if it is a leap year, you need to return 1. If it's not a leap year, you need to return 0.

It ought to look something like this (But bear with me, I'm used to C++, not Java):

public static boolean isLeap (int year) {

if( year%400==0||(year%4==0&&year%100!=0)) {
return 1;
}

return 0;
}


If the statement inside the if statement is true, the method returns 1, and it is therefore a leap year. If the the statement inside the if statement is false, it'll return 0, and it is therefore not a leap year.

  • 12.09.2012 11:32 PM PDT

  • Pages:
  • 1
  • 2
  • of 2