The Artima Developer Community
Sponsored Link

Java Answers Forum
Creating a Method

1 reply on 1 page. Most recent reply: Jan 29, 2003 9:07 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jai Ki

Posts: 1
Nickname: freakin
Registered: Jan, 2003

Creating a Method Posted: Jan 29, 2003 8:04 PM
Reply to this message Reply
Advertisement
Hi....I'm new (really new) to Java, and my question is this:

I have my class:
public class MyDate{
//...code that defines state and behaviour of all date objects
}

and I have my method:public class MyDate
{
public static void main(String [] args)
{
int year;
boolean isLeapYear = false;

//get the year from the user
year = Input.readInt("Enter year:");

System.out.println();


//determine whether the year is a leap year


if (year % 4 == 0)
{
//see if the year is divisible by 400
if (year % 400 == 0)
{
isLeapYear = true;
}
//if it is not divisible by 400, check whether
//it is divisible by 100.
else if (year % 100 != 0)
{
isLeapYear = true;
}
}

//display the result

if (isLeapYear)
{
System.out.println(year + " is a leap year.");
}
else
{
System.out.println(year + " is not a leap year.");
}

System.out.println();
}
}


How do I make LeapYear a 'method' of MyDate?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Creating a Method Posted: Jan 29, 2003 9:07 PM
Reply to this message Reply
/**
 * MyDate.java
 *
 * Simple demo of methods in a class.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * Turning in this code as homework is a violation of ethical principals,
 * and punishable by severe penalties, even if you change the variable
 * names.
 */
public class MyDate
{
   private int year;
 
   /**
    * This is the constructor, which will set up the year value for this
    * object.
    */
   public MyDate( int y )
   {
      year = y;
   }
 
   /** 
    * Here is the isLeapYear() method, which you can call to find out
    * whether this instance of the MyDate object is a leap year.
    */
   public boolean isLeapYear()
   {
      boolean isLeap = false;
      
 
      // Here is my somewhat questionable method of determining whether
      // the year is a leap year:
      isLeap = ((new java.util.Random()).nextInt(2) == 0 ? true : false);
 
      // Instead of my technique, you will want to replace it with your code
      // to check whether the year is really a leap year; if it is, change
      // isLeap to true.
 
      return isLeap;
   }
 
   /**
    * I also added this toString() method, to facilitate displaying 
    * information about the object at runtime.
    */
   public String toString()
   {
      return Integer.toString(year);
   }
 
   /**
    * This the the static (static means there is only one) entry point.
    * It should not be heavily used for any kind of activity specific
    * to your class.   Here it is just used as kind of a quick demo
    * of using the MyDate class.
    */
   public static void main( String[] args )
   {
      int year = 1972;
 
      // Your code here to read the year from the keyboard, or wherever.
 
      // I'll just read it from the command line, if a parameter was
      // specified.   I am foolishly throwing caution to the wind and 
      // assuming that the parameter, if specified, will be a nicely 
      // formed integer value.   This will probably be the case, one in
      // ten times and more often than not, this program will crash with
      // an ugly stack trace about some crazy thing called a
      // NumberFormatException and the user will curse my lazy bones.
      if( args.length > 0 )
         year = Integer.parseInt(args[0]);
 
      // Before you can use your MyDate object, you must create an instance
      // of it, with "new" and initialize it with the year value from above:
      MyDate myDate = new MyDate( year );
 
      if( myDate.isLeapYear() )
         System.out.println( myDate.toString() + " is a leap year." );
      else
         System.out.println( myDate.toString() + " isn't a leap year." );
   }
}

Flat View: This topic has 1 reply on 1 page
Topic: Round Robin Scheduler. Previous Topic   Next Topic Topic: create executable jar

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use