CS 46A - Lecture 2

Cover page image

Pre-class reading

The Java Programming Language

Our First Program

public class HelloPrinter
{
   public static void main(String[] args)
   {
      // Display a greeting in the console window

      System.out.println("Hello, World!");
   }
}

Program Run:

The Structure of a Simple Program: Class Declaration

The Structure of a Simple Program: main Method

The Structure of a Simple Program: Comment

The Structure of a Simple Program: Statements

The Structure of a Simple Program: Method Call

Syntax 1.1 Method Call

Syntax 1.1 Method Call

The Structure of a Simple Program: Strings

Lecture 2 Clicker Question 1

What does this program print?

public class HelloPrinter
{
   public static void main(String[] args)
   {
      // Display Hello World
      System.out.println("Hello");
      System.out.println("World");
   }
}
  1. Hello World
    Hello
    World
  2. Hello
    World
  3. Hello World
  4. Something else

Writing, Compiling and Running a Java Program

From Source Code to Running Program

From Source Code to Running Program

Errors

Lecture 2 Clicker Question 2

What is true about this program:

public class HelloPrinter
{
   public static void main(String[] args)
   {
      Display Hello World
      System.out.println("Hello World);
   }
}
  1. The program has no syntax errors and no run-time errors
  2. The program has no syntax errors and one run-time error
  3. The program has one syntax error
  4. The program has two syntax errors

Lecture 2 Clicker Question 3

What is wrong with this program?

public class HelloPrinter
{
   public static void main(String[] args)
   {
      // Display Hello World!
      System.out.println("Hello World!")
   }
}
  1. There should have been a semicolon at the end of the first line
  2. There should have been a comma after Hello
  3. There should have been a semicolon at the end of the sixth line
  4. There is nothing wrong with the program

Lecture 2 Clicker Question 4

Which part of the program contains the first error that is reported when you try to compile this program?

public class Hell0Printer
{
   public static void main(String[] args)
   {
      // Display Hell0, W0rld!
      System.0ut.println("Hell0, W0rld!");
   }
}
  1. public class Hell0Printer
  2. // Display Hell0 W0rld!
  3. System.0ut.println
  4. "Hell0 W0rld!"

Lecture 2 Clicker Question 5

Which identifier is a cause of the first error that is reported when you try to compile this program?

public Class HelloPrinter
{
   public static void Main(string[] args)
   {
      // Display a greeting in the console window

      system.out.println("hello, world!");
   }
}
  1. Class
  2. Main
  3. string
  4. system

Tip: Download the Program Code

Tip: Understand the File System

Tip: Know These Folders

Tip: Make These Folders

Lecture 2 Clicker Question 6

You want to start working on Homework 1. Into which folder should you put it?

  1. On the desktop
  2. Homework 1 inside My Documents
  3. hw1 inside the home directory
  4. hw1a (or hw1b or hw1c) inside cs46a