1 / 21

Molecular Ball Simulation

Molecular Ball Simulation. Jeff Forbes Owen Astrachan September 3, 2017. Goals for the Week. Be able to explain what a class is, what the parts of a class are, how classes are used in Java Programs Methods, Constructors, Instance Variables, Keyword: this

Anita
Download Presentation

Molecular Ball Simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Molecular Ball Simulation Jeff Forbes Owen Astrachan September 3, 2017 Compsci 201, Fall 2017, Simulation

  2. Goals for the Week • Be able to explain what a class is, what the parts of a class are, how classes are used in Java Programs • Methods, Constructors, Instance Variables, Keyword: this • Be able to explain the differences between an array and an ArrayList • Includes creating, access, update, auto-boxing/unboxing • Be able to create a Java class starting with nothing, be able to modify a Java class • Using Eclipse, but also using IDE-independent concepts • Working on the N-Body assignment • Be able to test methods and classes • Using supplied test files/cases, toward your own testing • Be able to parse data from text files Compsci 201, Fall 2017, Simulation

  3. C is for … • Class • Framework for creating objects • Computing • It’s what we do • Conventions • How should I write code in 201? • Collaboration • Review the policy • CodingBat.com • Practicing Java! Compsci 201, Fall 2017, Simulation

  4. MolecularBall Simulation • What is a class, what is an object, how to use them? • Methods, Constructors, Instance Variables, • Keyword: this • Difference between array and ArrayList • Create, access, update, auto-boxing/unboxing • Know enough to be able to succeed with N-Body Compsci 201, Fall 2017, Simulation

  5. Class: State and Behavior • Each object has IRL state • Molecule: mass, size, type of element, … • Atmosphere, mass, distance from star, … • Each object has state in simulation of motion • Coordinates in space (x,y) or (x,y,z) or (x,y,z,t) • Objects have behavior: bond, move, bounce, … Compsci 201, Fall 2017, Java Basics

  6. Writing N-Body • Fork and clone the nbody-start-17 project • Create a Planet class • Implement and test constructors and methods in the Planet class • Implement and test readRadius and readPlanets in the NBody class • Implement and verify the code that runs a planetary simulation in the NBodyclass • Answer questions in the REFLECT Compsci 201, Fall 2017, Simulation

  7. Class encapsulates state and behavior • Class is a template, object has characteristics • Dogs have fur, speed, temperament, size, … • Typically we don’t use examples like this, but they can help build intuition and understanding • Class dog, retriever extends dog, method bark() Compsci 201, Fall 2017, Simulation

  8. Classes in Java • Define class named Foo in Foo.java • Create object by calling new Foo(..) • Access object by calling methods: obj.doSomething() • Some methods return values, use them! State Constructor Methods (behavior) Compsci 201, Fall 2017, Simulation

  9. Making a class “Hey, I have some data that are too complicated for a primitive (or array of primitives).” • Decide what data you need. (nouns) • Decide what methods you need (verbs) • Make a new class: • Add your data Insidethe class; Outsidethe methods • Write a constructor. - fills in your data • Write the rest. Compsci 201, Fall 2017, Simulation

  10. Simulation of Bouncing Balls • Approximation of SuperBalls colliding • See code run: https://youtu.be/dxSMYE2Do-c • Code in Git project https://coursework.cs.duke.edu/201fall17/molecularball • Walk through BallWorld.java • Using Eclipse • Where is program launch point? • Answer questions WOTO in Compsci 201, Fall 2017, Simulation

  11. Understanding the Simulation • WOTO! http://bit.ly/201-f17-0906-1 • As you read the code, notice conventions: naming local variables, instance variables, methods, classes • Look at documentation too! • How is the class StdDraw used in simulation? • You'll use this class in NBody simulation as well

  12. Classes in Java • Name of class? • Name of file? • State? • Constructor? • Behavior? • Interaction with other classes? • Answer questions about MolecularBall class • http://bit.ly/201-f17-0906-2 Compsci 201, Fall 2017, Simulation

  13. Access Control • public: accessible anywhere • private: accessible only within methods of this class • protected: accessible to this class or subclasses • No modifier: accessible within class and package More later on these… Compsci 201, Fall 2017, Simulation

  14. Review: What is a class? • Encapsulates state and behavior • State is: instance variables, specific to each object • Behavior is methods: update state, possibly use or report on state • Class is a blueprint or template: object creation • Call new, invokes constructor, initialize object • Objects communicate via methods, parameters • Object can pass itself: this refers to self CompSci201, Fall 2017, Simulation

  15. Charles Isbell • Context matters • Machine learning researcher • Systems that interact intelligently with many other intelliggence agents • Exec. Assoc. Dean @ Georgia Tech • Rethinking education: Online Masters in Computer Science . • http://www.pbs.org/newshour/bb/online-graduate-programs-offer-degrees-significant-savings/ • For me, the differences are simple to state: Computationalistsgrok that models, languages and machines are equivalent.

  16. ArrayList • An array that does not have fixed length • No primitives! • Only Objects! ArrayList<String> list = new ArrayList<String>(); // add to ArrayList list.add("hello"); //check if element is in ArrayList boolean inList = list.contains("hello"); //get element at index five String word = list.get(5); Compsci 201, Fall 2017, Simulation

  17. Conventions & Documentation • See Resources/Advice on Sakai • Variable name guidelines • Use nouns that describe what value is being stored • Don’t reiterate the type involved • Comments • Abstraction: What does it do? • Comments for methods and classes • Implementation: How does it do it? • Inline comments as needed Compsci 201, Fall 2017, Simulation

  18. Conventions & Documentation • Classes start with capital letter and then we have: • They’re public, except nested class? • camelCaseForMethods and ForClasses • Fields & instance variables: mySize, myMap, … • Constants (public static) are ALL_CAPS • Standard identifiers • i, j, k: integer loop counters • n, len, length: integer number of elements in collection • x, y: Cartesian coordinates (integer or real) • head, current, last: references used to iterate over lists. Compsci 201, Fall 2017, Simulation

  19. Solving problems • Prof. Drew Hilton’s 7 step process Compsci 201, Fall 2017, Simulation

  20. Do it on paper! • Find the maximally occurring word • How do you count? • What data structures will you need? • Review code solutions later Compsci 201, Fall 2017, Simulation

  21. Summary • Differentiate classes, objects, values, and references • Understand context, policies, and conventions of CompSci 201 • Reflect – • What’s clear? What’s still muddy? • http://bit.ly/201-f17-reflect • To-do • Review N-Body • Submit APTs for grading by Thursday night* Compsci 201, Fall 2017, Simulation

More Related