compsc sci 201 201 wo work nbody dy arrayl ylists ts
play

Compsc sci 201 201 Wo Work, Nbody dy, , ArrayL yLists ts - PowerPoint PPT Presentation

Compsc sci 201 201 Wo Work, Nbody dy, , ArrayL yLists ts Susan Rodger January 29, 2020 1/29/2020 Compsci 201, Spring 2020 1 F is for Folder aka Directory where things are stored in Git Function Abstraction a


  1. Compsc sci 201 201 Wo Work, Nbody dy, , ArrayL yLists ts Susan Rodger January 29, 2020 1/29/2020 Compsci 201, Spring 2020 1

  2. F is for … • Folder • aka Directory – where things are stored in Git • Function • Abstraction – a method in Java 1/29/2020 Compsci 201, Spring 2020 2

  3. PFTW • Get etting ng t thing ngs done i ne in 201 • How to succeed and enjoy the effort • Mund ndan ane J e Java-is isms ms • From char to autoboxing: primitives • What is this ? • Gener neric classes es: H How Ar ArrayList st wo works • Design, create, test, measure 1/29/2020 Compsci 201, Spring 2020 3

  4. Getting Things Done in 201 • What do these d dat ata m mean f n for y you, u, f for m me, f for t the communit unity o of 286 s stud udent ents i in Compsc sci 201? 201? 1/29/2020 Compsci 201, Spring 2020 7

  5. From Last Time … Go over WOTO: Correctness Counts http://b ://bit.l .ly/20 /201s 1spring2 g20-012 124-2 1/24/2020 Compsci 201, Spring 2020 8

  6. Object class, equals method • In In Java vaDoc • Signa natur ure o e of equals als met etho hod 1/29/2020 Compsci 201, Spring 2020 9

  7. @Override .equals • Create a new P Point m met etho hod • Use annotation @Override, help with errors boolean equals(Object o) { … Must u use t e this is s signa natur ure, t to implem lement ent: • • Cast parameter appropriately • Compare instance fields 1/29/2020 Compsci 201, Spring 2020 10

  8. Point inherits Object.equals • This d s doesn sn’t w t work f k for Point o t objects ts! • Def efault ult simply uses ==, no idea about points • a.equals(b) if a and b ref eference nce t the same o e object ect • Two different (0,0) points not the same 1/29/2020 Compsci 201, Spring 2020 11

  9. Point equals fixed! • Must u use s e same s e signa natur ure • Must c st cast st O Object to t to Point 1/29/2020 Compsci 201, Spring 2020 12

  10. Contract for Equality • Reflex lexiv ive e x.equals(y) then y.equals(x) • Trans ansit itivit ivity: : x.eq(y), y.eq(z) then n x.eq(z) • Check x.equals(x) as as a a special c ecial cas ase w e with = h == • Check this.getClass() == o.getClass() • Don’t want to have an apple == orange • Cas ast O Object ct p param ameter er a and u use i e instance ance va variab iables les • See Point as example 1/29/2020 Compsci 201, Spring 2020 16

  11. Amanda Randles, Duke 2005 • ACM Grace M Murray H Hoppe pper Aw Award ( d (<= 35 yo yo) For developing HARVEY, a massively parallel circulatory simulation code capable of modeling the full human arterial system at subcellular resolution and fostering discoveries that will serve as a basis for improving the diagnosis, prevention, and treatment of human diseases. //XXX and Amanda Peters //Compsci 100: Huffman Coding //November 19, 2002 I felt like working in a pair was a really successful way to complete the program. It helped the most when it came to working out basic logic and finding errors. I found it really helpful because he often would see the basic logic to the code and I could help more with the implementation. I feel like it was a successful group and we both contributed a lot. 1/29/2020 Compsci 201, Spring 2020 17

  12. Reading Points • We'll t ll typic ically lly u use a e a Scanne nner t to read value ues • Use .hasNext(),.hasNextDouble(), … • If/while there's more to read? Call .next() • Met etho hod .next() retur urns ns a a String ing • Method .nextDouble() returns a double … • See e PointRea eader er.j .java cla lass, us useful in in NB NBod ody 1/29/2020 Compsci 201, Spring 2020 18

  13. Scanner Sources for Reading • Construct uct a a Scanne anner f from System em.in .in • Reads from keyboard/console • .hasNextX() true until end-of-file OR no X • Control-D on OS X, Control-Z on Windows • Cons nstruct a a Scanner f from a a File ile • Reads from file, ex exception c could uld hap happen • .hasNextX() true until all of file read OR no X • Each call of .nextX() returns the next X, internally the Scanner "remembers" where it last read 1/29/2020 Compsci 201, Spring 2020 19

  14. Scanner hasNext and next • Think nk a about ut s scanner nner a as a a long r reel/s l/sour urce ce of data • If .hasNext() returns true, there is something to read by Scanner cursor/reader • Calling .next() returns and and ad advances cursor • Scanner object maintains cursor internally • Source: f : file, S , String ing, , terminal inal, … , … 1/29/2020 Compsci 201, Spring 2020 20

  15. N-Body Simulation • Class Celes lestialB ialBody repres esent ents C Celes lestia ial B l Body • Planet, Sun, Asteroid • Models an object in 2D space, not 3D • Position, Velocity, Mass, Image for display • Class NB NBod ody drives es t the s simula ulation • Compute gravitational forces: physics • Time-step simulation • compute all forces, update ,display 1/29/2020 Compsci 201, Spring 2020 21

  16. Class CelestialBody • Illus lustrates s stand andard J Java i idioms • Constructors, Methods, Instance Variables • State i is private: s : six i instanc nce v e variab iable les • myXPos , … using my my convention - this object • Initialized by constructors • Methods ar are p e pub ubli lic • Include access ssors aka getters for state • No setters , cannot change myXPos other than via the update method, a mutator 1/29/2020 Compsci 201, Spring 2020 22

  17. The Object Concept • Every i instan ance ce v variab able a and ever ery non-sta tatic tic m meth thod d acces cessed ed/cal called ed a after Obj bject. t.Dot • b.getX(), b.calcForcExertedBy(other) • From w withi hin a n a class, e e.g., Celestia tialB lBody • myXPos , getX(),this.myXPos , • All are equivalent as is this.getX() • Some e prefer er a always using t this. – clear earer er? 1/29/2020 Compsci 201, Spring 2020 23

  18. NBody numbers • Floating ing p point nt i issue ues, p , proble lems, q , quand ndaries ies • When is (a + b) + c != a + (b + c) • When is a/b * c != a*c / b • Watch for this in Gradescope tests!! 1/29/2020 Compsci 201, Spring 2020 25

  19. Debugging Arithmetic • Order er o of operatio ions ns w with f h floating ing p point v values ues c can n result ult i in overflo low, u , under erflo low, m , more • Small number + Big number … 1/29/2020 Compsci 201, Spring 2020 26

  20. Debugging double Arithmetic • Integer v values ues a are n not the s same as e as D Double v le values ues • 1/0 is … whereas 1.0/0 is … 1/29/2020 Compsci 201, Spring 2020 27

  21. Completing NBody • Please r e read t the e TL;DR d docum cument nt • Test at each step, push constantly using Git • Afte ter usin ing suppli pplied T d Test… c classes, p proceed d to to simu mula lati tion • Must be able to read data file to simulate • Understand the basics, read carefully • Analysis is: comple mplete b before s submit mittin ting to to Gradesc scop ope for final s submissi ssion on 1/29/2020 Compsci 201, Spring 2020 28

  22. Now look at DNAMaxNucleotide • Retur urn n the s strand and f from s strand ands a array w with m h most occur urrence nces o of nucleo cleotid ide e nuc nuc. R . Retur urn l n longes est such s ch strand nd • Exa Exampl ple 1/29/2020 Compsci 201, Spring 2020 29

  23. Algorithm - DNAMaxNucleotide • Does es t thi his co code m e mak ake t the he alg algorithm c cle lear? • Why must count be a helper method? • Why can't max = 0 before loop? 1/29/2020 Compsci 201, Spring 2020 30

  24. Two Versions of Helper Method • Iter erating ing o over each c ch char aract acter er o of a string ng • Note that nuc is a one-character string • How does s.substring(a,b) work? 1/29/2020 Compsci 201, Spring 2020 31

  25. Critique of another implementation • Where d e does t this is s solut lutio ion c n come from? • Strings are immutable, s.replace(…) • Replace every "a" with "" (nothing) • Is this better? Different? More clever? More of a hack? … 1/29/2020 Compsci 201, Spring 2020 32

  26. WOTO http:// //bi bit.ly/2 /201spr pring20-01 0129 29-1 1/29/2020 Compsci 201, Spring 2020 33

  27. Donald Knuth • aka “The Donald” • Turing award (and others) • Author of “The Art of Computer Programming” • Arguably most important book written in Computer Science • First publication: Mad Magazine If you optimize everything you will always be unhappy. Everyday life is like programming, I guess. If you love something you can put beauty into it. https://www.youtube.com/watch?v=cK7yyjXfbc4 1/29/2020 Compsci 201, Spring 2020 34

  28. From Array to ArrayList • Have int[], String[], CelestialBody[] • Array of any type, but d doesn sn't g 't grow • Can't use .contains with array, can't print • The The java.ut utils ils.Arrays cla lass ha has s some hel help 1/29/2020 Compsci 201, Spring 2020 35

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