Java course written by John Sweetnam, '09. Updated for online viewing by Will Levine and Erik Ruggles.
This course will teach you the basics of programming with Java. You will start off
learning the basics of coding with Java and then go on to design and construct Graphical
User Interfaces (GUIs). Along the way you will learn what object oriented programming
is and how to do it with Java. By the end of this course, you will be able to read and
code object oriented programs proficiently with Java.
The associated text used in this course and for your readings is “An Introduction to
Object-Oriented Programming with Java: fourth edition” by C. Thomas Wu. You will be
expected to obtain a copy of this and to complete the assigned readings before
attempting the lesson exercises and preferably before starting the lesson.
All exercises are to be turned in properly commented and fully functional. Be sure that
your name and all contributors to your work are mentioned in the comments at the
beginning of your code.
|
- Read Chapter 1 of Wu’s text. The purpose is to gain an understanding of what
object oriented programming is.
- Read Chapter 3.5 of Wu’s text. It covers System.out.println() in greater detail.
- It is assumed that you have had some programming experience, so there will not be a
great deal of explanation into some of the more generic terminology.
- Some background on Java and the Java Virtual Machine
- Write and modify a “Hello World” program
- Introduction to strings and string manipulation
- Properly use and manipulate strings
- Properly use the System.out.println() command
- Learn how to correctly name, compile, and run Java programs
Java is an object-oriented language that needs to be compiled before running. This
means that before your original Java code is run, it must be converted to a language
that is more efficient for the computer to read: bytecode. Once compiled, this bytecode
can be run on any machine with the Java Virtual Machine (JVM) installed. This
combination of efficiency and portability is one of the many advantages of programming
with Java.
Java makes uses of standardized libraries to provide access to wide variety of features,
some of which we will be using over the course of these lessons. Java also employs an
automatic garbage collection system. This means that Java will automatically allocate
memory and deal with unused objects (garbage) for your program.
This course will start out with a simple program to give you a feel for syntax and
string manipulation in Java.
For this first program, there may be things about the methods, classes, or syntax in
general that you might find a bit confusing. Don’t worry about that now, methods and
classes will be covered at a later date. Right now, we are just focusing on three
things: 1) being able to look at the code of a simple program and understanding what it
is doing, 2) being able to compile and run a Java program, and 3) some string
manipulation.
Take a look at this code:
class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
Note the directions in this section assume that you are using a computer that has the
javac style compiler installed. If you are using a different development tool,
determine how java code is compiled by checking your development tool’s manual or by
searching online for how yours compiles.
What do you think it does? Try running it and find out. First, copy or write this code
into a file that you save as HelloWorld.java. It is important to remember that Java is
case sensitive so you need to make sure the name of your file is the same as the class
name, followed by a “.java”. Once you have done this, navigate to the file directory
you have saved it in with the terminal, and type the command javac HelloWorld.java.
This will compile the .java file and create a file named HelloWorld.class. After this
is done, type the command java HelloWorld into the terminal and check that the words
Hello World! appear, followed in the next line by the prompt. If this isn’t what
happened, check your syntax carefully, because otherwise there could be something wrong
with your JVM.
In the Exercises section of this lesson, you are going to add additional lines of code
to your HelloWorld.java program inside the main() method. Be sure to type them in
yourself, as copying them from some text editor can occasionally copy the wrong type of
symbol and confuse the compiler. You must write these lines of code between the two {}
of the main() method. Once you add each line of code, you should recompile the entire
program again with the javac command and run it to see any differences.
It is very important to remember that each line you add needs to end with a semicolon.
This is what lets the compiler know that this is the end of the line.
- As you make the following changes, keep these questions in mind: After each
modification to the code, what differences do you notice when it is run? Do you
understand why the program printed the way that it did?
1) System.out.println(“Hello “ + “World”);
2) System.out.print(“Hello World”);
3) System.out.print("Hello World!\n");
Now you are going to add four further lines of code to your program. Be sure to add
them inside of the main method like the others before it.
String x = "I'm a variable!";
System.out.println(x);
System.out.println("x");
System.out.println("Hello World! " + x);
What happens when you do this?
This lesson will conclude with a quick review of the commands in your program.
System.out.println is the preferred print command in java. It prints to the terminal
whatever is enclosed in its parenthesis and then moves on to the next line.
System.out.print is similar, but does not move to the next line. If you were to print
something again, it would continue on that line. When \n is added into a string it
moves everything following it to the next line of the terminal. The x is a variable,
which can contain a string and, when printed, prints out what it contains rather than
its name. However, quotations can be used to print out exactly what is put into the
parenthesis, rather than what the variable contains.
|