COMP Introduction to Programming Midterm Review

Similar documents
COMP Introduction to Programming Primitive Types, Strings and Console I/O

COMP Introduction to Programming If-Else Statement, Switch Statement and Loops

Darrell Bethea May 25, 2011

COMP 110 Introduction to Programming. What did we discuss?

Darrell Bethea May 20, 2011

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP 110 Introduction to Programming. What did we discuss?

PROGRAMMING FUNDAMENTALS

Static Imports, Default Values, Number Types and More!

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Control Structures in Java if-else and switch

Programming with Java

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Java is an objet-oriented programming language providing features that support

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Control Structures in Java if-else and switch

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 110 Introduction to Programming. What did we discuss?

AP Computer Science A

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CS171:Introduction to Computer Science II

Lecture 2, September 4

CS111: PROGRAMMING LANGUAGE II

Getting started with Java

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CS 231 Data Structures and Algorithms, Fall 2016

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Announcements. 1. Forms to return today after class:

1.00/1.001 Tutorial 1

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

BM214E Object Oriented Programming Lecture 4

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Lab5. Wooseok Kim

In this chapter, you will:

Introduction to Programming Using Java (98-388)

Handout 4 Conditionals. Boolean Expressions.

Loops. CSE 114, Computer Science 1 Stony Brook University

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Fall 2017 CISC124 9/16/2017

Chapter 4: Control Structures I

VARIABLES AND TYPES CITS1001

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CS 251 Intermediate Programming Java Basics

COMP 11 Class 17 Outline

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS Programming I: Primitives and Expressions

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

COMP 202 Java in one week

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Lecture Set 4: More About Methods and More About Operators

Computational Expression

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Basics of Java Programming

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Full file at

Building Java Programs

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Program Fundamentals

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Lec 3. Compilers, Debugging, Hello World, and Variables

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Prof. Navrati Saxena TA: Rochak Sachan

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Building Java Programs

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

13 th Windsor Regional Secondary School Computer Programming Competition

Loop - 3 & Class. Cheng, Wei COMP May 22, Title

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

You must pass the final exam to pass the course.

What did we talk about last time? Examples switch statements

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Computer Science II (20082) Week 1: Review and Inheritance

Question: Total Points: Score:

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Question: Total Points: Score:

Simple Java Reference

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Expressions & Flow Control

Chapter 2: Data and Expressions

Midterms Save the Dates!

Software Practice 1 Basic Grammar

Chapter 3 Selection Statements

Transcription:

COMP 110-003 Introduction to Programming Midterm Review March 5, 2013 Haohan Li TR 11:00 12:15, SN 011 Spring 2013

Announcements The grades for Lab 3 and Program 2 are released Overall, most of you are doing quite well Similar code has been detected. I will report officially next time Some programs use the same techniques please limit your discussions

Suggestions that I receive Your suggestions in Lab 4 Mostly mentioned: the instructor should show the procedure of writing a program, like the case study for loops I will try to invent more case studies on class Many new concepts can not be introduced using a full program. We have to continue the current way using small pieces of code to introduce new concepts, and learning to combine them by assignments

Suggestions that I receive Your suggestions in Lab 4 More collaborations That s not going to happen! I hope that you can use the lab time and office hour more effectively Also you can send me emails. I usually respond quickly. Another TA That s not going to happen, either Someone mentioned that the tutoring group was helpful

Suggestions that I receive Your suggestions in Lab 4 Focus on one topic, explain slowly and clearly Next time when I ask any questions, please understand it as do you want me to repeat Please don t keep silent. I do reserve time for your questions but it was seldom used in recent lectures Bring back daily jokes

Midterm Review Computer basics Primitive types and strings Branch statements and loop statements Classes and objects

Midterm Review Computer basics Primitive types and strings Branch statements and loop statements Classes and objects

Instructions An instruction is a sequences of 0 s and 1 s that represents a single operation on the computer Example: 00000101 00000001 00000010 Means: ADD 1 2 Instruction Data The output will be 3 These 0 s and 1 s are called bits Why only 0 and 1? Because it is easy to make an electrical device that has only two stable states

Hardware vs Software (Abstractly) Software An organized collection of instructions Hardware Circuits that execute, store and interact with instructions Execution: CPU Storage: Memory Interaction: Peripherals, like keyboards, monitors, networks

CPU (Central Processing Unit) It is the brain of the computer CPU executes the instructions CPU s working routine read instructions and data from memory do calculation write calculation results back to memory Intel Core i7 3.4 GHz Executes at most 3,400,000,000 instructions per second Recall: KB, MB, GB, TB

From Languages to Instructions The translator is called compiler It is also a program From human-readable to machine-readable COMPILER

Midterm Review Computer basics Primitive types and strings Branch statements and loop statements Classes and objects

Primitive Types Integer (byte, short, int, long) 0, -3, 5, 43 Floating-point number (float, double) 0.5, 12.4863, -4.3 Characters (char) A, r, %, T Boolean (boolean) true, false

Variable Declaration Syntax: type variable_1, variable_2, ; Examples: int count, score, myint; char letter; double totalcost, ratio;

How to name an identifier Naming rules: Letters, digits(0-9) First character cannot be a digit No spaces Java is case sensitive Legal names pinkfloyd, b3atles, eyecolor Illegal names michael.bolton, kenny-g, 1CP

Assign and Change Variables int changingvar = 0; Declare and assign value type variable = value; changingvar = 5; Assign/change value, variable must be declared before variable = value; changingvar = changingvar + 4; Can refer to itself It means newvalue = oldvalue + 4. Now changingvar =?

Special Assignment Operators Some operators are new to you total += 5; // is the same as total = total + 5; count++; // is the same as count = count + 1; They are created because It s shorter Less possibility of making mistakes

Assignment Compatibilities You can only put small things into bigger things byte->short->int->long->float->double Some examples myshort = myint; Wrong mybyte = mylong; Wrong myfloat = mybyte; Right mylong = myint; Right Recall: double d = int1 / int2;

Type Casting You can ask the computer to change the type of values which are against the compatibility. myfloat = mydouble; mybyte = myint; myshort = myfloat; myfloat = (float)mydouble; mybyte = (byte)myint; myshort = (short)myfloat; You may lose information Recall: int i = (int)(double1 / double2);

Modular Arithmetic - % Remainder 7 % 3 = 1 (7 / 3 = 2, remainder 1) 8 % 3 = 2 (8 / 3 = 2, remainder 2) 9 % 3 = 0 (9 / 3 = 3, remainder 0) clock arithmetic Minutes on a clock are mod 60 Recall: what does (a%2 == 0) mean?

indexof() and substring() 2. 5 + 3 0 1 2 3 4 5 6 String s = "2.5 + 3"; int p1 = s.indexof(" "); int p2 = s.lastindexof(" "); System.out.println(p1 + "," + p2); The output will be 3,5

indexof() and substring() 2. 5 + 3 0 1 2 3 4 5 6 String operand1 = s.substring(0, p1);

indexof() and substring() 2. 5 + 3 0 1 2 3 4 5 6 String operand1 = s.substring(0, p1); String operator = s.substring(p1, p2);

indexof() and substring() 2. 5 + 3 0 1 2 3 4 5 6 String operand1 = s.substring(0, p1); String operator = s.substring(p1, p2); String operand2 = s.substring(p2);

Midterm Review Computer basics Primitive types and strings Branch statements and loop statements Classes and objects

Boolean Expressions A combination of values and variables by comparison operators. Its value can only be true or false Example expressions 5 == 3; // false variable <= 6; // depending on the value of variable What if variable is 5? What if variable is 6? myint!= temp; // depending on both values What if myint is 0 and temp is 2? Am I lying? Syntax rule for if statement: if (boolean expression) { statements;

Logical Operators

More Complex Boolean Expressions Combination of && and ( ( (3 < 7) (2==5) ) && ( (4!=2) && (1 <= 1) ) ) ( ( (true) (false) ) && ( (true) && (true) ) (true) && (true) true if ( ( (I m at Subway) && (You re at Subway) ) ( (I m at Starbucks ) && (You re at Starbucks) ) { I will meet you;

If and Else You can use only one if statement if (boolean expression) { statements; other statements; Other statements will always be executed You can also use an if-else statement if (boolean expression) { statements 1; else { statement 2; If the expression is true, run statement 1, otherwise run statement 2

Multibranch and Switch Statement if (year == 1) System.out.println("freshman"); else if (year == 2) System.out.println("sophomore"); else if (year == 3) System.out.println("junior"); else if (year == 4) System.out.println("senior"); else if (year == 5) System.out.println("super senior"); else System.out.println("huh?"); switch (year) { case 1: System.out.println("freshman"); break; case 2: System.out.println("sophomore"); break; case 3: System.out.println("junior"); break; case 4: System.out.println("senior"); break; case 5: System.out.println("super senior"); break; default: System.out.println("unknown"); break;

While Statement Flow of while statement Start from expression evaluation As long as it s true, repeat instructions in brackets while (count <= number) { System.out.println(count); count++;

Do-While Statement Flow of do-while statement Start from body statements Repeat instructions in brackets as long as the expression is true do { System.out.print(count); count++; while (count <= number);

For Statement Flow chart for (Initializing_Action; Boolean_Expression; Update_Action){ Body; for (count = 1; count <= number; count++) { // all the actions

Midterm Review Computer basics Primitive types and strings Branch statements and loop statements Classes and objects

Classes vs. Objects Classes: What we can create Specify the data to save Objects: What have been created Save actual data

Defining a class public class Student { public String name; public int classyear; public double GPA; public String major; //... public String getmajor() { return major; Class name Data (or attributes, or instance variables) public void increaseyear() { classyear++; Methods

Methods returns a String public String getmajor() { return major; public void increaseyear() { classyear++; return type returns nothing

Methods with Parameters public class Student { public String name; public int classyear; // public void setname(string studentname) { name = studentname; public void setclassyear(int year) { classyear = year;

Local Variable Rule Usually, a variable is only accessible in its surrounding brackets public class Variable { String a = "a"; public void f() { String b = "b"; if (a.equals("b")) { String c = "c";

public/private Modifier public: there is no restriction on how you can use the method or instance variable private: can not directly use the method or instance variable s name outside the class

public/private Modifier public class Student { public int classyear; private String major; public class StudentTest{ public static void main(string[] args){ Student jack = new Student(); jack.classyear = 1; OK, classyear is public jack.major = Computer Science ; // ERROR!!! Error!!! major is private

Sample Questions The Fibonacci sequence is defined such that a number in the sequence is the sum of the previous two number in the sequence such that the first two numbers are 0, 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, Write a function, fib that takes an integer as input and displays the Fibonacci sequence to the first value greater than the input. Example: fib(6) would display 0, 1, 1, 2, 3, 5, 8

Sample Questions What is the value of temp at the end of the loop for the following input? (23) 9 4 2 3 8 11 7 System.out.print("Please input an int: "); int input = kb.nextint(); int temp = 0; for(int i=0; i < 6; i++){ if(input % 2 == 1) temp += input; System.out.print("Please input an int: "); input = kb.nextint();

Sample Questions Write a method that returns the absolute value of the integer passed in to the method. The absolute value of a number is its numerical value without regard to its sign. For example, the absolute value of -3 is 3. The absolute value of 3 is also 3. Fill in the return type for the method below, and then fill in the body. public absolutevalue(int num) {

Sample Questions public int absolutevalue(int num) { if (num < 0) return num; else return num; Another possibility: public int absolutevalue(int num) { if (num < 0) num = -num; return num;