08.02.2015 Views

CS 112 Introduction to Programming - Yale University

CS 112 Introduction to Programming - Yale University

CS 112 Introduction to Programming - Yale University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>CS</strong> <strong>112</strong> <strong>Introduction</strong> <strong>to</strong> <strong>Programming</strong><br />

(Spring 2012)<br />

Lecture #3: <strong>Programming</strong> Environment<br />

Zhong Shao<br />

Department of Computer Science<br />

<strong>Yale</strong> <strong>University</strong><br />

Office: 314 Watson<br />

http://flint.cs.yale.edu/cs<strong>112</strong><br />

Acknowledgements: some slides used in this class are taken directly or adapted from those accompanying the two textbooks: <strong>Introduction</strong> <strong>to</strong> <strong>Programming</strong> in Java: An<br />

Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne and Building Java Programs: A Back <strong>to</strong> Basics Approach by Stuart Reges and Marty Stepp


<strong>Programming</strong> in Any Language<br />

q <strong>Programming</strong> consists of three tasks<br />

❍ Create and edit source code file<br />

• The source code is usually human understandable<br />

❍ Compile the source code in<strong>to</strong> machine code<br />

• Machine code ranges from vaguely understandable <strong>to</strong><br />

nondescript 0's and 1’s<br />

❍ Execute/run/test machine code<br />

• Your computer (specifically the CPU) executes the<br />

machine code, 1 instruction at a time.


Java <strong>Programming</strong><br />

q <strong>Programming</strong> in Java consists of the same tasks, but they are<br />

slightly specialized<br />

o<br />

Create and edit “Java source code” (.java files)<br />

• Eg. Hello.java<br />

q Compile in<strong>to</strong> “Java bytecode” (.class files)<br />

o<br />

Eg. javac Hello.java <strong>to</strong> produce Hello.class<br />

q Execute/run/test bytecode with “Java interpreter”<br />

o<br />

Eg. java Hello<br />

source code<br />

compile<br />

byte code<br />

run<br />

output


A Simple Java Program<br />

public class Hello {<br />

public static void main(String[] args) {<br />

System.out.println("Hello, world!");<br />

System.out.println();<br />

System.out.println("This program produces");<br />

System.out.println("four lines of output");<br />

}<br />

}<br />

Program output (in red for emphasis):<br />

$ java Hello<br />

Hello, world!<br />

This program produces<br />

four lines of output<br />

$<br />

4


Java Is Simple<br />

q All you have <strong>to</strong> do is <strong>to</strong> write a text file<br />

using the correct syntax, and then:<br />

1) Execute “javac” with the filename<br />

l<br />

This gives you a .class file<br />

2) Execute “java” with the .class file<br />

q You're probably wondering, “How do I write<br />

a text file using the correct syntax”<br />

❍ There are two main choices<br />

5


Writing Java Programs<br />

q The basic way is <strong>to</strong> use a text edi<strong>to</strong>r<br />

❍ Example edi<strong>to</strong>rs: Notepad, emacs, pico, vim,<br />

kwrite, kate, gedit, etc.<br />

• Note: MS Word is NOT a text edi<strong>to</strong>r<br />

❍ The key is that your .java file cannot include<br />

any markup or stylistic formatting; just text.<br />

❍ You enter your Java code following Java<br />

Language syntax, then you compile it.<br />

6


Writing Java Programs (Step 1): Create/Edit<br />

<br />

Create/Edit the program by typing it in<strong>to</strong> a text<br />

edi<strong>to</strong>r, and save it as HelloWorld.java.<br />

/*******************************************<br />

* Prints "Hello, World"<br />

* Everyone's first Java program.<br />

*******************************************/<br />

public class HelloWorld {<br />

public static void main(String[] args) {<br />

System.out.println("Hello, World");<br />

}<br />

}<br />

HelloWorld.java


Writing Java Programs (Step 2): Compile<br />

<br />

<br />

Create the program by typing it in<strong>to</strong> a text edi<strong>to</strong>r, and<br />

save it as HelloWorld.java.<br />

Compile it by typing at the command-line:<br />

javac HelloWorld.java.<br />

command-line<br />

% javac HelloWorld.java<br />

<br />

This creates a Java bytecode file named: HelloWorld.class.


Writing Java Programs (Step 3): Execute<br />

<br />

<br />

<br />

Create the program by typing it in<strong>to</strong> a text edi<strong>to</strong>r, and<br />

save it as HelloWorld.java.<br />

Compile it by typing at the command-line:<br />

javac HelloWorld.java.<br />

Execute it by typing at the command-line:<br />

java HelloWorld.<br />

command-line<br />

% javac HelloWorld.java<br />

% java HelloWorld<br />

Hello, World


Writing Java Programs using an IDE<br />

q Another way is <strong>to</strong> use an Integrated Development<br />

Environment (IDE)<br />

❍ Example IDEs: DrJava, Eclipse, NetBeans, BlueJ, etc.<br />

❍ An IDE usually presents the user with a space for text (like an<br />

edi<strong>to</strong>r) but layers additional features on <strong>to</strong>p of the text for<br />

the user's benefit.<br />

• Note: The underlying file contains pure text, just like a text edi<strong>to</strong>r.<br />

❍ These features can be very useful and save time.<br />

• Example features are code completion, instant compilation, and<br />

syntax highlighting.<br />

❍ IDEs need <strong>to</strong> know where <strong>to</strong> find the “java” and “javac”<br />

programs.<br />

q For simplicity and consistency, we will use DrJava.<br />

10


Setting up DrJava on your Lap<strong>to</strong>p<br />

q Please follow the directions on the main<br />

textbook site --- see Part 1 of Assignment 0.


Pantheon Environment<br />

The "Pantheon" is a collection of<br />

computers at <strong>Yale</strong> named after<br />

Greek gods<br />

Generally, you use Secure Shell <strong>to</strong><br />

login <strong>to</strong> eli.yale.edu; eli.yale.edu will<br />

au<strong>to</strong>matically direct you <strong>to</strong><br />

one of the machines<br />

http://pantheon.yale.edu/help/ssh/<br />

They run a variant of Unix<br />

Linux<br />

General help on pantheon is available at:<br />

http://pantheon.yale.edu/help/<br />

12


Login, Edit, Compile, Run on a Pantheon Computer<br />

- The Pantheon is the first method of Java <strong>Programming</strong> (ie. text<br />

edi<strong>to</strong>r, no IDE)<br />

- Login <strong>to</strong> a pantheon machine using ssh<br />

❍ Instructions at http://www.yale.edu/its/stc/faq/Pantheon<br />

/SSHTerminal.html<br />

- Create a direc<strong>to</strong>ry for your cs<strong>112</strong> files for the semester<br />

$ mkdir cs<strong>112</strong><br />

- Change <strong>to</strong> the direc<strong>to</strong>ry<br />

$ cd cs<strong>112</strong><br />

- Edit your Java program using any text edi<strong>to</strong>r, e.g., pico<br />

$ pico HelloWorld.java<br />

type in your program, press control-X, type y, press return<br />

13


Login, Edit, Compile, Run on a Pantheon Machine<br />

(Cont.)<br />

- Compile a Java program<br />

$ javac HelloWorld.java<br />

- Take a look <strong>to</strong> see that HelloWorld.class is generated<br />

$ ls<br />

HelloWorld.java HelloWorld.class<br />

- Run Java interpreter<br />

$ java HelloWorld<br />

14


q White space<br />

Syntax: White Space<br />

❍ includes spaces, new line characters, tabs<br />

❍ white space is used <strong>to</strong> separate words and<br />

symbols in a program<br />

❍ extra white space is ignored<br />

q White space allows a Java program <strong>to</strong> be<br />

formatted in many ways, and should be<br />

formatted <strong>to</strong> enhance readability<br />

o the usage of white space forms part of<br />

programming style<br />

15


Syntax: Comments<br />

q Two types of comments in Java<br />

• single-line comments use //…<br />

// this comment runs <strong>to</strong> the end of the line<br />

• multi-lines comments use /* … */<br />

/* this comment runs <strong>to</strong> the terminating<br />

symbol, even across line breaks */<br />

q Comments are ignored by the compiler:<br />

❍ used only for human readers (i.e., inline<br />

documentation)<br />

16


Using comments<br />

q Where <strong>to</strong> place comments:<br />

❍ at the <strong>to</strong>p of each file (a "comment header")<br />

❍ at the start of every method (see later)<br />

❍ <strong>to</strong> explain complex pieces of code<br />

q Comments are useful for:<br />

❍ Understanding larger, more complex programs.<br />

❍ Multiple programmers working <strong>to</strong>gether, who<br />

must understand each other's code.


Practice Slides


Questions<br />

q What is the output of the following println<br />

statements<br />

System.out.println("\ta\tb\tc");<br />

System.out.println("\\\\");<br />

System.out.println("'");<br />

System.out.println("\"\"\"");<br />

System.out.println("C:\nin\the downward<br />

spiral");<br />

q Write a println statement <strong>to</strong> produce this<br />

output:<br />

/ \ // \\ /// \\\


Answers<br />

q Output of each println statement:<br />

\\<br />

'<br />

"""<br />

C:<br />

in<br />

a b c<br />

he downward spiral<br />

q println statement <strong>to</strong> produce the line of output:<br />

System.out.println("/ \\ // \\\\ /// \\\\\\");


Questions<br />

q What println statements will generate this output<br />

This program prints a<br />

quote from the Gettysburg Address.<br />

"Four score and seven years ago,<br />

our 'fore fathers' brought forth on<br />

this continent a new nation."<br />

q What println statements will generate this output<br />

A "quoted" String is<br />

'much' better if you learn<br />

the rules of "escape sequences."<br />

Also, "" represents an empty String.<br />

Don't forget: use \" instead of " !<br />

'' is not the same as "


Answers<br />

q println statements <strong>to</strong> generate the output:<br />

System.out.println("This program prints a");<br />

System.out.println("quote from the Gettysburg Address.");<br />

System.out.println();<br />

System.out.println("\"Four score and seven years ago,");<br />

System.out.println("our 'fore fathers' brought forth on");<br />

System.out.println("this continent a new nation.\"");<br />

q println statements <strong>to</strong> generate the output:<br />

System.out.println("A \"quoted\" String is");<br />

System.out.println("'much' better if you learn");<br />

System.out.println("the rules of \"escape sequences.\"");<br />

System.out.println();<br />

System.out.println("Also, \"\" represents an empty<br />

String.");<br />

System.out.println("Don't forget: use \\\" instead of<br />

\" !");<br />

System.out.println("'' is not the same as \"");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!