Вы находитесь на странице: 1из 31

Lecture

1: Playing with Java


This lecture covers part of Chapter 1
of the textbook

1. A BRIEF SPIEL ABOUT


COMPUTING
Mitsu Ogihara, CSC120

Why Study CompuGng?


Steady job market for those who can compute
Prevalence and growing need of compuGng
technologies
Business and educaGon computer systems
Mobile phone applicaGons

Mitsu Ogihara, CSC120

What Is Computer Science?


CS is the scienGc and pracGcal approach to
computaGon and its applicaGons. It is the
systemaGc study of the feasibility, structure,
expression, and mechanizaGon of the methodical
processes (or algorithms) that underlie the
acquisiGon, representaGon, processing, storage,
communicaGon of, and access to informaGon,
whether such informaGon is encoded as bits in a
computer memory or transcribed in genes and
protein structures in a human cell. Wikipedia
(as of May, 2014)
Mitsu Ogihara, CSC120

Examples of Areas in Computer


Science
Theory of compuGng = the study of laws that govern
computaGon; what can be computed, and what can be
computed eciently
ArGcial Intelligence = the study of algorithmic ways to
mimic human intelligence
Computer systems = the study of ecient computer
systems, e.g., coordinaGon of computaGon carried out
by mulGple computers
ComputaGonal biology = the study of methods for
understanding biological (and medical data)
Databases = the study of methods for recording,
organizing, and analyzing data
Mitsu Ogihara, CSC120

MoGvaGon for the Course


Since compuGng is so prominent, learning
how to write a computer program is perhaps
as important as learning how to use computer
tools for producGvity (email, word processing,
etc.)
In the future college graduates might be
expected to know how to program . . .

Mitsu Ogihara, CSC120

Is Learning How to Program Easy?


No. Knowing a programming language is
totally dierent from being able to code in
that language.

Mitsu Ogihara, CSC120

The Right Mindset


Do not be afraid of making mistakes
Embrace mistakes, and learn from them

Maintain a high spirit


Do not get too frustrated if you do not get it right

Try to think logically


If your program does not work, it is due to logical
aws that you put in your code
Oben the aws are so simple that you fail to noGce
them

Try to spend much Gme on coding


Mitsu Ogihara, CSC120

2. JAVA AND OUR FIRST PROGRAM

Mitsu Ogihara, CSC120

What Is a Computer Program?


A computer program, or just a program, is a
sequence of instrucGons, wricen to perform a
specied task with a computer
Three representaGons of a program
1. Algorithm: systemaGc idea for performing the task;
can be verbally described and wricen as diagrams
2. Source code: a program wricen using a computer
programming language
3. Executable: a program that the machine at hand can
read and execute to complete the task
Mitsu Ogihara, CSC120

Programming Languages
There are hundreds of
computer programming
languages, some are
historical (i.e., not many
acGve users)
Each language is
designed with specic
features to implement

C++, Java
Lisp
Perl, Python, R, Ruby
Pascal
Fortran
COBOL
C
BASIC

Mitsu Ogihara, CSC120

What Is Java?
Similar to C/C++
Designed with a goal of being able to simulate
a machine virtually

Mitsu Ogihara, CSC120

ManipulaGng Java Programs


(in the CSC120 Style)
Open a Terminal console
1. Use an editor (vi/gedit/emacs) to write a code Lets
say you have saved it as HelloWorld.java
2. Compile the program Type in a console: javac
HelloWorld.java
3. Fix error, if any Now HelloWorld.class is generated
4. Run the program Type in a console: java HelloWorld
5. Repeat the above if necessary
See the demonstraGon!
Mitsu Ogihara, CSC120

The First Program, HelloWorld.java


(the line numbers not part of code)
1
2
3
4
5

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Mitsu Ogihara, CSC120

The Outcome of ExecuGon




% is called a prompt
It nudges the user to type a command

%
% java HelloWorld
Hello, World!

Mitsu Ogihara, CSC120

An Anatomy
Each program starts with public class followed
by the name of the program, called a class name
1
2
3
4
5

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

The lines 2-4 dene a method



Every executable Java program must have a method with the name of main

Mitsu Ogihara, CSC120

HelloWorld.java

1
2
3
4
5

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello,
World!");
C
}
}
This is called a statement and a major building block of the Java program.
Each statement has a ; at the end.
The ; is used as the punctuaGon.

Mitsu Ogihara, CSC120

What Does public class HelloWorld


Mean?
1. It states that this Java program is called
HelloWorld
2. Because of the name it also requires that the
program le name should be
HelloWorld.java
3. Successful compilaGon produces
HelloWorld.class

Mitsu Ogihara, CSC120

The Keywords public and class


1. public: The code can be accessed by other
code and by the Java execuGon command
2. class: The methods in the code can be run
AlternaGves are: interface and abstract class
We will study interface later

Mitsu Ogihara, CSC120

What Does public staGc void


main(String[] args) Mean?
1. public: The code can be accessed by other
code and by the Java execuGon command
2. staGc: The method can be specied by telling
Java HelloWorlds main method
3. void: The method returns nothing
4. Strings[] args: SomeGmes a programmer
allows the user to provide addiGonal
informaGon for execuGon, aber java
programName and the args contains this
informaGon
Mitsu Ogihara, CSC120

System.out.println
System.out.println(. . .) prints on the screen
the symbols appearing within the double
quotes verbaGm and then a carriage return

Mitsu Ogihara, CSC120

Matching Braces
1
2
3
4
5

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Parts of code are surrounded by a


matching pair of { and }.

Each Gme a new level of {} is addiGonal indentaGon is given.


The increment of indentaGon is usually two or four white
spaces.
The indentaGon makes easier to read the code.

Mitsu Ogihara, CSC120

3. CODE EDITING

Mitsu Ogihara, CSC120

Code EdiGng
The CSC120 labs will take place in Ungar426
There are 18 machines, each running a Unix-
like operaGng system
Three recommended code ediGng programs
vi, emacs, gedit
gedit is a wysiwyg editor

Mitsu Ogihara, CSC120

funcXon

vi versus emacs
(examples)

vi
emacs

Moving cursor

Arrow keys

Arrow keys

AlternaGve (Leb,
Down, Up, Right)

h, j, k, l

C-b, C-n, C-p, C-f

InserGng text at the


current posiGon

i to start inserGon; esc to Just start typing


conclude

Replacing a line with


some text

0, D, a, type the text, and C-k and then type the text
then esc

Beginning of line

C-a

End of line

C-e

Saving the le and


close

esc (if not pressed already) C-x C-c


: w q

Mitsu Ogihara, CSC120

4. CODE WRITING PRINCIPLES AND


COMMENTING
Mitsu Ogihara, CSC120

The Naming Rules


The name of the class
The class name starts with a lecer, a $, or a _
The name can contain only alphabets, numbers,
$, and _

Mitsu Ogihara, CSC120

The Art of CommenGng


Three ways to comment
//
Usually for comments that require only one

/* */
Usually for comments that require mulGple lines

/** */
The comments appearing this way can be shared with
others, called javadoc

We will visit javadoc later in the course


Mitsu Ogihara, CSC120

Comments Example
Comments.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

/*
* Class for showing comment examples
* Written by Mitsunori Ogihara
*/
public class Comments {
/**
* main method
* @param args
the arguments
*/
public static void main(String[] args) {
// There is only one line in the program
System.out.println(A code needs comments!");
System.out.println(A code needs indentation!);
}
}
Mitsu Ogihara, CSC120

Code WriGng Principles


1. Indent further whenever a new level of {} is
generated
2. Provide comments so as to remember what
the code is supposed to be doing
3. Insert space between lines for readability, if
necessary
4. No more than one statement per line

Mitsu Ogihara, CSC120

A Program with No IndentaGon


CommentsNoIndent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

/*
* Class for showing comment examples
* Written by Mitsunori Ogihara
*/
public class Comments {
/**
* main method
* @param args
the arguments
*/
public static void main(String[] args) {
// There is only one line in the program
System.out.println(A code needs comments!");
System.out.println(A code needs indentation!");
}
}
Mitsu Ogihara, CSC120