This is the code I'm using to figure out if a year is a leap year or not:
public class leap_year {
public static void main(String args[])
{
isLeapYear(2009)
}
public static boolean isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
}
But when I run the code nothings shows up. What am I doing wrong?
Nothing prints out, because you haven't ordered it! Use System.out.println(...) to print the output out to the console.
Your code should look like this:
boolean bool = isLeapYear(2009);
System.out.println(bool);
Or print it out directly without using any variable:
System.out.println(isLeapYear(2009));
Your code is working perfectly, you just need to print the result of the method somewhere...
use System.out.println and your method as parameter....
System.out.println(isLeapYear(2009));
System.out.println(isLeapYear(2016));
Related
Im currently working on a program to given integer input from user, print only those ones contain numbers 1 2 or 3. Here is my code so far:
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int[] x=new int[5];
System.out.println("Enter 5 integers: ");
for(int i=0; i<x.length;i++) {
x[i]=s.nextInt();
boolean temp=recursion(x[i]);
if(temp==true) {
System.out.print(x[i]);
}
}
}
public static boolean recursion(int y) {
if(y%10==1 || y%10==2 || y%10==3) {
return true;
}
else if(y%10==0) {
return false;
}
else {
int remain=y/10;
recursion(remain);
}
}
So my approach is quite simple, I used a simple bool recursion to return true or false, true if it does contain 1 2 or 3 and false if not. The problem I am having is that I am not returning anything in my recursion else statement. I know I have to return something but not sure what to return or if its necessary. Anything I can change to make this work?
Your base cases for recursion are a little off, so in the statement
else if(y%10==0), the number 100 would cause this to fail even though it is valid. you want your false base case to actually be else if (y == 0) which means you have gone through the entire number. the solution looks like:
public static boolean recursion(int y) {
int positive = Math.abs(y);
if(positive%10==1 || positive%10==2 || positive%10==3) {
return true;
} else if (positive == 0) {
return false;
}
return recursion(positive / 10);
}
edit: Calling absolute value on the number makes it work for negative numbers as well, but you can call abs on the integer being passed in to the same effect.
I think you just have to do
return recursion(remain);
Use below code, hope this will help.
public static boolean recursion(int y) {
if(y%10==1 || y%10==2 || y%10==3) {
return true;
}
else if(y%10==0) {
return false;
}
else {
int remain=y/10;
return recursion(remain);
}
}
I am trying to create a method that will return true if the inputted integer has a one in it, and false if it does not. The method works correctly when the inputted number doesn't have a one or ends in a one. But if the one is not the last digit in the int, it incorrectly returns false. Any ideas whats wrong? Here is my script:
public static boolean hasOne(int n) {
boolean retval = false;
if (n % 10 == 1) {
retval = true;
} else {
dropLastDig(n);
}
return retval;
}
public static void dropLastDig(int input) {
int newNum = input/10;
if (newNum > 0) {
hasOne(newNum);
}
}
1000 should return true
211 should return true
1 should return true
3 should return false
234 should return false
You can use recursive function, it's faster:
public static boolean hasOne(int n) {
if(n<0) return hasOne(-n); // check for negatives
if(n==0) return false; // exit condition
if (n % 10 == 1) {
return true;
}
return hasOne(n/10);
}
Or cast it to String, and then check:
String.valueOf(n).contains("1");
Just use String.valueOf instead
return String.valueOf(x).contains("1");
try String.valueOf(n).contains("1")
I have this piece of code, I think I have all the braces covered. Still getting braces not closed error. When I put more braces, then gives me something else. Can you anybody tell me what I am doing wrong here?
public static boolean isValid(int day, int month, int year)
{
if (year < 1900)
{
return false;
}
else {
if (month>0 && month<13)
{
if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
return day > 0 && day <=31;
}
else if (month==4 || month==6 || month==9 || month==11)
{
return day>0 && day<=30;
}
else if (month==2)
{
if (isLeap(year))
{
//(d= 29);
return day>0 && day <=29;
}
else {
return day>0 && day<= 28;
}
}
}
}
}
}
Because of your if-else conditions it may not be able to return a boolean value.That is the problem.Check your code's logic.
I don't think you are missing any braces but you are not returning values for every possible execution path.
if you put a return false at the bottom of the function it will compile.
Little tip, to save the reader all of those nested ifs and braces:
if(day>0 && day<= 28){
return true;
else{
return false;
}
you can do this:
return day > 0 && day <= 28;
if you're using eclipse use ctrl shift f to balance braces easier to read and see if you miss one or need one
public static boolean prime(int n){
if(n<=1)
return false;
int z=2;
if(n==2)
return true;
while(z<Math.sqrt(n)){
if(z mod n==0)
return false;
z++;
}
return true;
}
Any clues what is wrong with my code?
I am receiving 7 errors of "class, interface, or enum" expecting...
You cant execute stand alone code, everything must exist in a class, interface or enum.
This needs to live inside a class. This should be in a file called MyClass.java
E.g.
public class MyClass {
public static boolean prime(int n){
if(n<=1)
return false;
int z=2;
if(n==2)
return true;
while(z<Math.sqrt(n)){
if(z mod n==0)
return false;
z++;
}
return true;
}
}
It can then be called by running MyClass.prime(7);
As mentioned by others, mod is also an invalid keyword and should be replaced by the % operator
If this is all of your code, you will need to enclose it in a class as the error indicates :)
Secondly, there's no mod keyword in Java, replace with %. Putting the 2 together:
public class MyPrimeTest {
public static void main(String[] args) {
boolean primeCheck = prime(43);
...
}
public static boolean prime(int n) {
if (n <= 1) {
return false;
}
int z = 2;
if (n == 2) {
return true;
}
while (z < Math.sqrt(n)) {
if (z % n == 0) {
return false;
}
z++;
}
return true;
}
}
Classes
Modulo operator
"class, interface, or enum expected" error occurs mostly due to missing {} curly brackets. Check your program thoroughly.
If the above mentioned is all your code,then you should encapsulate it within a class.
Dont forget to write main() too.
There is no 'mod' keyword in java. Use '%' instead.
import java.util.*;
class Prime
{
public ...........main(.... args[])
{
//accept integer
if(prime(n))//calling prime()
..
}
and then your method..
Cheers!
I have an assignment for school to make a program which results in either true or false. It's about wether a year is a leap year or not. The problem I have at the moment is that i'm using a public static boolean instead of a public boolean.
This is my code:
public class Assignment {
static boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
isLeapYear(year);
}
public static boolean isLeapYear(int year) {
if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
return isLeapYear;
}
}
The int year is 2000 at the moment but the rules are like this:
A leap year is a year wich can be divided by 4 unless the year is the beginning of a new century (1700, 1800, 1900.....). So even though you can divide 1900 by 4 you can't divide it by 400 so it's false.
So again the question: What do I need to do so i'm able to use a public boolean instead of a public static boolean?
You would need to create an instance of your class to invoke that method from your main method, if you want to make your method non-static. And then you can make your isLeapYear variable non-static: -
boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
new Assigment().isLeapYear(year);
}
public boolean isLeapYear(int year) {
// access isLeapYear as `this.isLeapYear` or just `isLeapYear`
}
But, precisely, you don't need to store your result in a boolean variable. If you want to return a boolean value of some expression, then you can just return that expression.
So, just having this code in your method would also work fine, and it is more readable, and let that method be static: -
return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
And from your main call: -
System.out.println("Year : " + year + ", is leap year: " + isLeapYear(year));
You don't need to store this result anywhere.
Use:
public static boolean isLeapYear(int year)
{
return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0));
}
Static methods can only access static variables, only instance methods can access instance methods, which you can infer if you think Object oriented.
Just in case you should store the Boolean isLeapYear
public class Testing {
boolean isLeapYear;
public static void main(String[] args)
{
int year = 2000;
new Testing().isLeapYear(year);
}
public boolean isLeapYear(int year) {
if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
isLeapYear = true;
else
isLeapYear = false;
System.out.println(isLeapYear);
return isLeapYear;
}
}
Does your assignment say it has to be stored in a class or instance variable? If not, there is no need for public boolean isLeapYear or public static boolean isLeapYear, just return the result of the calculation and store it in a local variable like this:
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
in main method:
int year = 2000;
boolean isLeap = isLeapYear(year);
System.out.println(isLeap);