Skip to content

Instantly share code, notes, and snippets.

@ogilviemt
Created November 12, 2013 20:00
Show Gist options
  • Save ogilviemt/7437627 to your computer and use it in GitHub Desktop.
Save ogilviemt/7437627 to your computer and use it in GitHub Desktop.
Calculate age in days -- teaching myself java
//
// This is not my code!
//
// Stolen from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
// for purposes of teaching myself java. As per their license, the following must be included:
//
// Copyright (c) 2013, Oracle America, Inc.
// All rights reserved.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL
// , EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
// To do: accept user input (birth day, birth month, birth year)
// calculate number of days per year for all years from birth year to current year
// calculate how many days are left in the current year
// output users age in days
//
//
//
//
//this is the default program copied from above site, to be modified
class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = "
+ numDays);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment