Review Session Ryan Ly (slides by Jai Madhok)

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Review Session Jai Madhok
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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 Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Flow of Control Part 1: Selection
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lecture 2 Objectives Learn about objects and reference variables.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
By Mr. Muhammad Pervez Akhtar
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Control Statements: Part1  if, if…else, switch 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.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
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.
Chapter 3 Selection Statements
Java Fundamentals 4.
Chapter 4 – C Program Control
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Objectives You should be able to describe: Interactive Keyboard Input
Selections Java.
Chapter 5: Control Structures II
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Chapter 4 - Program Control
elementary programming
Chapter 4 - Program Control
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Review Session Ryan Ly (slides by Jai Madhok)

Key Concepts  Identifiers- What are they?  Rules to determine invalid vs. valid identifiers  Cannot be a reserved word  No spaces in between  No symbols other than the ‘$’ and ‘_’  Cannot begin with a digit

Data Types  3 kinds of primitive data types  Type conversion between data types  Implicit vs. explicit type conversion  The ‘Cast’ operator: Syntax: (dataType) expression  What happens when an operator has mixed operands?

Strings  A String is an ‘Object’  In Java, they are enclosed in double quotes  What is a null string? Empty string?  How does indexing work?  Predefined methods:  substring(startIndex, stopIndex)  charAt(index)  indexOf(ch)  length()  replace()  toLowerCase(), toUpperCase()

Named Constants  Use the keyword final  Identifier should be in all Capital Letters  Memory location whose content is not allowed to change during program execution  Ex: final int CENTS_PER_DOLLAR= 100;  Several advantages:  Prevent typos ▪ If you mistype the name of the constant, the compiler will warn you as compared to typing in a wrong value which the compiler will blindly accept  Easy to modify the program in the future  Good programming practice

Input Statements  To put data into variables from an input device, we first create an input stream object and associate it with a standard input device Scanner scan= new Scanner(System.in);  More about Scanner:  next()  nextInt()  nextDouble()  nextLine()  How do we read in a single character using a Scanner object? Know how to use these!

Tokenizing & Parsing  Sometimes we read in input in the form of a string and then want to extract numeric data from it  How do we do this? Tokenize and Parse  Syntax: StringTokenizer t= new StringTokenizer(str, delim)  Say we want an integer: int a= Integer.parseInt(t.nextToken())  Other method: Double.parseDouble()  Note: Parsing and type casting are different things

File Handling  A file is defined as an area in secondary storage used to hold information Scanner scan= new Scanner(new FileReader(filename1)); PrintWriter oFile= new PrintWriter(filename2);  Warning: Closing the file you are writing to is more important than you think it is  Use same methods for reading and writing/formatting files as you have been for user input/output

Formatting Output With printf  Syntax: System.out.printf(formatString, argumentList)  formatString: string specifying the format  argumentList: list of arguments containing constant values, variables, or expressions, separated by comma  Example: System.out.printf(“There are %.2f inches in %d centimeters \n”, cm/2.54, cm)  Where cm is a variable of the type int.  Note: You can also use the DecimalFormat class to format your decimal output. Take your pick!

More Fun With Strings  compareTo()  Compare Strings character by character until a mismatch is found  What does this method return?  What is the basis for deciding what a mismatch is and what it isn’t?  equals()  Case sensitive method  How to solve the case sensitivity issue?  What does the method return?

Operators- Order of Precedence (). [] Unary Operators: negation, casting, not (!), ++, -- Arithmetic Operators I: * / % Arithmetic Operators II: + - Comparison Operators: >= Equivalence Operators: == != Logical AND: && Logical OR: || Ternary Operator: ?: Assignment Operators: = += -= *= /= %=

Control Structures  Provide alternatives to sequential program execution and are used to alter flow of execution  Alternatives are:  Selection – if, if/else, switch/case  Repetition – for, while, do-while

While Loops  Using loop control variables  Counter controlled while loops  Sentinel controlled while loops  Flag controlled while loops  EOF controlled while loops  Do-while loops are different from a generic while  Run body then check condition to decide whether to loop T F

for (initial statement; loop condition; update statement) KNOW THIS LOOP COLD! Initial Statement Loop Condition Statements Update Statement T F

 Exit early from a loop  Skip the remaining cases in a switch case construct  Don’t try and use them at random places to make your code work, most of the time it won’t.

 java.util – Scanner, StringTokenizer  java.io – BufferedReader, FileReader, PrintWriter  java.text – DecimalFormat  java.lang – String, Math  Don’t need to import

 Reading flow in java: a = a + 2;  ‘A’ in ASCII is 65  ASCII is a subset of Unicode  Escape sequences  Compound operators  Increment/Decrement operators, pre/post

Documentation Reading  Read up some common methods of the following classes for use in the assignments and exams:  String  Scanner  Math  StringTokenizer  Character

 True/False  Multiple Choice  Code Tracing  Code Completion  Finding Errors  Other random kinds of questions like matching exercises are a possibility

Exam Tips  Read through all questions carefully  Write answers legibly  Show all work for partial credit  Do not spend too much time on any one problem if it is causing trouble, come back to it later on  Read through the chapter summaries in the text book and all of Dr. S’s class notes  Good Luck!

?

char let=‘A’; for (int i=1; i<=3; i++) { for(int j=i; j<5; j++) System.out.print(let); System.out.println(); let+=1; } Predict the output [6 points] Difficulty: Medium

int n1=6, n2=10; if(n1 >= n2/2) { if(true && false) System.out.print(“one”); } else System.out.print(“two”); System.out.print(“three”); Predict the output [3 points] Difficulty: Easy

int num=6; while(num<16) { switch(num%4) { case 1: System.out.println(“one”); break; case 2: System.out.println(“two”); case 3: System.out.println(“three”); break; case 0: System.out.println(“multiple”); } num+=3; } Predict the output [4-6 points] Difficulty: Medium

boolean a=true, b=false; if (a && (true|| !b) ) if( b || (!a && false) ) System.out.println(“happy”); else System.out.println(“ halloween”); System.out.println(“goblins”); Predict the output [6 points] Difficulty: Medium

Sample Code Tracing #5 double a = 1.6; int b = 13; double c = (int) a * 2 + 1; for (; b > 0; b-=3){ b /= c; // legal b/c equivalent to b = (int) (b/c); System.out.println(b); System.out.println(++b * b--); } Predict the output [9 points] Difficulty: Hard

Sample Error Finding #1 String s = “hello world”; String space = s.indexOf(“ “); String pt2 = s.substring(space,s.length); System.out.println(pt2); Find the error [3 points] Difficulty: Easy

Sample Error Finding #2 int a, b=10; do { if (b = 10) a = 1.5; b = 10; else if ((b-1)%2>5)) a = 3; System.println(“a is ” + a “ and b is ” + b); } while (a != 5) Find the 8 errors [16 points] Difficulty: 5 Medium, 3 Hard