cs 112 introduction to programming · 6 writing java programs ! the basic way is to use a text...

22
CS 112 Introduction to Programming (Spring 2012) Lecture #3: Programming Environment Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements: some slides used in this class are taken directly or adapted from those accompanying the two textbooks: Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne and Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp

Upload: others

Post on 27-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

CS 112 Introduction to Programming

(Spring 2012)

Lecture #3: Programming Environment

Zhong Shao

Department of Computer Science Yale University

Office: 314 Watson

http://flint.cs.yale.edu/cs112

Acknowledgements: some slides used in this class are taken directly or adapted from those accompanying the two textbooks: Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne and Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp

Page 2: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Programming in Any Language

q  Programming consists of three tasks ❍ Create and edit source code file •  The source code is usually human understandable

❍ Compile the source code into machine code •  Machine code ranges from vaguely understandable to nondescript 0's and 1’s

❍ Execute/run/test machine code •  Your computer (specifically the CPU) executes the machine code, 1 instruction at a time.

Page 3: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Java Programming

q  Programming in Java consists of the same tasks, but they are slightly specialized

o  Create and edit “Java source code” (.java files) •  Eg. Hello.java

q  Compile into “Java bytecode” (.class files) o  Eg. javac Hello.java to produce Hello.class

q  Execute/run/test bytecode with “Java interpreter” o  Eg. java Hello

run output

source code compile

byte code

Page 4: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

A Simple Java Program

4

public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } } Program output (in red for emphasis): $ java Hello Hello, world! This program produces four lines of output $

Page 5: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

5

Java Is Simple

q All you have to do is to write a text file using the correct syntax, and then: 1)  Execute “javac” with the filename

l  This gives you a .class file 2)  Execute “java” with the .class file

q You're probably wondering, “How do I write a text file using the correct syntax?” ❍ There are two main choices

Page 6: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

6

Writing Java Programs

q The basic way is to use a text editor ❍ Example editors: Notepad, emacs, pico, vim,

kwrite, kate, gedit, etc. •  Note: MS Word is NOT a text editor

❍ The key is that your .java file cannot include any markup or stylistic formatting; just text.

❍ You enter your Java code following Java

Language syntax, then you compile it.

Page 7: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Writing Java Programs (Step 1): Create/Edit   Create/Edit the program by typing it into a text

editor, and save it as HelloWorld.java.

HelloWorld.java

/******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }

Page 8: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

  Create the program by typing it into a text editor, and

save it as HelloWorld.java.   Compile it by typing at the command-line:

javac HelloWorld.java.

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

command-line % javac HelloWorld.java

Writing Java Programs (Step 2): Compile

Page 9: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

  Create the program by typing it into a text editor, and

save it as HelloWorld.java.   Compile it by typing at the command-line:

javac HelloWorld.java.   Execute it by typing at the command-line:

java HelloWorld.

% javac HelloWorld.java % java HelloWorld Hello, World

command-line

Writing Java Programs (Step 3): Execute

Page 10: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

10

Writing Java Programs using an IDE

q  Another way is to use an Integrated Development Environment (IDE) ❍  Example IDEs: DrJava, Eclipse, NetBeans, BlueJ, etc. ❍  An IDE usually presents the user with a space for text (like an

editor) but layers additional features on top of the text for the user's benefit.

•  Note: The underlying file contains pure text, just like a text editor. ❍  These features can be very useful and save time.

•  Example features are code completion, instant compilation, and syntax highlighting.

❍  IDEs need to know where to find the “java” and “javac” programs.

q  For simplicity and consistency, we will use DrJava.

Page 11: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Setting up DrJava on your Laptop

q  Please follow the directions on the main textbook site --- see Part 1 of Assignment 0.

Page 12: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

12

Pantheon Environment

The "Pantheon" is a collection of computers at Yale named after Greek gods Generally, you use Secure Shell to login to eli.yale.edu; eli.yale.edu will automatically direct you to one of the machines http://pantheon.yale.edu/help/ssh/ They run a variant of Unix

Linux General help on pantheon is available at: http://pantheon.yale.edu/help/

Page 13: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

13

Login, Edit, Compile, Run on a Pantheon Computer -  The Pantheon is the first method of Java Programming (ie. text

editor, no IDE) -  Login to a pantheon machine using ssh

❍  Instructions at http://www.yale.edu/its/stc/faq/Pantheon/SSHTerminal.html

-  Create a directory for your cs112 files for the semester $ mkdir cs112

-  Change to the directory $ cd cs112 -  Edit your Java program using any text editor, e.g., pico

$ pico HelloWorld.java type in your program, press control-X, type y, press return

Page 14: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

14

Login, Edit, Compile, Run on a Pantheon Machine (Cont.)

-  Compile a Java program $ javac HelloWorld.java

-  Take a look to see that HelloWorld.class is generated

$ ls HelloWorld.java HelloWorld.class -  Run Java interpreter $ java HelloWorld

Page 15: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

15

Syntax: White Space

q White space ❍  includes spaces, new line characters, tabs ❍ white space is used to separate words and

symbols in a program ❍  extra white space is ignored

q White space allows a Java program to be formatted in many ways, and should be formatted to enhance readability o  the usage of white space forms part of

programming style

Page 16: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

16

Syntax: Comments

q Two types of comments in Java •  single-line comments use //… // this comment runs to the end of the line

•  multi-lines comments use /* … */

/* this comment runs to the terminating symbol, even across line breaks */

q Comments are ignored by the compiler:

❍  used only for human readers (i.e., inline documentation)

Page 17: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Using comments

q Where to place comments: ❍  at the top of each file (a "comment header") ❍  at the start of every method (see later) ❍  to explain complex pieces of code

q Comments are useful for: ❍ Understanding larger, more complex programs. ❍ Multiple programmers working together, who

must understand each other's code.

Page 18: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Practice Slides

Page 19: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Questions

q  What is the output of the following println statements?

System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral");

q  Write a println statement to produce this output:

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

Page 20: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Answers

q  Output of each println statement:

a b c \\ ' """ C: in he downward spiral

q  println statement to produce the line of output:

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

Page 21: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Questions

q  What println statements will generate this output?

This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation."

q  What println statements will generate this output?

A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget: use \" instead of " ! '' is not the same as "

Page 22: CS 112 Introduction to Programming · 6 Writing Java Programs ! The basic way is to use a text editor Example editors: Notepad, emacs, pico, vim, kwrite, kate, gedit, etc. • Note:

Answers

q  println statements to generate the output:

System.out.println("This program prints a"); System.out.println("quote from the Gettysburg Address."); System.out.println(); System.out.println("\"Four score and seven years ago,"); System.out.println("our 'fore fathers' brought forth on"); System.out.println("this continent a new nation.\"");

q  println statements to generate the output:

System.out.println("A \"quoted\" String is"); System.out.println("'much' better if you learn"); System.out.println("the rules of \"escape sequences.\""); System.out.println(); System.out.println("Also, \"\" represents an empty

String."); System.out.println("Don't forget: use \\\" instead of

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