15: Pure Java, Finals 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 Java 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!");
  • We must save this source code in a file named HelloWorld.java
  • We save our source code with a text editor like TextPad or NotePad++
  • 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 the 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
  • The main() method also has curly braces around the code it executes
  • Greenfoot has a main() method, which is why we have not written one so far
  • For pure Java, we must code our main() method with the same signature (header) as above
  • The main() method tells Java code where to start executing
  • The third line of code tells the computer to print "Hello, World" to the terminal window
    System.out.println("Hello, world!");
  • This code is the same as we have used before to print to the Greenfoot terminal window

15.1.3: Compiling and Running Programs Using the Command Line

  • Now that we have a source code file, we need to compile the code and run the program
  • We may run all compilers for all languages from the command line, including Java
  • As a programmer, we should always know how to compile and run programs using the command line
  • Sometimes we get into a situation where we need to work with the command line

Compiling Source Code

  • To compile and run a program from the command line, we first open a terminal window
  • On Windows:
    1. Click the Start button
    2. In the text box, type: cmd and press the Enter key
  • On Mac:
    1. Press ⌘ Command+Space.
    2. Type Terminal in the search field and press the Return key
  • Next we change the directory (using cd) to where we saved the source code like:
  • 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 on Windows or ls command on Mac
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
  • When the program runs, the input statement causes the program to wait 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

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
  • Also, 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

Published Schedule: Exam Schedule
Date and Time: Tuesday, December 10 @ 10:00am-12:50pm
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

15.2.3: What the Final Exam Covers

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
  • 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
  • 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
  • Be prepared to vote on the guild contribution of each member
  • Each voter can designate only one guild leader
  • Remember to study for the final!

15.3.2: Votes and Other Feedback

  • With the project completed we can now vote on guild contribution of each member
  • Voting is by secret ballot as shown below
  • No more that two Guild Leaders per ballot
  • However, can have any rating for the rest of the votes
  • Final extra credit XP is based on the average (mean) of the votes

Extra credit applied at end of final project (secret ballot):

  1. Guild Leader: 20 pts. (major player in the guild; gets things done)
  2. Raid Leader: 15 pts. (major contributor to the guild and its projects)
  3. Solid Guild: 10 pts. (could be counted on to do their part, but not more)
  4. Needs Rez: 5 pts. (coasting, minimal effort, needs resurrection as a player)
  5. Leeroy Jenkins: 0 pts. (Usually AFK or minimal contribution)

Each voter can designate only one guild leader. Please vote thoughtfully.

End of Course Survey

Wrap Up

When class is over, please shut down your computer if it is on
Study for the final!
Last Updated: December 05 2019 @13:53:48