15: Review and Projects

What We Will Cover


Questions from last class?

Questions on the Final Project?

15.1: Programming Pure Java

Learner Outcomes

At the end of the lesson the student will be able to:

  • Explain the basic elements of a Java program
  • Compile and run Java programs, given the source code
  • Display program output to the command line
  • Explain how a virtual machine helps Java achieve platform independence

15.1.1: Creating a Java Program

  • As we have seen in our Greenfoot editor, Java code is written in text
  • We may use a simple text editor to write source code
  • Converting Java source code into machine code takes several steps
    1. Writing and saving the source code
    2. Compiling the source code to bytecode
    3. Executing (running) the program

Compiling and Running Java Programs

  1. Writing and saving the source code
    • We write a program's source code using a text editor
    • We save the source code in a file from the text editor
    • All source code files must have a .java suffix
  2. Compiling the source code
    • We use a compiler to translate the source code to bytecodes
    • Files with translated bytecodes have .class suffix
  3. Executing (running) the program
    • We use the java program to run the bytecodes
    • When the java program starts, a class loader moves bytecodes from a file into main memory
    • Then a bytecode verifier tests the bytecodes to establish safety and security
    • Finally, the Java Virtual Machine (JVM) translates bytecodes into machine language

15.1.2: Example Java Source Code

  • As an example, here is the source code for a simple Java program
public class HelloWorld {
     public static void main(String[] args) {
        System.out.println("Hello, world!");
  • The source code for all Java programs have one or more classes
  • The first line declares a class named HelloWorld
  • public class HelloWorld {
  • The word public means all parts of the application may use this class
  • Notice the curly braces used in the code
  • The left brace { begins the body of a class
  • Likewise, the right brace } ends body of every class
  • Any code between the curly braces is called the class definition
  • The second line of code declares a main method
  • public static void main(String[] args) {
  • Every Java program must have at least one main() method
  • Greenfoot starts from a main() method, which hides main() from view
  • The main() method also has curly braces around the code it executes
  • For pure Java, we must code our main() methods exactly like this one
  • The third line of code tells the computer to print "Hello, World" to the screen
    System.out.println("Hello, world!");
  • We save our source code from a text editor like TextPad or NotePad++

15.1.3: Compiling and Running Programs Using the Command Line

  • Now that we have saved our source code file, we need to compile the code and run the program
  • We can run all compilers from the command line, including Java
  • Sometimes we get into a situation where we need to work with the command line
  • As a programmer, we should always know how to compile and run programs using the command line
  • If you are not familiar with the Windows command line, see: How To Use a Windows Console

Compiling Source Code

  • To compile and run a program from the command line, we first open a console window
    1. From the Start menu, Select Run...
    2. In the text box, type:
      • cmd for Windows NT/2000/XP/Vista/7/10
      • command for Windows 95/98/ME
  • Then we change the directory (using cd) to where we saved the source code
  • C:\>cd Desktop
  • Now at the command line type: javac HelloWorld.java
  • If we do not see any errors, the compiler creates a class file
  • We can verify the HelloWorld.class file exists using the dir command
C:\Users\edparrish\Desktop>dir HelloWorld.*
 Volume in drive C is XXXX
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Users\edparrish\Desktop

05/08/2017  08:58 PM               427 HelloWorld.class
05/08/2017  08:53 PM               130 HelloWorld.java
               2 File(s)            557 bytes
               0 Dir(s)  XXX,XXX,XXX,XXX bytes free

Running a Program

  • At the command line type: java HelloWorld
    • Note that we do NOT type the .class suffix
    • The JVM loads the .class file for HelloWorld
  • The JVM calls the main() method of the class to start the program
C:\Users\edparrish\Desktop>java HelloWorld
Hello, world!

About the Java Compiler and Other Tools

  • Oracle, Inc. supplies the Java compiler and some other development tools for free
  • Collectively, these tools are known as the Java Development Kit, or JDK
  • For instructions on installing the JDK, see: How do I install Java ?
  • In this following sections we look at examples of Java programs outside of Greenfoot

15.1.4: Examples of Text-Based Java Programs

  • In this section we look at examples of text-based Java programs
  • All of these programs may run from the command line

Printing to a Terminal

  • As we saw before, we can print to a terminal window using
    System.out.print("Hello, ");
  • This works the same in pure Java as in Greenfoot

Terminal Window Input

  • Oftentimes we want a program to ask a user questions and read the input from the user
  • We can read input interactively from the command line (console) using a Scanner object
  • Scanner is located in the java.util package, so we need to import the class using either:
    import java.util.Scanner;
    import java.util.*;
  • We then create a scanner object like:
    Scanner input = new Scanner(System.in);
  • Note that System.in is used to get data from the standard input stream
  • To use one of the methods of a Scanner object, call it like the following:
    int count = input.nextInt();
  • When we call a method of a Scanner object, the program waits for the user to enter data with the keyboard
  • Each group of characters that a user enters is called a token
  • The program waits until the user presses the Enter key
  • The user can enter multiple tokens by separating them with whitespace
  • If the user enters the wrong type, an error occurs and the program aborts
  • When we combine different scanner methods, we sometimes have to include an extra call to nextLine() to get rid of a newline

Some Commonly Used Methods of a Scanner Object

Method Description
next() Returns the next token as a String object.
nextLine() Returns the rest of the current line as a String object and positions the input cursor at the beginning of the next line.
nextDouble() Returns the next token as an double value.
nextInt() Returns the next token as an int value.

Example Program Using Scanner for Processing User Input

import java.util.Scanner;

 * Basic Java Input.
 * @author Ed Parrish
 * @version 1.0 1/10/2010
public class InputApp {
    public static void main(String[] args) {
        String name;
        int age;
        double weight;
        Scanner input  = new Scanner(System.in);

        System.out.print("Enter your name: ");
        name = input.next();
        System.out.print("Enter your age: ");
        age = input.nextInt();
        System.out.print("Enter your weight: ");
        weight = input.nextDouble();

        System.out.println("\nYou entered:");
        System.out.println("Name:" + name);
        System.out.println("Age:" + age);
        System.out.println("Weight:" + weight);

Variables, Conditional, Loops, Methods and Classes

  • Variables, conditional (if) statements and loops are programmed the same as in Greenfoot
  • In addition, we define methods and classes the same as we do in Greenfoot
  • The one difference is we do not need to extend Actor or World

15.1.5: Examples of Graphical Java Programs

  • Java has standard built-in Graphics libraries
  • Many Greenfoot methods are built on these standard libraries
  • The following programs show some of the capabilities of these libraries

Example Programs

15.1.6: Summary

  • Java started development in 1991 and was officially released in 1996
  • The original focus was Applets, but Java is now a widely-used general-purpose programming language
  • Developing Java programs requires three step that we repeat over and over:
    1. Create source code with an editor (or IDE)
    2. Compile to bytecode using a compiler (javac FileName.java)
    3. Execute using a JVM (java FileName)
  • We write and edit source code as text
  • Many text editors provide features to make it easier to compile and run programs
  • Many Java programs are text based and run from a terminal window
  • Java has built-in graphics libraries as well

Check Yourself

  1. What is the purpose of the Java compiler?
  2. What is the file extension of compiled Java files?
  3. What makes Java portable across multiple platforms?
  4. What type of program can you use to write Java source code?
  5. Can you write a Java program without using a class?
  6. What code do you use to print text to the console or command line?
  7. What code do you write for a main() method?
  8. How does a Java program end?
  9. What do you type at the command line to compile the program: Foo.java
  10. What do you type at the command line to run the program named Foo?

Exercise 15.1

Take one minute to prepare an answer the following questions:

Q1: What do you type to compile a program named Foo.java?

Q2: What do you type to run the source code you compile with question 1?

15.2: Final Exam Preparation

Learner Outcomes

At the end of the lesson the student will be able to:

  • Discuss how to prepare for the final exam
  • Describe how to take the final exam

15.2.1: About the Final Exam

Important Final Exam Information

Date and Time: Exam Schedule
Scheduled time: 10:00 am-12:50 pm on Thursday, May 18
Location: Regular classroom

  • You must attend the exam or you will receive a score of zero (0)
    • Except by prior arrangement with the instructor for unforeseeable emergency and justifiable reasons
  • Bring a valid ID to the exam
  • Be on time as you can only work on the exam during the scheduled time

15.2.2: How the Final Exam Works

  • The final exam is a Lab Practical
  • This means that you must write code for the exam
  • You will be given a series of programming problems to solve
  • Successfully completing each problem is worth some number of points
    • Each problem has the number of points listed
  • If your code does not compile, you will receive no more than half credit.

Ground Rules

  • I am using Canvas to administer the test
  • The exam is closed books and closed notes
  • However, you may have one 3" x 5" card of notes for the exam
  • Also, you may have one sheet of blank scratch paper
  • You must use a classroom computer for accessing the exam in Canvas
  • Also, you can use Greenfoot to compile and run your code
    • However, your code must compile to receive more than half-credit on the entire exam
    • Partial credit is available if you comment out your problem code
  • You may NOT use the computer to search the Internet
  • You may NOT use any electronic device during the exam except the computer in the classroom
    • Thus, you cannot use your own computer to take the exam
  • You may NOT communicate with anyone but the instructor during the exam

3"x5" Card Requirements

  • Put your name on your card
  • Maximum card or paper size is 3 inches by 5 inches
  • You may use both sides of the card
  • Notes must be handwritten and NOT photocopied
  • No more than three statements in a sequence on the card — only snippets
  • Any 3" x 5" cards violating these rules will be confiscated before the test
  • You must turn in your 3" x 5" card after the exam in any case

15.2.3: What the Final Exam Covers

  • The final exam is cumulative -- you should know everything we have covered
  • However, the focus is writing code for what we have learned
  • Newer exam topics are listed below

New Exam Topics

  1. Drawing text and fonts (10.3.2, 10.3.3)
  2. Animation using a list of images (12.2.1, 12.2.2)
  3. Writing code to implement caching (12.2.3, 12.2.4)
  4. Characters and String Methods (12.3.4)
  5. Scrolling the Background (12.4.1)
  6. Overriding Methods (12.4.2)
  7. The instanceof Operator (12.4.3)
  8. Opening and layering file streams for reading or writing (13.2.3, 13.2.4)
  9. Writing different types of data to a file (13.2.5)
  10. Reading different types of data from a file and a JAR file (13.2.6)
  11. Using loops to read a file (14.1.2)

15.2.4: Recommended Preparation

  • Study over several sessions instead of one cram session
  • Review your homework assignments and solutions
  • Complete the Final Lab Problems (important!)
  • Complete the Final Review Quest
  • Practice common Greenfoot algorithms with Jumbled Games
  • Work through the Practice Final in Canvas:
    • Work the problems in groups if it helps you
    • Get explanations for anything you do not understand

    Tip: Complete the entire practice exam.

  • Make notes about the problems you had with the Practice Final
  • Make sure you know how to solve those types of problems
  • Do a quick review of your notes just before bed to let your subconscious aid in long term memory.
  • Repeat the Practice Final to practice the techniques
  • If you need more practice, review the quests from the semester starting with the most recent
  • Also, the CodeLab problems may help you study
  • Before the test, review your notes and prepare your 3" x 5" card
  • Get plenty of rest before the exam

15.2.5: Exam Taking Tips

  • Arrive at the examination room a little ahead of time.
  • Listen carefully to any oral instructions for taking the exam and read instructions carefully.
  • Read every word in each test question
  • Note that you do not need to comment code for the final exam
    • Unless specifically instructed to in the exam question
  • Use the full time allowed

15.2.6: Questions, Answers and Review

15.3: Project Demonstration

Learner Outcomes

At the end of the lesson the student will be able to:

  • Present their projects

15.3.1: Project Presentation

  • Here are the steps for an effective project presentation

Before the Presentation

  • Submit the Final Project following the instructions
  • Remember that there are no late submittals accepted for the project
  • If you have problems completing your part of the project:
    1. Make sure what you have compiles
    2. Send what you have to the rest of the guild on time
    3. Come to class for your part of the presentation
  • Bring a paper copy of your specifications report and give to the instructor at the start of class
    • If you are using your paper for your presentation, be sure to bring an extra copy!
  • Also, be sure your guild has a quick way to access the project during class to minimize setup time
    • Flash drive (recommended in case of network problems)
    • Canvas
    • Online service
  • Be prepared to vote on the guild contribution of each member
  • In addition to ranking, also include an approximate percentage of the contribution of each guild member

Project Setup

  • The best time to setup is before class
  • Copy your files to the instructor machine in your own folder on the Desktop
  • Verify your code compiles and your project loads its files
  • If you get stuck or do not know what to do, ask for help

During the Presentation

Present the following information:

  • Introduce the guild and state the purpose of your project
  • Compile your project code completely
  • Demonstrate your project, explaining aspects as you go
    • Show and explain all the extra-credit features
    • Point out the interesting effects so we can all appreciate them
    • Explain how the interesting effects or features work
  • Limit the presentation to 15 minutes or less

After the Presentation

  • Please be seated and enjoy the rest of the scenarios
  • Remember to study for the final!

Wrap Up

When class is over, please shut down your computer if it is on
Study for the final!
Home | Canvas | Schedule | Syllabus | Room Policies
Help | FAQ's | HowTo's | Links
Last Updated: May 14 2017 @02:15:25