JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Introduction to Java Java Translation Program Structure
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Programming Principles Operators and Expressions.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Sachin Malhotra Saurabh Choudhary
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Advanced Programming in Java
Object-Oriented Programming
Advanced Programming Behnam Hatami Fall 2017.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Java - Data Types, Variables, and Arrays
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Chapter 2: Java Fundamentals
Expressions and Assignment
elementary programming
In this class, we will cover:
Problem 1 Given n, calculate 2n
Presentation transcript:

JAVA (something like C)

Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation – java has a ‘class’. The data is the state and the methods (code) is the behavior. You control the methods and access to the data Inheritance – where one class acquires the properties of another class Polymorphism – one interface multiple methods

JAVA editors did a ‘search’ for JAVA editors on the NET and obtained... JCreator – –Download the free one editPlus - I have used this one – –shareware - pay after 30 days WingSoft –

comments A single line comment int row; // keeps track of the row multiple line comments /* this is one line of the comment this is the 2nd line this is the third line */

Documentation comments acts like multiple line comments /** this is one line of the comment this is the 2nd line this is the third line */ note the initial /** The javadoc program will collect these blocks of comments as documentation for the program javadoc filename.java

special symbols { } indicates a block of code ( ) parameters to a method [ ] indicates an array ; terminates a statement,a seperator.used to separate package names Note table of reserved words on page 39

Identifier names Start with a letter Contains letters (upper or lower case) numbers _ (the underscore) $ Can be any length No imbedded spaces or special characters Do not start an identifier with a number

Examples/Naming Conventions numberOfRows num_rows row8 Naming Conventions classes start with a capitol letter HelloWorld ShoppingCart methods & identifiers with a small letter println() getChar() constants are all caps PIE

System.out.println( ) System.out.println (….) sends a String to the monitor System.out.println(“Hi Mom”); The + ‘concatenates’ two variables int aVal = 17; System.out.println(“a = “ + aVal); System.out.println(“a=“ + aV + “ b=“ + bV)

Primitive data types JAVA has 8 primitive data type Name size | range | byte 8-bits -128 ~ 127 short 16-bits -32k ~ +32k int 32-bits -2**31 ~ +2**31 long 64-bits -2**63 ~ + 2**63

Primitive data types (cont.) Name size | range | float 32-bits X 10**38 double 64-bits X 10**308 char 16-bits 0 ~ 65,535 boolean 1-bit true or false (not a 0 or a 1)

Examples of primatives int x; int x, y=7, zz, rows=8; boolean isRunning = false; double dd; float f = 10.3; (error - defaults to a double) float fx = 22.4f; note the ‘f’ byte b1, b2 = 44, cx; short ss=5;

The char char uses a 16-bit Unicode character set The Unicode set allows 65,536 different characters. (about half are assigned) char ch = ‘a’; char ch1, ch2 = ‘7’, ch3 = ‘X’; char delta = ‘\u0394’ delta symbol char copyrt = ‘\u00AE’ copy write symbol escape seq. – page 32 (EX \t is tab)

the String class String is a class, not a primitive data type is a sequence of characters not an array of char terminated by a null immutable – cannot be changed once created String ss; String s1 = “hi mom”; String s1, su=“XXX”, s3=“SDE”;

Java DOS commands Javac A:\>javac test.java –compiler –uses a text file named ????.java –creates a ????.class file of byte code java A:\>java test –the interpreter (runs the class code) –runs an application appletviewer A:\>appletviewer test.html –runs an applet

Minimum JAVA program class Skeleton { public static void main(String args[]) { } // end main } // end class

arithmetic Expression – series of variables (methods) that evaluate to a single variable = assign (evaluate right, assign to right) + add x = x + 7; - subtractx = 8 – cx; * multiplypay = rate * time; /dividetax = too_much / 1.0; %remainder (mod) row = x % 4; row is the remainder of x / 4 ans = 33 % 6; ans is 3

Consider the primitives byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits char 16 bits

Some examples Define and initialize all the primitives Add the following to skeleton and compile short = byte; byte = short; double = float float = double; int = boolean; int = short * byte; int = short * float; double = int * double; float = short * double; int = char; chat = int;

Conversion and casting Automatic – types are compatible destination is larger than the source Casting (target type) value byte b = (byte) someValue; byte b = intExample; error byte b = (byte) intExample; OK byte b = 50; byte x = b/2; Error Byte x = (byte)(b/2); ok

Shortcuts in arithmetic Shortcuts x++ x = x + 1; x--x = x – 1; x+=7;x = x + 7; x*=3;x = x * 3; x/=2;y = y / 2; q-=5;q = q – 5;

ternary expression expression01 ? expression02 : expression03 Expression01 must evaluate to a boolean If expression01 is true, expression 02 is evaluated, else expression03 is evaluated

Example of ? : if (x > y) max = x; else max = y; is the same as max = x>y ? x : y;

‘pre’ & ‘post’ Difference between x++; and ++x; Example int x = 5; System.out.println(x + “ “ + x++); int x = 5; System.out.println(x + “ “ + ++x);

Operator precedence Do work within ( ) Then, left to right… * / % + - = What’s the answer to the following.. n = * ;

if statement if(expression) // expression is evaluated block of code// execute if true else block of code// execute if false Note – a block of code can contain a nested if statement

examples if (x > 6) System.out.println(“x is bigger); else System.out.println(“x is less than 6); if (x>8) {} else System.out.println(“x is less than 8”);

logical operators &evaluate both, both must be true |evaluate both, either must be true && both must be true. If 1 st is false, do not evaluate the 2 nd ||one must be true. If 1 st is true, do not evaluate the 2 nd ~ tilda - not Shift and bit-wise operators – you are on your own (page 80)

&& and & if ((a>b) & (c<=7)) System.out.println(“both true”); else System.out.println(“one not true”); if ((a>b) && (c<=7)) System.out.println(“both true”); else System.out.println(“one not true”);

|| and | if ((a>b) | (c<=7)) System.out.println(“at least one true”); else System.out.println(“both false”); if ((a>b) || (c<=7)) System.out.println(“at least one true”); else System.out.println(“both false”);

if ((a>b) && (a++)) System.out.println(“(true)a=” + a); else System.out.println(“(false)a=” + a); if (a>b) if (c!=7) System.out.println(“123456”); is the same as If((a>b) & (c!=7)) System.out.println(“123456”);

Relational operators ==equal to !=not equal >greater than <less than >=greater than or equal to <=less than or equal to

The loops For a loop you need 3 things a counter of some kind a comparison statement - a test increment the counter some time the loops are

for loop for (int i = 6; i<5; i++) { System.out.println(“counter is “ + i); } the ‘for’ statement contains the counter, increment, and test all together

example For (int k=0; k<3; k++) System.out.println(k); for (int d=5; d<3; d++) System.out.println(k); for (int d=3, int e=7; d<e; d++, e--) System.out.println(d + “ and e=“ + e);

for loop – using a comma for (a =1, b=4; a<b; a++, b--) { System.out.println(“a = “ + a); System.out.println(“b = “ + b); } Gives a = 1 – b = 4 – a = 2 – b = 3

for loop – some variations boolean done = false; for (a =1; !done; a++) { ……… done = true; } boolean done = false; for ( ; !done; ) { ……… done = true; }

while loop int x=0; // counter while (x<5) // test { System.out.println(“counter = “ + x); x++; // increment }

Example #1 int x=4; // counter while (x<5) // test { System.out.println(“counter = “ + x); x++; // increment }

Example #2 int x=4; // counter while (x<5) // test { System.out.println(“counter = “ + x); }

Example #3 int x=14; // counter while (x<5) // test { System.out.println(“counter = “ + x); x++; // increment }

do/while loop int c=3; // counter do { System.out.println(“counter = “ + c); c++; // increment } while (c<5); // test what’s ‘special’ about a do/while loop??

Example #1 int c=3; // counter do { System.out.println(“counter = “ + c); } while (c<5); // test

Example #2 int c=13; // counter do { System.out.println(“counter = “ + c); c++; // increment } while (c<5); // test

Switch statement switch(expression) { case value01:statement; break; case value02:statement; break; default: statement; } The expression is evaluated. Jump to the corresponding case statement with = value

expression must be a byte, short, int, or char When you jump to the proper case value, you execute code until the end of the switch statement or a break statement is executed No matches – jump to default No default – out of switch

Switch(month) { case 1:days=31; break; case 2:days=28; break; case 3: days=31; break; case 4: days=30; break; | case 12:days=31; break; default: days=0; }

switch(month) { case 9: case 4: case 6: case 11:days=30; break; case 2: days=28; break; default: days=31; }

More use of “break” hitting a ‘break’ will terminate a loop for(int x=0; x<100; x++) { if (x==5) break; System.out.println(“X=“ + x); } X=0 X=1 X=2 X=3 X=4

Using ‘break’ in nested loops for(int x=1; x<4; x++) { System.out.println(“X=“ + x); for (int y=0; y<30; y++) { if (y==x) break; System.out.println} (“Y=“ + y); }

“break” with the while loop while (x<100 { if (x==5) break; System.out.println(“X=“ + x); x++; } X=0 X=1 X=2 X=3 X=4

break with a label outer: for(i=1; i<100; i++) { for (j=4; j<50; j++) { ……. if((j%i)==0) break outer; …….. } // end j for loop } // end i for loop

continue statement for (int x=0; x<8; x++) { System.out.print(x + “ “); if(x%2) continue; System.out.println(“ “); }

break / continue / go to break and continue may specify a label a label is a JAVA identifier that indicates a block of code aLabel: { stuff; } It is close to a GoTo. Don’t use it;