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

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

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

Building Java Programs

CS 241 Data Organization. August 21, 2018

Building Java Programs

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

Welcome to CompSci 201

CS111: Intro to Computer Science

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

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

CompSci 125 Lecture 02

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

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

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

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

Data Structure and Programming Languages

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

CS 177 Spring 2010 Exam I

CSC116: Introduction to Computing - Java

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

Building Java Programs

Repetition with for loops

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Topic 6 Nested for Loops

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Introduction to Data Structures

AP Computer Science A: Java Programming

CSC116: Introduction to Computing - Java

Topic 4 Expressions and variables

CS 211 Programming I for Engineers

Announcements. 1. Forms to return today after class:

Primitive data, expressions, and variables

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

CIS 110: Introduction to Computer Programming

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

Computer Science II Data Structures

Midterms Save the Dates!

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

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

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

CS 116 Week 8 Page 1

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

Announcements. Course Staff. Course Staff. Lectures 10/19/11. CS/ENGRD 2110 Fall Instructor. Administrative Assistant

CSC116: Introduction to Computing - Java

Building Java Programs

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

Building Java Programs

CS 241 Data Organization using C


EECS168 Exam 3 Review

Building Java Programs

Project #1 Computer Science 2334 Fall 2008

CS 335 Lecture 02 Java Programming

Building Java Programs Chapter 2

COMP 401 COURSE OVERVIEW

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

CSCI 355 LAB #2 Spring 2004

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

Syllabus COSC-051-x - Computer Science I Fall Office Hours: Daily hours will be entered on Course calendar (or by appointment)

CSCI 355 Lab #2 Spring 2007

Computer Science II Lecture 1 Introduction and Background

Prelim One Solution. CS211 Fall Name. NetID

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

Lab5. Wooseok Kim

CS 106 Introduction to Computer Science I

Data Structures and Algorithms

Central Washington University Department of Computer Science Course Syllabus

CS 0449 Intro to Systems Software Fall Term: 2181

COMP 110/L Lecture 6. Kyle Dewey

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

I. True/False: (2 points each)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

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

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Object-Oriented Programming

EE 209: Programming Structures for Electrical Engineering

Lecture 4: Introduction to Java and your Development Environment

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Welcome (back) to CS1007!

CS 231 Data Structures and Algorithms, Fall 2016

6.092 Introduction to Software Engineering in Java January (IAP) 2009

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

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

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

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CS Programming Language - JAVA. Fall 2004 Sept. 15th

CS 11 java track: lecture 1

Lesson 36: for() Loops (W11D1)

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Object-Oriented Programming for Managers

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

COMP-202 Unit 0: Course Details

AP CS Unit 3: Control Structures Notes

Transcription:

CS160 - Intro CS 161: Object Oriented Problem Solving CS160 was your first programming course Like 160 a combination of programming and discrete math Procedural Java Object Oriented programming Instructor: Asa Ben-Hur Teaching Assistants: Nathan Lindzey, Erin Nagoshi Course staff Instructor: Asa Ben-Hur Course webpage: http://www.cs.colostate.edu/~cs160/ ramct will be used as little as possible TAs: Nathan Lindzey, Erin Nagoshi Texts Data Abstraction and Problem Solving with Java: Walls & Mirrors, 2nd edition. Frank M. Carrano and Janet J. Prichard. Discrete Mathematics and Its Applications, Sixth Edition. Kenneth Rosen Components of the course Java: an introduction to problem solving and programming, Fifth edition. Walter Savitch and Frank M. Carrano forum posting grades Quizzes & Class Participation: are you with us? Tests: what have you learned? Programming assignments: can you implement it? Written assignments: do you understand the theory? 1

Lectures Recitation Help you with programming and homework assignments, reinforce material from lecture. you get credit for attending and participating in recit! Grading Assignments 40% Quizzes 10% Recitation (attendance + completion) 10% Midterms (2) 20% Final 20% You need to have a passing grade on the exams to get a passing grade in the course CS building Make sure you can get into the Unix lab! If you have keycard access problems: CS students: talk to any CS front desk person (Kim/Jessica/Taj/Carol/Sharon) First Week Preview Recap of cs160. Lecture: Overview of Java program structure and syntax Recitation: Write simple programs, homework submission using checkin, linux refresher Homework: More practice The structure of a Java program //**************************************** // Hello.java // author : Asa Ben-Hur // Program to practice Java and Eclipse //**************************************** public class Hello { System.out.println("Hello World! ); Reading values from a file import java.util.*; Issues with the code? class FileReader1{ while(in.hasnext( )){ // do something with the array 2

Methods C2F converter = new C2F(); System. out.print(converter.c2f(100) + F ); public double c2f(double celsius) { Static Methods System. out.println(c2f(100) + F ); public static double c2f(double celsius) { Methods are discussed in pages 249-258 in Savitch Static vs. Object Methods C2F converter = new C2F(); System. out.print(converter.c2f(100) + F ); public double c2f(double celsius) { System. out.println(c2f(100) + F ); public static double c2f(double celsius) { 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. int x = 5; int y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17 Primitive variables as method parameters When primitive variables (int, double) are passed as parameters, their values are copied. Modifying the parameter inside the method will not affect the variable passed in. int x = 23; strange(x); System.out.println("2. x = " + x); // unchanged... public static void strange(int x) { x = x + 1; System.out.println("1. x = " + x); Output: 1. x = 24 2. x = 23 Reading values from a file (again) import java.util.*; class FileReader1{ while(in.hasnext( )){ // do something with the array 3

import java.util.*; Using methods 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( )) { return list; Arrays is a class which is in the Java package java.util import java.util.*; Using methods class FileReader3 { FileReader3 filereader = new FileReader3(); filereader.readfile(filename, 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 and the other doesn t? Objects and references values Object variables do not actually store an object; they store the address of an object's location in the computer's memory (references / pointers). int [] values = new int[5]; 5 7 10 6 3 int x = 1; x 1 pages 245 248 in Savitch Arrays as method parameters Arrays are objects. When passed as parameters, a reference is passed. (Changes made in the method will also be seen by the caller.) int[] list = {126, 167, 95; doubleall(list); public static void doubleall(int[] values) { for (int i = 0; i < values.length; i++) { values[i] = 2 * values[i]; Example list int[] list = {126, 167, 95; doubleall(list); public static void doubleall(int[] vlaues) { for (int i = 0; i < values.length; i++) { values[i] = 2 * values[i]; Output: [126, 167, 95] [252, 334, 190] values index 0 1 2 value 126 252 167 334 190 95 4

scope: The part of a program where a variable exists. A variable's scope is from its declaration to the end of the { braces in which it was declared. If a variable is declared in a for loop, it exists only in that loop. If a variable is declared in a method, it exists 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 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 See page 259 268 in Savitch It is legal to declare variables with the same name, as long as their scopes do not overlap: 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); 5