Download - Java Coding
![Page 1: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/1.jpg)
Java Coding
David Davenport
Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.
email: [email protected]
Syntax for Variables & ConstantsInput, Output and Assignment
a complete Java programdata representations
![Page 2: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/2.jpg)
IMPORTANT… Students…
This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn!
Instructors…You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.
thank you, David.
![Page 3: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/3.jpg)
From problem to program… The story so far...
JavaSourceCode
Javabytecode
Machinecode
Problem AlgorithmData/Memory requirements
![Page 4: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/4.jpg)
Need Java Syntax for… Algorithm (in pseudo-code)
Sequence, Decision & Repetition, of Data flow operations
• Input, Output & Assignment
Data/Memory requirements Meaningfully named memory locations Restriction on data (data types) Variables or Constants & initial value
Plus comments & methods!
![Page 5: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/5.jpg)
Comments & White space Comments Syntax:
// any text on remainder of current line /* any text across multiple lines */
Examples: // Author: David.
// Date: Oct. 2002
/* This program blah, blah, blah */
Java ignores line endings, blanks lines & white space!
Layout program code for ease of reading!
![Page 6: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/6.jpg)
Identifiers User-defined names
Used for variables, constants, methods, etc. Any sequence of letters, digits and the
underscore character only. First character may not be a digit! Upper and lower case are considered different
(i.e. case sensitive!) Cannot use Java reserved words
• i.e. words such as while, for, class, if, etc.
CS101 rule: Names must be meaningful!
![Page 7: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/7.jpg)
Data Types For now, use only the following…
Primitive int (for numeric integer, e.g. 5, -27, 0, 510…) double (for numeric real, e.g. 5.75, 3.0, -2.6…) char (for any character, e.g. A, a, B, b, 3, ?, &, … ) boolean (for true / false only)
Non-primitive String (for any sequence of zero or more characters
e.g. “CS101”, “A”, “Well done!”, … )
![Page 8: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/8.jpg)
Declaring Variables Syntax:
Type Any Java type
Name (identifier) Convention:
first letter of embedded words capital, except first!
Examples: int age; double area; long initialSpeed; char letterGrade; char lettergrade; boolean exists;
CAUTIONJava is case
sensitive!
type name;
![Page 9: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/9.jpg)
Declaring Constants Syntax:
Type Any Java type
Name (identifier) Convention: all capital letters (& underscore!)
Value (literal, variable, constant, expression)
Examples: final int SPEEDOFLIGHT = 300; final float PI = 3.142; final String COMPANY = “Bilkent”; final char LETTER_GRADE = ‘A’;
Literal valuesString use “…”
char use ‘.’
final type name = value;
![Page 10: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/10.jpg)
Output (1) Syntax:
where output is Literal value
eg. “The area is “, ‘?’, 12.5, …
Named variable or constanteg. area, userName, TAXRATE, …
Expressioneg. 2 * PI * radius,
“The area is “ + area
System.out.println( output );
Value is output exactly as is!
Value in named memory location
is output
Resulting value of expression is
output
Note use of + for string concatenation
![Page 11: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/11.jpg)
Output (2) Use
To output the value & leave text cursor on current line.
System.out.print( output );
System.out.println( “Welcome to CS101”);System.out.println( “The tax rate is “ + TAXRATE + ‘%’);
System.out.println( “Welcome to CS101”);System.out.print( “The tax rate is “);System.out.print( TAXRATE);System.out.println( ‘%’);
System.out.println();Output blank line!
![Page 12: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/12.jpg)
Outline Java Program The CS101 console template…
import java.util.Scanner;
/** …description… @author …yourname… @version 1.00, date*/public class ClassName {
public static void main( String[] args) {Scanner scan = new Scanner( System.in);
// constants
// variables
// program code }
}
ClassName.java
In Java program = class
ClassnameConvention:first letters capitalised
Filename & classname MUST be the same.
![Page 13: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/13.jpg)
Input Syntax:
Examples
StringVariable = scan.next();
intVariable = scan.nextInt();
doubleVariable = scan.nextDouble();
userName = scan.next();age = scan.nextInt();salary = scan.nextDouble();str = scan.nextLine();
• Standard from Java5.0 on• Invalid input may give run-time error!• Program must include:
•import java.util.Scanner;•Scanner scan = new Scanner( System.in);
Variables must be declared
before use
![Page 14: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/14.jpg)
Assignment Syntax:
where expression is operand or operand operator operand
& Operand is
• Literal value• Named Variable or constant• Result of method call• Expression (can use brackets to disambiguate)!
Operator is• +, -, *, /, % (modulus, remainder after integer division)
resultVariable = expression;
Result of expression must
be of suitable type to put into resultVariable!
… is assigned the result of …
![Page 15: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/15.jpg)
Assignment Examples
What is the result of this?
Evaluation rules Bracketed sub-expressions first Operator precedence ( * / % before + - ) Left to right
total = 0; x = y;sum = firstNumber + secondNumber;netPay = grossPay * ( 1 – TAX_RATE/100);count = count + 1;c = Math.sqrt( a * a + b * b );
4 + 2 / 3 – 1
… is assigned the result of …
![Page 16: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/16.jpg)
Outline Java Program The CS101 console template…
import java.util.Scanner;
/** …description… @author …yourname… @version 1.00, date*/public class ClassName {
public static void main( String[] args) {Scanner scan = new Scanner( System.in);
// constants
// variables
// program code }
}
ClassName.java
In Java program = class
ClassnameConvention:first letters capitalised
Filename & classname MUST be the same.
![Page 17: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/17.jpg)
A Complete Example (1)
Problem – find area & circumference… Algorithm
Data requirements
1. Print welcome message2. Ask for & get radius from user3. Compute area as pi.radius.radius4. Compute circumference as 2.pi.radius5. Report area, circumference & radius
L radius - intL area, circumference - double
PI – double, constant = 3.142
![Page 18: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/18.jpg)
A Complete Example (2)
import java.util.Scanner;
/** …description… @author …yourname… @version 1.00, 2005/10/07*/
public class AreaCircum {
public static void main( String[] args) {
// constants
// variables
// 1. Print welcome message
// 2. Ask for & get radius from user
// 3. Compute area as pi.radius.radius
// 4. Compute circumference as 2.pi.radius
// 5. Report area, circumference & radius}
}
AreaCircum.java
![Page 19: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/19.jpg)
A Complete Example (3)
import java.util.Scanner;
/**
* AreaCircum - computes area & circum of circle given radius
*
* @author David
* @version 1.00, 2005/10/07
*/
public class AreaCircum
{
public static void main( String[] args)
{
// constants
final double PI = 3.142;
// variables
int radius;
double area;
double circumference;
Header has been edited to include program description & author name
AreaCircum.java
![Page 20: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/20.jpg)
A Complete Example (3)Scanner scan = new Scanner( System.in);
// 1. Print welcome message
System.out.println( "Welcome to area circumference finder.");
// 2. Ask for & get radius from user
System.out.print( "Please enter the radius: ");
radius = scan.nextInt();
// 3. Compute area as pi.radius.radius
area = PI * radius * radius;
// 4. Compute circumference as 2.pi.radius
circumference = 2 * PI * radius;
// 5. Report area, circumference & radius
System.out.print( "The area of a circle of radius ");
System.out.print( radius);
System.out.print( " is ");
System.out.println( area);
System.out.print( "and its circumference is ");
System.out.print( circumference);
System.out.println();
}
} // end of class AreaCircum
Template line required for Keyboard input.
Steps 2 & 5 expanded as per original algorithm.
![Page 21: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/21.jpg)
Compile & Run…
Um… why the weird circumference value?
![Page 22: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/22.jpg)
Testing… It compiled & ran, but…is it correct? How can you tell?
Enter input & check results it outputs(e.g. radius 5 area 78.55 & circumference 31.42)
are these actually the right answers? Really need more input/output sets
what input values should we use? & how many do we need?
Thinking about testing during designcan help produce better programs!
![Page 23: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/23.jpg)
DATA & DATA TYPESIn more depth…
![Page 24: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/24.jpg)
Communication…
![Page 25: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/25.jpg)
Communicating…
a) One man multiple messages
b) Many men
• multiple messages
• combine state for single message
![Page 26: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/26.jpg)
Communicating… Can use
anything…Abstractions
Up / down On / Off X / O True / False 1 / 0
![Page 27: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/27.jpg)
Data Types For now, use only the following…
Primitive int (for numeric integer, e.g. 5, -27, 0, 510…) double (for numeric real, e.g. 5.75, 3.0, -2.6…) char (for any character, e.g. A, a, B, b, 3, ?, &, … ) boolean (for true / false only)
Non-primitive String (for any sequence of zero or more characters
e.g. “CS101”, “A”, “Well done!”, … )
![Page 28: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/28.jpg)
Numeric representations
Number bases
58310
5.102 + 8.101 + 3.100
4178
4.82 + 1.81 + 7.80
1102
1.22 + 1.21 + 0.20
Base 2 - binary
digits 0 & 1 2n values 0 (2n – 1)
000001010011100101110111
00011011
0000000100100011010001010110011110001001101010111100110111101111
01
![Page 29: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/29.jpg)
Characters… Coding
Size…?
ASCII•128 bit, •English only!
UNICODE•16 bit•All
languages!
0000 — ‘a’ 0001 — ‘b’0010 — ‘c’0011 — ‘d’0100 — ‘+’0101 — ‘-’0110 — ‘*’0111 — ‘/’
1000 — ‘0’1001 — ‘1’1010 — ‘2’1011 — ‘3’1100 — ‘ ’1101 — ‘x’1110 — ‘y’1111 — ‘z’
‘A’.. ‘Z’ 26‘a’.. ‘z’ 26‘0’.. ‘9’ 10punc. ??
62
26 = 6427 = 12828 = 256
Standard Codes
![Page 30: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/30.jpg)
Data Types Primitive
byte, short, int, long (numeric integer) float, double (numeric real)
char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char)
boolean - true / false
Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values
anything & everything else! (we will come to these shortly)
![Page 31: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/31.jpg)
Primitive Numeric Types
Type
byteshortintlong
floatdouble
Storage
8 bits16 bits32 bits64 bits
32 bits64 bits
Min Value
-128-32,768-2,147,483,648-9 x 1018
3.4 x 10 38 7 significant digits 1.7 x 10 308 15 significant digits
Max Value
12732,7672,147,483,6479 x 1018
integer
real
![Page 32: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/32.jpg)
Data Types Primitive
byte, short, int, long (numeric integer) float, double (numeric real)
char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char)
boolean - true / false
Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values
anything & everything else! (we will come to these shortly)
![Page 33: Java Coding](https://reader033.vdocument.in/reader033/viewer/2022061603/568133ea550346895d9adab5/html5/thumbnails/33.jpg)
Misc… Why so many numeric types? Error in reals? Typecasting
int into double, but not double into int! Overflow/underflow Division by zero Why not use String for everything?