Tuesday, May 10, 2016

Inheritance


Inheritance is when you make an extension of a class. This extension is called the child class of the parent. You can extend a class, by writing extends (parent_class_name). When you make a child class that gives the class access to all the methods in the parent class. Inheritance is used when you want to add new methods to the parent class. So the child class is the parent class plus a little more. Some classes can’t be extended, like the String class which is a final class.



Another thing you can do in a child class, is override methods in the parent class. When overriding a method, the header of the method has to be the same. You can then change what the method does in the child class. So whenever you call that method in the main class, to a child class object it does the override method. If you put @Override before overriding a method the program will make sure that the method is overriding a method in the parent class. So it will make sure you don’t spell the parent method wrong, and just end up creating a totally new class.



Sometimes you won’t be able to access every part of a parent class. For example if you declare an instance variable private it is only accessible in that class. So you won’t be able to use that variable in the child class. You can get rid of private which allows access to that variable with in the package of the class. So if the child class is within the package of the parent class it has access to that variable. Lastly if you make an instance variable protected, you can access the variable anywhere in the package of the class, and in the child classes.


Sunday, May 8, 2016

Static and Final


Static member variables or class variables belong to the class. Unlike instance variable values, each object does not get its own unique value. A class variable belong to the class and sense there is only one class there is only one copy. When writing a class variable you must add the key word static. Later when accessing that class variable, you must use the (class_name. variable_name.)

A static method also uses the key word (static method_name.) You can access a static method by using the (class_name.method_name.) Static methods can access static variables in the class, but they can’t access instance variables.

A static method can be used if you have a method that doesn’t deal with instance data of the class. Another typical use of static method is when you access a constants. We can make or own constant variable by using the key words final and static. When making a constant variable you must set its value in the class, and it will remain constant. This mean you cannot reassign that variable later on. Below shows examples of static variables, static methods, and constants.



class Thing {

      public String name;

      public static String description;

      public final static int LUCKY_NUMBER = 7;

     

      public static int count = 0;

     

      public int id;

     

      public Thing(){

           

            id = count;

            count++;

      }

     

      public void showName(){

            System.out.println("object id; " + id + description + ":" + name);

      }

     

      public static void showInfo(){

            System.out.println(description);

           

      }

}



public class App {

      public static void main(String[] args) {

           

            Thing.description = "I am a thing";

           

            Thing.showInfo();

           

            System.out.println("Before creating objects, count is: " + Thing.count);

           

            Thing thing1 = new Thing();

            Thing thing2 = new Thing();

           

            System.out.println("After creating objects, count is: " + Thing.count);

           

            thing1.name = "bob";

            thing2.name = "sue";

           

           

            thing1.showName();

            thing2.showName();

           

            System.out.println(Math.PI);

           

            System.out.println(Thing.LUCKY_NUMBER);

        }



}


Saturday, May 7, 2016

Constructors


A constructor is a special method that runs every time you create an instance of a class. Constructors unlike regular methods do not need a return type. The name of the constructor always has to be the same of the class you make it in. When you make an instance of a class it will run the constructor. A common use for a constructor is initialization of instance variables. For example if you have a class called dog, and an instance variable “name”. So you want to make objects of that class, where all the dogs’ names are the same. A constructor allows you to make each object have that name, without declaring each name individually to each class. All you have to do is make a new object with the constructor that declares the name. Another useful thing you can do with constructors is call other constructors. The way you call or access a constructor is by using the key word “this()”, where you put your parameters for the constructor your calling in the parenthesis.

Week6. Progress

Progress


This week we mainly focused on graphics and button functionality. We succeed to put graphic on the buttons, and created a function to adjust scales. 

To put images on the buttons, we first had to pack all the icons we want to use into .pack file. 
The icons should be .png file to pack. After pack all the images, there will be two outputs. One is packed png file and the other one is .pack file. To use the images in android studio, those two should go to same file under asset file. We put those two files in asset file and call the pack file from the script. And then call the name of the icon by using getDrawable. This is the picture of menu button.


Future Plans

We are going to keep working on out graphics and button functionality. For now we only worked out with one icon but at the end, each button will have its own icon. Also we are going to keep working on right scales. 

Wednesday, May 4, 2016

Method Parameter


 A method parameter changes how the method works, by making a variable you can pass into it. You can make a parameter in the main method and define the variable when declaring it to a body. A key aspect to remember when defining a variable, is to make sure it is the same type as the parameter in the method. When declaring two variables in a parameter use a comma. When referring to the method put your variables in the same order as declared. When you define the variable for the parameter you can either put it right into the open parenthesis, or you can specify the variable before, and then put that variable into the open parenthesis. Below is code showing three different methods all using parameters to assign data to a body.



            class Robot {

      public void speak(String text) {

            System.out.println(text);

           

      }

      public void jump(int height){

            System.out.println("jumping: " + height);

      }

      public void move(String direction, double distance){

            System.out.println("moving " + distance + "metres in direction" +direction);

      }

}





public class App {



      public static void main(String[] args){

            Robot sam = new Robot();

           

            sam.speak("Hi I'm Sam.");

            sam.jump(7);

            sam.move("west", 12.2);

            String greeting = "Hello there. ";

            sam.speak(greeting);

           

            int value = 14;

                        sam.jump(value);

           

      }

}


Monday, May 2, 2016

Week 5 Update

Progress:
This week we made a lot of progress toward the user interface of the app. We added some functionality to the buttons and created some visual queues for body selection and launching.

All of the button functionality improvements were already talked about in detail in previous posts this week. Besides attaching some function calls to a few of the buttons (such as adding bodies), we also implemented a slider for adjusting mass.

The next major update was the adding of a visual queue for selection. The selected body gets highlighted. This was done by extending our DynamicSprite class to create a BodyHighlight class. BodyHighlight overrides the update() method so that it can update the location of the highlight sprite to lay on top of the DynamicSprite (which lays over the body). The highlight sprite is basically just a semi-transparent yellow circle with dimensions slightly larger than the body it’s covering. This creates the following effect:

Here, the small body in the bottom of the screen is selected.

The complete code can be found here:


The last major improvement we added was a simulator to predict how a body will be launched. This uses Euler’s method to take into account the forces from all of the other bodies given a specific launch velocity. With every step (TIME_STEP), the position is updated based on the previous step’s velocity and the velocity is updated based on the forces acting on the body in its new (simulated) location. The code responsible can be found here (look at the doSimulation() method:



The dots show a simulation of the launch before it happens. 

Future Plans
We’ve mentioned this already but we really need to start focusing on scale and units. We are working on optimizing the scale of the numbers (distance, mass, gravitational constant, etc) so that users can experiment and see realistic results. If we didn’t do this, all of the bodies would be incredibly far away from each other. If you wanted to see multiple planets at the same time, they wouldn’t even be a pixel in diameter. It would also literally take years for a model of the earth to orbit the sun. We also want to add informational menus that will pop up where users can see numbers for velocities, distances, and masses. If we’re going to do this, we need to decide where in our code we want to convert units so that the user is presented with the realistic values. It doesn’t matter if the simulation uses drastically different numbers than the user actually sees as long as the user sees realistic numbers. The main reason we have to do these conversions is because libGDX and box2D have a speed limit of moving bodies 2 units per frame. We just need to decide what these units should be so that it works out the way we need it to.

Moving forward, we also need to finish up the menu and optimize everything for mobile devices. These things are all obviously easier said than done and it’s going to be a lot more work than it looks like on paper. However, we are definitely approaching our deadline and we need to start wrapping things up. I think we’re in good shape.

Sunday, May 1, 2016

Getter Methods


When writing a method you must define what it is beforehand. If you define a method as Void it won’t return anything. In order to get a method to return a value you must define the type. The second thing you must do is type return then that variable, at the end of the method. Whenever you call that method to an object it calls that return value. This allows you to use that value in your main method.

A nice way to get instance variables from classes, is to make a getter method that returns that variable. Getter methods are important when you start to make instance variables private. Which means you won’t be able to use that instance variable in your main method. So you can make a getter method for that variable then use it wherever you want. Below is an example when I used getter methods to assign variables age and name.

class Person {

      String name;

      int age;

     

      void speak() {

            System.out.println("My name is:" + name);

      }

      int calculateYearsToRetirement(){

            int yearsLeft = 65 - age;

            return yearsLeft;

      }

      int getAge() {

            return age;

      }

      String getName(){

            return name;

      }

}



public class App {



      public static void main(String[] args){

            Person person1 = new Person();

            person1.name = "joe";

            person1.age = 25;

   

            person1.speak();

           

            int years = person1.calculateYearsToRetirement();

            System.out.println("years till retirements " + years);

            int age = person1.getAge();

            String name = person1.getName();

           

            System.out.println("Name is: " + name);

            System.out.println("Age is: " + age);

      }    

}