Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Advancing day w/ if/else & switch statement

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Red face Advancing day w/ if/else & switch statement

    I have a project due Monday for my Intro Java class and I really need to do well on it! I need to write a MyDate class that has 3 integers representing month, day & year. in it, I need a MyDate method, and adavceByOneDay method. In my tester class, I need to run scanner input and propts/reads month, day, year inputs. It also needs to print the date before and after it has been advanced by a day. I need to take into account month/year rollover, and leap years. I think using the gregorian calendar may be easier to set up the date, but the section we've been working on is "if/else" and "switch" statements. I think my part of my problem is that I don't fully understand how to relate the tester class with the main class. Anyhelp is greatly appreciated!

    public class MyDate {
    	public int month, day, year;
    	public MyDate(int month , int day, int year) {
    		MyDate date = new MyDate(month, day, year);
    	}
     
    	public boolean advanceByOneDay(){
    		boolean isAdvanced = true;
    		if(day+1> daysInMonth())
    		{
    			if(month+1 > 12)
    			{	
    			month = 1;
    			day = 1;
    			year++;
    			}
    			else
    			{
    			month++;
    			day = 1;		
    			}
    		}
    		else
    		{
    			day++;
    		}
    		return isAdvanced;
    }	
     
     
    	public static String getMonthInString(int n) {
    		String result = "";
    		switch(n) {
    		case 1: result = "January"; break;
    		case 2: result = "Febuary"; break;
    		case 3: result = "March"; break;
    		case 4: result = "April"; break;
    		case 5: result = "May"; break;
    		case 6: result = "June"; break;
    		case 7: result = "July"; break;
    		case 8: result = "August"; break;
    		case 9: result = "September"; break;
    		case 10: result = "October"; break;
    		case 11: result = "November"; break;
    		case 12: result = "December"; break;
    		default: result = "Error"; //break;
    		}
    		return result;		
    	}
     
    	private boolean isLeapYear(){
    		boolean leap = false;
    		if (year % 4 == 0) 
    		{
    			if (year % 100 == 0)
    			{
    				if (year % 400 == 0)
    				{
    					leap = true;
    				}
    				else 
    					leap = false;
    			}
    		}
    		return leap;
    	}
     
     
     
     
    	private int daysInMonth() {
    		switch (month){
    		case 1:day = 31;break;
    		case 2:if (isLeapYear())
    				day = 29;
    				else
    					day = 28;break;
    		case 3:day = 31;break;
    		case 4:day = 30;break;
    		case 5:day = 31;break;
    		case 6:day = 30;break;
    		case 7:day = 31;break;
    		case 8:day = 31;break;
    		case 9:day = 30;break;
    		case 10:day = 31;break;
    		case 11:day = 30;break;
    		case 12:day = 31;break;
    		default:
    		if ((month < 1)||(month > 12));
     
     
    		}
    		return day;
    	}

    import java.util.Scanner;
     
    public class MyDateTester {
    	public int month, day, year;
     
     
     
     
    	public static void main(String[] args) {
    		int month;
    		int day;
    		int year;
     
    		Scanner input = new Scanner(System.in);
    		System.out.println("This program was written by Robert Read");
    		System.out.println("This program will allow you to input month, day, and year in digits, advance it by one day and print both dates in proper format.");
    		System.out.println("Enter the month by it numerical value (1 for January, 2 for Febuary, etc): ");
    		month = input.nextInt();
    		System.out.println("Enter the Day (between 1-31 depending on the month): ");
    		day = input.nextInt();
    		System.out.println("Enter the four digit year; ");
    		year = input.nextInt();	
     
    		return(isAdvanced);
    }
    }


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Advancing day w/ if/else & switch statement

    Hi rbread80,
    in tester class, you set from standard input 3 integer: day, month and year.
    But you don't use your class MyDate.

    Maybe the solution is:
    put in your tester class this code:

    MyDate myDate = new MyDate(month,day,year);
    return (myDate.advanceByOneDay);

    2 things I don't understand:

    1) the constructor of MyClass
    2) function advanceByOneDay returns ever TRUE
    so what do u want test if the result is ever TRUE?

    Tell me!

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Advancing day w/ if/else & switch statement

    I've changed it some, but here is my new advanceByOneDay method

    public String advanceByOneDay(){
    		/**
    		* advances date by one day
    		**/
     
    		String result= " ";
     
    		if(day+1> daysInMonth())
    		{
    		if(month+1 > 12)
    		{ 
    		month = 1;
    		day = 1;
    		year++;
    		}
    		else
    		{
    		month++;
    		day = 1; 
    		}
    		}
    		else
    		{
    		day++;
    		}
    		return result;
    		}

    honestly, i'm not too sure about my constructor too. if you have any suggestions, let me know!
    Right now, it looks like my advanceByOneDay and isLeapYear methods aren't working right. I think it may be that advanceByOneDay doesn't read the scanner input right, and isLeapYear works, except that it won't respond with the correct days in a leap year.
    thanks for the reply!
    brandon

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Advancing day w/ if/else & switch statement

    After my tweaking, this is my entire updated code:

    public class MyDate {
     
    	public int month, day, year;
     
     
    	public MyDate(int month , int day, int year) {
     
    		this.month = month;
    		this.day = day;
    		this.year = year;
    	}
     
    	int daysInMonth() {
    		/**
    		 * Gives the number of days in a year
    		 **/
     
    		switch (month){
    		case 1:day = 31;break;
    		case 2:if (isLeapYear())
    				day = 29;
    				else
    					day = 28;break;
    		case 3:day = 31;break;
    		case 4:day = 30;break;
    		case 5:day = 31;break;
    		case 6:day = 30;break;
    		case 7:day = 31;break;
    		case 8:day = 31;break;
    		case 9:day = 30;break;
    		case 10:day = 31;break;
    		case 11:day = 30;break;
    		case 12:day = 31;break;
    		default:
    		if ((month < 1)||(month > 12));
     
    		}
    		return day;
    	}
     
    	public String advanceByOneDay(){
    		/**
    		* advances date by one day
    		**/
     
    		String result= " ";
     
    		if(day+1> daysInMonth())
    		{
    		if(month+1 > 12)
    		{ 
    		month = 1;
    		day = 1;
    		year++;
    		}
    		else
    		{
    		month++;
    		day = 1; 
    		}
    		}
    		else
    		{
    		day++;
    		}
    		return result;
    		} 
     
    	private boolean isLeapYear(){
    		/**
    		 * 	Decides if year is leap year.
    		 **/
    		boolean leap = false;
    		if (year % 4 == 0) 
    		{
    			if (year % 100 == 0)
    			{
    				if (year % 400 == 0)
    				{
    					leap = true;
    				}
    				else 
    					leap = false;
    			}
    		}
    		return leap;
    	}
     
     
    	public static String getMonthInString(int month, int day, int year) {
    		/**
    		 * changes input numerical month to written month
    		 **/
     
    		String result = " ";		
    		switch(month) {
    		case 1: result = "January"; break;
    		case 2: result = "February"; break;
    		case 3: result = "March"; break;
    		case 4: result = "April"; break;
    		case 5: result = "May"; break;
    		case 6: result = "June"; break;
    		case 7: result = "July"; break;
    		case 8: result = "August"; break;
    		case 9: result = "September"; break;
    		case 10: result = "October"; break;
    		case 11: result = "November"; break;
    		case 12: result = "December"; break;
    		default: result = "Error"; //break;
    		}
    		return result;		
     
    }	
     
     
     
     
     
    	/*
    	public String toString1(){		
    		return getMonthInString(day, day, day);
    	}
    	*/
     
    	public int getMonth() {return month;}
    	public int getDay() {return day;}
    	public int getYear() {return year;}
    	public void setMonth(int month){this.month = month;}
    	public void SetDay(int day){this.day = day;}
    	public void setSalary(int year) {this.year = year;}
     
    }



    import java.util.Scanner;
     
     
    public class MyDateTester {
    		public static int month, day, year;
     
     
    	public static void main(String[] args) {
    		MyDate e = new MyDate(month, day, year);
     
     
     
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("This program was written by Robert Read");
    		System.out.println("This program will allow you to input month, day, and year in digits, advance it by one day and print both dates in proper format.");
    		System.out.println("Enter the month by it numerical value (1 for January, 2 for February, etc): ");
    		int month= input.nextInt();
    		System.out.println("Enter the Day (between 1-31 depending on the month): ");
    		int day= input.nextInt();		
    		System.out.println("Enter the four digit year; ");
    		int year= input.nextInt();			
     
     
     
    		System.out.print(e.getMonthInString(month, day, year) + " " + e.day + ", " + e.year);
     
    		System.out.print(e.daysInMonth());
     
    		//System.out.println(e.advanceByOneDay(month, day, year) + " " + MyDate.getMonthInString(month, day, year) + " " + e.getDay() + ", " + e.getYear());
     
     
    		//advanceByOneDay does not respond by itself
    		//isLeapYear not working?
    	}
     
    }

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  3. How to populate a switch statement with a linked list
    By macnasty in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 10:47 PM
  4. switch with Enums
    By chronoz13 in forum Loops & Control Statements
    Replies: 17
    Last Post: October 8th, 2009, 08:08 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM

Tags for this Thread