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