Lab 1
Javac, Java, Git
Last updated
Was this helpful?
Javac, Java, Git
Last updated
Was this helpful?
Lab spec:
Welcome to the first lab of the semester! This lab is primarily focused on getting your computer set up with all the course software. I'm planning to write a more comprehensive setup guide later, so I won't be touching on that. For now, I'll be focusing on the Java assignment: isLeapYear()
.
For many students, this is your first time reading Java code! Let's go over the skeleton code provided. First, the class definition:
Line 4 is called the class definition. Class definitions are crucial for the Java compiler to make sense of your program, especially when your programs span multiple files. public
is a keyword that controls access -- but don't worry too much about it, you'll learn about it later. class
denotes that the next word is the class name. Year
is the class name for this particular file, and it's important to note that the class name must (almost) always match the file name. Another note on class names -- the convention is to use CapitalizedCamelCase. Finally, we have an open curly brace {
, and at the end of the class we will have a closing curly brace }
.
Line 7 is called the method signature. Methods in Java are similar to functions in Python. Like public
, static
is a keyword that you can ignore for now. Here's where methods in Java differ: types are specified in the method signature. In this case, the return type of the method is boolean
, aka true
or false
. After the return type is the method name, which is conventionally formatted in camelCase. Finally, the method takes in one parameter, int year
, which are specified in the parentheses ()
. Then, like the class definition, we have a pair of open/close curly braces {}
.
At a glance, the structure of a method signature is:
accessKeywords returnType methodName(type parameter) { ... }
Line 8 is the body of the method. It contains a return
statement that returns an expression with the same type as the return type of the method signature. All methods must contain a return statement, with one notable exception (which I cover in the next paragraph). In this case, since the return type is boolean
, we are able to return true
.
Line 12 is another method signature. What's notable about this one is that the return type is void
, which is a special keyword indicating no return type. As you see in the rest of the method body, there is no return statement.
Line 22 is what's known as the main method. Any program that you want to run will contain a main method, and the method signature is always the same:
public static void main(String[] args) { ... }
The main method signature is not syntactically different from any other method signature -- the only special things about the main method is that
It's called main
Java will start running your program from this method
Its parameters read in arguments from the command line
You are asked to write a function that returns true
if a year is a leap year, and false
otherwise. A year is considered a leap year if it is:
Divisible by 400, or
Divisible by 4 but not divisible by 100
There are two things that often trip up students when faced with these criteria. I will list these points a little more explicitly:
Only one of the two conditions need to be true for the year to be a leap year. That is, you don't need both conditions to be met in order to be true.
The second condition, "Divisible by 4 but not divisible by 100", is one single condition, and should be grouped in our code as such.
At this point, I recommend you go and try to figure out the answer by yourself. If you are confused on any Java syntax, search it up! Just don't search up the solution -- that defeats the whole purpose of this assignment, and you won't learn the skills you need to complete future assignments.
Below I list some incorrect solutions that a student may come up with. Before you expand each explanation, I highly recommend trying to figure out why it's wrong by yourself. Doing so will help train your code-reading and debugging skills, which are crucial to your success in this course.
Lines 1-3 are called a Javadoc comment. A Javadoc comment explains what a particular snippet of code is/does, and contains various other bits of information. For example, we have an @author
tag here, which denotes the author of that particular snippet of code. You should replace YOUR NAME HERE
with your name. Writing Javadoc comments is best practice, and you can learn more information .