3.5 Interfaces & Inner-Classes 3.5.1 Questions 1. What is the difference between an Interface and an Abstract class? Explain when it is best to use Interfaces and when abstract classes. 2. Write a short code snippet to define a new interface I3 that inherits two existing interfaces I1 and I2. What happens if the definitions of the I1 and I2 are: public interface I1 public interface I2 What about if the two interfaces are: public interface I1 public interface I2 3. Point any errors in the code snippet below and explain them: interface Shape public class Tile voidsetHeight(int height) voidsetWidth(int width) } public class TileShape extends Tile implements Shape public static void main(String[] args) } 4. What are the access modifiers of the fields and methods inside the interface? Is it possible to declare an interface as being static? What about private? 5. What are the differences between inner classes and nested classes? 6. Provide a short code snippet to define an inner anonymous class: a. That implements a given interface I1 with one method: void f(); b. That extends a given class C1 c. That extends a given class C2 with no default constructor and a constructor with two string parameters. 7. Which of the following are true when talking about inner classes (non-anonymous): a. Can be declared inside other inner classes b. Can be declared inside methods and blocks of code c. Can access private members from the outer class(es) d. Can have more than one constructor e. Defines an IS-A relationship between the outer and the inner class f. Can be further extended by other classes 8. Given the code below, how can you access from the method f() the x member of the outer class? . public class A } } 9. Given the following interface definition: public interface I1 public void f1(); } Which of the following definitions are correct (and explain 1. public class A implements I1 ; } 2. public class A implements I1 ; } public void f1() ; } 3. public class A2 implements I2 { public void f2() } 4. public class A2 implements I1.I2 10. Given the following class, how can you create a new instance of the classes A and B in the main method(): public class Test static class B } classMyTest { public static void main(String[] args) } 3.5.2 Problems 1. Modify the class below (using an inner class) to allow using it as anIterator (make an example call of the method foo). public interface Iterator public class Map ; } . public void foo(Iterator it) 2. Given the CDPlayer class with the definition below, extend the CDPlayer class and add the necessary changes so it can be used as iterator. (hint: create an method inside the derived class that returns an Iterator and uses an anonymous inner class do to this) public interface Iterator public class CDPlayer ; privateintcurrentTrack; public void next() public void play() public void stop() public void prev() } public void foo(Iterator it) 3.8 Error handling with exceptions 3.8.1 Questions: 1. What information you can print about an exception? 2. What is the class name of the standard Java exception that does not have to be handled? 3. What is the exception restriction related to method overriding? Do constructors have the same restriction? Why? 4. a. Is the exception specification part of the method signature? Prove your answer by writing a small piece of code to justify your answer; comment your proof. b. Which parts of the method declaration go in the method signature? 5. Knowing that System.out.print does not throw any exception, consider the following of code: private static intgetI() catch (Throwable t) } finally } What strings are printed to the console, and what is value getI returns? 3.8.2 Problems 1. Follow the steps below to create a small Java program with 2 classes: a. create a class RandomPlay with a method that generates two random numbers between 1 and 10 (see java.lang.Math.random() for generating random numbers) b. if the difference of the 2 numbers is higher than 3, throw an exception you'll specially define for this case c. add a main() method to class RandomPlay that repeatedly calls the first method until it throws no exception d. exit the program by printing the number of times the first method threw your exception 2. Consider the following class definitions: public class Exception1 extends Exception public class Exception2 extends Exception and method: publicthrowException() throws Exception1, Exception2 and the following 2 blocks of code: block1: try catch (Exception1 e) catch (Exception2 e) block2: try catch (Exception e) Replace the commented line in block 2 with your code so that block 2 has the same functionality as block 1. 3.10 Detecting types 3.10.1 Questions 1. What is the difference between upcasting and downcasting? 2. What is the difference between "traditional" RTTI and reflection? 3. Let there be the following code fragment: public class Circle public class Shape extends Circle public class Test } 4. Pretend you are an implementation of the Java Virtual Machine and try to evaluate (to a boolean value) the following expressions: a. c.isInstance Circle) b. c instanceof Shape c. c.getClass ) == Shape.class d. c.isInstance Shape) e. c.getClass ) == Circle.class 5. Is it possible to build a valid line of Java code in a string and then execute it at run-time (Like in the sample below)? Write a short program to back-up your answer. String line = "someClass.method )"; run(line); 3.10.2 Problems: 1. Write a very simple graphical editor that allows you to draw objects deriving from the Shape class. Read the available classes of shapes from a configuration file, and present to the user a simple menu that allows him to select the shape to be drawn. public interface Shape Note: Write the simplest thing that meets the requirements. The dimensions of the shapes will be hardcoded (for ex.: every Circle drawn will have the same radius) 2. Write a program that by means of reflection creates an instance of the following class: public class Unknown } 3. Write a test program using reflection to print out the value of the hidden attribute after an instance of the Unknown class has been created in the previous exercise. If you have succeeded, try to double the value of the hidden attribute of this instance. Hint: check-out the java.lang.reflect package.
Document Info
Accesari:
1568
Apreciat:
Comenteaza documentul:
Nu esti inregistrat Trebuie sa fii utilizator inregistrat pentru a putea comenta