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!

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Class Hierarchy (Inheritance)
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10: Inheritance and Polymorphism
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Io package as Java’s basic I/O system continue’d.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Java Review I. What is a stream? What is a byte stream? what is a character stream? What types of files does a program will typically handle?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Java Polymorphism. What we have learned Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Object-Oriented Programming: Polymorphism Chapter 10.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
BY:- TOPS Technologies
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSC 205 Programming II Lecture 5 AWT - I.
Modern Programming Tools And Techniques-I
“Form Ever Follows Function” Louis Henri Sullivan
Inheritance-Basics.
Graphical User Interface (pronounced "gooey")
I/O Basics.
Computer Science II Exam 1 Review.
Java Programming Language
Constructors, GUI’s(Using Swing) and ActionListner
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

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 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 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 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 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 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 True 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 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 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 IDProject Name Students’ Grade (40% ) TA’s Grade (10%) Instructor’s Grade (50%) Final Grade 1Graph 2Sudoku 3ATM 4Sudoku 6ATM 7 - DavisLibrary 7- RonakATM 8 9- JohnsonGraph 9- the restATM 10- NguyenSudoku 10-HewlettATM 11Sudoku 12- FloresATM 13Graph 14ATM 15ATM 16Graph

Group 9-1 (Aaron Johnson) Graph Group 4 (Darya Balybina, Thomas Doyle) Sudoku Group 13 (Gjvon Graves, Vicki Lee, Hejal Soni) Graph

Given the outcome and main function, can you define an interface? public class AnimalDemo { public static void main (String[] args) { Animal p[3]; p[0] = new Bird(); //eat bugs p[1] = new Monkey(); //eat banana p[2] = new Human(); //eat whatever we find for (int i=0; i<3; i++) { System.out.println (p[i].what_am_I() + p[i].what_do_I_eat()); } Expect output: I am a bird. I eat bugs. I am a monkey. I eat bananas. I am a human. I eat whatever I can find Please define an Animal interface and the three classes that implement the Animal interface.