OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.

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

Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Lecture 3 Java Basics Lecture3.ppt.
Introduction to Computer Programming Decisions If/Else Booleans.
Some basic I/O.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
DAT602 Database Application Development Lecture 5 JAVA Review.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Computer Science Selection Structures.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
1 Printing characters : Revisited class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade);// prints A System.out.println((int)grade);
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
CS0007: Introduction to Computer Programming
Key Words / Reserved Words
Chapter 4: Control Structures I
Chapter 2 Clarifications
INTERMEDIATE PROGRAMMING USING JAVA
Chapter 2 Basic Computation
Introduction to programming in java
User input We’ve seen how to use the standard output buffer
SELECTION STATEMENTS (1)
Object-Oriented Programming
Control Statement Examples
Introduction to Programming in Java
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Fundamentals 2.
AP Java Review If else.
Self study.
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
AP Java Review If else.
Control Structure.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

OOP (pre) Basic Programming

Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous print println will display string to the screen with the newline character adding at the end of string public class HelloWorld { public static void main(String args[ ]) { System.out.println(“Hello World!”); } System.out.print(“string to be printed”); or System.out.println(“string to be printed”);

Primitive Data Types in the Java TypeDescription booleantrue or false. byte8-bit two’s-complement integer with values between –2 7 and 2 7 –1 (–128 to 127) short16-bit twos-complement integer with values between –2 15 and 2 15 –1 (–32,768 to 32,767) char16-bit Unicode characters from \u0000 to \uFFFF. For alpha-numerics, these are the same as ASCII with the high byte set to 0 int32-bit two’s-complement integer with values between –2 31 and 2 31 –1 (–2,147,483,648 to 2,147,483,647) long64-bit twos-complement integer with values between –2 63 and 2 63 –1 (–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) float32-bit single precision floating-point numbers using the IEEE standard (+/– about ). double64-bit double precision floating-point numbers using the IEEE standard (+/– about ).

Reading from Keyboard String str1 = “”; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); str1 = in.readLine(); } catch(IOException e) { System.out.println(e); } System.out.println(“You enter “ + str1);

Reading from KB using Scanner Class import java.util.Scanner; public class cond { public static void main(String[] args) { System.out.println("Enter your number:"); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); sc.close(); System.out.println(“x = “ + x); }

Parsing String to int String str1 = ""; int x = 0; Scanner sc = new Scanner(System.in); str1 = sc.nextString(); sc.close(); x = Integer.parseInt(str1); System.out.println(“You enter “ + x);

Operations on Boolean Expressions OperationName =Assignment ==Equality !=Inequality !Logical NOT &AND |OR ^XOR &&Logical AND ||Logical OR ?:if-then-else

Relational Operators OperatorBoolean Result <Less than <=Less than or equal to >Greater than >=Greater than or equal to public class QuickTest { public static void main(String args[ ] ) { System.out.println(“5 is greater than 6: ” + (5>6)); System.out.println(“6 is greater than or equal to 3: ” + (6>=3)); System.out.println(“8 is less than 10: ” + (8<10)); }

if Statement expression is evaluated to be boolean value: true, false –If it is true, the following statement will be executed –If it is false, the following statement will be skipped if (expression) statement; int score = 55; //try to play with this value if (score >= 50) System.out.println(“ Pass ”); System.out.println(“ Fail ”); Note: Let’s see what would happen to this code.

If-else Statement expression is evaluated to be boolean value: true, false –If it is true, statement 1 will be executed –If it is false, statement 2 will be executed if (expression) statement1; else statement2; int score = 55; //try to play with this value if (score >= 50) System.out.println(“ Pass ”); else System.out.println(“ Fail ”); Note: Now what happens.

Conditional Operator expression1 must evaluate to a Boolean value –If it is true, then expression2 is evaluated –If it is false, then expression3 is evaluated expression1 ? expression2 : expression3; int score = 55; System.out.println((score>=50) ? “ Pass ” : “ Fail ”); int score = 55; String result = (score>=50) ? “Pass” : “Fail”; System.out.println(result); or

switch Statements Control expression that drives a switch statement and the label values for each case must evaluate to a –byte, short, char, or int switch (expression) { case C1: statement1; break; case C2: statement2; break; default: statementD; }

switch Statements (cont) What happens if we do not have “break” switch (1) { case 1: System.out.println ("one"); case 2: System.out.println (“two"); default: System.out.println (“Default"); }

switch Statements (cont) What happens if we do not have “break” switch (1) { case 1: System.out.println ("one"); break; case 2: System.out.println (“two"); break; default: System.out.println (“Default"); }

switch Statements (cont) Notice that –blocks are not required for the statements associated with each case because a label identifies the start of each one and a break statement marks the end. switch (strike) { case 0: case 1: System.out.println("Got a 0 or 1, want to do the same thing"); break; case 2: System.out.println("Got a 2, do something different"); break; default: System.out.println("Value out of range"); }