DAT602 Database Application Development Lecture 5 JAVA Review.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

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.
Introduction to Programming G51PRG University of Nottingham Revision 1
Air Force Institute of Technology Electrical and Computer Engineering
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Outline Java program structure Basic program elements
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 2: Major Concepts of Programming.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Introduction to Computer Systems and the Java Programming Language.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Java Java Translation Program Structure
Основы языка. 1.Переменные 2.Операторы 3.Примитивные типы 4.Массивы 5.Выражения 6.Блоки 7.Управление циклами.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
COMP Loop Statements Yi Hong May 21, 2015.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
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.
Information and Computer Sciences University of Hawaii, Manoa
Java Language Basics.
Chapter No. : 1 Introduction to Java.
Lecture 2: Data Types, Variables, Operators, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Primitive Data, Variables, Loops (Maybe)
Data types and variables
Introduction to Programming in Java
Starting JavaProgramming
An Introduction to Java – Part I, language basics
Java Language Basics.
Sridhar Narayan Java Basics Sridhar Narayan
Arrays in Java.
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

DAT602 Database Application Development Lecture 5 JAVA Review

JAVA is a product of Sun Microsystem. It is a object oriented language. Some features of JAVA: -interpreted -write once, run anywhere -support multi-thread -cross-platform Database Application Development - Lecture 5

Running mechanism of JAVA Database Application Development - Lecture 5 Java Source (.java) Java Class File (.class) compile Java Virtual (JVM) WindowsLinux execute Interpret

JAVA Editions J2EE(Java 2 Platform Enterprise Edition) J2SE(Java 2 Platform Standard Edition) J2ME(Java 2 Platform Micro Edition) JDK (Java Development Kit) is used for developing Java applications. Database Application Development - Lecture 5

Source code of a simple Java application. HelloWorld.java public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World!"); } } We can use command “javac” to compile java file. The compiled file’s extension name is.class Database Application Development - Lecture 5

Some Java’s Primitive Data Types byte: 8-bit, -128 to 127 short: 16-bit, -32,768 to 32,767 int: 32-bit, -2,147,483,648 to 2,147,483,647 float: 32-bit double: 64-bit char: 16-bit Unicode character boolean: true or false Database Application Development - Lecture 5

How to declare and initialize a variable with primitive data? DATATYPE VariableName = initialValue; int x = 12,y = 300; float z = 3.24; double w = 1.2; boolean b = true; char c = 'A'; Database Application Development - Lecture 5

Array An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. Database Application Development - Lecture 5

Simple example of array class ArrayDemo { public static void main(String[] args) { int[] anArray; // declares an array of integers anArray = new int[10]; // allocates memory for 10 integers anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element anArray[2] = 300; // etc. int value = anArray[2]; } Database Application Development - Lecture 5

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Database Application Development - Lecture 5 OperatorsPrecedence postfixexpr++ expr-- unary++expr --expr multiplicative* / % additive+ - relational = equality== != logical AND&& logical OR||

The Equality and Relational Operators == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to Database Application Development - Lecture 5

Expressions, Statements, and Blocks Now that you understand variables and operators, it's time to learn about expressions, statements, and blocks. Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks. Database Application Development - Lecture 5

Expression An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. You've already seen examples of expressions, illustrated in bold below: int cadence = 0; anArray[0] = 100; Database Application Development - Lecture 5

Statement Statements are roughly equivalent to sentences in natural languages. A statementforms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;). Database Application Development - Lecture 5

Blocks A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. Database Application Development - Lecture 5

The following example, BlockDemo, illustrates the use of blocks: class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); }//end block 2 } Database Application Development - Lecture 5

Control Flow Statements Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. Database Application Development - Lecture 5

Following slides describes the decision-making statements (if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language. Database Application Development - Lecture 5

The if-then Statement The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. Database Application Development - Lecture 5

For example, the Bicycle class could allow the brakes to decrease the bicycle's speed only if the bicycle is already in motion. One possible implementation of the applyBrakes method could be in next slide. Database Application Development - Lecture 5

void applyBrakes(){ if (isMoving){ // the "if" clause: bicycle must be moving currentSpeed--; // the "then" clause: decrease current speed } If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end of the if-then statement. Database Application Development - Lecture 5

The if-then-else Statement The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. You could use an if- then-else statement in the apply Brakesmethod to take some action if the brakes are applied when the bicycle is not in motion. Database Application Development - Lecture 5

In this case, the action is to simply print an error message stating that the bicycle has already stopped. void applyBrakes(){ if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle is stop!"); } Database Application Development - Lecture 5

If – else – then statement The following program, IfElseDemo, assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on. Database Application Development - Lecture 5

class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = ‘A’;} else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } Database Application Development - Lecture 5

The switch Statement Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and intprimitive data types. Database Application Development - Lecture 5

The switch Statement The following program, SwitchDemo, declares an int named month whose value represents a month out of the year. The program displays the name of the month, based on the value of month, using the switch statement. Database Application Development - Lecture 5

class SwitchDemo { public static void main(String[] args) { int month = 8; switch (month) { case 1:System.out.println("January");break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; default: System.out.println("Invalid month."); } Database Application Development - Lecture 5

The while Statements The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as: while (expression) { statement(s) } Database Application Development - Lecture 5

class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } Database Application Development - Lecture 5

do-while statement The Java programming language also provides a do-while statement, which can be expressed as follows: do { statement(s) } while (expression); Database Application Development - Lecture 5

The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. for (initialization; termination; increment) { statement(s) } Database Application Development - Lecture 5

class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } Database Application Development - Lecture 5

Reference: nutsandbolts/index.html Database Application Development - Lecture 5