Welcome to Lesson 3!


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.


Announcements

  • Quiz 1 on Thursday
    • Through Today's Lesson
    • To Prepare: Look over review activities, in class activities and assignments
    • Answer 6 questions in 10 minutes
  • CodeLab access code
  • De Anza Women in Computer Science Club Meeting
    • tomorrow (Wednesday) from 12:30 - 1:30pm
    • ATC 202 classroom
    • Amazon speaker
  • Come check in with me at break if you are still hoping to get enroll in this class

Review

What is the Decimal Number of the Computer Science Class He is Entering?
binary cartoon humor: Computer Science Class 10101010101.

Partner Activity (3 minutes)
  • With a partner, 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?
    4. Why does a computer only understand 1s and 0s?
    5. What is a compiler?


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.

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).

Basic Java Skills

Setting Up Your Environment

  • Let's take a moment to create a new folder in your personal documents folder (Z drive) where you can save all of your in-class coding exercises. Name it CIS36a (with no spaces).
  • Open up Eclipse by double clicking on the icon
  • Browse for your course folder (remember where this is!) and select it.
  • 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:

  • 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:

backslash forward slash stick figures leaning backwards and forwards.
Image Source

Whitespace

  • 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[]) {
        // WRITE YOUR CODE HERE
}
  • 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.

Blocks

  • 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("Hi");
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("Hi");
    System.out.println("How are you?");

Will print the following:

Hi
How are you?

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

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

This week, we will be practicing our pair programming skills during our in-class exercises, but not on the homework. Next week, we will start pair programming 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 3.1Your Java Screenplay (10pts)

  • This exercise will be your first chance to try out pair programming! 
  • Find a partner. Remember that you can work with no more than one other person.
  • Open up a new Java project in Eclipse.
  • Name the project Screenplay and save it in your personal CIS36A folder.
  • Write both names at the very top of the file by placing them 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 one of the following genres for your screen play:
    • Western
    • Romance
    • Science Fiction
  • Give names to your two characters.
  • Write a minimum of 8 lines of dialogue between the two characters, following the conventions of your selected genre. 
  • Note: You should have 8 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.
  • Remember that each person should drive 50% of the time.
  • When finished, upload your Screenplay.java file to Canvas.
  • You will need to look under CIS36a -> Screenplay -> src -> Screenplay -> Screenplay.java
  • Each person needs to upload the file.
  • The output of your program should look similar to the one below (but with 8 lines, rather than 2).
Martha: Fred, I need to confess something to you. I have been cheating on you with your evil twin, Ted.

Fred: Actually, Ted isn't the evil one, Martha. I am! Bwahahaha!

  • 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.


CodeLab

  • Log onto CodeLab and work through the first few exercises to get a start on Lab 2.
  • Make sure you know how to logon and how to work through the first few exercises.

Review: 

  • With a Partner, answer the questions from our learning objectives.

Homework:


~ See You Thursday! ~