3 Replies - 882 Views - Last Post: 30 September 2008 - 02:10 PM Rate Topic: -----

#1 davedave22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-September 08

Some problems getting started out

Posted 29 September 2008 - 08:06 PM

Okay, so my teacher has given us the task of creating a code that will ask for a user input for the current year and the month. The output will give the number of days in that month. My teacher is asking us to use multiple classes to implement this and i know there is simpler functions to count in leap years and stuff, however he wants us to do it the long way. He also wants us to use if and else statements instead of the simple way (forgot the name but i was just looking at it). So, i know exactly what i have to do but i am having trouble getting started. He gave us two classes and we have to create the second class. Here are the classes :
the first

import java.util.*;
import javax.swing.*;

public class AnalyseMonth {

	public static void main(String[] args){
		String theMonth = ""; 					//the month as text, without abbreviation
		int daysInTheMonth;						//the number of days in the month

		// use GregorianCalendar to ascetain the current year (default setting)
		GregorianCalendar today;
		today = new GregorianCalendar();
		int theYear = today.get(Calendar.YEAR);

		// get year and month in question from user.  The current year is set as a default in the dialog box
		theYear = Integer.parseInt(JOptionPane.showInputDialog("Which year?", Integer.toString(theYear)));
		theMonth = JOptionPane.showInputDialog(null, "Which month?");

		// create myDaysInMonth object and set year and month
		DaysInMonth myDaysInMonth;
		myDaysInMonth = new DaysInMonth();

		myDaysInMonth.setYear(theYear);
				   	myDaysInMonth.setMonth(theMonth);

		// get the days in month.  If month was mis-spelled, myDaysInMonth returns -1
		daysInTheMonth = myDaysInMonth.getDaysInMonth();

		// depending on datum returned, display error message or days in the month message
		if (daysInTheMonth == -1)
		{
			JOptionPane.showMessageDialog(null, "Bad month: " + theMonth);
		}
		else
		{
			JOptionPane.showMessageDialog(null, theMonth.toUpperCase() + ", " + theYear + " has " + daysInTheMonth + " days.");
		}

	}
}


and the last

public class CheckIfLeapYear {


   int y;

   //Constructor accepts an integer as a parameter
   public CheckIfLeapYear(int year){
	   y = year;
   }

   // Using known leap year criteria, determine, by way of 
   // modulo division, the leap year status of the year.
   // This class returns a boolean.

   public boolean isLeapYear(){
	   if (y % 400 == 0)
	   {
		   return true;		//it is a leap year.
	   }
	   else if (y % 100 == 0)
	   {
		   return true;		//it is a leap year.
	   }
	   else if (y % 4 == 0)
	   {
		   return true;	  //it is a leap year.
	   }
	   else
	   {
		   return false; 	// it is not a leap year.
	   }
   }
}




I am a new user to java but i have been getting through all the assignments except this one I just dont know how to begin. Again, he wants us to put in the middle class named "DaysInMonth". All help is appreciated. Thanks!

Here is how i tried starting out. I just wanted to see if i could get a quick print out if i just entered the month may. I know i did it wrong and I am very confused on how to bring in "theMonth" input into the "DaysInMonth" class.
import java.util.*;
import javax.swing.*;

class DaysInMonth {

	public DaysInMonth() {

		if (theMonth.equalsIgnoreCase(daysInTheMonth) ==  May)
		{ JOptionPane.showMessageDialog(null, + theMonth); }

		else { JOptionPane.showMessageDialog(null, "Bad month: " + theMonth);}
		}

	}



I'm looking for someone to point me in the right direction. Thanks guys!

This post has been edited by davedave22: 29 September 2008 - 08:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Some problems getting started out

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Some problems getting started out

Posted 29 September 2008 - 10:20 PM

Well your class is going to have three methods minimum. setYear, setMonth and getDaysInMonth. As you can guess the setYear is going to take one parameter which is the year, and one parameter for setMonth which is the month name. That function is where you are going to put if else statements to check the name of the month and return the appropriate days.

public string getDaysInMonth(String monthname) {
     if (monthname.equals("January")) {
          return 31;
     }
     else if (monthname.equals("February")) {
          
     }
     ....



The trick you are going to have is february as you probably guessed. Here you are going to need to run a check on the year (used by the setYear) and make sure it is a leap year because you need to return 28 if it is not a leap year or 29 if it is. I will leave that part up to you but now you have an idea of how you can go about setting up this function.

Enjoy!

"At DIC we be days in month getting code ninjas... and lets just say your days are numbered!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 davedave22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-September 08

Re: Some problems getting started out

Posted 30 September 2008 - 08:26 AM

Thank you for the help Martyr2. This is what i have so far

import java.util.*;
import javax.swing.*;
import java.lang.String.*;

public class DaysInMonth {

	 public int getDaysInMonth() {  
	 if (Month.equalsIgnoreCase("January")) {  
		  return 31;  
	 }  
	 else if (theMonth.equalsIgnoreCase("February")) {  
			return 28; 
	 		}  


Now i am having trouble coming up with the method for setting the month in the code. Once i figure that out i can get this done @_@ the rest is easy just the whole get/set methods are very confusing to me.
Looking at this :
DaysInMonth myDaysInMonth;
		myDaysInMonth = new DaysInMonth();

		myDaysInMonth.setYear(theYear);
		myDaysInMonth.setMonth(theMonth);

okay, so now "theMonth" should go to the DaysInMonth class right? I just dont know how to implement it ;< "public string..." maybe? thanks again for the assistance
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Some problems getting started out

Posted 30 September 2008 - 02:10 PM

I dont see a setMonth setter in your DaysInMonth class. I havnt gone through all ur code but if you want to create a date object, you would create a constructor of the class like
import java.util.*;
import javax.swing.*;

public class DaysInMonth {

int year
String month;
int days;

public DaysInMonth(int Year, String Month, int Days){  //creates a date object
this.year= Year;
this.month = Month;
this.days=Days;
}


//Setter for Month
public void setMonth(String Month) //pass it a month which is a String
{
this.month = Month;
}

//getter for Month

public String getMonth
{
return month;
}


Now if your in another class and you want to use these methods, you would do somthing like;
public class Month
{
   DaysInMonth diM;  //create a reference to this class

  public static void main(String[] args)
  {
   int Year = 1983;
   String Month = "Janurary";
   int days = 31;
   
   diM dateObject = new diM(Year, Month, days); // Uses the constructor in DaysInMonth to create a new Object
   
   diM.setMonth(Month);  //If you want to set them seperately, i can send Month to the setMonth setter.   

  }
} 


Hope this shows you a little about setters and getters. Anyone, if i have made any errors just correct me, i am half asleep! :blink:

This post has been edited by nick2price: 30 September 2008 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1