Learn How to Create Boolean Isleapyear() Method

CS120 Lab 7 – Summer 2017

Objective of Lab 7:

·  Learn how to copy files from an existing src folder to a new project’s src folder

·  Learn how to create “boolean isleapYear() ” method

·  Learn how to create switch statement to replace if … else if … else if … else statement

Task 1: launching the Eclipse program from your desktop, click on “File” button on the toolbar, select “New Java Project”, and give Lab7 as the project name.

Task 2: double click src/default package folder from Lab6 project icon, hold “shift” button and right click mouse, copy Season.java and SeasonTester.java files into src/default folder in Lab 7 project.

Task 3: add an instance variable called year in part1 of Season class: private int year;

Task 4: rewrite the constructor like the below:

public Season(int iniMonth, int iniDay, int iniYear)

{

month = iniMonth;

day = iniDay;

year=iniYear;

}

Task 5: under a method called findSeason(), delete the below if …else if …else java statement like the below:

if (month>=1 & month<=3)

{season="Winter";}

else if (month>=4 & month<=6)

{season="Spring";}

else if (month>=7 & month<=9)

{season="Summer";}

else

{season="Fall";}

Instead, please use switch statement to rewrite the above statement by using a switch statement. In this switch statement, if a user enters a month number which is not in the range of 1-12, your program should print the message like “Invalid month input”.

Task 6: When a user enters a day number which is not in the range of 1-31 (like -5 or 32), your program should display a message box like “Invalid day input”. Under a method called findSeason(), please add if statement, which can identify if a day number entered is great than 31 or less than/equal zero, it should display a message box like “Invalid day input”. Please add System.exit(0) to termite your program. Do the same thing for year input.

Task 7: However, the last of day is in February, if it is leap year, it should be 29 (like 1980, 2012), and if it is not leap year (common year), it should be 28 (like 2011). A year is a leap year if it is divisible by 400 (like 2000), or divisible by 4 (like 1980), and it is not divisible by 100 (like 1900). Please add a method called isLeapYear to find out that year is a leap year or not. Hint:

public boolean isLeapYear(){ //start boolean isLeapYear() method

}//close method

Task 8: under a method called findSeason(), if a user entered a month as 2, and if it is leap year, and if a day great than 29, your program should print “Invalid day input”. Moreover, if it is not leap year, and if a day great than 28, your program should also print “Invalid day input”.

Task 9: Open “SeasonTester” class from Lab 7 project window, rewrite the code by using below codes:

String monthString=JOptionPane.showInputDialog("Please enter a number for month: ");

int monthInteger =Integer.parseInt(monthString);

String dayString=JOptionPane.showInputDialog("Please enter a number for day ");

int dayInteger=Integer.parseInt(dayString);

String yearString=JOptionPane.showInputDialog("Please enter a number for year");

int yearInteger=Integer.parseInt(yearString);

Season findSeason=new Season (monthInteger, dayInteger,yearInteger);

JOptionPane.showMessageDialog (null, "The season is "+ findSeason. findSeason() )

Task 10: click run button to have first test your program with the following data:

You should see the output like:

Task 11: click run button to have 2rd test your program with the following data:

1.  Please enter a number for month: 2

2.  Please enter a number for day: 28

3.  Please enter a number for year: 2011

You should see:

The season is Winter

Task 12: click run button to have 3rd test your program with the following data:

1.  Please enter a number for month : 2

2.  Please enter a number for day (select from 1 to 31): 29

3.  Please enter a number for year: 1980

You should see:

The season is Winter

Task 13: click run button to have 4th test your program with the following data:

1.  Please enter a number for month: 13

2.  2. Please enter a number for day: 5

3.  3. Please enter a number for year: 2012

You should see:

Show your Lab 7 result to your Lab instructor to get your credit.

Thanks! Dr. Zhang