
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();
}
}

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
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.