cs 161 object oriented
play

CS 161: Object Oriented and discrete math . Why is math important to - PowerPoint PPT Presentation

About this course About this course Course webpage: Like 160, 161 is a combination of programming http://www.cs.colostate.edu/~cs161/ CS 161: Object Oriented and discrete math . Why is math important to us? What does that Problem


  1. About this course About this course  Course webpage:  Like 160, 161 is a combination of programming http://www.cs.colostate.edu/~cs161/ CS 161: Object Oriented and discrete math .  Why is math important to us? What does that Problem Solving  Slides/recitations/assignments are posted on the course have to do with computer science? webpage’s schedule page.  From procedural to object oriented programming  Canvas will be used for: forums, grades Texts Components of the course About this course  iClicker quizzes  Java: an introduction to problem solving and  Lectures programming, 7 th edition. “ are you with us? ”  You pay for them, might as well use them  Tests:  Slides are posted ahead of time so you can print “ what have you learned? ” Important checkpoints!! them and take notes during class  Programming assignments:  Recitation  Discrete Mathematics and Its Applications, “ can you implement it? ” Important!! We will use the 7 th edition. Kenneth Rosen.  Help you with programming and homework same automatic submission/grading system used last semester in 160. assignments, reinforce material from lecture. The library reserve desk has many copies you an  Written assignments:  You get credit for attending and participating in check out! “do you understand the theory?” recit 1

  2. Grading CS building Professional class behavior Assignments (around 8)  We all have to have respect for each other,  Make sure you can get into the Unix lab (CSB independent of race, gender, ability Clicker quizzes 120)! Recitation (attendance + completion) If you have keycard access problems:  Laptop usage: use the back row of the class  CS students: talk to a CS accounting person (Kim Midterms (2) ) or student employee) Final  Non CS students: Key Desk at Facilities  THERE ARE NO STUPID QUESTIONS Management For the percentages see course website.  Your classmates will be grateful you asked.  Questions outside of class: use Piazza rather than emailing your instructor/TA Cheating First topic: Recap of CS160. A barebones Java class public class Point {  What is cheating? What is not?  Lecture: Overview of Java program structure private int x; // instance variable private int y;  What is gained / lost when cheating?  Recitation: Write simple programs public static void main(String[ ] args){ Point p = new Point();  What are the consequences? p.x = 1;  Programming assignment 1: Review p.y = 2; int x = 10; // local variable  When / how does it happen? } }  How can cheating be avoided? 2

  3. Reading from a file Methods Primitive variables import java.io.*; public class C2F {  When a primitive variable is assigned to another, the Issues with the code? import java.util.*; public static void main(String[ ] args){ value is copied. class FileReader1{ C2F converter = new C2F(); argument System. out .print(“100C eq.”); public static void main(String[ ] args) throws IOException{  Therefore: modifying the value of one does not affect String filename = "values.dat"; System. out .print (converter.c2f(100) + “ F”); the copy. } Scanner in = new Scanner(new File(filename)); parameter int index = 0;  Example: public double c2f(double celsius) { int[ ] list = new int [9999]; return celsius * 9 / 5 + 32; int x = 5; while(in.hasNextLine() ){ } String line = in.nextLine(); int y = x; // x = 5, y = 5 list[index] = Integer.parseInt(line); } y = 17; // x = 5, y = 17 index++; x = 8; // x = 8, y = 17 } in.close( ); // do something with the array } Methods are discussed in section 5.1 in Savitch } Primitive variables as method arguments Primitive variables as method arguments Objects and the new operator When primitive variables ( int , double ) are passed as arguments, their values are copied. Modifying the parameter inside the method will not affect  Compare What will be the output of a call to the method caller()? the variable passed in. int[ ] list = new int [9999]; public void caller() { public void caller() { int x = 23; with int x = 23; strange(x); strange(x); int index = 0; System.out.println("2. Value of x in caller = ” + x); System.out.println("2. Value of x in caller = ” + x); }  Why does one use the new operator while the } public void strange(int x) { other doesn’t? public void strange(int x) { x = x + 1; x = x + 1; System.out.println("1. Value of x in strange = ” + x ); 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 3

  4. Objects and references Example Reading values from a file (again) list import java.io.IOException; public static void main(String[] args) {  Object variables store the address of the object import java.util.Scanner; int[] list = {126, 167, 95}; value in the computer's memory. System.out.println(Arrays.toString(list)); import java.io.File; doubleAll(list); class FileReader1{ System.out.println(Arrays.toString(list)); public static void main(String[ ] args) throws IOException{ }  Example: values String filename = "values.dat"; int [] values = new int[5]; public void doubleAll(int[] values) { Scanner in = new Scanner(new File(filename)); for (int i = 0; i < values.length; i++) { int[ ] list = new int [9999]; values[i] = 2 * values[i]; values 5 7 10 6 3 Let’s improve this } int index = 0; } version using methods! while(in.hasNextLine() ){  Output: String line = in.nextLine(); [126, 167, 95] int x = 1; list[index] = Integer.parseInt(line); [252, 334, 190] index index 0 0 1 1 2 2 index++; x 1 } value value 252 126 334 167 190 95 in.close( ); // do something with the array } } Variable scope import java.io.IOException; import java.io.*; import java.util.Scanner; import java.util.*; import java.util.Arrays; class FileReader3 { class FileReader2{ public static void main(String[ ] args) throws IOException{  scope : The part of a program where a variable exists. public static void main(String[ ] args) throws IOException { String filename = "values.dat";  A variable's scope is from its declaration to the end of the { } String filename = "values.dat"; FileReader3 fileReader = new FileReader3(); braces in which it was declared. FileReader2 fileReader = new FileReader2(); int[ ] list = new int [9999]; int [ ] values = fileReader.readFile(filename) ; fileReader.readFile(filename, list);  If a variable is declared in a for loop, it exists only in that loop. System.out.println(Arrays.toString(values)); System. out.println(Arrays.toString(list));  If a variable is declared in a method, it exists only in that method. } } public int [ ] readFile(String filename) throws IOException { public void readFile(String filename, int [ ] values ) throws public void example() { Scanner in = new Scanner(new File(filename)); IOException { int x = 3; Scanner in = new Scanner(new File(filename)); int index = 0; int index = 0; for (int i = 1; i <= 10; i++) { int[ ] list = new int [9999]; while(in.hasNextLine( )) { System.out.println(“i=“ + i + “ x =“ + x); while(in.hasNextLine() ){ //read the next line and store the value in the array //read the next line and store the value in the array } } } // i no longer exists here in.close( ); in.close( ); } // x ceases to exist here } return list; } } } 4

  5. Variable scope Variable scope  It is legal to declare variables with the same name, as  It is illegal to try to use a variable outside of long as their scopes do not overlap: its scope. public static void main(String[] args) { int x = 2; for (int i = 1; i <= 5; i++) { public void example() { int y = 5; System.out.println(y); int x = 3; } for (int i = 1; i <= 10; i++) { for (int i = 3; i <= 5; i++) { System.out.println(x); int y = 2; int x = 4; // illegal } System.out.println(y); System.out.println(i); // illegal } } } Public void anotherMethod() { int i = 6; int y = 3; System.out.println(i + ", " + y); } 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend