Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.

Slides:



Advertisements
Similar presentations
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Advanced Programming in Java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Flow of Control Part 1: Selection
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,
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 4: Control Structures II
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.
August 6, 2009 Data Types, Variables, and Arrays.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Basic Concepts Mehdi Einali Advanced Programming in Java 1.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Elementary Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Yanal Alahmad Java Workshop Yanal Alahmad
Repetition-Sentinel,Flag Loop/Do_While
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 2: Basic Elements of Java
Problem 1 Given n, calculate 2n
Presentation transcript:

Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2

Review Variables Primitive data types Operators Methods Parameter passing Call by value Conditions If, else, else if Loops while do-while for 3

User Input Print on console System.out.println How to read from console? Scanner Example: Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); double d = scanner.nextDouble(); 4

Example Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); long pow = power(a,b); System.out.println(pow); 5

Type Checking Java has a strong type-checking mechanism Some assignment is not permitted int intVal = 2; long longVal =12; intVal = longVal;  Syntax Error longVal = intVal;  OK intVal = (int)longVal;  OK (Type Casting) 6

Direct Type Conversion The arrows are transitive All other conversions need an explicit cast boolean is not convertible char is a special type 7 byte char short int long float double boolean

Type Conversion Grid 8

Type Conversion 9 N : the conversion cannot be performed Y : the conversion is performed automatically and implicitly by Java C : the conversion is a narrowing conversion and requires an explicit cast Y* : the conversion is an automatic widening conversion, but that some of the least significant digits of the value may be lost by the conversion

Example int i = ; //a big integer float f = i; //f stores and approximation of i System.out.println(f);//output : E8 i = (int) f; System.out.println(i); //output : floating-point types are approximations of numbers They cannot always hold as many significant digits as the integer types 10

Switch statement An alternative to if-else Better structure Before Java 1.7 can use byte, short, char, and int (note: not long) primitive data types or their corresponding wrapper types Starting with J2SE 5.0, it is possible to use enum types With Java 1.7 Strings are also allowed 11

switch example switch (i) { case 1: System.out.println("1"); break; case 2: System.out.println("2"); break; default: System.out.println("default"); } 12

Scanner scanner = new Scanner(System.in); boolean again = true; while(again){ System.out.println("1: Play"); System.out.println("2: Setting:"); System.out.println("3: Exit"); System.out.print("Enter Your Choice:"); int i = scanner.nextInt(); switch (i) { case 1: play(); break; case 2: setting(); break; case 3: again = false; break; default: System.out.println("Enter a valid number"); } 13

Break Breaks the execution of a loop while(true){ int nextInt = scanner.nextInt(); if(nextInt==0) break;... } 14

Continue Stops the execution of the body of the loop and continues from the beginning of the loop for(int i=0;i<10;i++){ if(i==4)continue; System.out.println(i); } Difference between continue in for and while 15

Nested Loops Scanner scanner = new Scanner (System.in); int nextInt; do{ nextInt = scanner.nextInt(); for(int i=0;i<nextInt;i++){ System.out.println(i); } }while(nextInt>0); How to break or continue from outer loop? 16

Label outer: for (int i = 0; i < 10; i++) inner: for (int j = 0; j < 10; j++) { if (j == 2) break outer; else { System.out.println(i); System.out.println(j); continue inner; } 17

Tip of the Day: Indentation int nextInt; do{ nextInt = scanner.nextInt(); for(int i=0;i<nextInt;i++){ System.out.println(i); } }while(nextInt>0); 18

Tip of the Day: Indentation int nextInt; do{ nextInt = scanner.nextInt(); for(int i=0;i<nextInt;i++){ System.out.println(i); } }while(nextInt>0); 19

Comments Comments are ignored by compiler Usually is used for documentation and description of the code On line comment //nextInt = scanner.nextInt(); Some line comment /*nextInt = scanner.nextInt(); for(int i=0;i<nextInt;i++){ System.out.println(i); } */ 20

String A sequence of characters Character: char ch = ‘a’; char ch = ‘1’; char ch = ‘#’; Strings: String st = “Ali”; String st = “123”; String st = “1”; String st = “”; String is not a primitive type 21

String String in C and C++ char* and char[] \0 at the end of String Some functions strlen, strcpy, … String in java is a class String in java is not equal to char[] Constant strings “salam!” “Hellow World!” 22

Example Scanner scanner = new Scanner(System.in); String input; input = scanner.next(); switch (input) { case "Salam": System.out.println("Hi!"); break; case "Khdahafez": System.out.println("Bye!"); break; default: System.out.println("Ha?!"); break; } System.out.println(input); 23

Example(2) String input = "Nader and Simin, A Separation"; char ch = input.charAt(0); int i = input.indexOf("Nader"); int j = input.lastIndexOf("Simin"); String newS = input.replace("Separation", "Reconciliation"); String sth = newS + ch + i + j; System.out.println(sth); 24

String methods charAt concat  plus (+) operator contains startsWith endsWith indesxOf  first index of sth lastIndexOf replace substring length split 25