Welcome Back!

Learning Objectives

By The End of this Class You Should Know...

  • How to create a new Java file in Eclipse
  • The basic building blocks of a Java program, including:
    • Whitespace
    • Comments
    • Classes and Packages
    • The Main Method
    • Proper Indentation
  • How to print to the console using System.out.print() and System.out.println()
  • What pair programming is and the basic rules you should follow when pair programming.

Before You Begin this Lesson -> Install Eclipse on your home computer or laptop

  • Follow my tutorial to install Java and Eclipse on your computer, and create a new project. There is also a small trouble-shooting guide.
  • Alternately, see the De Anza CIS Tutorial on Eclipse for Mac and Windows
  • Note that this may be the biggest challenge you face in the class this quarter (... sort of joking!). Installing new software can be tricky!
  • Get started on your installation early. If you run into trouble, contact me or the TAs on Zoom office hours.

1. Lesson 1 Practice Midterm Questions:

What is the Decimal Number of the Computer Science Class He is Entering?
Computer Science 1010101010101

  • Answer the following questions:
    1. What are the five main components of a computer?
    2. What is the difference between hardware and software?
    3. What is an algorithm? What is a program? How are they different?
    4. Why does a computer only understand 1s and 0s?
    5. Define a programming language
    6. What is a compiler?

2. Introduction to Java - A Language That Humans Can Understand

  • Java is THE most popular language in use today.
    • Please see the Tiobe Community Programming Index for its current ranking.
    • It's popularity is due in large part to Android development, which uses a special Java library
    • Java is also a popular language for the web
  • Java was developed at Sun Microsystems by a team of programmers, known as the Green Team, under the supervision of James Gosling.
  • It was originally released in 1991 under the name Oak and was intended to be used in embedded chips in consumer electronic appliances.
  • In 1995 it was re-released under the name Java. Java was a re-designed version of Oak targeted at developing web applications.
  • Java took off when the Netscape Navigator internet browser, the most popular browser in the 1990s, chose to incorporate Java Technology.
  • Sun Microsystems was bought out by Oracle in 2010. Oracle chose to continue support for the Java language.

Special Features of the Java Language

The Java Virtual Machine (JVM)

  • The Java Virtual Machine (JVM) is a unique feature of the Java language which allows it to run on any platform.
  • Rather than a physical computer, the JVM is a virtual computer.
  • When you install the Java JDK (downloaded from Oracle - you will do this for tonight's homework), then you will be downloading a copy of the Java Virtual Machine along with some other software tools.
  • This JVM virtual computer is responsible for interpreting compiled Java code (aka Java bytecode) for any computer's processor, regardless of the operating system (Windows, OSX, etc) or other specifications of the platform.
  • The JVM translates each step of the Java bytecode into the target machine language code.
  • Each step is then executed on the target machine.

Image source.

For More Information:

The Java Runtime Environment (JRE)

  • The JRE is a software package that contains what is required to run a Java program, including the Java Virtual Machine implementation and the Java Class Library. It is not for programmers who want to write code. It is for people who want to use Java, like in their web browsers.

The Java Development Kit (JDK)

  • The JDK is for programmers who wish to write and execute Java code. It contains the JRE, as well as a compiler for the Java language (Javac).

3. Basic Java Skills

Setting Up Your Environment

  • Let's take a moment to create a new folder on your desktop where you can save all of your in-class coding exercises. Name the CIS36A (important - this folder should have *no spaces* in its name).
  • Open up Eclipse by double clicking on the icon
  • Then a box will pop up asking you to Select a directory as workspace. What Eclipse is really asking here is where do you want to save your work. You should press the Browse button, navigate to the CIS36A folder on your desktop, and select it. Then, press Launch.

  • From here, Go to File->New->Java Project

  • Next it will prompt you to enter the name for your project. A project is where you will store the files related to your program. Give the project an appropriate name and click finish. In the below example, I selected the name Test.

  • The name of your project should now appear in project explorer on the left. If not, click on the button with the two boxes on the right hand side of the screen (as shown below)

  • Now you should see the name of your project on the left in the project explorer. Click on the arrow beside the project to see the files and folders that are contained in this project.

  • Next, click on the project name so it is highlighted, then got to File->New File->Class
  • A new window appears. Enter the name of your class here. This can be the same as the project. Again, I selected the name Test.

  • Once you have entered the name of the class, click the Finish button (in blue above)
  • Now, you should see the Test class appear under the src folder in the project explorer.
  • Also, if you click on the name of this file, some Java code will appear in the large middle box that reads public class Test { }

Building Blocks of a Java Program


  • Comments are ignored by the compiler. Instead, they are notes to anyone reading the code. 
  • Comments are used to make your program more understandable to someone who is looking at it.
  • There are two types of comments in Java:
    • Multi-line comments - comments that are more than one line long.
      • Multi-line comments must be placed between /* */ as in lines 1-6 above.
      • Another example: 
* This is a multiline comment
* It spans more than one line
* in the program
  • Inline comments - comments that are only 1 line long.
    • Inline comments follow a double forward slash //
    • There is an inline comment on line 12 of the program above. Do you see it?
    • Another example:

//This is an inline comment. It can only be one line long

  • Block Comments are multiline comments that begin your program. It is good style to always include a comment at the start of every program with your name and some additional information.
  • At the top of our programs, if we begin to type a multiline comment, Eclipse will provide us with one like this:

 * @author parrishj

  • Let's alter it to have more specific information about us and our class.
* @author Jennifer Parrish
* CIS 36A
  • Note that Eclispe will color these comments in your code. You know that a line of code is a comment if it is blue. 
Backslash vs Forward Slash - How to Remember the Difference:

Stick figures demonstrate forward and back slash

Image Source


  • Whitespace is ignored by the compiler.
  • Whitespace includes extra lines, extra spaces and extra tabs in your program.
  • It is important to use a lot of white space to make your code readable.
  • Add a couple of lines of white space to your program by pressing the enter key twice.
The Main Method
  • Java programs are structured into subprograms called methods.
  • Method = a named block of code that executes a series of statements.
  • Every Java program has one or more methods, including a main method defined like this:
    public static void main(String args[]) {
  • Programs begin executing at the first line of the main method.
  • In this class, you will be writing all of your lines of code inside this main method.

Classes and Packages

  • In Java, we often group code together into things called classes and packages.
  • These classes and packages are simply a way to organize our code.
  • A package is a collection of more than one class.
  • A single class is a collection of multiple lines of code that are related together
  • You will learn more about classes and packages next quarter.
  • For now, it is important for you to know that your class name and file must be identical or you will get an error.
  • Also, your main method must be placed inside of a class.


  • A block is a section of code grouped together
  • Java is known as a block-structured language
  • This means that most source code is grouped within pairs of matching { }
  • Left brace { begins body of every method
  • Right brace } ends body of every method
  • All methods have associated blocks
  • However, as we will see later in the course, we can use blocks other places as well
  • Make Sure Your Curly Brace Has a Matching Curly Braces { }.

Programming Style: Indentation Inside Braces

  • We should always indent our statements inside braces
  • This makes the structure of our code easier to follow as our programs grow more complex
  • When we get to the end of a method, we remove the level of indentation
  • Verify that your statements are indented within the curly braces.
Your Program Should Now Look Something Like This:
A basic Java program
  • Now Run the Program and Verify that You Get No Error Messages. Press the Green Arrow (Run)
Your First Words in the Java Language
  • System.out.print() is a command to tell the computer to print a message to the screen.
  • The message to be printed goes between double quotation marks.
  • Each complete System.out.print statement is followed by a semi-colon:
System.out.print("It's alive!!!!");
  • Add a System.out.print statement to your program with any message that you want.
  • Press the Run Button again.
  • Verify that your message appears on the console screen below your source code.
  • You should also get a report that of Build Successful if there were no errors in your code
Output of the program to the console

Remember: Programming is for detail-oriented people!

Group Exercise: Let's Experiment!

  • Try adding whitespace (blank lines, extra spaces, extra tabs) in various places in your program. Each time you change the program, press the build and run button. Where does whitespace matter and where doesn't it matter?
  • Return your program to its original format.
  • Try commenting out your System.out.print statement by placing // in front of it. What happens? What happens when you press the build and run button?
  • Now, remove the comment and press build and run again to ensure your program is working correctly.
  • Take off the semi-colon ;  from the end of your statement.
  • Press run. What happens? Note the message the compiler gives to you.
  • Replace the missing semi-colon ; and verify your program is working again.
  • Now add a second System.out statement.
  • Press the run button.
  • What happens? Is that the effect that you wanted.

System.out.print vs System.out.println

  • There are two ways to print a message to the console
  • The first - System.out.print("") will print on a single line.
  • If you want to print on a line and then move to the next line, you will need to use System.out.println("")
System.out.print("How are you?");

Will print the following:

HiHow are you?
  • If you want, Hi and How are you? to be printed on separate lines, you will need to do the following:
    System.out.println("How are you?");

Will print the following:

How are you?

  • Add a second System.out.println("") statement to your program, and verify you got the effect you wanted.

4. Pair Programming

What is Pair Programming?

  • Pair programming is a style of programming in which two people work together on one computer at the same time:
    • Exactly two people: not one nor three or more
    • Exactly one computer: not two or more
  • One person types the code and the other reviews each line of code as it is entered
  • The person using the mouse and keyboard is called the driver
  • The person reviewing is called the navigator
  • In addition to reviewing, the navigator:
    • Analyzes the design and code to prevent errors
    • Looks up reference materials like program syntax
  • Each person "drives" about half the time:
    • Physically get up and move positions when switching roles
  • At most 25% of your time is spent working alone:
    • Any work done alone is reviewed by the other person
  • The objective is to work together and to learn from each other
  • You cannot divide the work into two pieces with each partner working on a separate piece
  • If you are not both engaged in the process, you will not learn the material

Starting next week, we will be practicing our pair programming skills on homework assignments!

Why Pair Program?

  • Students who pair program report:
    • Higher confidence in a program solution
    • More satisfaction with programming
  • Instructors report higher completion and passing rates

Video Explaining Pair Programming

More Information

Activity 2.1Your Java Screenplay (10pts)

  • Open up a new Java project in Eclipse named Activity2 <-- no spaces in the name
  • Save the project in your personal CIS36A folder.
  • Now, open a new class named Screenplay and save in the Activity2 folder.
  • Write your name at the very top of the file by placing it inside of a multi-line comment, along with your section information.
  • The objective of your program is to create a dialogue between two different characters.
  • Select a genre for your screen play:
    • Western
    • Romance
    • Science Fiction
    • Comedy
    • Daily Life at De Anza
    • etc
  • Give names to your two characters.
  • Write a minimum of 6 lines of dialogue between the two characters, following the conventions of your selected genre. 
  • Note: You should have 6 separate System.out.println() statements .
  • Write each line of dialogue, by typing the character's name followed by a colon.
  • The dialogue should appear on the console when you press the run and build button.
  • When finished, upload your Screenplay.java file to Canvas.
  • The output of your program should look similar to the one below (but with 6 lines, rather than 2).
2 lines of dialogue between characters in the screen play

  • Please avoid using any foul language, sex, excessive violence or other offensive material in your screen play, or you will receive no credit for this activity.
  • How to locate this file for submission: You will need to look under CIS36A -> Activity2 -> src -> Screenplay.java

Activity 2.2: Your Future Goals (10 pts)
  • Where do you see yourself in 5 years, 10 years, 15 years, 25 years, and 50 years?
  • Objective: Write a program to print this information out onto the screen.
  • Open a new Java class inside the Activity2 folder
  • Name the class FutureGoals
  • Add a block comment at the top of your program to contain your name and section information like so:

* @author YourNameHere
* CIS 36A

  • Create 5 System.out.println statements with an answer to the above 5 questions.
  • Each answer needs to be printed on a separate line with a .
  • When finished, upload your FutureGoals.java file to Canvas.
  • To find this file, you will (most likely) need to look under CIS36A -> Activity2 -> src -> FutureGoals.java
  • However, if you close Eclipse and re-open it, it will remind you where your workspace is (i.e. where it is saving your files).
  • When you are finished, run your program and you should get output similar to the following (but not identical).
    • You should print out YOUR answers to the questions.

In five years, I expect to have graduated from college.
In ten years, I will be working at Google.
In fifteen years, I will be married with 3 kids.
In twenty-five years, I will own my own home in Cupertino.
In fifty years, I will be retired and cruising the Carribean.

Wrap Up:

  • Answer the following questions:
    • Write a complete program to print out the message Greetings! to the console window.
    • Assume you are writing it in a file named Greeting.java
    • Also, make sure that you have a correct comment up top with your name and section information
    • Add an inline (single line) comment to your program that states System.out is Java's name for the screen
    • Place this comment above or next to your command to print the greeting

Upcoming Assignments:

  • Activity 2.1 and 2.2 due Thursday at 11:59pm
  • Quiz 1 due Friday at 11:59pm on Canvas
  • Assignment 1 due Friday at 11:59pm on Canvas
  • Assignment 2 due Tuesday at 11:59pm on Canvas

~ Have a Great Weekend! ~