Advertisement
HcNguyen111

Practice Assessable Prac 1 - magicDates

Mar 18th, 2013
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class magicDates
  4. {
  5.    
  6. /*
  7. Question 1
  8.  
  9. The Question
  10.  
  11. The date 10th June 1960 is special because when we write it in the following format, the month times
  12. the day equals the year: 10/6/60
  13.  
  14. Using Eclipse create a project and a class (named magicDates) to write a Java program that asks the
  15. user to enter a month (in numeric form), a day, and a two-digit year. The program should then determine
  16. whether the day times the month is equal to the year; if so, it should display a message indicating that
  17. the date is magic. Otherwise, it should display a message indicating that the date is not magic. Your
  18. program code should incorporate an appropriate loop to ensure that the value for the year that is
  19. entered by the user is in the range 1 to 99 (note: for the purpose of this exercise you do not need to
  20. validate the day or month values). 50768.
  21.  
  22. Include in the comments at the top of the program:
  23.  
  24. - your student id,
  25. - your full name,
  26. - campus,
  27. - practical class day and time
  28. - tutors name
  29. */
  30.    
  31.     public static void main(String[] args)
  32.     {
  33.         int day, month, year;
  34.  
  35.         Scanner keyboard = new Scanner(System.in);
  36.        
  37.         System.out.println("What is the day?");
  38.         day = keyboard.nextInt();
  39.        
  40.         System.out.println("What is the month?");
  41.         month = keyboard.nextInt();
  42.        
  43.         System.out.println("What year is it?");
  44.         year = keyboard.nextInt();
  45.        
  46.         /*Loop that keeps asking for a new Year to be entered, if the year entered is invalid,
  47.         i.e. not between 1 and 99.*/
  48.         while ( (year < 1) || (year > 99) )
  49.         {
  50.             System.out.println("Please re-enter a valid year (1-99.)");
  51.             year = keyboard.nextInt();
  52.         }
  53.        
  54.         if ( (day * month) == year )
  55.         {
  56.             System.out.println("\nYou entered a Magic Date!");
  57.         }
  58.         else
  59.         {
  60.             System.out.println("\nThe date entered is not a Magic Date.");
  61.         }
  62.        
  63.         keyboard.close();
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement