Jump to content

Java Program - class year & subclass Leap Year

- - - - -

  • Please log in to reply
1 reply to this topic

#1
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
 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();
}
}
});
}
} 

I am getting the following errors:
2 warnings found:
File: C:\Users\mercy\Documents\CIS131_Fall2011\Year.java [line: 59]
Warning: The static method getDays() from the type LeapYear should be accessed in a static way
File: C:\Users\mercy\Documents\CIS131_Fall2011\Year.java [line: 63]

Please help!
Warning: The static method getDays() from the type Year should be accessed in a static way

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


class Year {
    static int days;


    Year() {
        days = 365;
    }


    Year(int y) {
        days = y;
    }


[COLOR=#ff8c00]    [B]public static void getDays() {[/B][/COLOR]
        JOptionPane.showMessageDialog(null, "The year has " + days + " days.");
    }
}


class LeapYear {
    static int days;


    LeapYear() {
        days = 366;
    }


[B][COLOR=#ff8c00]    public static void getDays() {[/COLOR][/B]
        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) {
[COLOR=#ff8c00]                    [B]lyear.getDays();[/B][/COLOR]
                } else {
[B][COLOR=#ff8c00]                         year.getDays();[/COLOR][/B]
                }
            }
        });
    }
}
First of all, it's "just" a warning your code will work because the compiler will change it in what it needs to be.

The orange part is the problem. The method is static, but you use it as if it wasn't.
Static methods don't require objects to use and are called using ClassName.method(); In this case: Year.getDays(); Or LeapYear.getDays();
Using the bold text instead of the orange will fix the warning.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users