Saturday, February 23, 2013

Blog post 2/17

Sort method:

A good way to sort a list of names is to use this method, which sorts things alphabetically. Here's a good example:

//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]

However, if you wanted to sort in descending order, you would use array.reverse() instead:

//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]


With numbers, sorting them numerically and ascending would go something like this: 

var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]


Sorting them numerically and descending would go something like this:

var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return b - a}) //Array now becomes [41, 25, 8, 71]


Say you wanted to sort the order of the items in an array randomly. Here's an example on how to do that:

var myarray=[25, 8, "George", "John"]
myarray.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled



Tuesday, February 19, 2013

Quiz Inheritance Corrections

Example 1:

Consider the following class definitions.
public class A{
private int a1;
public void methodA() {
methodB();          // Statement I
}
}
public class B extends A {
public void methodB() {
methodA();          // Statement II
a1 = 0;             // Statement III
}
}
Which of the labeled statements in the methods shown above will cause a compile-time error?


II and III
exact_answernone8514

III only
exact_answernone9197
I and II
exact_answernone8885
I and III
 
EXPLANATION:
I think a1 is declared as a private int in class A, so when class B extends A, it does not need to re-assign it to 0. I'm actually  not sure if this is the real reason, but that's what I'm going with for now. When it does this, it causes a compile-time error. methodB() will also cause this error because it is not defined.
 
exact_answernone6792
 Example 2:

true_false_question 26865425
A subclass of an abstract class must always implement every defined abstract method.
exact_answernone238
 
False

 EXPLANATION:
If the subclass is also an abstract class, it does not need to implement the abstract methods.



















 

Thursday, February 7, 2013

Abstract

Summary of Presentation:
Abstract classes are never instantiated, yet they can have constructors.
Good abstract example to go off of:

public class abstract Shape
{ private int x, y;
   public Shape(int xx, int yy)
       { x = xx;
          y = yy;
        }
  public abstract double getArea();

public class Circle extends Shape
{ private double radius;
   public Circle(int x, int y, double r)
        { super(x, y); radius = r}

  public double getArea
  { return Math.PI*r*r;}

  }

Overriding:

The word final signifies that a variable's value may never change.
Example:


public class abstract Shape
{ private int x, y;
   public Shape(int xx, int yy)
       { x = xx;
          y = yy;
        }
  public int getX()
  { return x;}
  public abstract double getArea();

public class Circle extends Shape
{ private double radius;
   public Circle(int x, int y, double r)
        { super(x, y); radius = r}
         public int getX()
         { return y;}

  public double getArea
  { return Math.PI*r*r;}

  }

This is bad because you have just overridden the getX() method. On the other hand, if you have a final before the method: public final int getX(), then you won't override the method.
If this is put into the code, the code won't compile because it can't override the method.

Sunday, February 3, 2013

Examples

Interfaces and Inheritance Example:

Interface is like a class, but you can only declare methods and variables in the interface. You cannot implement methods...

I found that this was a standard example of interfaces/inheritance and I wanted to post it here just for my own understanding. It is possible for an interface to inherit from another interface, just like classes can inherit from other classes. You specify inheritance using the extends keyword. 

public interface MySuperInterface {

    public void saiHello();

}
public interface MySubInterface extends MySuperInterface {

    public void sayGoodbye();
}

The interface MySubInterface extends the interface MySuperInterface. That means, that the MySubInterface inherits all field and methods fromMySuperInterface.


Here is an example of an interface that inherits from multiple interfaces:
public interface MySubInterface extends
    SuperInterface1, SuperInterface2 {

    public void sayItAll();
}
As when implementing multiple interfaces, there are no rules for how you handle the situation when multiple interfaces have methods with the same signature (name + parameters).