Question

Instructions ASSIGNMENT 2 is at bottom of the question. Very simple program and these intructions are...

Instructions

ASSIGNMENT 2 is at bottom of the question. Very simple program and these intructions are only to modify it slightly, should only take about 10 minutes says my proffessor.

Your file should be named “Main.java”.
  1. Ask the user how many year inputs he wants to check - an integer.
    1. Assume that the user always gives an input which is between 1 and 5 (inclusive).
  2. Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop).
  3. Check for each year input whether that year is a leap year or not (Assignment 2).
    1. Take year input (positive integer - should be between 1500 and 2017, inclusive).
    2. If the year input is not between 1500 and 2017, do not check for leap year, rather print that this year cannot be checked.
    3. If the input year is a leap year, print that year is a leap year; otherwise, print that the year is not a leap year.
    4. make sure to handle the centurial years, i.e., years ending in 00, e.g., 1900, 1800, 1700, 2000, etc.?
  4. Copy the code written for Assignment 2 and add the 'while loop' on top of that. Use while loop only for this assignment.

Goals

  1. Gain experience in input taking using the scanner class in Java.
  2. Gain experience in if/else selection statement in Java.
  3. Gain experience in while loop techniques in Java.

Sample Code Structure

// add necessary import commands

public class samplecode{

public static void main (String[] args){

// create the required variables first.

while (not all inputs have been taken - add this condition here){

//add the code from assignment 2.

}

}

}

Test Your Program

  • Once the code is typed, you will compile+run (execute) your code.
  • You will input the number of years (integer input).
  • Next input the actual year values between 1500 and 2017  (integer input).
  • See the output given by your code.

Sample Output

Your output should look like this:

  • How many years do you want to check (must be between 1 and 5)? 2
    What's the year you want to test? (make sure it's between 1500 and 2017): 1400
    This year cannot be checked. Try again!
    What's the year you want to test? (make sure it's between 1500 and 2017): 2016
    Yes, 2016 is a leap year!
  • How many years do you want to check (must be between 1 and 5)? 3
    What's the year you want to test? (make sure it's between 1500 and 2017): 1400
    This year cannot be checked. Try again!
    What's the year you want to test? (make sure it's between 1500 and 2017): 2016
    Yes, 2016 is a leap year!
    What's the year you want to test? (make sure it's between 1500 and 2017): 2017
    Nope, 2017 is not a leap year!

ASSIGNMENT 2:

import java.util.Scanner;

public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("What year do you want to test? (make sure it's between 1500 and 2017): ");
int year = sc.nextInt();
  
if(year < 1500 || year > 2017)
System.out.println("This year cannot be checked. Try again!");
else
{
if(year % 400 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else if(year % 100 != 0 && year % 4 == 0)
System.out.println("Yes, " + year + " is a leap year!");
else
System.out.println("No, " + year + " is NOT a leap year!");
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:-

import java.util.Scanner;

class MyClass {
public static void main(String args[]) {
   Scanner sc = new Scanner(System.in);
   System.out.println("How many years do you want to check (must be between 1 and 5)?");
   int n=sc.nextInt();
   while(n>0)
   {
       System.out.print("What year do you want to test? (make sure it's between 1500 and 2017): ");
       int year = sc.nextInt();
       if(year < 1500 || year > 2017)
       System.out.println("This year cannot be checked. Try again!");
       else
       {
       if(year % 400 == 0)
       System.out.println("Yes, " + year + " is a leap year!");
       else if(year % 100 != 0 && year % 4 == 0)
       System.out.println("Yes, " + year + " is a leap year!");
       else
       System.out.println("No, " + year + " is NOT a leap year!");
       }
       n--;
   }
}
}

OUTPUT:-

How many years do you want to check (must be between 1 and 5)? 3
What year do you want to test? (make sure it's between 1500 and 2017): 1400
This year cannot be checked. Try again!
What year do you want to test? (make sure it's between 1500 and 2017): 2016
Yes, 2016 is a leap year!
What year do you want to test? (make sure it's between 1500 and 2017): 
No, 2017 is NOT a leap year!

// If any doubt please comment

Add a comment
Know the answer?
Add Answer to:
Instructions ASSIGNMENT 2 is at bottom of the question. Very simple program and these intructions are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This question deals with extending already existing Java code via a while loop. Ask the user...

    This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...

  • Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the...

    Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the user for a year as an input (positive integer - should be between 1500 and 2020, and can include 1500 and 2020). 2. If the year input is not between 1500 and 2020, do not check for leap year. Instead, print that this year cannot be checked. 3. If the input year provided by the user is a leap year, print that year is...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Java Help Please: I'm doing something wrong with my while loop. I want my code to...

    Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • The menu Here is what the menu should look like when running your program:                              &nbsp

    The menu Here is what the menu should look like when running your program:                                         ***********************************                                         *         FUN MENU             *                                         ***********************************                                                 <S>um of natural integers                                                 <L>eap year check                                                 <C>ount vowels                                                 <E>xit Menu tasks Sum of natural integers ask users to enter an integer (n), then use for loop to compute the sum of the first n integers and finally display the result. If users enter negative integer display an error message. Leap year check...

  • Follow exactly all the instructions provided below: Do not deviate from the instructions provided below or...

    Follow exactly all the instructions provided below: Do not deviate from the instructions provided below or add any unnecessary code to program. Pay close attention to the directions! Make absolutely certain that you are doing exactly what the assignment asks you to do without any more code required. Keep the program's code as simple as possible and begineer friendly level with no advanced level Java concepts/skills. Write a program that asks the user for a number. The program should check...

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT