Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This part of the homework requires you to submit a single java file, called Number DaysLeft This Year.java. Make a folder called homework3 inside

image text in transcribed imageimage

This part of the homework requires you to submit a single java file, called Number DaysLeft This Year.java. Make a folder called homework3 inside of the cs110Submissions folder in your computer, and you can save and edit your code as you work on this programming assignment, but it is only the file that you upload to Canvas that will be scored. Always upload only *java files. For this programming task, you'll use conditional control statements (if, if-else, if-else-if, etc.), as well as the switch statement, that you learned about in Lecture 8. Your program will prompt the user to input the day (a number), month (a string), and year (a number). You can use the Scanner class, or the JOptionPane class, to receive input from the user. Using that information, your program will calculate the number of complete days that are remaining in the year specified by the user. For full credit, your program will need to consider whether the year supplied is a leap year, which is governed by the rules in Figure 1. Samples of code were also given in class. 1. 2. 3. 4. 5. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5. The year is a leap year (it has 366 days). The year is not a leap year (it has 365 days). Figure 1 Use a switch statement to process the month (of type String) that is input by the user, and any combination of if, if-else, if-else-if, if-else-then, etc. conditional statements, to calculate the number of days left in a year. An example of a pseudo-code for the entire program is given to you in Figure 2. // declare variables dayOfMonth and year of type int CS110, Tatiana Harrison, CWU, HW 3: Conditionals, the switch constructor Page 1 of 3 // declare variable monthName of type String // set up scanner and ask user to provide month, day of month, and year // using nextLine() for monthName, and nextInt() methods for dayOfMonth and year // declare variable days Leftin Year of type int, and assign it the value 0 // declare variable isLeap Year of type boolean, and assign it the value false // Determine whether the year is a leap year, using the following rules: // 1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5. // 2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4. // 3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5 // 4. The year is a leap year (it has 366 days). // 5. The year is not a leap year (it has 365 days). // Use a switch statement to determine the number of full days left in the year. // For example, December has 31 days, so daysLeftinYear = 31 - dayOfMonth. // For November, there are 30 days +31 days in December, so if user inputs November, days LeftinYear = 61 - dayOfMonth. // You will need to look up the number of days in each month. // For February, IF it is a leap year, then daysLeftinYear = 335 - dayOfMonth, and if it is NOT a // leap year, then daysLeftinYear = 334 - dayOfMonth. // For January, if it is a leap year, then daysLeftinYear = 366 - dayOfMonth. // If it is not a leap year, then if user indicates January, then daysLeftinYear = 365 - dayOfMonth. // output the correct text, (don't forget plural vs singular), to indicate how many days there are left in the year. Figure 2 The partial code for the switch statement is shown in Figure 3. switch(monthName}{ case "December": // always has 31 days daysLeftln Year = 31; daysLeftln Year -= dayOfMonth; break; case "November": // always has 30 days daysLeftinYear = 61; daysLeftinYear - dayOfMonth; break; Figure 3 To test your code, be sure to use a variety of month/day/year combinations. Using the leap year algorithm in Figure 1, the year 2000 is a leap year, but the 1900 was not. Also, the year 2007 was not a leap year, but 2008 was. Use at least these four years, to test your program for days in months that were before the leap day, and after the leap day. Sample invocations are shown in Figure 4. Sample invocation 1 (2007 is NOT a leap year): What month is it? December What day of the month is it? 30 What year is it? 2007 There is 1 complete day left in the non-leap year 2007. Sample invocation 2 (2008 is a leap year, and date input is in January): What month is it? January What day of the month is it? 31 CS110, Tatiana Harrison, CWU, HW 3: Conditionals, the switch constructor Page 2 of 3 Page < 2 of 3 ZOOM + What year is it? 2008 There are 335 complete days left in the leap year 2008. Sample invocation 3 (2009 is NOT a leap year): What month is it? January What day of the month is it? 15 What year is it? 2009 There are 350 complete days left in the non-leap year 2009. Figure 4 As you have done for homework #2, you should also submit as comments in your java file: The pseudocode of your program A description of two ways that your program crashes when it is run. RUBRIC Item Code compiles and correct logic Program compiles w/o errors Switch statement is correct If statements for leap year logic calculation is correct calculation is correct Use of boolean variables is correct Program outputs proper messages (leap / non-leap year plural vs singular number of days so far) Output is correct Year 2007: non-leap year Year 2008: leap year Year 2009: non-leap year Leap/non-leap year day counts are correct Code commented / indented Name included Date included Short description included Good variable names Inputs that break code (A description of two ways that your program crashes when it is run) Pseudocode Total CS110, Tatiana Harrison, CWU, HW 3: Conditionals, the switch constructor Points Available 20 10 10 10 10 20 5 5 5 5 100 Page 3 of 3 Page < 3 of 3 ZOOM +

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Below is the Java code for the NumberDaysLeftThisYear program Ive also included pseudocode comments and descriptions of potential crashes as required ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Programming questions