KEMBAR78
Revision of introduction in java programming.pptx
Programming with java
Lecture 1
Revision
Structural programming (java)
Constants and Variables
 Named constant
◦ cannot be changed during program execution
◦ declared by using the reserved word final
◦ initialized when it is declared
 Variable (name, value, data type, size)
◦ content may change during program execution
◦ must be declared before it can be used
◦ may not be automatically initialized
◦ if new value is assigned, old one is destroyed
◦ value can only be changed by an assignment statement
or an input (read) statement
Data Types
 Data type: set of values together with a set of operations
Primitive Data Types
Primitive Data Types
 Floating-Point Data Types
◦ Float: precision = 6 or 7
◦ Double: precision = 15
 Boolean: two values
◦ True
◦ False
Integral Data Types
Values and Memory Allocation
for Integral Data Types
Arithmetic Operators and Operator
Precedence
 Five Arithmetic Operators
◦ + addition
◦ - subtraction
◦ * multiplication
◦ / division
◦ % mod (modulus) operator (integer operands only)
 Unary operator: operator that has one operand
 Binary operator: operator that has two operands
Order of Precedence
1. * / % (same precedence)
2. + - (same precedence)
 Operators in 1 have a higher precedence
than operators in 2.
 When operators have the same level of
precedence, operations are performed from left
to right
Expressions
 Integral expressions
 Floating-point or decimal expressions
 Mixed expressions
Integral Expressions
 All operands are integers
 Examples:
2 + 3 * 5
3 + x – y/7
x + 2 * (y – z) + 18
Floating-point Expressions
 All operands are floating-point numbers
 Examples:
12.8 * 17.5 – 34.50
x * 10.5 + y - 16.2
Mixed Expressions
 Operands of different types
 Examples:
2 + 3.5
6/4 + 3.9
 Integer operands yield an integer result; floatingpoint
numbers yield floating-point results
 If both types of operands are present, the result is
a floating-point number
 Precedence rules are followed
Operators and expressions
 An operator specifies an operation for the JVM to
carry out, E.g., x + y means:
get the value of x,
get the value of y,
add the two values;
 In this example, x and y are the operands
 An expression is a series of operations specified using
constants, variables and operators. E.g.,
x + y, count + 1, a - b - c, a * (b - c)
 The value of an expression is the resulting value after
evaluation
Type conversions (casting)
In general, the assignment statement
<variable> = < expression>;
asks the JVM to evaluate the <expression> and stores the
result in the cells represented by the <variable> .
If the type of the result is different from that of the variable,
a type conversion (casting) is conducted. E.g.,
int x;
double y;
y = 7 / 3; //from int to double, casting is automatic
x = 7.0 / 3; //from double to int, explicit casting is needed
x = (int) (7.0 / 3); //Alright. (int) specifies the casting
// operation that changes the result from double to int
Type Conversion (Casting)
 Used to avoid implicit type coercion
Syntax
(dataTypeName) expression
 Expression evaluated first, then type
converted to dataTypeName
 Examples:
(int)(7.9 + 6.7) = 14
(int)(7.9) + (int)(6.7) = 13
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
Declaring and Initializing
in One Step
 int x = 1;
 double d = 1.4;
 float f = 1.4;
Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
Flow of control
( control structure)
(if/if – else) statements
 Examples:
boolean move = false;
if (move)
System.out.println ("I am selling my house.");
else
System.out.println ("I am staying.");
if (name == "fred") {
x = 4;
}
else {
x = 20;
}
NOTE: When the conditional if statement is used only to make an assignment to
one variable:
x = (name == "fred") ? 4 : 20;
Switch-case
 Example:
int day; // Assume this was initialized somewhere.
boolean exerciseDay = false;
boolean weekend = false;
switch (day) {
case 1: // Monday
System.out.println("Monday ... Arr ...sucks");
break;
case 2: // Tuesday
exerciseDay = true;
break;
case 4: // Thursday
exerciseDay = true;
break;
case 0: // Sunday
break;
case 6: // Saturday
weekend = true;
break;
default: // Everything else.
System.out.println ("Ordinary work day.");
break;
}
While loops
 Examples:
int i = 0;
while ((i * i) != 81)
++i;
System.out.println ("The square root of 81 is: " +i);
int j = 0;
while (j < 10) {
System.out.println ("j = " + j); ++j;
}
Continue statements
int x = 0;
while (x < 10) {
x++;
System.out.println(x);
continue;
// you will never get to this point!!
};
Break statements
int x = 0;
while (x < 10) {
x++;
System.out.println(x);
break;
// you will never get to this point!
};
Do-while loops
 Examples:
char ch = 'z';
do {
System.out.print (ch);
--ch;
} while (ch != 'a');
// Loop doing the same thing
while (ch != 'a') {
System.out.print (ch);
--ch;
}
For loops
 Examples:
int sum = 0;
for (int i = 1; i <= 100; ++i)
sum += i;
int loops = 0;
for (int j = 1000; j > 0; j /= 2) {
System.out.println ("j = " + j);
++loops;
}
System.out.println ("It took " + loops + " loops.");
Methods
 Also called “functions"
 Declared with:
◦ A return type (void if no return)
◦ A name
◦ 0 or more parameters
 May be called within any expression
 May be "overloaded" - i.e., multiple functions
may have the same name, as long as they
take different combinations of parameters
Methods
 A Method is a small program that is invoked to
accomplish a task. E.g., b = Math.cos(1.0);
 Math is a built-in class that has many methods. cos() is a
method in Math.
 When the above statement is executed, the method is invoked
and x matches with 1.0. The method computes cos(1.0) and
returns the result, 0.999847. The result is assigned to b.
 Other methods in Math are sin(x), tan(x), sqrt(x), …
Overloaded methods and coercion of
arguments
 A method may have 0, 1, or more parameters. Eg,
public static double random()
 Some of methods are Overloaded methods, which means
that more than one method exists with the same name, but
with different type of parameters.
 Java automatically converts arguments of an incorrect type
into arguments of the type required by the method.
 This process is known as the coercion of arguments
Normal Function
// This function adds all numbers from 1 // to the argument and
returns the sum.
int computeSum (int arg) {
int sum = 0;
for (int i = 1; i <= arg; ++i)
sum += i;
return sum;
}
// This function prints out all of the sums for //each odd number
from 1 to 19.
void functionB () {
for (int i = 1; i <= 19; i += 2)
System.out.println ("computeSum(i)=" + computeSum (i));
}
Main method
 Special function where applications are first
called
 Command line arguments are available as an
array of Strings
 Always declared as "public static void"
 Example:
public static void main (String[] args) {
// Print out the hello world
System.out.println("Testing - Hello World");
}

Revision of introduction in java programming.pptx

  • 1.
    Programming with java Lecture1 Revision Structural programming (java)
  • 2.
    Constants and Variables Named constant ◦ cannot be changed during program execution ◦ declared by using the reserved word final ◦ initialized when it is declared  Variable (name, value, data type, size) ◦ content may change during program execution ◦ must be declared before it can be used ◦ may not be automatically initialized ◦ if new value is assigned, old one is destroyed ◦ value can only be changed by an assignment statement or an input (read) statement
  • 3.
    Data Types  Datatype: set of values together with a set of operations
  • 4.
  • 5.
    Primitive Data Types Floating-Point Data Types ◦ Float: precision = 6 or 7 ◦ Double: precision = 15  Boolean: two values ◦ True ◦ False
  • 6.
  • 7.
    Values and MemoryAllocation for Integral Data Types
  • 8.
    Arithmetic Operators andOperator Precedence  Five Arithmetic Operators ◦ + addition ◦ - subtraction ◦ * multiplication ◦ / division ◦ % mod (modulus) operator (integer operands only)  Unary operator: operator that has one operand  Binary operator: operator that has two operands
  • 9.
    Order of Precedence 1.* / % (same precedence) 2. + - (same precedence)  Operators in 1 have a higher precedence than operators in 2.  When operators have the same level of precedence, operations are performed from left to right
  • 10.
    Expressions  Integral expressions Floating-point or decimal expressions  Mixed expressions
  • 11.
    Integral Expressions  Alloperands are integers  Examples: 2 + 3 * 5 3 + x – y/7 x + 2 * (y – z) + 18
  • 12.
    Floating-point Expressions  Alloperands are floating-point numbers  Examples: 12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2
  • 13.
    Mixed Expressions  Operandsof different types  Examples: 2 + 3.5 6/4 + 3.9  Integer operands yield an integer result; floatingpoint numbers yield floating-point results  If both types of operands are present, the result is a floating-point number  Precedence rules are followed
  • 14.
    Operators and expressions An operator specifies an operation for the JVM to carry out, E.g., x + y means: get the value of x, get the value of y, add the two values;  In this example, x and y are the operands  An expression is a series of operations specified using constants, variables and operators. E.g., x + y, count + 1, a - b - c, a * (b - c)  The value of an expression is the resulting value after evaluation
  • 15.
    Type conversions (casting) Ingeneral, the assignment statement <variable> = < expression>; asks the JVM to evaluate the <expression> and stores the result in the cells represented by the <variable> . If the type of the result is different from that of the variable, a type conversion (casting) is conducted. E.g., int x; double y; y = 7 / 3; //from int to double, casting is automatic x = 7.0 / 3; //from double to int, explicit casting is needed x = (int) (7.0 / 3); //Alright. (int) specifies the casting // operation that changes the result from double to int
  • 16.
    Type Conversion (Casting) Used to avoid implicit type coercion Syntax (dataTypeName) expression  Expression evaluated first, then type converted to dataTypeName  Examples: (int)(7.9 + 6.7) = 14 (int)(7.9) + (int)(6.7) = 13
  • 17.
    Assignment Statements x =1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;
  • 18.
    Declaring and Initializing inOne Step  int x = 1;  double d = 1.4;  float f = 1.4;
  • 19.
    Constants final datatype CONSTANTNAME= VALUE; final double PI = 3.14159; final int SIZE = 3;
  • 20.
    Flow of control (control structure)
  • 21.
    (if/if – else)statements  Examples: boolean move = false; if (move) System.out.println ("I am selling my house."); else System.out.println ("I am staying."); if (name == "fred") { x = 4; } else { x = 20; } NOTE: When the conditional if statement is used only to make an assignment to one variable: x = (name == "fred") ? 4 : 20;
  • 22.
    Switch-case  Example: int day;// Assume this was initialized somewhere. boolean exerciseDay = false; boolean weekend = false; switch (day) { case 1: // Monday System.out.println("Monday ... Arr ...sucks"); break; case 2: // Tuesday exerciseDay = true; break; case 4: // Thursday exerciseDay = true; break; case 0: // Sunday break; case 6: // Saturday weekend = true; break; default: // Everything else. System.out.println ("Ordinary work day."); break; }
  • 23.
    While loops  Examples: inti = 0; while ((i * i) != 81) ++i; System.out.println ("The square root of 81 is: " +i); int j = 0; while (j < 10) { System.out.println ("j = " + j); ++j; }
  • 24.
    Continue statements int x= 0; while (x < 10) { x++; System.out.println(x); continue; // you will never get to this point!! };
  • 25.
    Break statements int x= 0; while (x < 10) { x++; System.out.println(x); break; // you will never get to this point! };
  • 26.
    Do-while loops  Examples: charch = 'z'; do { System.out.print (ch); --ch; } while (ch != 'a'); // Loop doing the same thing while (ch != 'a') { System.out.print (ch); --ch; }
  • 27.
    For loops  Examples: intsum = 0; for (int i = 1; i <= 100; ++i) sum += i; int loops = 0; for (int j = 1000; j > 0; j /= 2) { System.out.println ("j = " + j); ++loops; } System.out.println ("It took " + loops + " loops.");
  • 28.
    Methods  Also called“functions"  Declared with: ◦ A return type (void if no return) ◦ A name ◦ 0 or more parameters  May be called within any expression  May be "overloaded" - i.e., multiple functions may have the same name, as long as they take different combinations of parameters
  • 29.
    Methods  A Methodis a small program that is invoked to accomplish a task. E.g., b = Math.cos(1.0);  Math is a built-in class that has many methods. cos() is a method in Math.  When the above statement is executed, the method is invoked and x matches with 1.0. The method computes cos(1.0) and returns the result, 0.999847. The result is assigned to b.  Other methods in Math are sin(x), tan(x), sqrt(x), …
  • 30.
    Overloaded methods andcoercion of arguments  A method may have 0, 1, or more parameters. Eg, public static double random()  Some of methods are Overloaded methods, which means that more than one method exists with the same name, but with different type of parameters.  Java automatically converts arguments of an incorrect type into arguments of the type required by the method.  This process is known as the coercion of arguments
  • 31.
    Normal Function // Thisfunction adds all numbers from 1 // to the argument and returns the sum. int computeSum (int arg) { int sum = 0; for (int i = 1; i <= arg; ++i) sum += i; return sum; } // This function prints out all of the sums for //each odd number from 1 to 19. void functionB () { for (int i = 1; i <= 19; i += 2) System.out.println ("computeSum(i)=" + computeSum (i)); }
  • 32.
    Main method  Specialfunction where applications are first called  Command line arguments are available as an array of Strings  Always declared as "public static void"  Example: public static void main (String[] args) { // Print out the hello world System.out.println("Testing - Hello World"); }