Sadegh Aliakbary Sharif University of Technology Fall 2012.

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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
S517 Web Programming Assistant Professor Xiaozhong Liu
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
DAT602 Database Application Development Lecture 5 JAVA Review.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Repetition Statements while and do while loops
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
By Mr. Muhammad Pervez Akhtar
Java Review if Online Time For loop Quiz on Thursday.
“Hello World” In Java Mehdi Einali Advanced Programming in Java 1.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
“Hello World” In Java Mehdi Einali Advanced Programming in Java 1.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Control Structures.
Primitive Data, Variables, Loops (Maybe)
C Short Overview Lembit Jürimägi.
Advanced Programming in Java
مفاهیم اولیه زبان جاوا Java Basic Concepts
Starting Out with Java: From Control Structures through Objects
Starting JavaProgramming
An Introduction to Java – Part I, language basics
Advanced Programming Behnam Hatami Fall 2017.
class PrintOnetoTen { public static void main(String args[]) {
Advanced Programming in Java
Chapter 2 Programming Basics.
PROGRAM FLOWCHART Iteration Statements.
Review of Previous Lesson
Problem 1 Given n, calculate 2n
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2012

Quiz What does it mean: “Java is platform-independent” Which one is platform independent? Microsoft Word Mozilla Firefox JVM Fall 2012Sharif University of Technology2

Agenda Review First program in java Variables Methods Conditions Loops Fall 2012Sharif University of Technology3

Review Java is Simple object oriented Robust And popular Java is platform independent. Write Once, Run Anywhere! Fall 2012Sharif University of Technology4

First Example Create a file named First.java Java class files have.java extension Note to naming convention Copy this lines to the file Note: File name and class name should be the same. Fall 2012Sharif University of Technology5

First Example (2) Run javac First.java Run java First We don’t use any IDE now. To highlight compile and run stages. Lets watch it in real world! Fall 2012Sharif University of Technology6

Overview of the Example Fall 2012Sharif University of Technology7

Java Programs A simple java program is a file The file contains one class The class name equal to the file name The names are case sensitive The class contains a main method When we run the program, the main method is executed Fall 2012Sharif University of Technology8

Variables What is a variable? A piece of memory Holds data For example a number, string or Boolean Java variables have a fixed size Platform independence Fall 2012Sharif University of Technology9

Java Primitive Types Fall 2012Sharif University of Technology10

Arithmetic Operators Fall 2012Sharif University of Technology11

Operator Precedence * 3 = ? is treated as 1 + (2 * 3) Fall 2012Sharif University of Technology12

Equality and Relational Operators Fall 2012Sharif University of Technology13

Operators Fall 2012Sharif University of Technology14

Operators Fall 2012Sharif University of Technology15

Associativity When two operators with the same precendence the expression is evaluated according to its associativity. x = y = z = 17 is treated as x = (y = (z = 17)) since the = operator has right-to-left associativty 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity Fall 2012Sharif University of Technology16

A simple program int a; a = 12; a+= 2; int b; b = 4; b++; b = a*b; System.out.println(b); Fall 2012Sharif University of Technology17

Methods A method is like a machine Zero or more inputs Zero or one output Other names Function Procedure Fall 2012Sharif University of Technology18 method inputs output

Example double add(double a, double b){ double result = a+b; return result; } double x = 3; double y = 4; double add = add(x,y); System.out.println(add); Fall 2012Sharif University of Technology19 Method name Return type parameters

Parameter Passing Local variables Java passes the parameters by value Fall 2012Sharif University of Technology20

Call by Value public static void main(String[] args) { double x =3; double y =4; double add = add(x,y); System.out.println(add); System.out.println(x); } static double add(double a, double b){ a = a+b; return a; } Fall 2012Sharif University of Technology21

Conditions if(x>y){ System.out.println("X is greater than Y"); } else if(x==y){ System.out.println("X is equal to Y"); } else { System.out.println("Y is greater than X"); } Fall 2012Sharif University of Technology22

Conditions (2) boolean condition = x>y; if(condition){ System.out.println("X is greater than Y"); }else{ System.out.println(“Y >= X"); } Fall 2012Sharif University of Technology23

Loops while do-while for Fall 2012Sharif University of Technology24

While Loop long counter=0; while(counter<10){ counter++; System.out.println(counter); } Fall 2012Sharif University of Technology25

do-while Loop long counter=0; do{ counter++; System.out.println(counter); }while(counter<10); do-while loop is executed at least one time Fall 2012Sharif University of Technology26

for for (int i = 1; i <= 10; i++) { System.out.println(i); } Fall 2012Sharif University of Technology27

For Loop vs. While Loop for (X; Y; Z) { body(); } X; while(Y){ body(); Z; } Fall 2012Sharif University of Technology28

For Loop vs. While Loop (2) for (int i = 1; i <= 10; i++) { System.out.println(i); } Fall 2012Sharif University of Technology29 int i=1; while(i<=10){ System.out.println(i); i++; }

goto goto is a reserved word in java But forbidden! Fall 2012Sharif University of Technology30

Fall 2012Sharif University of Technology31