Java fix and writes Assume all variables are declared and th

Java fix and writes Assume all variables are declared and the entered city and country\' names contain no spaces. Fix the following Java: Write a validation loop that prompts the user for a leap year between 2004 and 2060. inclusive. If the year is invalid, print an error message and prompt the user again. If the year is valid, print a message that a valid leap year has been entered.

Solution

21)

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       //Scanner class object is used to read the inputs entered by the user
       Scanner keyboard=new Scanner(System.in);
       String country=null;
      
       //Getting the city entered by the user
       System.out.print(\"Enter a city ( \'Last\' to exit ):\");
       String city=keyboard.next();
      
       //This loop continues to executes until the user enters \"Last\" as city
       while(!city.equalsIgnoreCase(\"Last\"))
       {
          
           //Getting the country entered by the user
           System.out.print(\"Enter a countrty:\");
           country=keyboard.next();
          
           //Displaying the city and country
           System.out.println(\"Location : \"+city+\" \"+country);
          
           //Getting the city entered by the user
           System.out.print(\"Enter a city ( \'Last\' to exit ):\");
           city=keyboard.next();
       }
      

   }

}

___________________________________

Output:

Enter acity ( Last to exit ):hyderabad
Enter a countrty:india
Location : hyderabad india
Enter acity ( Last to exit ):banglore
Enter a countrty:india
Location : banglore india
Enter acity ( Last to exit ):bombay india
Enter a countrty:Location : bombay india
Enter acity ( Last to exit ):last

___________________________________________

23)

package org.students;

import java.util.Scanner;

public class LeapYearOrNot {

   public static void main(String[] args) {

       //Declaring variables
       int year;
      
       // Scanner class object is used to read the inputs entered by the user
       Scanner keyboard = new Scanner(System.in);

      
       while (true) {
          
           //getting the year entered by the user
           System.out.print(\"Enter year :\");
           year = keyboard.nextInt();
          
           /* validating whether the year is between 2004 and 2016 or not
           * If yes,display error message and prompts the user to enter again
           * If not, checking whether the year is leap year or not
           */
           if(year<2004 || year>2060)
           {
               //Displaying error message
               System.out.println(\":: Invalid Year.Must be between 2004 and 2060 inclusive ::\");
           }
           else
           {
           /* Checking whether the year entered is leap year or not
           * If yes.display valid year and exit the program
           * if not display error message and prompts the user to enter again  
           */
           if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
               System.out.println(\":: Valid Leap year ::\");
               break;
           } else {
               System.out.println(\":: Invalid Leap Year ::\");
               continue;
           }
           }

       }
   }
}

_____________________________________

Output:

Enter year :2061
:: Invalid Year.Must be between 2004 and 2060 inclusive ::
Enter year :2001
:: Invalid Year.Must be between 2004 and 2060 inclusive ::
Enter year :2009
:: Invalid Leap Year ::
Enter year :2010
:: Invalid Leap Year ::
Enter year :2011
:: Invalid Leap Year ::
Enter year :2012
:: Valid Leap year ::

_________________________Thank You

 Java fix and writes Assume all variables are declared and the entered city and country\' names contain no spaces. Fix the following Java: Write a validation lo
 Java fix and writes Assume all variables are declared and the entered city and country\' names contain no spaces. Fix the following Java: Write a validation lo
 Java fix and writes Assume all variables are declared and the entered city and country\' names contain no spaces. Fix the following Java: Write a validation lo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site