CS 161: Object Oriented Problem Solving

Similar documents
CS 161: Object Oriented Problem Solving

CS 161: Object Oriented Problem Solving

CS 161: Object Oriented Problem Solving

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

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

Data Structure and Programming Languages

Building Java Programs

Repetition with for loops

Topic 6 Nested for Loops

Building Java Programs

AP CS Unit 3: Control Structures Notes

Research Group. 3: Loops, Arrays

Recitation: Loop Jul 7, 2008

CS 241 Data Organization. August 21, 2018

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Full file at

Exercise 4: Loops, Arrays and Files

I. True/False: (2 points each)

Project #1 Computer Science 2334 Fall 2008

King Saud University College of Computer and Information Sciences Computer Science Department

CIS 110: Introduction to Computer Programming

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

CS 112 Introduction to Programming

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

COMP 110/L Lecture 6. Kyle Dewey

CS 177 Spring 2010 Exam I

CS 335 Lecture 02 Java Programming

Building Java Programs

Object Oriented Programming. Java-Lecture 1

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Primitive data, expressions, and variables

CS 6371: Advanced Programming Languages

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

File I/O Array Basics For-each loop

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture Set 4: More About Methods and More About Operators

Bjarne Stroustrup. creator of C++

Building Java Programs

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

Introduction to Data Structures

CS 241 Data Organization using C

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

CSCI455: Introduction to Programming System Design

Last Name: Circle One: OCW Non-OCW

CIS October 16, 2018

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Methods and Data (Savitch, Chapter 5)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS 101 Fall 2005 Midterm 2 Name: ID:

CS 116 Week 8 Page 1

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Building Java Programs

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Welcome to CompSci 201

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Object-Oriented Programming

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

Possible Exam Questions

last time in cs recitations. computer commands. today s topics.

Building Java Programs

Programming with Java

Algorithms and Data Structures Spring 2008

Over and Over Again GEEN163

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

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Dr. Angela Guercio Dr. Natalia Dragan. Spring 2011

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Prelim One Solution. CS211 Fall Name. NetID

CS 112 Introduction to Programming

CS 177 Recitation. Week 8 Methods

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Question 1a) Trace of program

A Foundation for Programming

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Input from Files. Buffered Reader

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

CS 177 Week 5 Recitation Slides. Loops

CS 101 Fall 2006 Midterm 1 Name: ID:

Lab5. Wooseok Kim

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

CSCI 355 Lab #2 Spring 2007

CSE 114, Computer Science 1 Course Information. Spring 2017 Stony Brook University Instructor: Dr. Paul Fodor

MIDTERM REVIEW. midterminformation.htm

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

CS111: PROGRAMMING LANGUAGE II

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Final Examination Semester 3 / Year 2007

Transcription:

About this course CS 161: Object Oriented Problem Solving About this course Course webpage: http://www.cs.colostate.edu/~cs161/ The course webpage is our major communication tool. Check it on a daily basis! RamCT will be used for: forums, grades Piazza instead of RamCT for forums Like 160, 161 is a combination of programming and discrete math. Why is math important to us? What does that have to do with computer science? From procedural to object oriented programming Texts Java: an introduction to problem solving and programming, 5th or 6th edition. Discrete Mathematics and Its Applications, 6th or 7th edition. Kenneth Rosen. 1

Components of the course q Quizzes & class participation: are you with us? Not worth many points but useful finger on the pulse (for you and me) q Tests: what have you learned? Important checkpoints!! q Programming assignments: can you implement it? Important!! You get a chance to learn from your mistakes (regrade) q Written assignments: do you understand the theory? About this course Lectures q You pay for them, might as well use them q We are here to help Recitation q Help you with programming and homework assignments, reinforce material from lecture. q you get credit for attending and participating in recit Grading Assignments Quizzes and participation Recitation (attendance + completion) Midterms (2) one of these is a programming midterm Final CS building Make sure you can get into the Unix lab! If you have keycard access problems: q CS students: talk to any CS front desk person (Kim, Sharon or student employees) For the percentage break down see web page. You need to have a passing grade on the exams (>= 60) to get a passing grade in the course. 2

Professional class behavior We all have to have respect for each other, independent of race, gender, ability THERE ARE NO STUPID QUESTIONS q Your classmates will be grateful you asked. Cheating What is cheating? What is not? Where would you find a definition? What is gained / lost when cheating? What are the consequences? When / how does it happen? How can cheating be avoided? First topic: Recap of CS160. Lecture: Overview of Java program structure Recitation: Write simple programs, homework submission via checkin Homework assignment 1: More recap Reading values from a file import java.io.*; import java.util.*; Issues with the code? class FileReader1{ public static void main(string[ ] args) throws IOException{ while(in.hasnext( )){ list[index] = in.nextint( ); // do something with the array 3

Methods public class C2F { public static void main(string[ ] args){ C2F converter = new C2F(); System.out.print( 100C eq. ); argument System.out.print(converter.c2f(100) + F ); parameter public double c2f(double celsius) { return celsius * 9 / 5 + 32; Primitive variables When a primitive variable is assigned to another, the value is copied. Therefore: modifying the value of one does not affect the copy. Example: int x = 5; int y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17 Methods are discussed in section 5.1 in Savitch Primitive variables as method arguments When primitive variables (int, double) are passed as arguments, their values are copied. Modifying the parameter inside the method will not affect the variable passed in. public void caller() { int x = 23; strange(x); System.out.println("2. Value of x in caller = + x); public void strange(int x) { x = x + 1; System.out.println("1. Value of x in strange = + x); Output: 1. Value of x in strange = 24 2. Value of x in caller = 23 Reading values from a file (again) import java.io.ioexception; import java.util.scanner; class FileReader1{ public static void main(string[ ] args) throws IOException{ while(in.hasnext( )){ list[index] = in.nextint( ); // do something with the array Let s improve this version using methods! 4

import java.io.ioexception; import java.util.scanner; import java.util.arrays; class FileReader2{ public static void main(string[ ] args) throws IOException { ReadFile2 filereader = new FileReader2(); int [ ] values = filereader.readfile(filename); System.out.println(Arrays.toString(values)); public int [ ] readfile(string filename) throws IOException { while(in.hasnext( )) { list[index] = in.nextint( ); return list; import java.io.*; import java.util.*; class FileReader3 { public static void main(string[ ] args) throws IOException{ FileReader3 filereader = new FileReader3(); filereader.readfile(filename, list); System.out.println(Arrays.toString(list)); public void readfile(string filename, int [ ] values) throws IOException { while(in.hasnext( )) { values[index] = in.nextint( ); Objects and the new operator Compare with Why does one use the new operator while the other doesn t? Objects and references Object variables store the address of the object value in the computer's memory. Example: int [] values = new int[5]; values 5 7 10 6 3 int x = 1; x 1 5

Example public static void main(string[] args) { list int[] list = {126, 167, 95; System.out.println(Arrays.toString(list)); doubleall(list); System.out.println(Arrays.toString(list)); values public static void doubleall(int[] values) { for (int i = 0; i < values.length; i++) { values[i] = 2 * values[i]; q Output: [126, 167, 95] [252, 334, 190] index 0 1 2 value 126 252 167 334 190 95 Variable scope scope: The part of a program where a variable exists. q A variable's scope is from its declaration to the end of the { braces in which it was declared. q If a variable is declared in a for loop, it exists only in that loop. q If a variable is declared in a method, it exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println( i= + i + x= + x); // i no longer exists here // x ceases to exist here Variable scope It is illegal to try to use a variable outside of its scope. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); System.out.println(i); // illegal Variable scope It is legal to declare variables with the same name, as long as their scopes do not overlap: public static void main(string[] args) { int x = 2; for (int i = 1; i <= 5; i++) { int y = 5; System.out.println(y); for (int i = 3; i <= 5; i++) { int y = 2; int x = 4; // illegal System.out.println(y); public static void anothermethod() { int i = 6; int y = 3; System.out.println(i + ", " + y); 6