2016-11-10 Abstraction! HW!

posted Nov 10, 2016, 6:51 AM by Samuel Konstantinovich   [ updated Nov 10, 2016, 5:21 PM ]

Goal: Abstraction!

Abstract class means it is not complete. It needs another class to complete it and/or its functionalities. You need to extend the abstract class. It will be useful with Certain class eg. Fruit all fruits have the same property like color. But you can have different properties for different fruits eg. number of segments for an orange, would not apply to many other fruit.


Abstract classes cannot be instantiated directly.

Declaring a class as abstract means that you do not want it to be instantiated and that the class can only be inherited. You are imposing a rule in your code that must be followed. This reasoning is similar to why we specify functions and fields as public, private or protected. If you declare a function or member as private you are in effect protecting it from improper access from client code. Privates are meant to be used within the class and that's it. Abstract classes are meant to be inherited and that's that.


Why do you WANT to use abstract classes?

Abstract classes allow you to provide default functionality for the subclasses. The consequences of this are as follows:

If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Why? Because you can make a change to it and all of the inheriting classes will now have this new functionality!


Goal: Arrays in 2D!

int[][]a = new int[4][3];

Remember row vs col:
                 Col 0     Col 1   Col 2
Row 0        a[0][0]   a[0][1] a[0][2]
Row 1        a[1][0]   a[1][1] a[1][2]
Row 2        a[2][0]   a[2][1] a[2][2]
Row 3        a[3][0]   a[3][1] a[3][2]



Practice problems/ Homework, due Monday. Sorry for late post!

2D Array Practice: 

public class ArrayMethods{

   //PART 1 (these two methods are almost the same! -- I count them as 1 problem)
   public static int rowSum(int[][] AR, int x){}
    //returns the sum of the elements in Row x of AR. 

   public static int columnSum(int[][] AR, int x){}
    //returns the sum of the elements in Column x of AR (careful with rows of different lengths!).

   //PART 2
   public static int[] allRowSums(int[][] AR){}
    //calculates the row sum for every row and returns each of the values in an array. 
    //Index i of the return array contains the sum of elements in row i.

   //PART 3 (one should just use your part 2 answer... look at the difference in the readability)
   public static boolean isRowMagic(int[][] AR){}
     //checks if the array is row-magic (this means that every row has the same row sum).

   public static boolean isColumnMagic(int[][] AR){}
    //checks if the array is column-magic (this means that every column has the same column sum).

}


Comments