CS 161: Object Oriented Problem Solving

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "CS 161: Object Oriented Problem Solving"

Transcription

1 About this course CS 161: Object Orieted Problem Solvig Like 160, 161 is a combiatio of programmig ad discrete math. q Why is math importat to us? What does that have to do with computer sciece? From procedural to object orieted programmig About this course Course webpage: Texts Java: a itroductio to problem solvig ad programmig, 7 th editio. q Slides/recitatios/assigmets are posted o the course webpage s schedule page. q Cavas will be used for: forums, grades q Piazza will be our primary commuicatio tool Discrete Mathematics ad Its Applicatios, 7 th editio. Keeth Rose. The library reserve desk has may copies you a check out! 1

2 Compoets of the course q iclicker quizzes: are you with us? q Tests: what have you leared? Importat checkpoits!! q Programmig assigmets: ca you implemet it? Importat!! We will use the same automatic submissio/gradig system used last semester i 160. q Writte assigmets: do you uderstad the theory? About this course Lectures q You pay for them, might as well use them q Slides are posted ahead of time so you ca prit them ad take otes durig class Recitatio q Help you with programmig ad homework assigmets, reiforce material from lecture. q You get credit for attedig ad participatig i recit Gradig Assigmets (aroud 8) Clicker quizzes Recitatio (attedace + completio) Midterms (2) the first is a programmig midterm (i recit) Fial For the percetages see course website. CS buildig Make sure you ca get ito the Uix lab (CSB 120)! If you have keycard access problems: q CS studets: talk to a CS accoutig perso (Kim or studet employee) q No CS studets: Key Desk at Facilities Maagemet 2

3 Professioal class behavior We all have to have respect for each other, idepedet of race, geder, ability Laptop usage: use the back row of the class THERE ARE NO STUPID QUESTIONS q Your classmates will be grateful you asked. q Questios outside of class: use Piazza rather tha ig your istructor/ta Cheatig What is cheatig? What is ot? What is gaied / lost whe cheatig? What are the cosequeces? Whe / how does it happe? How ca cheatig be avoided? First topic: Recap of CS160. Lecture: Overview of Java program structure Recitatio: Write simple programs Programmig assigmet 1: aalyze twitter data A bareboes Java class public class Poit { private it x; private it y; // istace variable public static void mai(strig[ ] args){ Poit p = ew Poit(); p.x = 1; p.y = 2; it x = 10; // local variable 3

4 Readig from a file import java.io.*; import java.util.*; Issues with the code? class FileReader1{ public static void mai(strig[ ] args) throws IOExceptio{ Strig fileame = "values.dat"; Scaer i = ew Scaer(ew File(fileame)); it idex = 0; it[ ] list = ew it [9999]; while(i.hasnextlie() ){ Strig lie = i.extlie(); list[idex] = Iteger.parseIt(lie); idex++; i.close( ); // do somethig with the array Methods public class C2F { public static void mai(strig[ ] args){ C2F coverter = ew C2F(); System.out.prit( 100C eq. ); argumet System.out.prit(coverter.c2f(100) + F ); parameter public double c2f(double celsius) { retur celsius * 9 / ; Methods are discussed i sectio 5.1 i Savitch Primitive variables Whe a primitive variable is assiged to aother, the value is copied. Therefore: modifyig the value of oe does ot affect the copy. Example: it x = 5; it y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17 Primitive variables as method argumets What will be the output of a call to the method caller()? public void caller() { it x = 23; strage(x); System.out.pritl("2. Value of x i caller = + x); public void strage(it x) { x = x + 1; System.out.pritl("1. Value of x i strage = + x); 4

5 Primitive variables as method argumets Whe primitive variables (it, double) are passed as argumets, their values are copied. Modifyig the parameter iside the method will ot affect the variable passed i. public void caller() { it x = 23; strage(x); System.out.pritl("2. Value of x i caller = + x); public void strage(it x) { x = x + 1; System.out.pritl("1. Value of x i strage = + x); Output: 1. Value of x i strage = Value of x i caller = 23 Objects ad the ew operator Compare it[ ] list = ew it [9999]; with it idex = 0; Why does oe use the ew operator while the other does t? Objects ad refereces Object variables store the address of the object value i the computer's memory. Example: it [] values = ew it[5]; values it x = 1; x Example public static void mai(strig[] args) { list it[] list = {126, 167, 95; System.out.pritl(Arrays.toStrig(list)); doubleall(list); System.out.pritl(Arrays.toStrig(list)); values public void doubleall(it[] values) { for (it i = 0; i < values.legth; i++) { values[i] = 2 * values[i]; q Output: [126, 167, 95] [252, 334, 190] idex value

6 Readig values from a file (agai) import java.io.ioexceptio; import java.util.scaer; import java.io.file; class FileReader1{ public static void mai(strig[ ] args) throws IOExceptio{ Strig fileame = "values.dat"; Scaer i = ew Scaer(ew File(fileame)); it[ ] list = ew it [9999]; it idex = 0; while(i.hasnextlie() ){ Strig lie = i.extlie(); list[idex] = Iteger.parseIt(lie); idex++; i.close( ); // do somethig with the array Let s improve this versio usig methods! import java.io.ioexceptio; import java.util.scaer; import java.util.arrays; class FileReader2{ public static void mai(strig[ ] args) throws IOExceptio { Strig fileame = "values.dat"; FileReader2 filereader = ew FileReader2(); it [ ] values = filereader.readfile(fileame); System.out.pritl(Arrays.toStrig(values)); public it [ ] readfile(strig fileame) throws IOExceptio { Scaer i = ew Scaer(ew File(fileame)); it idex = 0; it[ ] list = ew it [9999]; while(i.hasnextlie() ){ //read the ext lie ad store the value i the array i.close( ); retur list; import java.io.*; import java.util.*; class FileReader3 { public static void mai(strig[ ] args) throws IOExceptio{ Strig fileame = "values.dat"; FileReader3 filereader = ew FileReader3(); it[ ] list = ew it [9999]; filereader.readfile(fileame, list); System.out.pritl(Arrays.toStrig(list)); public void readfile(strig fileame, it [ ] values) throws IOExceptio { Scaer i = ew Scaer(ew File(fileame)); it idex = 0; while(i.hasnextlie( )) { //read the ext lie ad store the value i the array i.close( ); Variable scope scope: The part of a program where a variable exists. q A variable's scope is from its declaratio to the ed of the { braces i which it was declared. q If a variable is declared i a for loop, it exists oly i that loop. q If a variable is declared i a method, it exists oly i that method. public void example() { it x = 3; for (it i = 1; i <= 10; i++) { System.out.pritl( i= + i + x= + x); // i o loger exists here // x ceases to exist here 6

7 Variable scope It is illegal to try to use a variable outside of its scope. public void example() { it x = 3; for (it i = 1; i <= 10; i++) { System.out.pritl(x); System.out.pritl(i); // illegal Variable scope It is legal to declare variables with the same ame, as log as their scopes do ot overlap: public static void mai(strig[] args) { it x = 2; for (it i = 1; i <= 5; i++) { it y = 5; System.out.pritl(y); for (it i = 3; i <= 5; i++) { it y = 2; it x = 4; // illegal System.out.pritl(y); Public void aothermethod() { it i = 6; it y = 3; System.out.pritl(i + ", " + y); 7