1 / 47

Final Reviews

Final Reviews. The final exam is on May 8th from 11:00 am - 2:00 pm at the room CEMO 100D. It covers everything about Java!. Major Points. Java class basic Inheritance Polymorphism File I/O Interface Swing. Are the following statements true or false?.

mayes
Download Presentation

Final Reviews

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Final Reviews The final exam is on May 8th from 11:00 am - 2:00 pm at the room CEMO 100D. It covers everything about Java!

  2. Major Points • Java class basic • Inheritance • Polymorphism • File I/O • Interface • Swing

  3. 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.

  4. Are the following statements true or false? True Everything in Java is class, and all the classes are derived from the class Object. False There can be a global variable or global function in Java. False The default access modifier for a member in a class is “protected”. True To create an object, one needs to use the “new” operator. True There could have multiple constructors in a class, but only one has empty argument list. False Member variables must be initialized in a constructor.

  5. 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 { … };

  6. Are the following statements true or false? False Java supports both function overloading and operator overloading. False Function overloading occurs in the same class, while function overriding occurs in two classes without direct relation. True 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. True In Java, the inheritance is achieved by using the following format: public class Derived_Class : public Base_Class { … }; False

  7. 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.

  8. Are the following statements true or false? False In function overriding, both the return type and the argument list can be changed arbitrarily. False 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. True False Both this(…) and super(…) operations can be used in the same constructor. True 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.

  9. What is the outcome? • public class Base1 • { • public Base1(intval) • { System.out.println(“Base1: “+val); • counter++; • } • public static int counter = 0; • } • public class Derived21 extends Base2 • { • public Derived21 (intval) • { super(val); • System.out.println(“Derived21: “+val); • counter++; • } • } • public class Derived11 extends Base1 • { • public Derived11 (intval) • { super(val); • System.out.println(“Derived11: “+val); • } • } • public class Base3 • { • public Base3(intval) • { System.out.println(“Base3: “+val); • } • } • public class Derived31 extends Base3 • { • private Derived11 comp1; • private Derived21 comp2; • public Derived31(intval) • { 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 Derived12 extends Derived11 • { • public Derived12 (intval) • { super(val); • System.out.println(“Derived12: “+val); • } • } • public class Base2 • { • public Base2(intval) • { System.out.println(“Base2: “+val); • counter++; • } • public intcounter = 0; • }

  10. What is the outcome? • public class Base1 • { • public Base1(intval) • { System.out.println(“Base1: “+val); • counter++; • } • public static int counter = 0; • } • public class Derived21 extends Base2 • { • public Derived21 (intval) • { super(val); • System.out.println(“Derived21: “+val); • counter++; • } • } Base1 Base2 Base3 Derived11 Derived21 Derived31 • public class Derived11 extends Base1 • { • public Derived11 (intval) • { super(val); • System.out.println(“Derived11: “+val); • } • } • public class Base3 • { • public Base3(intval) • { System.out.println(“Base3: “+val); • } • } Derived12 • public class Derived31 extends Base3 • { • private Derived11 comp1; • private Derived21 comp2; • public Derived31(intval) • { 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 Derived12 extends Derived11 • { • public Derived12 (intval) • { super(val); • System.out.println(“Derived12: “+val); • } • } • public class Base2 • { • public Base2(intval) • { System.out.println(“Base2: “+val); • counter++; • } • public intcounter = 0; • }

  11. What is the outcome? • public class Base1 • { • public Base1(intval) • { System.out.println(“Base1: “+val); • counter++; • } • public static int counter = 0; • } • public class Derived21 extends Base2 • { • public Derived21 (intval) • { super(val); • System.out.println(“Derived21: “+val); • counter++; • } • } Base1 Base2 Base3 Derived11 Derived21 Derived31 • public class Derived11 extends Base1 • { • public Derived11 (intval) • { super(val); • System.out.println(“Derived11: “+val); • } • } • public class Base3 • { • public Base3(intval) • { System.out.println(“Base3: “+val); • } • } Derived12 • public class Derived31 extends Base3 • { • private Derived11 comp1; • private Derived21 comp2; • public Derived31(intval) • { 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 Derived12 extends Derived11 • { • public Derived12 (intval) • { super(val); • System.out.println(“Derived12: “+val); • } • } • public class Base2 • { • public Base2(intval) • { System.out.println(“Base2: “+val); • counter++; • } • public intcounter = 0; • }

  12. 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.

  13. Are the following statements true or false? True 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. True False To utilize polymorphism, you will need inheritance mechanism only. True 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. True

  14. 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. There cannot be member variables in the interface. 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.

  15. Are the following statements true or false? 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. True False The member functions in an interface can be either public, private, or protected. False There cannot be member variables in the interface. True 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. True

  16. 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.

  17. Are the following statements true or false? Java does not support multiple inheritance. However, similar functionality can be achieved using multiple interfaces. True False Java interface cannot be extended for the definition of another interface. False 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. True

  18. Given the following definition 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Is Square a concrete or abstract class? How about Cube?

  19. Legal or Illegal? Why? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Figure fig1 = new Figure();

  20. Legal or Illegal? Why? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Figure fig2 = new Square();

  21. Legal or Illegal? Why? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Figure fig3 = new Cube(0,0,0,1);

  22. Legal or Illegal? Why? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Square fig4; fig4.reset_center(0.3, 0.8);

  23. Legal or Illegal? Why? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Shape3D fig5; fig5 = new Cube(0.5, 0.5, 0.5, 1);

  24. What will be the output? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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; } Shape3D fig5; fig5 = new Cube(0.5, 0.5, 0.5, 1); fig5.draw(); fig5.area();

  25. What will be the output? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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 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);

  26. What will be the output? 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()=0; 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 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()=0; public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=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 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 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);

  27. 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.

  28. Are the following statements true or false? 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. False 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. True False There can be multiple exceptions caught in each try-throw-catch trio and each catch block, respectively. False The overriding methods can have completely different list of exceptions.

  29. 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 use readLine() with a BufferedReader object to read from a text file, the reading reaches the end of the file if readLine() return -1. When using nextInt() with a Scanner object, one can use hasNextInt() to determine whether the end of the file is reached.

  30. Are the following statements true or false? False There are two types of streams in Java File I/O that handles the input and output of data, and all File I/O classes are in the java.file package. True 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. True 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 When using nextInt() with a Scanner object, one can use hasNextInt() to determine whether the end of the file is reached. True

  31. 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 a binary file. Any objects can be serializable in Java. When a serializable class has instance variables of a class type, then all those classes must be serializable.

  32. Are the following statements true or false? 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. False Object serialization is built on top of the byte stream, i.e. its output and input is similar to the write and read a binary file. True False Any objects can be serializable in Java. When a serializable class has instance variables of a class type, then all those classes must be serializable. True

  33. Object Serialization • Use the writeObject method of the class ObjectOutputStream to write an object to a binary file • Use the readObjectmethod 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: • SomeClasssomeObject=(SomeClass)objectInputStream.readObject();

  34. import java.io.*; public class ObjectIODemo { public static void main(String[] args) { SomeClassoneObject = new SomeClass(1, 'A'); SomeClassanotherObject = new SomeClass(42, 'Z'); try { ObjectOutputStreamoutputStream = 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 { ObjectInputStreaminputStream = new ObjectInputStream(new FileInputStream("datafile")); SomeClassreadOne = (SomeClass)inputStream.readObject( ); SomeClassreadTwo = (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."); } } Example of Object Serialization import java.io.Serializable; public class SomeClassimplements Serializable { private int number; private char letter; public SomeClass( ) { number = 0; letter = 'A'; } public SomeClass(inttheNumber, char theLetter) { number = theNumber; letter = theLetter; } public String toString( ) { return "Number = " + number + " Letter = " + letter; } }

  35. 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

  36. Hierarchy of Swing and AWT Classes

  37. Events-Driven Programming • Event-driven programmingis 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

  38. 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

  39. Are the following statements true or false? All the swing classes are defined under the package java.swing. A listen 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.

  40. Are the following statements true or false? False All the swing classes are defined under the package java.swing. A listen is the object of a class that implements the interface ActionListener. This interface has only one function void actionPerformed(ActionEvent e) . True Each event handler function void actionPerformed(ActionEvent e) can only handle one event. False The listener object needs to be bound with the component object in the interface using the addActionListener(…) function. True

  41. 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. 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. The default layout for a JFrame object is FlowLayout. The default layout for a JPanel object is FlowLayout.

  42. 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. False The typical containers used in the Java GUI program include Frame, Panel, MenuBar, and Window. True 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 False The default layout for a JFrame object is FlowLayout. True The default layout for a JPanel object is FlowLayout.

  43. Are the following statements true or false? JTextfield is only used to display one line text on the interface. 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. An image can be added to a button, a label, or a menuitem object directly from a source image. A window’s event can be handled by a window listener that implements the WindowListener interface. A window listener has at least seven methods. 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.

  44. Are the following statements true or false? False JTextfield is only used to display one line text on the interface. 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. True An image can be added to a button, a label, or a menuitem object directly from a source image. False A window’s event can be handled by a window listener that implements the WindowListener interface. A window listener has at least seven methods. True False 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.

  45. Given the following layout of the interface and the description of the use cases, can you implement it?

  46. Here are your nominees! Group 1 Group 2 Sudoku Group 3 library Group 5 ATM Group 4 Sudoku Group 3.1 library Group 6 ATM Group 7 Sudoku Group 9 library Group 8 ATM

  47. Group 7 (Benito Martinez, Madison Morris) Sudoku Group 5 (Carlos Puerta, Joey Lin, Paul Pham) ATM Group 9 (Jairo Hernandez, Nikhil Baby) library

More Related