In the fifth tutorial, I mention generics, interface instances, stacks and I review the Inheritance concept.
Check out rest of the Tutorials: https://berksoysal.blogspot.com/2016/06/java-se-tutorials-basics-exercises.html
3. Java Generic methods and generic classes enable
programmers to specify, with a single method declaration,
a set of related methods or, with a single class declaration,
a set of related types, respectively.
public class GenericMethodTest {
public static < E > void printArray( E[] inputArray ) {
for ( E element : inputArray ){
System.out.print( "%s ", element ); }
}
public static void main( String args[] ) {
Integer[] intArray = { 1, 2, 3, 4, 5 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
printArray( intArray ); // pass an Integer array
printArray(charArray); // pass a Character array
} }
E becomes
Integer
and then
Character
2016 Winter
4. Let’s revisit the class Combination from laboratory 2 with only 2 inputs.
Make all the necessary changes to the class Combination so that it
implements the interface java.lang.Comparable.
if(first<other.first) result = -1;
else if(first>other.first) result = 1;
…
else result=0;
The interface Comparable is part of a package, called lang, that is
imported by default. Therefore, no need to import Comparable.
Write a test program for thoroughly testing your implementation.
2016 Winter
5. An interface is a way to describe what classes should
do, without specifying how they should do it. It’s not a class
but a set of requirements for classes that want to conform
to the interface, for example;
public interface Comparable<E> {
public int compareTo(E other);
}
This requires that any class implementing the Comparable
interface contains a compareTo method, and this method must
take an Object parameter and return an integer.
2016 Winter
6. • Once a Java class implements an Java interface you can use an instance of that
class as an instance of that interface. Here is an example:
MInterface myInterface = new MInterfaceImpl();
myInterface.sayHello();
• Notice how the variable is declared to be of the interface type MInterface
while the object created is of type MInterfaceImpl.
• Java allows this because the class MInterfaceImpl implements the MInterface
interface. You can then reference instances of the MInterfaceImpl class as
instances of the MInterface interface.
• You cannot create instances of a Java interface by itself. You must always create
an instance of some class that implements the interface, and reference that
instance as an instance of the interface.
2016 Winter
7. A stack is a data structure that allows data to be inserted (a 'push'
operation), and removed (a 'pop' operation).
Are Stacks LIFO or FIFO ?
- They are Last In First Out
2016 Winter
8. • Let’s edit the Stack.java and add an abstract void method clear() to the
class.
• Create a class called ArrayStack that uses a fixed-size array and
implements the interface Stack.
• Now that the interface Stack has been modified to have a method
clear(), the current implementation of the class ArrayStack is broken
(try compiling it without making any change, what is the error message
displayed?).
• Since the class ArrayStack implements the interface Stack, it has to
provide an implementation for all the methods that are declared by the
interface. Consequently, write an implementation for the method
void clear(). It removes all of the elements from this ArrayStack.
• The stack will be empty after this call returns.
2016 Winter
9. • The class Balanced contains a simple algorithm to validate expressions.
• Observation: for an expression consisting of well balanced parentheses
the number of opening ones is the same as the closing ones.
> run Balanced {[()]}
algo1( "{[()]}" ) -> true
2016 Winter
10. • Perform the same operation using the ArrayStack.java and Stack.java
> run Balanced {[()]}
algo1( "{[()]}" ) -> true
algo2( "{[()]}" ) -> true
2016 Winter
11. • A Canadian postal code example: K1N 8A7 A US zip code example: FL 33130
Write a test class for the above classes.
• Declare an array, called codes, to hold exactly 100 postal codes.
• Create n = 10 postal codes, some are USZipCodes, some are CanadianPostalCodes,
some must
be valid and some must not be valid. Store these codes in the n left most positions of
the array;
• Knowing that exactly n postal codes are currently stored in the left most cells of the
array, write
a for loop to count the number of valid codes.
PostalCode
CanadianPostalCode USZipCode
Abstract class PostalCode
Constructor accepts String code.
Abstract boolean isValid();
String getCode()
2016 Winter