210 likes | 351 Views
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
E N D
Molecular Ball Simulation Jeff Forbes Owen Astrachan September 3, 2017 Compsci 201, Fall 2017, Simulation
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
Solving problems • Prof. Drew Hilton’s 7 step process Compsci 201, Fall 2017, Simulation
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
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