Introduction to Java

Welcome to the second CS15 lab! By now we've gone over objects, modeling, properties, attributes, and how to put all of these things together into Java classes. It's perfectly okay if you are feeling a little overwhelmed at this point; you've gone over a lot of new material very quickly. But don't worry! This lab will help you reinforce what you've learned by leading you through writing a small Java program. The TAs walking around are there to help you so if you have any questions about the lab, the lectures, or general course material, please ask us (sorry, this is not a time to ask specific assignment questions).

Before you start the lab, follow these directions to set some Kate settings

  • Open up Kate.
  • Click on the "Settings" menu -> "Configure Kate".
  • Go to the "Appearance" tab and check "Show line numbers." This will add the line numbers of your programs down the left margin of the screen, which will make it much easier to find errors.
  • Go to the "Open/Save" tab, and in the "Backup on Save" area, un-check both Local and Remote files.

That's it! Good luck with the lab...

General Programming Guidelines

Our goal is to get started using Java and learn how to create a class from scratch. Before we get started on the mini-assignment, let's go over some general Java and programming guidelines:

Packages

Java programs are built upon classes — each program needs to have at least one. As the programs you write grow in size, it can get messy to have all of your classes lying everywhere. Thankfully, Java organizes classes into packages. This is an extension of functions and classes to help keep code organized and manageable. The best way to think of a package is as a stand alone group of classes that performs a certain task. For example, if your program was a video game, possible packages within that game could include the graphics package, the physics package, the AI package, the Networking package, etc. Packages can consist of smaller packages (the physics package could contain a math package, the graphics package could contain a geometry package, and so on). All Java classes should belong to a package,(1) and you specify that package by typing:

at the very beginning of the file (before you even declare the class), and without the angle brackets. All this being said, the programs in CS15 aren't big enough to need multiple packages, so the name of your packages will simply be the names of the programs.

Classes

Now that we understand packages, let's move onto making a new class. Each Java class should be in its own file, with the name of the class being the name of the file (2). All Java files should end in .java. For example, a Clown class would be saved as Clown.java. If you don't remember how to declare a class, look back over the lecture notes. If you are unsure of how to make a new file, refer to lab 0 to see how that can be done in Kate.

Check Point 1

  1. Run the cs015_install labIntro script to get the stencil code for lab 1. This will create an App.java file in /home/<yourlogin>/course/cs015/labIntro/.
  2. cd into the labIntro folder and create a class MyClown. Put it in the same package as the App class by checking what package App.java is a part of, and writing that same package in your MyClown.java file.
  3. Write an empty constructor. This is just a simple constructor which does nothing.
  4. Compile your code by typing javac *.java into a shell, and then run it by typing java labIntro.App

Methods and Parameters

A class can't do anything without you defining some capabilities for it. If you had a Robot class, you would want it to do something cool like: walk, dance, cook, maybe even do your CS15 assignments for you. Java models these capabilities using methods. Methods have a specific syntax; refer to lectures or the book if you don't remember.

Sometimes a method needs outside information in order to perform its task. A way to pass information to a method is through parameters. For example, a Robot's cook(...) method needs to know what to cook. Since your Robot is a culinary genius, if you didn't use parameters, you would end up writing tons of cook methods for every possible dish your Robot can cook! Just think: cookChicken(), cookPork(), cookSteak(), cookTomatoes()... Or you can write one method that takes in a food that you want your Robot to cook as a parameter. When a method takes a parameter, it essentially uses that parameter as a variable.

Constructors

Let's not forget constructors; they are special methods that are automatically called when an object gets instantiated (i.e. every time you call new <someConstructor>();). Every class needs one (3), and they are usually used to instantiate instance variables and set default values. Going back to our Robot example, once a new Robot has been built, Java will look in the constructor to see what default values should be given to its color, name, height, weight, etc. Constructors have to be named the same as their class, so a Robot class will have a Robot constructor. Look into the lecture slides for specific syntax.

Just as in any method, parameters are very useful for constructors. They can be used to set up associations (knowledge of other objects) that are not known until an object is instantiated. Remember the Car and City example from lecture? We can use a parameter in the Robot class to let the Robot know which student it belongs to and what color the student ordered its Robot to be.

Now that you're more familiar with the Java syntax, it's time to get down to business...


  • (1) The only real exception is when you are writing a very small Java program that consists of only one class.
  • (2) There are classes called inner classes that do not follow this rule. We will be going over them in class in 2 weeks.
  • (3) If you don't write a constructor, Java will automatically create one for you, but it won't do anything.