Here is the code:
class Time {
public static void printTime (int hour, int minute) {
System.out.print (hour) ;
System.out.print (":") ;
System.out.print (minute) ;
}
public static void main (String[] args) {
hour = 11 ;
minute = 30 ;
printTime () ;
}
}
Here is what terminal says when I try to compile it:
david-allenders-macbook-pro:~ davidallender$ Javac Time.java
Time.java:9: cannot find symbol
symbol : variable hour
location: class Time
hour = 11 ;
^
Time.java:10: cannot find symbol
symbol : variable minute
location: class Time
minute = 30 ;
^
Time.java:11: printTime(int,int) in Time cannot be applied to ()
printTime () ;
^
3 errors
david-allenders-macbook-pro:~ davidallender$
I’m in the process of learning so I don’t really know what’s going on. Right now I’m in the section in the book about parameters in/on/within/above/preposition (I’m not sure what the right preposition is) methods.
- What does a parameter do?
- Why is it useful?
- What did I do wrong in the above code?
- What did the error message mean?
Parameters give a method information it needs to do its job. For example, a function to find the square root of a number would have that number as a parameter.
You need to pass arguments to give the parameters values. So instead of trying to set
minuteandhourin themainmethod, you need to callAs a sort of meta-comment, this is the kind of thing you tend to learn pretty early – and while sites like this can help you with specific questions, you’d be better off reading a good entry level book about Java. If you’re already reading a book but it didn’t describe parameters clearly, you might want to think about getting another book 🙂