print a leap year in a certain format - java

i would like to print leap years in between two years given in a format like
'[2120,2024,2028]'.i want them with these commas and square brackets..kindly help me with this.in java
my program:
System.out.print("[");
for(int i=2017;i<=2040;i++)
{
if(i%4==0)
{
System.out.print(i);
if(i!=2040)
{
System.out.print(",");
}
}
}
System.out.print("]");
if i change the value the comma is not working right

It is easier to put a comma before each number and not after. The effect is the same, but then you just need to make the first leap year a special case:
System.out.print("[");
boolean isFirstLeapYear = true;
for (int i=2017;i<=2040;i++) {
if (i%4 == 0 && (i%100 != 0 || i%400 == 0)) {
if (isFirstLeapYear) {
isFirstLeapYear = false;
} else {
System.out.print(",");
}
System.out.print(i);
}
}
System.out.print("]");

There is more rules about leap year.
I suggest you to study this method and use it in your code:
boolean isLeapYear(year) {
if (year % 400 == 0) {
return true;
} else {
if ((year % 4 == 0) && (year % 100 != 0)) {
return true;
} else {
return false;
}
}
}

Related

Method won't run because it returns no string

I'm creating a battleship game where a ship occupies 3 cells and you have to guess which cell. Once they guess it you return "hit" and if not you return "miss". Once they hit all 3 you return "kill". I've written the code but it states I still haven't returned a string.
public class SimpleBattleShip{
int[] shipCoordinates;
int numOfHits;
String updateStatus(int guess){
for(int i=0;i<shipCoordinates.length;i++){
if(guess == shipCoordinates[i]){
numOfHits++;
if(numOfHits ==3){
return "kill";
}else{
return "hit";
}
}else{
return "miss";
}
}
}
}
Have you tried just separating the NumberofHits If statement from the for loop. The problem may be the for loop iterating the whole 'hit check' for each value of 'i' which may cause it to put up false values before tallying the full amount of hits.
I've tried throwing in an else if to maybe tighten the parameters. turn it back to else if you want (this is for hit & miss).
public class SimpleBattleShip {
int[] shipCoordinates;
int numOfHits;
String updateStatus(int guess) {
for (int i = 0; i < shipCoordinates.length; i++) {
if (guess == shipCoordinates[i]) {
numOfHits++;
}
}
if (numOfHits == 3) {
return "kill";
} else if (numOfHits < 3 && numOfHits >= 1) {
return "hit";
} else {
return "miss";
}
}
}

Reverse loop TicTacToe

I've got loop:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
I need it because I work on TicTacToe Java game.
Here is my code for check win:
static boolean checkWin(char dot) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[0][i] == dot && i == 2) {
return true;
}
if (map[1][i] == dot && i == 2) {
return true;
}
if (map[2][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
if (map[i][1] == dot && i == 2) {
return true;
}
if (map[i][2] == dot && i == 2) {
return true;
}
if (map[i][i] == dot && i == 2) {
return true;
}
if (map[i][0] == dot && i == 2) {
return true;
}
}
}
return false;
}
last thing that I need to refactor this method:
static boolean checkWin(char dot) {
if (map[0][0] == dot && map[0][1] == dot && map[0][2] == dot) {
return true;
}
if (map[1][0] == dot && map[1][1] == dot && map[1][2] == dot) {
return true;
}
if (map[2][0] == dot && map[2][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][0] == dot && map[2][0] == dot) {
return true;
}
if (map[0][1] == dot && map[1][1] == dot && map[2][1] == dot) {
return true;
}
if (map[0][2] == dot && map[1][2] == dot && map[2][2] == dot) {
return true;
}
if (map[0][0] == dot && map[1][1] == dot && map[2][2] == dot) {
return true;
}
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
return false;
}
}
I almost done it.
Last thing that i need refactor this part of code:
if (map[0][2] == dot && map[1][1] == dot && map[2][0] == dot) {
return true;
}
to something like
if (map[i][0] == dot && i == 2) {
return true;
}
Main question is here:
for (int i = 0; i<3; i++){
System.out.println(i);
}
output:
0
1
2
I need to reverse this loop to output:
2
1
0
to feel this last part:
if (map[i][0] == dot && i == 2) {
return true;
}
i need to feel this last condition with values 2 1 0
if (map[i][insert here] == dot && i == 2) {
return true;
}
Please Help.
If all you want to do is reverse the loop output, you can just invert what you're doing in the for loop
for (int i = 2; i>=0; i--){
System.out.println(i);
}
output:
2
1
0
As a quick example.

Confused about return value for a public int method [duplicate]

This question already has answers here:
Missing return statement for if/else statement
(8 answers)
Closed 7 years ago.
I'm finishing up a project for school, but I have to create a method that returns a value -1, 0, or 1 based on the alphabetical order of the first letter of name objects. I'm confused on why I keep getting an error asking me for a return value, perhaps I can't see what I'm missing but any help would be appreciated (I'll probably see a teaching assistant tomorrow or the day after).
public int compareTo(Name nameObject) {
if (middleName.equals(null)) {
if (getLastName().charAt(0) < nameObject.getLastName().charAt(0)) {
return -1;
} else if (getLastName().charAt(0) > nameObject.getLastName().charAt(0)) {
return 1;
} else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {
if (getFirstName().charAt(0) < nameObject.getFirstName().charAt(0)) {
return -1;
} else if (getFirstName().charAt(0) == nameObject.getFirstName().charAt(0)) {
return 0;
} else if (getFirstName().charAt(0) > nameObject.getFirstName().charAt(0)) {
return 1;
}
}
} else {
if (getLastName().charAt(0) < nameObject.getLastName().charAt(0)) {
return -1;
} else if (getLastName().charAt(0) > nameObject.getLastName().charAt(0)) {
return 1;
} else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {
if (getFirstName().charAt(0) < nameObject.getFirstName().charAt(0)) {
return -1;
} else if (getFirstName().charAt(0) > nameObject.getFirstName().charAt(0)) {
return 1;
} else if (getFirstName().charAt(0) == nameObject.getFirstName().charAt(0)) {
if (getMiddleName().charAt(0) < nameObject.getMiddleName().charAt(0)) {
return -1;
} else if (getMiddleName().charAt(0) == nameObject.getMiddleName().charAt(0)) {
return 0;
} else if (getMiddleName().charAt(0) > nameObject.getMiddleName().charAt(0)) {
return 1;
}
}
}
}
}
Your "else if" line(s) are dangling. E.g.,
} else if (getLastName().charAt(0) == (nameObject.getLastName().charAt(0))) {
Any "else if" needs to follow with an "else" -- or just a return value -- to ensure something is returned.

Nesting If Statements in Java

I am having trouble nesting these if statements from Python to Java.
def leapyear(yearr):
if (year % 4 == 0):
if (year % 100 == 0):
if (year % 400 == 0):
return True
else:
return False
else:
return True
else:
return False
I currently am working to convert the above to Java:
boolean leapyear(int year) {
if (year % 4==0) {
if (yearr%100==0) {
if (year%400==0) {
else
return false;
}
else
return true;
}
else
return false;
}
}
However, my Java conversion is giving me errors, mainly because I do not think my nested conditionals have the right closed braces. Can you give me any hints or resources that can help me figure this out?
It's not good practice to have so many possible exit points. Also, if you don't want to reinvent the wheel, you can use Java library code, for example:
boolean isLeapYear(int year){
GregorianCalendar cal = new GregorianCalendar();
return cal.isLeapYear(year);
}
Your else statements need braces too, you forgot a return statement for the innermost if, and you misspelled yearr in one location:
boolean leapyear(int year) {
if (year % 4==0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
Java doesn't need the indentation like Python does, but it'd help make your branching structure more readable if you did use it anyway:
boolean leapyear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
You don't need as many return statements here, the year % 400 == 0 already evaluates to a boolean:
boolean leapyear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
} else {
return true;
}
} else {
return false;
}
}
or as a one-liner:
boolean leapyear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
For completeness sake, here is the Python version the way I would write it:
def leapyear(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
or better still, use calendar.isleap() instead.
What about removing the ifs altogether?
public boolean leapyear(int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
heres my attempt:
boolean leapyear(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
return (year % 400 == 0);
}
else
{
return true
}
}
return false;
}
or use this:
return Java.util.GregorianCalendar.getInstance().isLeapYear(year)
boolean leapyear(int year)
{
if (year % 4==0)
{
if (year%100==0)
{
if (year%400==0)
{
return true;
}
else
{
return true;
}
}
else``
{
return false;
}
}
}

Javabat Help: alarmClock

I am working through the JavaBat questions and am confused about my logic.
Here's the task:
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
a boolean indicating if we are on vacation, return a string of the
form "7:00" indicating when the alarm clock should ring. Weekdays, the
alarm should be "7:00" and on the weekend it should be "10:00". Unless
we are on vacation -- then on weekdays it should be "10:00" and
weekends it should be "off".
alarmClock(1, false) → "7:00" alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"
Here's my code:
public String alarmClock(int day, boolean vacation) {
if ( (day >=1 && day <=5) && (!vacation)) {
return "7:00";
} else if ( (day >=1 && day <=5) && (vacation)) {
return "10:00";
} else {
return "off";
}
}
Why do these two tests fail?
alarmClock(0, false) → "10:00" "off" X
alarmClock(6, false) → "10:00" "off" X
Surely, this line covers it?
if (day >=1 && day <=5) && (!vacation))
how about this?
public String alarmClock(int day, boolean vacation) {
if (day >=1 && day <=5) {
return vacation ? "10:00" : "7:00";
} else {
return vacation ? "off" : "10:00";
}
}
Note it does depend if your coding convention allows the use of the turnary operator. But in this case I think the logic is easier to read.
Surely, this line covers it?
if ((day >=1 && day <=5) && (!vacation))
No, that line doesn't cover it. If the day is Sunday or Saturday (0 or 6), the first part of your "and" expression (day >=1 && day <=5) will be false, since 0 and 6 are not between 1 and 5 inclusive.
The only branch that handles days 0 and 6 is your else branch: "off".
This is a great time to use helper methods to express your logic closer to the English description:
if ( isWeekday(day) ) {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
} else {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
}
Then you just need to implement isWeekday:
private boolean isWeekday(int day) {
return /*fill this in*/;
}
Although the ternary operator is useful here, as this problem can be done in a single return statement, the following code below allows readability.
public String alarmClock(int day, boolean vacation) {
if(vacation){ //if we are on vacation
if(day > 0 && day < 6){ //if it is weekday and we are on vacation
return "10:00";
}
else return "off"; //it must be the weekend!
}
//from here on out all the cases where vacation is true have been weeded out
if(day > 0 && day < 6){
return "7:00";
}
else return "10:00";
}
if ((day == 0 || day == 6) && (!vacation) || (day >= 1 && day <= 5) && (vacation)) {
return "10:00";
}
if ((day >= 1 && day <= 5) && (!vacation)) {
return "7:00";
}
return "off";
public String alarmClock(int day, boolean vacation) {
if (((day==0)||(day==6))&&(!vacation)){
return "10:00";
}
else if (((day!=0)||(day!=6))&&(!vacation)){
return "7:00";
}
else if (((day==0)||(day==6))&&(vacation)){
return "off";
}
else{
return "10:00";
}
}

Categories