practice for ocp

Important: review code written in Eclipse (ocp package)

Enums

  • An enum is allowed to implement interfaces
  • Enum constants must be declared first (non-blank) line e.g. DOG, CAT, FISH;

see my class EnumsPractice (eclipse)

enum Title
{
    MR("Mr."), MS1("Ms."), MS2("Ms.");
    private String title;
    private Title(String s){
    title = s;
    }  
}

public class TestClass{
    public static void main(String[] args) {
        var ts = new TreeSet<Title>();
        ts.add(Title.MS2);
        ts.add(Title.MR);
        ts.add(Title.MS1);
        for(Title t :  ts){
            System.out.println(t);
        }
    }
}

the natural order of enums is the order in which they are defined (Treeset sorts elements in their natural sort order)

Inheritance

Review IS-A rule from both sides of the equals. Remember is-a on right is different rule to IS-A on left:

Consider the following classes :
interface I{
}

class A implements I{
}

class B extends A {
}

class C extends B{
}
And the following declarations:
A a = new A();
B b = new B(); 
Identify options that will compile and run without error.

b = (B)(I) a;

 

Inheritance and static and hiding

interface AmazingInterface{
    String value = "amazing";
    void amazingMethod(String arg);
}

abstract class AmazingClass implements AmazingInterface{
    static String value = "awesome";
    abstract void amazingMethod(String arg1, String arg2);
}


public class Awesome extends AmazingClass implements AmazingInterface {
    public void amazingMethod(String arg1){ }
    public void amazingMethod(String arg1, String arg2){ }
   
    public  void main(String[] args){
        AmazingInterface ai = new Awesome();

        //INSERT CODE HERE
    }

}

look at inserting this at // INSERT

 

interfaces

this is fine review it

interface T1{
}
interface T2{
   int VALUE = 10;
   void m1();
}

interface T3 extends T1, T2{
   public void m1();
   public void m1(int x);
}

Interface can have private methods (provided method is implemented below ):

private void foo(){ };

Interface MUST implement static methods ( below is not valid ):

static void foo();

Interface (that inherits from another can do this below ): FIRST CONCRETE CLASS MUST IMPLEMENT

@Override void moo();

 

 

API Knowledge

  • Collections (List, Map, Q, etc and subclasses )
  • Function / Streams ( Optional , andThen, collect etc … )
  • Threads (CyclicBarrier etc)
  • Date / Number formats ( Format, NumberFormat ,  DecimalFormat … )
  • Database stuff ( Statement, PreparedStatement … )

 

  • Streams (inc return type of count(), sum(), average() ) : re-read chapter in Book ?
        Deque<Integer> d = new ArrayDeque<>();
        d.push(1);
        d.push(2);
        d.push(3);
        System.out.println(d.pollFirst());
        System.out.println(d.poll());
        System.out.println(d.pollLast());

go over how this works (order )

 

Arrays.compare method:

What will the following code print?

var a = new int[]{ 1, 2, 3, 4, 5};
var b = new int[]{ 1, 2, 3, 4, 5, 3};
var c = new int[]{ 1, 2, 3, 4, 5, 6};

int x = Arrays.compare(a, c);
int y = Arrays.compare(b, c);
System.out.println(x+” “+y);

 

List.of() , Set.of() , Map.of ()

Returns an unmodifiable list ( Elements cannot be added, removed, or replaced or sorted ! ).

 

Concurrency API

AtomicInteger important

 

Java I/O API – Serialization

if fields are added to a previously serialized object (ie it was saved to File orginally with fewer fields )

When de-serializing (reading into a newer version of the object where fields have been added ), the new fields will be blank (set to default values). Provided the original (and newer version of the class, have a serialVersionUID field )

Threads

 

runnable.run() // doesnt return any thing

callable.call()   // returns something

 

ok

int a, b, c=100;

 

not ok

var a = 100, b = 10;

Arrays

Review before exam (close to , I’ll forget otherwise !!)

int[ ] a[ ] = new int [5][4] ; // good 

int a[ ][ ] = new int [5][4] ; // good 

int a[ ][ ] = new int [ ][4] ;  // bad invalid

int[ ] a[ ] = new int[4][ ] ;  // good

int[ ][ ] a = new int[5][4] ; // good

var[ ][ ] a = new int [5][4] ; // bad can’t use var here

 

Pre and Post Increment (important)

public class Operators{

    public static int operators(){
        int x1 = -4;
        int x2 = x1--;
        int x3 = ++x2;
        if(x2 > x3){
            --x3;
        }else{
            x1++;
        }
        return x1 + x2 + x3;
    }
    public static void main(String[] args) {
        System.out.println(operators());
    }
}

 

 

Switches

only accept the following as params:

  • byte
  • short
  • int
  • char
  • enum
  • String

null throws NullPointerException