47
Final Reviews The final exam is on May 14th from 11:00 am - 2:00 pm at the room CBB 104. It covers everything about Java!

Final Reviews The final exam is on May 14th from 11:00 am - 2:00 pm at the room CBB 104. It covers everything about Java!

Embed Size (px)

Citation preview

Final Reviews

The final exam is on May 14th from 11:00 am - 2:00 pm at the room CBB 104. It

covers everything about Java!

Major Points

• Java class basic• Inheritance• Polymorphism• File I/O• Interface• Swing

Are the following statements true or false?

Everything in Java is class, and all the classes are derived from the class Object.

There can be a global variable or global function in Java.

The default access modifier for a member in a class is “protected”.

To create an object, one needs to use the “new” operator.

There could have multiple constructors in a class, but only one has empty argument list.

Member variables must be initialized in a constructor.

Are the following statements true or false?

Everything in Java is class, and all the classes are derived from the class Object.

There can be a global variable or global function in Java.

The default access modifier for a member in a class is “protected”.

To create an object, one needs to use the “new” operator.

There could have multiple constructors in a class, but only one has empty argument list.

Member variables must be initialized in a constructor.

True

False

False

True

True

False

Are the following statements true or false?

Java supports both function overloading and operator overloading.

Function overloading occurs in the same class, while function overriding occurs in two classes without direct relation.

equal(…) and toString() functions are inherited from the class Object, and the user can use them as they are.

Inheritance mechanism is an important OOP mechanism that allows the software developer to reuse their previous codes.

In Java, the inheritance is achieved by using the following format:public class Derived_Class : public Base_Class{ …};

Are the following statements true or false?

Java supports both function overloading and operator overloading.

Function overloading occurs in the same class, while function overriding occurs in two classes without direct relation.

equal(…) and toString() functions are inherited from the class Object, and the user can use them as they are.

Inheritance mechanism is an important OOP mechanism that allows the software developer to reuse their previous codes.

In Java, the inheritance is achieved by using the following format:public class Derived_Class : public Base_Class{ …};

False

False

True

True

False

Are the following statements true or false?

In function overriding, both the return type and the argument list can be changed arbitrarily.

The final functions in a base class cannot be overridden and inherited in the derived class.

In the constructors of a derived class, the proper constructors from base class should be called. This can be achieved via the super(…) function.

Both this(…) and super(…) operations can be used in the same constructor.

In addition to inheritance, another way to reuse the existing code/classes is to define a member variable in the new class which is the existing class type.

Are the following statements true or false?

In function overriding, both the return type and the argument list can be changed arbitrarily.

The final functions in a base class cannot be overridden and inherited in the derived class.

In the constructors of a derived class, the proper constructors from base class should be called. This can be achieved via the super(…) function.

Both this(…) and super(…) operations can be used in the same constructor.

In addition to inheritance, another way to reuse the existing code/classes is to define a member variable in the new class which is the existing class type.

False

False

True

True

False

What is the outcome?public class Base1{

public Base1(int val){ System.out.println(“Base1: “+val); counter++;}public static int counter = 0;

}

public class Derived11 extends Base1{

public Derived11 (int val){ super(val); System.out.println(“Derived11: “+val);}

}

public class Derived12 extends Derived11 {

public Derived12 (int val){ super(val); System.out.println(“Derived12: “+val);}

}

public class Base2{

public Base2(int val){ System.out.println(“Base2: “+val); counter++;}public int counter = 0;

}

public class Derived21 extends Base2{

public Derived21 (int val){ super(val); System.out.println(“Derived21: “+val); counter++;}

}

public class Derived31 extends Base3{

private Derived11 comp1;private Derived21 comp2;public Derived31(int val){ super(val+1); comp1 = new Derived12(val+2); System.out.println(“counter1=“+comp1.counter); comp2 = new Derived21(val+4); System.out.println(“counter2=“+comp2.counter);}public static void main(String[] args){ Derived31 obj = new Derived31(1);}

}

public class Base3{

public Base3(int val){ System.out.println(“Base3: “+val);}

}

What is the outcome?public class Base1{

public Base1(int val){ System.out.println(“Base1: “+val); counter++;}public static int counter = 0;

}

public class Derived11 extends Base1{

public Derived11 (int val){ super(val); System.out.println(“Derived11: “+val);}

}

public class Derived12 extends Derived11 {

public Derived12 (int val){ super(val); System.out.println(“Derived12: “+val);}

}

public class Base2{

public Base2(int val){ System.out.println(“Base2: “+val); counter++;}public int counter = 0;

}

public class Derived21 extends Base2{

public Derived21 (int val){ super(val); System.out.println(“Derived21: “+val); counter++;}

}

public class Derived31 extends Base3{

private Derived11 comp1;private Derived21 comp2;public Derived31(int val){ super(val+1); comp1 = new Derived12(val+2); System.out.println(“counter1=“+comp1.counter); comp2 = new Derived21(val+4); System.out.println(“counter2=“+comp2.counter);}public static void main(String[] args){ Derived31 obj = new Derived31(1);}

}

public class Base3{

public Base3(int val){ System.out.println(“Base3: “+val);}

}

Base1

Derived11

Derived12

Base2

Derived21

Base3

Derived31

What is the outcome?public class Base1{

public Base1(int val){ System.out.println(“Base1: “+val); counter++;}public static int counter = 0;

}

public class Derived11 extends Base1{

public Derived11 (int val){ super(val); System.out.println(“Derived11: “+val);}

}

public class Derived12 extends Derived11 {

public Derived12 (int val){ super(val); System.out.println(“Derived12: “+val);}

}

public class Base2{

public Base2(int val){ System.out.println(“Base2: “+val); counter++;}public int counter = 0;

}

public class Derived21 extends Base2{

public Derived21 (int val){ super(val); System.out.println(“Derived21: “+val); counter++;}

}

public class Base3{

public Base3(int val){ System.out.println(“Base3: “+val);}

}

Base1

Derived11

Derived12

Base2

Derived21

Base3

Derived31

public class Derived31 extends Base3{

private Derived11 comp1;private Derived21 comp2;public Derived31(int val){ super(val+1); comp1 = new Derived12(val+2); System.out.println(“counter1=“+comp1.counter); comp2 = new Derived21(val+4); System.out.println(“counter2=“+comp2.counter);}public static void main(String[] args){ Derived31 obj = new Derived31(1);}

}

Are the following statements true or false?

Polymorphism is another OOP mechanism that supports the scalable development and maintenance of software.

Polymorphism is achieved via late/dynamic binding , i.e., the actual function that will be called is determined by the type of the calling object. Java late binding is automatically enable.

To utilize polymorphism, you will need inheritance mechanism only.

final and static methods cannot be used with polymorphism.

If a class contains at least one abstract method, the class needs to be specified as an abstract class.

Are the following statements true or false?

Polymorphism is another OOP mechanism that supports the scalable development and maintenance of software.

Polymorphism is achieved via late/dynamic binding , i.e., the actual function that will be called is determined by the type of the calling object. Java late binding is automatically enable.

To utilize polymorphism, you will need inheritance mechanism only.

final and static methods cannot be used with polymorphism.

True

False

True

True

If a class contains at least one abstract method, the class needs to be specified as an abstract class.

True

Are the following statements true or false?

An abstract method has only an empty function body.

An interface in Java is a collection of abstract methods. It is more abstract than an abstract class, but it is not a class.

The member functions in an interface can be either public, private, or protected.

The class that utilizes an interface needs to use the keyword “implements”.

In a class that implements an interface, only certain abstract methods need to be provided a detailed implementation, while the others can be provided an empty function body.

There cannot be instance variables in the interface.

Are the following statements true or false?

An abstract method has only an empty function body.

An interface in Java is a collection of abstract methods. It is more abstract than an abstract class, but it is not a class.

The member functions in an interface can be either public, private, or protected.

The class that utilizes an interface needs to use the keyword “implements”.

In a class that implements an interface, only certain abstract methods need to be provided a detailed implementation, while the others can be provided an empty function body.

There cannot be instance variables in the interface.

False

False

True

True

True

True

Are the following statements true or false?

Java does not support multiple inheritance. However, similar functionality can be achieved using multiple interfaces.

Java interface cannot be extended for the definition of another interface.

There will not have ambiguity using multiple interfaces.

A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface.

Are the following statements true or false?

Java does not support multiple inheritance. However, similar functionality can be achieved using multiple interfaces.

Java interface cannot be extended for the definition of another interface.

There will not have ambiguity using multiple interfaces.

A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface.

False

True

False

True

Given the following definition

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Is Square a concrete or abstract class? How about Cube?

Legal or Illegal? Why?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Figure fig1 = new Figure();

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

Legal or Illegal? Why?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Figure fig2 = new Square();

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

Legal or Illegal? Why?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Figure fig3 = new Cube(0,0,0,1);

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

Legal or Illegal? Why?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Square fig4;fig4.reset_center(0.3, 0.8);

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

Legal or Illegal? Why?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

Shape3D fig5;fig5 = new Cube(0.5, 0.5, 0.5, 1);

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

What will be the output?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}Shape3D fig5;

fig5 = new Cube(0.5, 0.5, 0.5, 1);fig5.draw();fig5.area();

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

What will be the output?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

public class test{public void call_FigureFunc(FigureFunc fig) {fig.draw(); fig.area();}…}

Figure fig1 = new Square(0,0,1);Shape3D fig2 = new Cube(0,0,0,1);call_FigureFunc(fig1);call_FigureFunc(fig2);

What will be the output?

public class Square extends Figure{ public Square(double cx, double cy, double size) { super(cx, cy); this.size=size;} public void draw() { System.out.println(“A Square.”); } public void area() {System.out.println("The area of the square is "+ size*size);} private double size=0;}

public abstract class Figure implements FigureFunc{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public void draw() {System.out.println(“A figure.”); } public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;}

public interface FigureFunc {public void draw();public void area();public double default_cx = 0, default_cy = 0;}

public class Cube extends Shape3D{ public Cube(double cx, double cy, double cz, double size) { super(cx, cy, cz); this.size=size;} public void draw() { System.out.println(“A Cube.”); } public void area() {System.out.println("The area of the cube is "+ size*size*6);} public void volume() {System.out.println("The volume of the cube is "+ size*size*size);} private double size=0;}

public abstract class Shape3D implements FigureFunc{ public Shape3D(double cx, double cy, double cz) { center_x=cx; center_y=cy; center_z=cz;} public Figure() { this (0,0); } public void draw() {System.out.println(“A 3D shape.”); } public void area(){} public void volume(); public void reset_center(double x, double y, double z) { center_x=x; center_y=y; center_z=z;} private double center_x=0, center_y=0, center_z=0;}

public class test{public void call_FigureFunc(FigureFunc fig) {fig.draw(); fig.area();}…}

Square fig1 = new Square();Cube fig2 = new Cube();call_FigureFunc(fig1);call_FigureFunc(fig2);

Are the following statements true or false?

In the Java exception handling, try-throw-catch trio is used. In some cases, one or more of these three components can be neglected.

Any types of variables (including complex objects or primitive types) can be thrown.

An exception can be thrown in a method and be caught outside of the method. However, the method heading should explicitly indicate the types of exceptions that may be thrown using the throw clause.

The overriding methods can have completely different list of exceptions.

There can be multiple exceptions caught in each try-throw-catch trio and each catch block, respectively.

Are the following statements true or false?

In the Java exception handling, try-throw-catch trio is used. In some cases, one or more of these three components can be neglected.

Any types of variables (including complex objects or primitive types) can be thrown.

An exception can be thrown in a method and be caught outside of the method. However, the method heading should explicitly indicate the types of exceptions that may be thrown using the throw clause.

There can be multiple exceptions caught in each try-throw-catch trio and each catch block, respectively.

The overriding methods can have completely different list of exceptions.

False

False

True

False

False

Are the following statements true or false?

There are two types of streams in Java File I/O that handle the input and output of data, and all File I/O classes are in the java.file package.

To read and write to a text file, the character stream is used. Particularly, we use a PrintWriter object to write to a text file.

In order to read from a text file, one can use either a BufferReader or a Scanner object. Either of them can serve as an intermediary between the file and the program.

When using nextInt() with a Scanner object, one can use hasNextInt() to determine whether the end of the file is reached.

When use readLine() with a BufferedReader object to read from a text file, the reading reaches the end of the file if readLine() return -1.

Are the following statements true or false?

To read and write to a text file, the character stream is used. Particularly, we use a PrintWriter object to write to a text file.

In order to read from a text file, one can use either a BufferReader or a Scanner object. Either of them can serve as an intermediary between the file and the program.

When using nextInt() with a Scanner object, one can use hasNextInt() to determine whether the end of the file is reached.

When use readLine() with a BufferedReader object to read from a text file, the reading reaches the end of the file if readLine() return -1.

False

True

True

False

True

There are two types of streams in Java File I/O that handle the input and output of data, and all File I/O classes are in the java.file package.

Are the following statements true or false?

To read and write to a binary file, the byte stream is used which is built on the character stream.

To read from a binary file, we use an ObjectInputStream object whose constructor takes the file name as the input.

Object serialization is built on top of the byte stream, i.e. its output and input is similar to the write and read of a binary file.

When a serializable class has instance variables of a class type, then all those classes must be serializable.

Any objects can be serializable in Java.

Are the following statements true or false?

To read and write to a binary file, the byte stream is used which is built on the character stream.

To read from a binary file, we use an ObjectInputStream object whose constructor takes the file name as the input.

Object serialization is built on top of the byte stream, i.e. its output and input is similar to the write and read of a binary file.

When a serializable class has instance variables of a class type, then all those classes must be serializable.

Any objects can be serializable in Java.

False

False

True

False

True

Object Serialization• Use the writeObject method of the class

ObjectOutputStream to write an object to a binary file

• Use the readObject method of the class ObjectInputStream to read an object from a binary file

• In order to use the value returned by readObject as an object of a class, it must be type cast first:

SomeClass someObject = (SomeClass)objectInputStream.readObject();

Example of Object Serialization

import java.io.*;public class ObjectIODemo{ public static void main(String[] args) { SomeClass oneObject = new SomeClass(1, 'A'); SomeClass anotherObject = new SomeClass(42, 'Z'); try { ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("datafile"));

outputStream.writeObject(oneObject); outputStream.writeObject(anotherObject);

outputStream.close( );

System.out.println("Data sent to file."); } catch(IOException e) { System.out.println("Problem with file output."); }

System.out.println( "Now let's reopen the file and display the data.");

try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("datafile"));

SomeClass readOne = (SomeClass)inputStream.readObject( ); SomeClass readTwo = (SomeClass)inputStream.readObject( );

System.out.println("The following were read from the file:"); System.out.println(readOne); System.out.println(readTwo); } catch(FileNotFoundException e) { System.out.println("Cannot find datafile."); } catch(ClassNotFoundException e) { System.out.println("Problems with file input."); } catch(IOException e) { System.out.println("Problems with file input."); }

System.out.println("End of program."); }}

import java.io.Serializable;

public class SomeClass implements Serializable{ private int number; private char letter;

public SomeClass( ) { number = 0; letter = 'A'; }

public SomeClass(int theNumber, char theLetter) { number = theNumber; letter = theLetter; }

public String toString( ) { return "Number = " + number + " Letter = " + letter; }

}

Introduction to Swing• A GUI (graphical user interface) is a windowing system that

interacts with the user

• The Java AWT (Abstract Window Toolkit) package is the original Java package for doing GUIs

• The Swing package is an improved version of the AWT– However, it does not completely replace the AWT– Some AWT classes are replaced by Swing classes, but other AWT

classes are needed when using Swing

• Swing GUIs are designed using a form of object-oriented programming known as event-driven programming

Events-Driven Programming• Event-driven programming is a programming style that uses

a signal-and-response approach to programming

• An event is an object that acts as a signal to another object know as a listener

• A listener object performs some action in response to the event

• Java GUI program can be designed and implemented in a hierarchical fashion– be aware of the relation between containers and components

Hierarchy of Swing and AWT Classes

What Can be Summarized?Derived a new container class from, say, JFrame

In the derived class• Define a constructor that sets up the title and size of the window• Set up the proper lay out of the outer container• Create inner containers (using JPanel or other containers)• Set up the proper lay out of each inner containers• Add the interface objects, such as buttons and others, to the

corresponding containers• Remember to associate a listener object for each interface object• Add the containers to the Frame object in order

Define listener classes to handle possible events fired by the interface objects added in the window

In the main function• Create the object of the derived window class• Launch the interface by setting it as visible

Are the following statements true or false?

All the swing classes are defined under the package java.swing.

A listener is the object of a class that implements the interface ActionListener. This interface has only one function void actionPerformed(ActionEvent e) .

Each event handler function void actionPerformed(ActionEvent e) can only handle one event.

The listener object needs to be bound with the component object in the interface using the addActionListener(…) function.

Are the following statements true or false?

All the swing classes are defined under the package java.swing.

A listener is the object of a class that implements the interface ActionListener. This interface has only one function void actionPerformed(ActionEvent e) .

Each event handler function void actionPerformed(ActionEvent e) can only handle one event.

The listener object needs to be bound with the component object in the interface using the addActionListener(…) function.

False

True

False

True

Are the following statements true or false?

Each function can address different types of events. In order to determine the component that fires the event, the getActionCommand() function can be used, which returns a unique integer ID of the component object. Similar, one can use setActionCommand() to modify the ID of a component.

The typical containers used in the Java GUI program include Frame, Panel, MenuBar, and Window.

The default layout for a JFrame object is FlowLayout.

The default layout for a JPanel object is FlowLayout.

Most of times each container needs to be specified a layout manager in order to handle multiple components. The classes related to the three layout managers are in the swing package.

Are the following statements true or false?

Each function can address different types of events. In order to determine the component that fires the event, the getActionCommand() function can be used, which returns a unique integer ID of the component object. Similar, one can use setActionCommand() to modify the ID of a component.

The typical containers used in the Java GUI program include Frame, Panel, MenuBar, and Window.

The default layout for a JFrame object is FlowLayout.

The default layout for a JPanel object is FlowLayout.

Most of times each container needs to be specified a layout manager in order to handle multiple components. The classes related to the three layout managers are in the swing package.

False

True

False

True

False

Are the following statements true or false?

JTextfield is only used to display one line text on the interface.

An image can be added to a button, a label, or a menuitem object directly from a source image.

The ordering of adding menu to the JFrame object is to first create a MenuBar object and add it to the JFrame object, then, create a Menu object and add it to the previous MenuBar object, finally, create the individual MenuItem objects and add them to the Menu object in order.

A window’s event can be handled by a window listener that implements the WindowListener interface. A window listener has at least seven methods.

JTextArea is used to display or get the input of multiple line text on the interface. To accommodate the text that is larger than the text area, scroll bar object can be used.

Are the following statements true or false?

JTextfield is only used to display one line text on the interface.

An image can be added to a button, a label, or a menuitem object directly from a source image.

The ordering of adding menu to the JFrame object is to first create a MenuBar object and add it to the JFrame object, then, create a Menu object and add it to the previous MenuBar object, finally, create the individual MenuItem objects and add them to the Menu object in order.

A window’s event can be handled by a window listener that implements the WindowListener interface. A window listener has at least seven methods.

JTextArea is used to display or get the input of multiple line text on the interface. To accommodate the text that is larger than the text area, scroll bar object can be used.

False

True

False

True

False

Given the following layout of the interface and the description of the use cases, can

you implement it?

Here are your nominees!Group ID Project Name Students’ Grade

(40% )TA’s Grade

(10%)Instructor’s Grade

(50%)Final Grade

1 Graph

2 Sudoku

3 ATM

4 Sudoku

6 ATM

7 - Davis Library

7- Ronak ATM

8 ATM

9- Johnson Graph

9- the rest ATM

10- Nguyen Sudoku

10-Hewlett ATM

11 Sudoku

12- Flores ATM

13 Graph

14 ATM

15 ATM

16 Graph

Group 9-1 (Aaron Johnson)Graph

Group 4 (Darya Balybina, Thomas Doyle)Sudoku

Group 13 (Gjvon Graves, Vicki Lee, Hejal Soni)Graph