#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Unhappy How would I fix this?


    Please don't disregard because of the length! I need to write a program in this stupid format that we have to use. It has to determine if a year is a leap year or not. It is a leap year if the year is divisible by 4 but not by 100. A year that is divisible by both 4 and 100 is a leap year if it is also divisible by 400, ex. 2000 is a leap year but 1800 is not. I don't know how to write one of the methods to return a string or a double (I don't know which I should use). I have to use the displayOutput method to say whether or not it is a leap year using System.out.println. I don't know how to get a string or whatever from the Year class into the Leapyear class to use in the displayOutput. The code has to be in 2 classes. It might be easier for anyone to help if you paste the code into an IDE. Any help would be appreciated.

    class 1:

    import javax.swing.*;

    public class Leapyear {

    //----------------
    // Data Members
    //----------------


    private Year year;

    //-------------------
    // Main Method
    //-------------------

    public static void main(String[] args) {

    Leapyear yesorno = new Leapyear();
    yesorno.start();

    }

    //constructors

    public Leapyear(){

    }

    //-----------------
    // Public Methods
    // void start( )
    //-----------------

    public void start() {

    describeProgram();
    getInput();
    displayOutput();
    }

    private void describeProgram(){
    System.out.println("This program determines if a year");
    System.out.println("is a leap year or not.");

    }

    private void displayOutput(){
    I HAVE TO USE SYSTEM.OUT.PRINTLN HERE

    }

    private void getInput(){

    double year;

    String inputStr;

    inputStr = JOptionPane.showInputDialog(null, "Enter a year: ");

    year = Double.parseDouble(inputStr);


    }

    }

    class 2:


    public class Year {

    //----------------
    // Data members
    //----------------


    private double years;


    public Year(double year){

    setYear(year);
    }

    //-----------------------------
    //
    // double getYear ()
    //
    // double getYesorNo ()
    //
    // void setYear ()
    //
    //-----------------------------

    public double getYear()
    {
    return years;
    }

    public String getYesorNo()
    {
    double yesorno;

    I think I need to use if's and else's in here
    }

    public void setYear( double year)
    {
    years = year;
    }


    }


    Something like this for determining if it's a leap year or not?

    if ( year % 4 == 0 )
    {
    if ( (year % 100 == 0) && (year % 400 != 0) )
    {
    System.out.println("not a leap year");
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    Originally Posted by kadrek
    The code has to be in 2 classes. It might be easier for anyone to help if you paste the code into an IDE.
    That's why we have [ CODE ] tags in this forum.

    I don't understand your use of types. Why should the year be a double? Is something like 2000.517861 really considered to be a valid date? If so, you can't just use "% 4 == 0", because 2000.517861 % 4 is 0.517861000000039 rather than 0.

    And why should an attribute named "yesorno" hold a double? Wouldn't a boolean be more appropriate? I mean, if I wanna know if a year is a leap year or not, I expect to get either true or false, not some weird floating point numbers. Does 0.52 mean "yes", and 3.1 means "no", or what?

    I think the first thing you should do is write a method
    Code:
    boolean isLeapYear(int year)
    I'll use integer years, you can add doubles later.

    The method should check whether or not the year is a leap year:
    Code:
    public boolean isLeapYear(int year) {
    	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }
    In your displayOutput(), you can then simply call this method. Depending on the return value, you either print "It's a leap year" or "It's not a leap year".
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo