Sunday, May 22, 2016

Upcasting and Downcasting


           Upcasting and downcasting allows you to change the reference variable type for an object. The type of the reference variable determines what methods you can call, but it’s the type of the object that the variable refers to which determines the methods that will be called. Unlike downcasting, upcasting is guaranteed by polymorphism.

            Similar to casting numbers, when down casting you need to specify what you’re casting to in parentheses before the variable. The variable your downcasting to must refer to the object type that your downcasting to. When your upcasting or downcasting you can't change the object only the reference variable.

Casting Numbers


In java you can cast from type to type as long as there in the same class. For example you can cast a floating number to a double, but not an integer to a string. Some types can only hold a certain amount of memory. So when converting between types you must be careful that the type you’re casting to can hold that value. If you do convert a number that doesn’t fit in that type, it may cut off the extra spaces. For example if you cast a floating number to an integer it will cut of the decimals. (It won’t round the number. Math.round does that for you.) When casting a number you must set a variable equal to the variable you want to cast. In parenthesis before the casted variable you must put the type you want to convert to.

Below shows code where I did many different casts. At the bottom I tried to cast a value of 128 to a byte. The problem is that a byte only holds up to 127, so what you get as an output is byte’s minimum, -127.

public class App {

      public static void main(String[] args){
            byte byteValue = 20;
            short shortValue = 55;
            int intValue = 808;
            long longValue = 3332322;
            float floatValue = 8834.8f;
            double doubleValue = 32.4;      
            
            System.out.println(Byte.MAX_VALUE);
            intValue = (int)longValue;
            System.out.println(intValue);
            doubleValue = intValue;
            System.out.println(doubleValue);       
            intValue = (int)floatValue;
            System.out.println(intValue);

            //ByteMax is 127;
            // the following won't work as we expect it to .
            //128 is to big for a byte.
            byteValue = (byte)128;
            System.out.println(byteValue);
          
      }
}

Wednesday, May 18, 2016

Encapsulation


Today I watched a tutorial on encapsulation. This is an extension of the public private and protected tutorial. Explaining why we declare instance variables, and methods private within a class. You want to declare a method private when you are only going to use it within the class. In general try not to make instance variables public unless they are constants. One reason we due this is to reduce cross linkages, you don’t want one class getting entangled in another class. Another thing the tutorial talked about is API documents. This is just the documents of built in classes for java. For example String is a built in class, meaning you don’t have to make a String class to create a String object in java. The API document gives you information on everything about the class, interfaces, instance variables, methods, constructers, etc. You will notice that in the built in classes there aren’t many instance variables that are public, except constants. The point of encapsulation is to define things private as much as you can unless, you need to use them outside that class.

Tuesday, May 17, 2016

Planets and App Logo

Today I spent time working on different skins for planets. Since you can't just copy images from online, because of legal reasons, I used Photoshop to make my own planets. One problem with our orbit simulator is that it runs slow. So we are looking for ways to increase the operation speed. One idea we thought of, was getting rid of friction. What we wanted to do was make planets that are symmetric so the rotation due to friction wouldn't matter. So however I spin a planet it will look exactly the same.

When making the app logo, I wanted to make sure the color scheme looked appealing. This will be the first thing the user sees, so it must look good. I experimented with different colors to try and get the most visual appealing one. In the process I asked friends which colors they liked the best, knowing that I am not the target audience. Below is the logo I made in Photoshop.


Monday, May 16, 2016

Polymorphism


Polymorphism essential means that if you have a child class of a parent class you can use the child class anywhere you would use the parent class.  For example you can declare variable with type of parent class to objects of child class. Then when you call a method to that variable it will call the method in the child class. When you run the method the type of the variable doesn’t matter. What matters is the type object containing the variables that’s going to run. So if you make a variable with a parent class type, but it refers to a child class object. It’s going to run the code in the child class.

Below shows many examples of polymorphism. In the main method I made a tree object and a plant object. Tree is a child class of plant. So we can declare a variable type plant to a tree object. We can now call a method that is in both the plant and tree class. When the method is called it will run the code in the tree class. If you try and call a method not in the plant class using a tree object it won’t work. This is shown in the comment below where shedLeaves is a method only in the tree class.



public class App {
     
      public static void main(String[] args) {

            Plant plant1 = new Plant();
            Tree tree = new Tree();         
            Plant plant2 = tree;
         
            plant2.grow();          
            tree.shedLeaves();
        
            //plant2.shedLeaves();
          
            doGrow(tree);
  
      }
    
      public static void doGrow(Plant plant){
            plant.grow();
      }

}


Sunday, May 15, 2016

Private, Public, Protected


Normally you don’t want to make instance variables public, and access them through methods. An exception to this rule would be when you want to make a constant variable. When you make an instance variable private you can only access it inside the class. This means if I make a child class I cannot use that private variable in that class. If for some reason you want a variable to be accessible in a child class and package, but nowhere else, you can make it protected. The last option is having no access modifier. This makes it so you can access that variable only in the same package.

Interface


Unlike classes when making a method in an interface, you don’t define a body. In fact you don’t even put code in an interface method. When implementing an interface into a class you write (implements interface_name), after the header of the class. This will force you to have all the methods that the interface has. In eclipse the body of the methods will automatically be added to the class. You then can add code to the method in your class telling it what to do. If you want to implement multiple interfaces, separate the names with a coma.
You can use that interface type to create new objects of classes that have that interface implemented. When you make a new object with a type interface, you no longer have access to all the methods in that class, you only have access to the interface methods. When you call a method with a parameter type interface, you must pass an object into a method that implements that specific interface.
Here’s an analogy that may help you understand interfaces better. Say you were making tons of different types of bikes. Each type of bike has two wheels a frame, and handle bars. An interface would allow you to make methods for each of those parts. Then when you make a class for a new type of bike you can implement that interface. This will add all the methods (wheels, frame, and handle bars) into the class. This will make sure your bike has all those parts.