Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);

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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Lecture 4 More on Java® Data Types, Control Structures.
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Chapter 8: Arrays.
Methods. Read two numbers, price and paid (via JOptiondialog) Calculate change; s = JOptionPane.showInputDialog("Enter price”); Double price = Double.parseDouble(s);
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Computer Programming Lab(7).
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSC 142 G 1 CSC 142 Conditionals [Reading: chapter 5]
SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
CS110 Programming Language I
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Computer Programming Lab 8.
Logic & program control part 3: Compound selection structures.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
If Statement if (amount
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS101 Computer Programming I Chapter 4 Extra Examples.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
A: A: double “4” A: “34” 4.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
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,
Decision making and control structure quiz
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
CSC111 Quick Revision.
Intro to CS Nov 2, 2015.
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Java for Beginners University Greenwich Computing At School DASCO
Primitive Data, Variables, Loops (Maybe)
Review Operation Bingo
Class Examples.
Chapter 4 Selection.
Building Java Programs
Welcome back to Software Development!
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
design OO words Debug Variables Data types
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CS 1054 Introduction to Programming in Java
Computer Science Club 1st November 2019.
Presentation transcript:

Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);

Fix the errors! boolean fun = true; int x = 5, y = x % 3; if(x = 5 && y = 2); System.out.print(“right!”); else system.out.print(“wrong!”);

A corrected version: boolean fun = true; int x = 5, y = x % 3; if(x == 5 && y == 2) System.out.print(“right!”); else System.out.print(“wrong!”);

Fix the errors! Assume the user entered the name “bob” String name = “”; //get name from user if(name = ‘Bob’) System.out.print(“hi!”); else if(name = ‘bob’) System.out.print(name);

A corrected version: String name = “”; //get name from user if(name.equals(“Bob”)) System.out.print(“hi!”); else if(name.equals(“bob”)) System.out.print(name);

Fix & predict output int x = 6, y = 12; If x > 5 System.out.print(“Hi there”); System.out.println(“buddy!”); if( y % 2 = 0 ) System.out.println(“even!”); System.out.println(“cool!”); Else (y % 2 != 0) System.out.println(“odd!”);

A corrected version int x = 6, y = 12; if( x > 5 ) { System.out.print(“Hi there”); System.out.println(“buddy!”); } if( y % 2 == 0 ) { System.out.println(“even!”); System.out.println(“cool!”); } else if(y % 2 != 0) System.out.println(“odd!”); Hi therebuddy! even! cool! Output 

Fix & predict output Assume the user entered the name “bob” char y = ‘a’; String x = JOptionPane.showInputDialog(“x: “); if (y = “A” and x = ‘bob’) System.out.print(“Hi bob! ”); System.out.print(“You get an A!”);

A corrected version: Assume the user entered the name “bob” char y = ‘a’; String x = JOptionPane.showInputDialog(“x: “); if (y == ‘A’ && x.equals(“bob”)) System.out.print(“Hi bob! ”); System.out.print(“You get an A!”); You get an A! Output 