Java Leap Year Problem

Java Leap Year Problem

I need help with a writing a program in Java that creates a class Year that contains a data field that holds the number of months in a year and the number of days in a year. It should include a get method that displays the number of days and a constructor that sets the number of months to 12 and the number of days to 365. Then create a subclass named LeapYear. LeapYear's constructor overrides Years constructor and sets the number of days to 366. Write an application named USEYear that instantiates one object of each class and displays their data. Save the files as Year.java, LeapYear.java and UseYear.java. I have some code here but I think I also need to have a GUI with it, couild someone please help:

public class Year

{

private int days;

Year()

{

days = 365;

}

Year(int y)

{

days = y;

}

public void getDays()

{

System.out.println("The year has " + days + " days.");

}

}

{

LeapYear()

{

super(366);

}

}

public class UseYear

{

public static void main(String args[])

{

Year year2001 = new Year();

LeapYear year2000 = new LeapYear();

year2001.getDays();

year2000.getDays();

}

}
View Answers

December 9, 2010 at 3:40 PM

Hi Friend,

Try the followig code:

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 class Year{
 static int days;
Year(){
days = 365;
}
Year(int y){
days = y;
}
public static void getDays(){
JOptionPane.showMessageDialog(null,"The year has " + days + " days.");
}
}
class LeapYear{
static int days;
LeapYear(){
days=366;
}
public static void getDays(){
JOptionPane.showMessageDialog(null,"The year has " + days + " days.");
}
}
public class UseYear{
public static void main(String args[]){
JFrame f=new JFrame();
JLabel lab=new JLabel("Enter Year");
final JTextField text=new JTextField(20);
JButton b=new JButton("Get");
JPanel p=new JPanel();
p.add(lab);
p.add(text);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        Year year=new Year();
        LeapYear lyear=new LeapYear();
String y=text.getText();
int yy=Integer.parseInt(y);
if(yy%4==0){
    lyear.getDays();
}else{
    year.getDays();
}
}
});
}
}

Thanks









Related Tutorials/Questions & Answers:
Java Leap Year Problem
Java Leap Year Problem  I need help with a writing a program in Java that creates a class Year that contains a data field that holds the number of months in a year and the number of days in a year. It should include a get method
java leap year
java leap year  write a java program that takes an input... whether itis a leap year. Aleap year is an integer greater than 1584...("It is a leap year."); } else{ System.out.println("It is not a leap
Advertisements
leap year logic - Java Beginners
leap year logic  hi frds.. can u give me logic for leap year..  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners/Leapyear.shtml Thanks
leap year
leap year  how to create class for leap year. It is leap year...(); if (year.isLeapYear(y)){ System.out.println("It is a leap year."); } else{ System.out.println("It is not a leap year
Determining If a Year is a Leap Year in Java
Determining If a Year is a Leap Year in Java       Leap Year: Leap Year is the year contains an extra day. In the leap year, February month contains 29 days since normally
Checking whether a year is leap or not
Checking whether a year is leap or not   ... for checking whether a year is a leap year or not. Here, we have taken the year 2000. So...;if else" condition. As we know leap year is divided by the integer 4 and so
leap
leap   Check the given year for leap year (Test Case: 2010, 2012 , 1900
Java converts any given year to Roman Numeral
Java converts any given year to Roman Numeral  Write a program that converts any given year to Roman Numeral
Retrieve all the students in the same year(Java ArrayList)?
Retrieve all the students in the same year(Java ArrayList)?  FoundStudents.jsp year- parameter I receive from a form search- is an object of type Student, with ID, name, year <% Student search = null; ArrayList<Student>
convert date month and year into word using java
convert date month and year Java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Java Date Month Format  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Match Date format  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Date Format in Jave  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Basic format for Date and Month  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Convert Date Month format  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Convert Date Format  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
Addintion in year fields of sql date in a java page
Addintion in year fields of sql date in a java page  In my database...("Year: " + simpleDateformat.format(date)); String k= simpleDateformat.format(date); } But I have to add 4 with the year value
ModuleNotFoundError: No module named 'leap'
ModuleNotFoundError: No module named 'leap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'leap' How to remove the ModuleNotFoundError: No module named 'leap' error
ModuleNotFoundError: No module named 'leap'
ModuleNotFoundError: No module named 'leap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'leap' How to remove the ModuleNotFoundError: No module named 'leap' error
ModuleNotFoundError: No module named 'leap'
ModuleNotFoundError: No module named 'leap'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'leap' How to remove the ModuleNotFoundError: No module named 'leap' error
Add year to Date without using Calendar in java
Add year to Date without using Calendar in java  how could i add a year to a date function without using calendar functions
pava program - Java Beginners
pava program  program to display whether the given year is LEAP YEAR... % 100 != 0) { System.out.println(year + " is a Leap year...) { System.out.println(year + " is a Leap year."); } // It is Divisible
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   import java.util.*; import java.text.
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   import java.util.*; import java.text.
java Problem
java Problem  I want to create a binary tree for displaying members in Downline. i am creating a site for MLM(Multi-Level MArketing). tree must be dynamically populated from database. is there any help for me. Thanks in advance
JAVA Problem
JAVA Problem  Write a program that takes two parameters 1. a word 2. an array of words It should then remove all instances of the word in the array. For Example: INPUT word="ravi" word_array = ["Chethan Bhagat
Java get Year
Java get Year       In this section, you will learn how to obtain the current year. The Calendar.... The method cal.get(Calendar.YEAR) will return the current year. Here
date problem - Java Beginners
date problem  how to extract day,month,year hours ,second from... ,year,date,hours,minute Please help me. Thanks in advance Sushil ...[] args) { String dat ="200912202934"; String year = dat.substring(0,4
resolution problem in java
resolution problem in java  I designed project in java in my PC when run the same project in some other PC i can't fully view my java forms.Some said that it is resolution problem
ModuleNotFoundError: No module named 'leap-client'
ModuleNotFoundError: No module named 'leap-client'  Hi, My Python... 'leap-client' How to remove the ModuleNotFoundError: No module named 'leap-client' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'leap-client'
ModuleNotFoundError: No module named 'leap-client'  Hi, My Python... 'leap-client' How to remove the ModuleNotFoundError: No module named 'leap-client' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'leap_gwas'
ModuleNotFoundError: No module named 'leap_gwas'  Hi, My Python... 'leap_gwas' How to remove the ModuleNotFoundError: No module named 'leap... have to install padas library. You can install leap_gwas python with following
ModuleNotFoundError: No module named 'leap-protocol'
ModuleNotFoundError: No module named 'leap-protocol'  Hi, My... 'leap-protocol' How to remove the ModuleNotFoundError: No module named 'leap-protocol' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'leap-server'
ModuleNotFoundError: No module named 'leap-server'  Hi, My Python... 'leap-server' How to remove the ModuleNotFoundError: No module named 'leap-server' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'linggle-leap'
ModuleNotFoundError: No module named 'linggle-leap'  Hi, My Python... 'linggle-leap' How to remove the ModuleNotFoundError: No module named 'linggle-leap' error? Thanks   Hi, In your python
java programming problem - JDBC
java programming problem  Hi, Request you to provide the source code in Java for the following programming problem : upload .csv file data into oracle database. please send the solution to [email protected]
Write a program that converts any given year to Roman Numeral in Java
Write a program that converts any given year to Roman Numeral in Java  Write a program that converts any given year to Roman Numeral
Problem in uploading java application
Problem in uploading java application  I have uploaded my java application (folder created under webapps) using Filezilla FtpClient.Application... this problem
for a problem in coading - Java Beginners
for a problem in coading  what is the problm in following coading...(String[] args) { mywindow (); } }   Hi Friend, There is no problem... mywindows.java Run : java mywindows Thanks RoseIndia Team
Multiplication problem - Java Beginners
Multiplication problem  I am facing a peculiar problem in java regarding a multiplication. Please see below: 19300 * 0.001 = 19.3 19400 * 0.001 = 19.400000000000002 (why is this ??) 19500 * 0.001 = 19.5 Can anybody help
Java programming problem - Java Beginners
Java programming problem  could anyone help me to solve this problem... Implement a superclass Person. Make two classes, Student and Instructor that inherit from Person. A Person has name, year of birth and day. Student has
Java get year from date
Java get year from date       In this section you will study how to retrieve the year from... showing the format of year. The method simpleDateformat.format(date) formats
java input problem - Java Beginners
java input problem  I am facing a Java input problem
code problem - Java Beginners
java script j2ee j2me sql plz help me to sort out this problem. thnx  ...code problem  Dear sir, I'm havin a problem that suppose i've got a file that contains the following lines- java java script j2ee php sql
Problem on JAVA Programme
Problem on JAVA Programme  public class AA { int add(int i) { int y = i; y += 20; if (y <= 100){ y +=30;add(y);} System.out.println("Final Value of y : " + y); return y; } public static void main
date problem - Java Beginners
date problem  how to extract day,month,year hours ,second from... ,year,date,hours,minute Please help me. Thanks in advance Sushil ...); System.out.println("Year"+date); System.out.println("Month"+dat.substring(4,6

Ads