Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-11-21 implementing CharSequence

posted Nov 21, 2018, 5:45 AM by Konstantinovich Samuel   [ updated Nov 21, 2018, 11:30 AM ]
Summary from yesterday:
-Correction : technically interfaces do create an is-a relationship.
-An interface is a reference type, similar to a class. (An interface name can be used anywhere a type can be used)
-It can contain only constants, method signatures, (default methods which we wont learn), static methods, and nested types.
-Method bodies exist only for (default methods which we won't learn)and static methods.
-Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
-IGNORE default methods, you do not need to touch them or implement them. We are NOT learning about them.
-The only methods that have implementations are static methods.
-A class that implements an interface must implement all the methods declared in the interface.

DO NOW:
1. What is wrong with the following interface?

public interface SomethingIsWrong {
    void aMethod(int aValue){
        System.out.println("Hi Mom");
    }
}

2. Is the following interface valid?

public interface Marker {
}

3. What methods would a class that implements the CharSequence interface have to implement?

Also for reference:
   https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html



Lab/Homework (due Tuesday)

Required: Make sure to test all of the methods as you are writing. Commit frequently and use comments.

Make a repo MKS21X-MyString  
1. Write a MyString that implements the CharSequence interface.
    -Your implementation should use an array of characters. You may NOT use a string as the data inside the class.
    -Your constructor should take a CharSequence as the only parameter.
   
-Your methods must behave as described in the CharSequence documentation including exceptions.
    -Your subSequence can return ANY CharSequence, either a String or a MyString would work!
2. Make your MyString implement Comparable<CharSequence>
    -Your compareTo must be written using a loop or equivalant. You cannot use a built in similar method to do this for you!

Hint: Test your constructors/compareTo with a working CharSequence like String. After you write your entire MyString class, try using MyString to initialize or compare against.

public class MyString implements CharSequence,Comparable<CharSequence>{
  private char[] data;
  public MyString(CharSequence s){

  }
}

Comments