Download MCS220Week2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java development environment
and Review of Java
EclipseTM Intergrated Development
Environment (IDE)

Running Eclipse:
Warning: Never check the “Use this as the default and do not ask again” box.
The importance of workspace



This is where you will find all your java files.
You can switch from one workspace to
another.
You need to define the workspace first
(where you want to put your files) before click
on OK
TM
Eclipse tutorial
Perspective is a set of
related Views (windows)
that enable a development
specialist to perform
specific tasks
Setting up preferences in Eclipse
If you did not download
the JDK earlier in this
tutorial, and your existing
JRE does not appear
in the “Installed JREs”
preference, you must
add it yourself.
Setting up preferences in Eclipse
Setting up preferences in Eclipse
Create an Eclipse project
From the Eclipse
menu bar select File,
New, Project to start
the “New Project”
wizard.
Select “Java Project”
and click “Next”.
Create an Eclipse project
Create a Java package

Structure in dot format:
for example: com.tm.tutorial.main
Create Java classes
Create Application class under com.tm.tutorial.main package
Create Java classes
Create Application class under com.tm.tutorial.main package
Implementation of Java class
Implementation of Java class
Generating setters and getters for
attributes
Implementation of Java class
Generating constructors
Implementation of Java class
Generating constructors
Compile Java Application
Compile Java Application
Run the application
Run the application
Run the application
Run the application
Run Application (Shorter way)
Review of Java fundamentals
Template for Class Definition
Import Statements
Class Comment
class
{
Class Name
Data Members
Methods
(incl. Constructor)
}
Review of Java fundamentals
Template for Class Definition
Import Statements
Class Comment
class
{
Class Name
Data Members
Methods
(incl. Constructor)
}
Numeric data
Variable declaration:
<data type> <variable_name>;
If more than one variable has the same data
type:
<data type> <name1>, <name2>..;
Six numerical data types






byte: -128 to 127
short:-32768 to 32767 (-215 to 215-1)
int: -231 to 231-1
long: -263 to 263-1
float: -3.4E+38 to 3.4E+38
double:-1.797E+308 to 1.797E+308
Assignment statement
<variable name> = <expression>;
Example:
x =2*5+6-1;
Variable names



It must be a legal identifier which is an
unlimited series of characters that begins with
a letter.
It must not be a keyword, a boolean literal
(true or false), or the reserved word null.
It must be unique within its scope.
Variable name (cont.)




Legal identifier:be composed of letters, numbers, _
and $. Identifiers may only begin with a letter, _, or
$.
Keyword:
http://java.sun.com/docs/books/tutorial/java/nutsand
bolts/_keywords.html
Variable names begin with a lowercase letter
Class names begin with an uppercase letter
Constant and variables

Constant:

Value it contains doesn’t change
final int MONTHS_IN_YEAR = 12;

Variables:

Value it contains may vary
double loanAmount;
 loanAmount =0;
 loanAmount = 1000.99;
Integer division and type casting

Integer division:




Integer/integer = integer, 7/2 = 3
Integer/double = double, 7/2.0 = 3.5
Double/integer = double, 7.0/2 = 3.5
Type casting: a process that converts a value of one
data type to another data type.
Implicit casting
Explicit casting
Type casting (cont.)

Implicit casting:


Operand is converted from a lower to a higher
precision
Higher precision: a data type with a larger range
of values



Double has a higher precision than float
Int has a higher precision than short
Operand: can be a constant, variable, method call
or another arithmetic expression
Type casting (cont.)

Explicit casting
(<data type>) <expression>
 Example:
float result;
result = (float) ((3+5)/6);
and
result = ((float) (5+3))/6;

Simple Choice Statement
if (<boolean expression>)
<block>;
else
<block>;
if (<boolean expression>)
single statement;
else
single statement;
Boolean expression

Boolean expression: is a conditional expression that
is evaluated to either true or false.

Conditional expression: is a three part expression:
<exp.>

<relational operators> <exp.>
Boolean expressions can be combined by boolean
operators
Relational Operators
a
a
a
a
a
a
Expression
== b
!= b
> b
< b
>= b
<= b
Meaning
Is a equal to b?
Is a not equal to b?
Is a greater than b?
Is a less than b?
Is a greater than or equal to b?
Is a less than or equal to b?
Boolean operators
&& means
AND
||
means OR
!
means NOT
The While Loop
while(<boolean expression>){
// Repeat multiple statements.
statement 1
statement 2
statement 3
...
}
The Do Loop
do{
// Repeat multiple statements.
statement 1
statement 2
statement 3
...
} while(<boolean expression);
•Note that the statements in the body of the loop
are always executed at least one.
•Note the final semicolon, which is required.
The For-Loop Outline
// Repeat multiple statements.
for(initialization; condition; post-body update){
// Statements to be repeated.
statement 1
statement 2
statement 3
...
}
•Commonly used with increment
and decrement operators.
Increment and Decrement

Used as a shorthand for add-one-to and
subtract-one-from:




value = value+1;
value += 1;
value++;
Prefix and postfix forms:



++value;
--value;
value--;
Attributes (Data Member) Declaration
<modifiers>
<data type> <name> ;
Modifiers
Data Type
private
String
Name
ownerName ;
Note: There’s only one modifier
in this example.
Method Declaration
<modifier>
<return type>
<method name>
( <parameters>
){
<statements>
}
Modifier
public
Return Type
void
Method Name
setOwnerName
ownerName = name;
}
(
Parameter
String
name
Statements
) {
Constructor

A constructor is a special method that is executed when a new
instance of the class is created.
public <class name> ( <parameters> ){
<statements>
}
Modifier
public
Class Name
Bicycle
Parameter
(
) {
ownerName = “Unassigned”;
}
Statements
Arguments and Parameters


An argument is a value we pass to a method.
A parameter is a placeholder in the called method to
hold the value of the passed argument.
class Sample {
class Account {
public static void
main(String[] arg) {
. . .
public void add(double amt) {
Account acct = new Account();
. . .
}
acct.add(400);
. . .
. . .
}
}
. . .
}
argument
balance = balance + amt;
Arguments and Parameters


An argument is a value we pass to a method.
A parameter is a placeholder in the called method to
hold the value of the passed argument.
class Sample {
class Account {
public static void
main(String[] arg) {
. . .
public void add(double amt) {
Account acct = new Account();
. . .
}
}
acct.add(400);
. . .
. . .
}
parameter
balance = balance + amt;
. . .
}