Object­-Oriented Java Programming

nutmegactSoftware and s/w Development

Nov 10, 2012 (4 years and 10 months ago)

158 views

Object
-
Oriented Java
Programming

CS30

Vrinten.

2

Basic OO


First Java Program


Hello World


Object
-
oriented nomenclature and
conventions

CS30

Vrinten.

3

The First Java Program

class
Hello

{



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


The name of the class is the same
as your file name

CS30

Vrinten.

4

The First Java Program

class

Hello {



/* The Welcome Program


-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


Java program source files (.java)
contain definition of
classes

CS30

Vrinten.

5

The First Java Program

class
Hello

{



/* The Welcome Program


-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


Curly braces pair enclose a block
of code, class
Hello

here

Don’t miss me! NetBeans will indicate
open and close braces

CS30

Vrinten.

6

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void
main

(String [ ] args)
{


System.out.println(“Hello World!");


}

}


Curly braces pair enclose a block
of code, method
main()

here

Don’t miss me!

CS30

Vrinten.

7

The First Java Program

class Hello {



/* The Welcome Program


-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


This is a block of
comments
, for
human, not for computer

Format is same as processing

CS30

Vrinten.

8

The First Java Program

class Hello {



/*

The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


/* and */ pair encloses a comment block

Don’t miss me!

CS30

Vrinten.

9

The First Java Program

class Welcome {



/* The Welcome Program by J M Bishop Dec 96


-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


This is a method of the class
Welcome, named
main()

CS30

Vrinten.

10

The First Java Program

class Hello {



/* The Welcome Program


-------------------


Illustrates a simple program displaying


a message.


*/



public static void main
(
String [ ] args
)

{


System.out.println(“Hello World!");


}

}


There
MUST

be a pair of
parentheses following
ALL

method names

Don’t miss me!

CS30

Vrinten.

11

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (
String [ ] args
) {


System.out.println(“Hello World!");


}

}


A method
may

take some input
from the caller, formally known as
arguments

or
parameters

CS30

Vrinten.

12

The First Java Program

class Hello {



/* The Welcome Program


-------------------


Illustrates a simple program displaying


a message.


*/



public static
void

main (String [ ] args) {


System.out.println(“Hello World!");


}

}


A method
may

give some output to the
caller too, known as
return value

void
means no return value

Vrinten.

13

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public
static

void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


The
static

keyword before a
method definition indicates this is
a
class
method

Vrinten.

14

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public

static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


This method is a
public

one,
others

can
call

me.

CS30

Vrinten.

15

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main

(String [ ] args) {


System.out.println(“Hello World


}

}


Standard properties of the
main()
method

Vrinten.

16

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


A
statement

(instruction) to display
a message

Vrinten.

17

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!")
;


}

}


After every statement, there must
be a semi
-
colon!

Vrinten.

18

The First Java Program

class Hello {



/* The Welcome Program
-------------------


Illustrates a simple program displaying


a message.


*/



public static void main (String [ ] args) {


System.out.println(“Hello World!");


}

}


How to ask the computer to act
according to the instructions in
this program?

Chapter 3

Code Conventions


An introductory comment should begin a program.


Package names should begin with a lowercase letter
and then an uppercase letter should begin each word
within the name.


Class names should be nouns and begin with an
uppercase letter and an uppercase letter should
begin each word within the name.


A comment block should be included before each
class.

.

20

Java Naming Conventions


Leading uppercase letter in class name


public class
MyClass

{


...

}



Leading lowercase letter in field, local
variable, and method (function) names


myField
,
myVar
,
myMethod

Chapter 3

Code Conventions
(
con't
)


Comments should not reiterate what is clear from the
code.


Statements in a method should be indented.


An open curly brace (
{
) should be placed on the same
line as the class or method declaration, and the closing
curly brace (
}
) should be on a separate line and
aligned with the class or method declaration.


Escape Sequences

An escape sequence is a backslash (
\
)
followed by a symbol that together represent a
character.


Commonly used escape sequences:


\
n

newline


\
t

tab (8 spaces)


\
\

backslash


\
"

double quotation mark