compsci 201
play

Compsci 201 Add yourself to compsci@duke.edu Classes, Arrays, APIs - PowerPoint PPT Presentation

Be in the know Compsci 201 Add yourself to compsci@duke.edu Classes, Arrays, APIs Duke University mailing lists add yourself https://lists.duke.edu/sympa State Compsci related events, jobs, research opportunities


  1. Be in the know … Compsci 201 • Add yourself to compsci@duke.edu Classes, Arrays, APIs • Duke University mailing lists – add yourself https://lists.duke.edu/sympa State • Compsci related events, jobs, research opportunities Constructor • Apply for Data+, CS+, Code+ Susan Rodger • summer research at Duke, paid, hire lots of 1 st January 17, 2020 Methods year students, 2cd year, etc. (behavior) • Apply in January! https://www.cs.duke.edu/undergrad/summer_research 1/17/2020 Compsci 201, Spring 2020 1 1/17/2020 Compsci 201, Spring 2020 2 C is for … Plan for the Day • Class • Review Object concept: classes, P0 • Framework for creating objects • What is a class, object, instance variable • Collections and Collection • See java.util.* for details • Review arrays in Java: methods and concepts • Required for APTs due next week • Move toward ArrayList and other collections • Collaboration • Coding and helper functions • Review the policy • Efficient programming and not efficient programs 1/17/2020 Compsci 201, Spring 2020 3 1/17/2020 Compsci 201, Spring 2020 4

  2. Strings: Example from last time Class • You can’t modify a string, always create new String • Adjective, noun, close to a verb • Show some ___ you're in a great ___, that's a String s = new String(“joy"); ___ act, let's ___-ify that String t = s; • Fundamental part of object-oriented programming • All Java code is in a class, alas the primitives • In Python int is a class, has no upper bound s = t + t; • In Java int is a primitive, 2 31 -1 maximal value // contents of boxes the same 1/15/2020 Compsci 201, Spring 2020 5 1/17/2020 Compsci 201, Spring 2020 9 Class and Object Class encapsulates state and behavior • Class is a template, object has characteristics • If we had a Retriever class we could instantiate an instance of the class, i.e., create an object • Dogs have fur, speed, temperament, size, … Retriever ks = new Retriever("kelsey"); • Typically we don’t use examples like this, but they • Class is an object factory , calling new creates a can help build intuition and understanding new object that is an instance of the class • Class dog, retriever extends dog, method bark() • We could call a method: ks.bark() 1/17/2020 Compsci 201, Spring 2020 10 1/17/2020 Compsci 201, Spring 2020 11

  3. Classes in Java Classes in Java • Define class Foo in • State: instance variables: private Foo.java State • Create object by calling • Constructors: initialize Constructor instance variables new Foo(..) • Access object by calling • Methods: functions Methods methods: aka behavior (behavior) obj.doSomething() • Documentation: • Some methods return a Javadoc and other value, use it! comments 1/17/2020 Compsci 201, Spring 2020 12 1/17/2020 Compsci 201, Spring 2020 13 Work-Flow for Assignments Using a shell • What is the work-flow for P0 and Assignments? • Place to type shell commands • Login to gitlab • On Mac use Terminal, Windows use Bash Git • What is this? • Code URL to P0 in gitlab • Fork it (makes a copy in the cloud) • It’s a path: • Folder named IdeaProjects • Clone with ssh • Folder named spring20 – May have other folders inside it • $ is a prompt, ready for a shell command 1/17/2020 Compsci 201, Spring 2020 15 1/17/2020 Compsci 201, Spring 2020 16

  4. Back to Work-Flow for Assignments A few shell commands • Clone with ssh h • pwd – display current path • cd – change into main folder/directory • Go to your shell • cd name -- change into folder named name • cd (to folder you want to put your P0 in) • cd .. – change back into parent folder • git clone (SSH URL you copied) • ls -- show files in current folder • ls (will show your files) • Using IntelliJ complete the assignment • Let’s see some of those…. • Save code often to gitlab! 1/17/2020 Compsci 201, Spring 2020 18 1/17/2020 Compsci 201, Spring 2020 19 Classes and P0 Work-Flow for Assignments (cont) • How many Person objects created? • Send code back to gitlab (DO OFTEN) • Each has a name and an age, different for each • cd ( into project folder) instance. Thus: instance variables • git add . • Puts code in staging area, tells git you want to update • git commit –m “comment on what you did” • Changes are recorded in local repo • git push • Uploads the changes to your gitlab repo • Now to Gradescope and submit project • Don’t like results - fix code, push code, run on Gradescope again 1/17/2020 Compsci 201, Spring 2020 20 1/17/2020 Compsci 201, Spring 2020 22

  5. Classes and P0 • How many Person objects created? • Each has a name and an age, different for each instance. Thus: instance variables • To create? Call new which invokes a constructor • No return type, initialize instance variables • Access levels: private only within class, public from other classes • Technically there is a package access, we ignore 1/17/2020 Compsci 201, Spring 2020 23 1/17/2020 Compsci 201, Spring 2020 24 What is this ? Constructor • Same name as class • An object instance refers to itself • No return type • Method or constructor: object references itself • Every reference to an instance variable myVar could be written as this.myVar • Overload with different parameters • Each should initialize all instance variables • Code for an object to pass itself: • callMethod(this,"hello"); • Factor out common code into helper method if lengthy • Constructor can call other constructor • Can call another constructor using this(…) • this("hello"); 1/17/2020 Compsci 201, Spring 2020 25 1/17/2020 Compsci 201, Spring 2020 26

  6. Running a Java Program WOTO (3 minutes) • On laptop/desktop launch/run point is the main method in any class http://bit.ly/201spring20-0117-1 • Driver programs in P0, runs/drives the code • Method signature required to run program 1/17/2020 Compsci 201, Spring 2020 27 1/17/2020 Compsci 201, Spring 2020 28 Luis von Ahn Arrays, APTs, and APIs Duke 2000, Math • Duke Honorary Degree 2017 • • Why is alliteration important in writing? CEO Duolingo • • Why are these important in programming? Macarthur Award, 2006 • • APIs create possibilities • MIT-Lemelson Prize, 2018 “It’s amazing how motivating it is to sit with somebody and say, ‘What you’re doing is really important.’ I use that a lot.” 1/17/2020 Compsci 201, Spring 2020 29 1/17/2020 Compsci 201, Spring 2020 30

  7. Array Details Indexing for loops and arrays • Constructing and initializing … • Once array created, it's size is fixed, can't grow! int[] a = new int[100]; • Indexable elements can be changed for(int k=0; k < a.length; k += 1){ a[k] = 99; • Using a[k] we can read/write values } • Instance variable a.length is size of array • No parentheses, hence not a method • Let an API-call fill in array: java.util.Arrays • Notice dot notation: object dot name • Arrays.fill(a,99); • https://docs.oracle.com/en/java/javase/11/docs/api/ java.base/java/util/Arrays.html 1/17/2020 Compsci 201, Spring 2020 31 Compsci 201, Spring 2020 32 1/17/2020 For each loops and arrays For Loop Summary • For each loop: no index, no changing what’s stored • for(init; boolean guard; update) {…} int[] a = {1,2,3,4,5,6,7,8,9,10}; • for(int k=0; k < a.length; k+=1) {…} int sum1 = 0; int sum2 = 0; • Initialization happens once, before guard for(int k=0; k < a.length; k += 1){ checked for the first time, never again sum1 += a[k]; • Initialization can introduce variables: loop scope } for(int value : a){ • Guard checked, if true loop body executes sum2 += value; } • After loop body, update executes, guard System.out.println(sum1 == sum2); checked 1/17/2020 Compsci 201, Spring 2020 33 1/17/2020 Compsci 201, Spring 2020 34

  8. From Control to APIs Control Construct Summary • List and ArrayList similar to array, but …. • Grow as needed, can't use [k] to access • if (boolean) {…} • Powerful APIs, e.g., as follows • Block executed when guard is true • {.} not needed for single statement, use anyway • if (boolean) {…} else {…} • Code in else block when negation true • while(boolean) {…} • Check boolean guard, execute body, repeat • Guard checked again after body executed 1/17/2020 Compsci 201, Spring 2020 35 1/17/2020 Compsci 201, Spring 2020 36 Solving an APT Together Think Before You Code • Totality (see APT page on course site) • Solve by hand … Check your understanding of examples … think about solution you’ll write ... http://www.cs.duke.edu/csed/newapt/totality.html • Then think before fingers on keys • Solve by hand: a = {20,30,40,50,60} stype=“odd” • Use what you know, but implement in Java • Check ideas using jshell (Java 9 and later) • Command line is your friend! 1/17/2020 Compsci 201, Spring 2020 38 1/17/2020 Compsci 201, Spring 2020 39

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