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

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    What's wrong with my code?


    Please don't disregard because of the length! I am very new to coding. 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 Loan class into the Leapyear class to use in the displayOutput. The code has to be in 2 classes. It has to be in the format that it's in now. Any help would be appreciated. I think the problems are in the public boolean getYesorNo and the displayOutput methods. I can't figure out how to make it output the right thing.

    Code:
    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(){
    		
    		if (year.getYesorNo() == true){
    			System.out.print("Year is a leap year");}
    			
    		else if (year.getYesorNo() == false){
    			System.out.println("Year is not a leap year");}
    		}
    			
    			
    		
    		
    	
    	private void getInput(){
    		
    		int year;
    		
    		String inputStr;
    		
    		inputStr = JOptionPane.showInputDialog(null, "Enter a year: ");
    		
    		year = Integer.parseInt(inputStr);
    		
    		
    	}
    
    }
    [code]


    public class Year {

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


    private int years;
    private int year;


    public Year(int year){

    setYear(year);
    }

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

    public int getYear()
    {
    return years;
    }

    public boolean getYesorNo()
    {
    int year;
    if (getYear() % 4 == 0 && getYear() % 100 !=0)
    {
    return true;
    }
    else if (getYear() % 4 == 0 && getYear() % 100 ==0)
    {
    if (getYear() % 400 == 0)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    else
    {
    return false;
    }




    }

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


    }

    Code:
    
    public class Year {
    
    	//----------------
    	//  Data members
    	//----------------
    	
    	
    		private int years;
    		private int year;
    		
    		
    		public Year(int year){
    			
    			setYear(year);
    		}
    		
    		//-----------------------------
    		//
    		//	int getYear ()
    		//	
    		//	int getYesorNo ()
    		//
    		//	void setYear ()
    		//
    		//-----------------------------
    
    		public int getYear()
    		{
    			return years;
    		}
    		
    		public boolean getYesorNo()
    		{
    			int year;
    			if (getYear() % 4 == 0 && getYear() % 100 !=0)
    			{
    			return true;
    			}
    			else if (getYear() % 4 == 0 && getYear() % 100 ==0)
    			{
    			if (getYear() % 400 == 0)
    			{
    			return true;
    			}
    			else
    			{
    			return false;
    			}
    			}
    			else 
    			{
    			return false;
    			}
    
    			
    			
    			
    		}
    		
    		public void setYear( int year)
    		{
    			years = year;
    		}
    	
    
    	}
    Thanks for your help!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    In this code

    Code:
    	private void getInput(){
    		
    		int year;
    		
    		String inputStr;
    		
    		inputStr = JOptionPane.showInputDialog(null, "Enter a year: ");
    		
    		year = Integer.parseInt(inputStr);
    		
    		
    	}
    ,

    nothing is going to happen that affects the instance variable year which is of type year. You have a local variable of that same name of type int which is going to shadow the instance variable year. You can use the this reference to indicate you want to use the year that is an instance variable instead of the local variable.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    This thread has been double-posted and cross-posted, so don't waste your time on this.
    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