Compsci 201 Java and Simulation Owen Astrachan ola@cs.duke.edu September 7, 2018 9/7/18 Compsci 201, Fall 2018, Java and Simulation 1 Reminder about class links • Compsci/Internet: http://bit.ly/201fall18 • Sakai: https://sakai.duke.edu/portal/site/201fall18 9/7/18 Compsci 201, Fall 2018, Java and Simulation 2 D is for … • Debugging • A key skill in making your programs run • Digital • All about the 1s and 0s 9/1/17 Compsci 201, Fall 2017, Java Basics 3 PFF • How to succeed in finishing and submitting APTs, and Assignment P0 • Java Knowledge • Git, Submission knowledge • Applying these concepts to P1: Nbody simulation • Solving a problem that requires computational simulation, not only mathematical analysis 9/7/18 Compsci 201, Fall 2018, Java and Simulation 4 Assignment P0 and P1 • Simple Java class with driver program • How is a program run? What is main? • See documentation and create methods • Example of 201 work-flow • GitHub (and classroom) • Using SSH and GitHub together • Submitting via Gradescope 9/7/18 Compsci 201, Fall 2018, Java and Simulation 5 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() 9/7/18 Compsci 201, Fall 2018, Java and Simulation 6 Classes in Java • Define class Foo in Foo.java • Create object by calling new Foo(..) • Access object by calling methods: obj.doSomething() • Some methods return values, use them! 9/7/18 Compsci 201, Fall 2018, Java and Simulation 7 State Constructor Methods (behavior) Classes in Java • State: instance variables: private • Constructors: initialize instance variables and other state • Methods: functions aka behavior • Documentation: Javadoc and other comments 9/7/18 Compsci 201, Fall 2018, Java and Simulation 8 Overloaded Methods/Constructors 9/7/18 Compsci 201, Fall 2018, Java and Simulation 9 • Same name, different parameters • Constructor has no return type: class name • Methods must have return type, can be void • Which one called? • Parameters determine • Can one call other? • Yes: parameters Constructor • Same name as class • No return type • Overload with different parameters • Each should initialize all instance variables • Factor out common code into helper method if lengthy • Can call another constructor using this(…) 9/7/18 Compsci 201, Fall 2018, Java and Simulation 10 What is this? • An object instance refers to itself • Method or constructor: object references itself • Every reference to an instance variable myVar could be written as this.myVar • Code for an object to pass itself: • callMethod(this,”hello”); • Constructor can call other constructor • this(“hello”); 9/7/18 Compsci 201, Fall 2018, Java and Simulation 11 Project P1 • See course website for details • Not quite this, but … • Read assignment before starting to code • Think before fingers on keys • Use Piazza and Helper Hours 9/7/18 Compsci 201, Fall 2018, Java and Simulation 12 What is a static method? • Objects are instances of a class • Thus objects have instance variables • Typically private, accessed in methods • Static method belongs to class, not object • No instance variables • Accesses static variables and methods • More on this later, for now? … 9/7/18 Compsci 201, Fall 2018, Java and Simulation 13 Projects in 201: Briefly • Start with GitHub classroom, fork repository • Now you have starter code and a place to store your work. • Clone repo to your machine: you need Git for this • You use GitHub. Only ola uses GitHub classroom • You manage your code/projects on GitHub • Make changes, complete project • Push changes frequently using Git (what?) 9/7/18 Compsci 201, Fall 2018, Java and Simulation 14 What is Git? • Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. https://git-scm.com/ • In teams, huge win. Individually, huge win • Git is complicated when it doesn’t work, lots of commands • Git relies on SSH to be secure 9/7/18 Compsci 201, Fall 2018, Java and Simulation 15 Submission and Grading • After completing assignment (or before) • Push code using Git • Submit code on Gradescope. Wait a minute • Connect to GitHub repository, grade • Can always upload Zip file • Submit before deadline! • Autograder runs and tells you what happened! • Don’t submit until you think code works! 9/7/18 Compsci 201, Fall 2018, Java and Simulation 16 Working on P0 (Redux) and P1 • Make sure you can start and finish before coding • What does that mean? 9/7/18 Compsci 201, Fall 2018, Java and Simulation 17 WOTO (redux) http://bit.ly/201fall18-sept5-2 9/7/18 Compsci 201, Fall 2018, Java and Simulation 18 Charles Isbell 9/7/18 Compsci 201, Fall 2018, Java and Simulation 19 http://www.pbs.org/newshour/bb/online-graduate-programs-offer- degrees-significant-savings/ For me, the differences are simple to state: Computationalists grok that models, languages and machines are equivalent. • Context matters: Threads • Machine learning researcher • Systems that interact intelligently with many other intelligence agents • Exec. Assoc. Dean @ Georgia Tech • Rethinking education: Online Masters in Computer Science N-Body Simulation • Class Body represents Celestial Body • Planet, Sun, Asteroid • Models an object in 2D space, not 3D • Position, Velocity, Mass, Image for display • Class NBody drives the simulation • Compute gravitational forces: physics • Time-step simulation: compute all forces, update ,display 9/7/18 Compsci 201, Fall 2018, Java and Simulation 20 Body.java and class Body • Illustrates stand Java idioms • Constructors, Methods, Instance Variables • State is private: six instance variables • myXPos, … using my convention - this object • Initialized by constructors • Methods are public • Include accessors aka getters for state • No setters, cannot change myXPos other than via the update method, a mutator 9/7/18 Compsci 201, Fall 2018, Java and Simulation 21 NBody Project and Body.java • No class provided, but client code uses Body • Eclipse and Java don’t like this • Instance variables, constructors, getter methods • A Body has x, y, xv, yv, mass, image file • Some of these are used by client code • The image file only used by Body.draw() • Getter methods getX(), getXVel(), etc. 9/7/18 Compsci 201, Fall 2018, Java and Simulation 22 WOTO http://bit.ly/201fall18-sept5-2 9/7/18 Compsci 201, Fall 2018, Java and Simulation 23