COSC 1320 Introduction to
Computer Science II
Having as many practices as possible is the right way to learn a
language!
There is NO lab for this course, so please do compile and modify
the provided examples!
Be sure to check the course webpage frequently for updates and
announcement!
Final Exam on May 6 (FRIDAY!!) from
8am-11am (location: SEC 100)!
The exam will cover all the materials of
Java (NO C++ content).
Spring 2016
- Instructor: Guoning Chen
- Location: SEC 204
- Time: Tue/Th. 8:30am~10:00am
- Office hours: Tue/Th. 10:00am~11:00pm
at PGH 566
- TAs:
- Lieyu Shi office hours: Mon/Wed 10-12pm;
Location PGH 314
- Quan Tran office hours: Tu 10-11am; Th 10-11am; Fri
3-5pm; Email: qatran3@uh.edu
- There will be a review section on EVERY FRIDAY 12-3pm at PGH
376
Course summary
This course aims to introduce the students the
principles of object-oriented programming with the examples of C++ and Java
languages.
The goal is to prepare students with the
fundamental programming skills of C++ and Java for the future large-scale OOP
tasks.
Prerequisites:
COSC 1410 Introduction to Computer Science I
Textbooks (required):
1) Absolute C++ (5th/6th edt.), 2) Absolute Java
(5th/6th edt.), Walter Savitch, Addison-Wesley.
Recommended Reading:
- C++ How to program , 8/e, Deitel and Deitel,
2012, Prentice Hall, ISBN-10: 0132662361 , ISBN-13: 9780132662369
- Java How to Program: Late Objects Version, 8/e,
Deitel, 2010 , Prentice Hall,ISBN-10: 0136123716 , ISBN-13:
9780136123712.
- Java Programming: Comprehensive Concepts and
Techniques, 3rd Edition
- Shelly, Cashman, Starks, Mick, 2006 Course
Technology, a division of Thompson Learning.
- Object-Oriented Software Engineering Using UML,
Patterns, and Java, 3/E, Bruegge & Dutoit, 2010, Prentice Hall.ISBN-10:
0136061257, ISBN-13: 9780136061250.
Software and IDE (tutorials should be
available in the blackboard learn):
- Microsoft Visual Studio
2015/2013/2012/2010/2008/ (for C++ on windows)
- Xcode (for C++ on
Mac)
- Eclipse (for
Java)
- UML editor (TBA)
Detailed syllabus
is also available in the blackboard learn.
Schedule
TIMELINE
|
MATERIAL
COVERED
|
WEEK 1 (01/19,
21) |
OOP Principles, Classes in C++ |
WEEK 2 (01/26,
28) |
Inheritance Mechanism |
WEEK 3 (02/02,
04) |
Polymorphism and Virtual Functions |
WEEK 4 (02/09,
11) |
Stream, File I/O, Templates |
WEEK 5 (02/16,
18) |
Exception Handling, UML (Exam 1) |
WEEK 6
(02/23,25) |
Classes in Java |
WEEK 7 (03/01,
03) |
Java Inheritiance, Polymorphism,
Abstract Class |
WEEK 8 (03/08,
10) |
Java File I/O, ASCII text file
processing (Exam 2) |
|
SPRING BREAK |
WEEK 9 (03/22,
24) |
Binary File I/O, |
WEEK 10 (03/29,
31) |
Java Exception Handling, Java Interface
and Inner Classes |
WEEK 11 (04/05,
07) |
Java Swing I |
WEEK 12 (04/12,
14) |
Java Swing II |
WEEK 13 (04/19,
21) |
Final Reviews |
WEEK 14 (04/26,
28) |
Final Group Project Presentations |
|
Final Exam on May 6 (Friday!!) from
8:00am-11:00am (location: SEC 100)! The exam will cover all the
materials of Java (NO C++ content). |
Schedule and Materials
Come and visit the course webpage often, as the
materials will be updated constantly without notice.
Main goal: review OOP principles,
important concepts of class, object, encapsulation, abstract data type (ADT),
constructors,
Knowledge points that you should
know:
- What is the difference between structured programming and object-oriented
programming? (algorithm centric vs. object centric)
- What is a class and its relation to object? (A class is a user-defined
type that consists of both member variables and functions. An object of a
defined class is its instance or variable)
- How to access member variables and functions? (Define an object and use
dot operator "." to access only "public" members)
- How to implement member functions outside of the class body? (
return_type class_name::function_name(argument list) {function
body}
)
- How to use "public" or "private" to limit the access to certain members
of a class? and Why? (to achieve encapsulation)
- NOTE THAT the default access without specification is "private" for a
class, but "public" for a structure!
- What is an abstract data type (ADT)?
- What does encapsulation mean? (enclose both attributes and behaviors,
information and implementation detailed hidden, good interface and
comments...)
- Can you tell the difference between structure and class now?
- How to define and initialize an object from a pre-defined class?
(class_name object_name(initial parameters);)
- What is the way to define a constructor (pp.276, must have the
same name as the class, NO RETURN TYPE)
- What does a constructor do? Can it be defined under a "private"
section?
- How many different constructors one class can have? (default constructor,
overloaded constructors)
- How to initialize member variables in a constructor? (direct or via an
initialization section)
- How to manage dynamic memory allocation? (see the additional examples,
new
operator)
- Why do we need destructor and how to define it?
(
~class_name(){body};
NO RETURN TYPE, ARGUMENT LIST IS
ALWAYS EMPTY)
- Using
"const
" in class and define member functions with
"const"
type. Why do we need that? (pp.295, Do not want a
member function to change member variables)
- Using "static" members in class. Why do we need that? (pp.303, All the
objects of the class shared one variable, e.g. a token for take or a
counter, it is better to be defined as "static")
- Overloaded operator functions. Why do we need that? (pp. 322, apply '-',
'+', '>', '==', etc. to objects that are not basic types, pp.343 the
rules)
- What is the difference when overloading operator functions as the member
functions vs. non-member functions? (one parameter less!)
- operator '=' is overloaded when the member variables have
pointer type!
- Friend functions (especially for overloaded operators). How do we define
them? Why do we need that? (pp.338, Auto type conversion +Access other
class private members directly, not-so encapsulated, though)
Additional examples: (Again, please
compile and play with the following examples, try to break the rules and see
what would happen!)
An example of course grade for students, cpp1,
cpp2,
(constructors, destructor, dynamic memory allocation)
An example of a clock class cpp3
(constructors and operator overloading)
An example of car cpp4
(constructors)
A different employee example cpp5
(constructors, destructor, dynamic memory allocation, dynamic creation of
objects)
Please be familiar with your selected
programming environment. Be sure to compile and execute the examples
of the textbook and the course webpage!
Week 2 (Ch.14)
Inheritance
Main goal: understand the mechanism
and benefits of inheritance of OOP, and know how to implement it in C++.
Knowledge points that you should
know:
- How to do composition and why it is useful? (To
build up complex objects by "compositing" many basic objects, like building
a car. In OOP, you use the objects of pre-defined classes as the
member variables.)
- How to initialize the composited objects? (see
the additional slides in the review of last week)
- What is the inheritance and why is it useful?
(see the additional slides below--programming efficiency, software
maintenance, resource reuse, etc.)
- What is a base (or super) class
and what is a derived class?
- How to specify inheritance relation between
classes in C++? (
class DerivedClass : <Access>
BaseClass
)
- What in the base class can be inherited by the
derived class? (pp. 620, everything in the public section except
constructors, destructor, and assignment operator)
- The member functions of the derived
class cannot access all private member variables and functions inherited
from the base class!(pp.626)
- Do we need to specify the inherited members from
the base class? (No, they are automatically inherited)
- Can we modify (or redefine, or overwrite) the
inherited members? (Only the inherited member functions)
- If we modify the types of the inherited
variables, it is called hiding not
overwriting.
- How can we initialize the inherited member
*variables* from the base class in the derived class? (pp. 624, in the
constructor, do
DerivedClass(argument list) :
BaseClass(arguments), ...
)
- Can you tell the difference between overwrite and
overload a function? (whether the new function has the same argument list
with the old one or not)
Additional materials:
- Examples shown in the class (cpp1,
cpp2,
cpp3)
- A different employee example (cpp4).
Week 3 (Ch.15)
Polymorphism and virtual functions
Main goal: Get familiar with the
Polymorphism mechanism of OOP and how to achieve that in C++.
Knowledge points that you should
know:
- What is a copy constructor of a class (with the
form "
ClassName(const ClassName & in_obj)
;".
It has to be a reference to the input
object!)
- What does a default copy constructor do? (default
copy constructor is created implicitly, and it does a member-wise
assignment)
- When do you need to explicitly specify a copy
constructor? (when there are pointer type of
the member variables)
- When will a copy constructor be called? (see the
slides)
- What is polymorphism? (same message with
different outcomes from different objects, it is typically
related to inheritance!)
- Why do we need that? (try to understand the
Figure example in the slides)
- Why do we say "polymorphism" can be used to
achieve software reuse? (pay attention to the
encapsulation principle in the Figure example,
try to fix the interface of using certain class and its child classes.)
- How to achieve polymorphism in C++? (there are
multiple ways, but at this moment, we focus on the "virtual
function")
- How to enable the polymorphism offered by virtual
functions? (Use base type of pointers to point to the objects
of derived classes)
- How and where to specify virtual functions? (IN
THE BASE CLASS, use keyword "
virtual
" in front of the
functions that will be overwritten/redefined in
the derived classes)
- How the compiler interpret the virtual functions?
(using "LATE BINDING", i.e. the actual function
that will be called is determined in run time based on the type of the
object)
- How to define a function in the base class that
does not have specific implementation? (using pure virtual
functions virtual return_type function_name(<argument
list>) = 0;)
- How to define an abstract class? (if the class
contains at least one pure virtual function, the class will be
abstract).
Additional materials:
- copy constructor example (cpp)
- polymorphism examples, the Figure example (cpp1)
- the Thing/Animal example (cpp2),
this example also shows the virtual destructor, remove the keyword
"virtual" and see what you will get
- the modified employee (cpp3)
- one simple example of virtual destructor (cpp4)
- an example of pure virtual function
and abstract base class (cpp5)
Week 4 (Ch.16) C++
Stream and File I/O, Template
Main goal: Get familiar with the
exception handling in C++ and another form of polymorphism, i.e., templates.
Knowledge points that you should
know:
- What is a stream? (a flow of data)
- What classes and objects do we use to perform
screen I/O? (istream--cin; ostream -- cout)
- What are the classes that we need for file I/O
processing in C++ (ifstream, ofstream, fstream)
- What is the general framework of file I/O
processing (create file I/O stream objects; connect them with the files;
read from/write to files via the stream objects; close files)
- How to read from an ASCII text file? What
functions can we use? ( operator>>, the overloaded get() functions,
getline(), etc.)
- How to determine the end of a file? (using eof()
function, or EOF, or the failure reading of get() and getline()
functions)
- What is the benefit of using template and its
relation to polymorphism? (Efficient coding, another form of
polymorphism)
- How to define template functions? (using the
template prefix
template<class T>
, here
class
means "type", T is the parameter whose values are the
types, e.g. int, float, char,...)
- How does template work? (create the instance
function based on the type of the variable, see slides for detailed
explanation via examples)
- What other things need to be aware of when using
template functions? (you can have as many the type variables as you
want in the template, but each type
variable has to be used in the function. Please see slides
for more details)
- How to define class template?
(
template<class T> class class_name{};
)
- What is the requirement when defining the member
functions of a class template? (use
class_name<T>::
in
front of each function)
- How to use a class template to create an object?
(use
class_name<specific type> object_name
)
- Can a class template be used to derive new
classes? Any things need to be aware of?
Additional materials:
File I/O examples (cpp1,
cpp2,
cpp3);
Template function examples (cpp1,
cpp2)class
templates (ch 7.3 the std::vector example!)
Week 5 Exception
Handling, UML introduction, Exam 1 reviews
Main goal: Get familiar with C++
exception handling; introduction to the unified modeling language (UML).
Knowledge points that you should
know:
- What is the difference between if condition and
exception handling? (exception handling separates the "exception" from
"normal", but if condition does not)
- What is the benefit of using exception handling?
(separate the exception handling code from the normal code)
- How to implement exception handling?
(try-throw-catch, i.e. normal code in try,
throw exception instance when it is needed, catch the exception instance
and process it, change control flow!)
- What can be the types that
throw
throws (any pre-defined types including complex objects)
- How many parameters can each catch block has? (No
more than one! Each catch captures one type of exceptions)
- What should be the order of multiple catch
blocks? Why? (More specific exceptions go first, more general exceptions go
later because of the automatic type conversion)
- Can we throw an exception inside a function?
(Yes) Who is responsible to handle that? (the function's caller where the
try-catch blocks are defined.)
Additional materials:
Exception handling examples (cpp1,
cpp2,
cpp3,
cpp4)
NOTICE THE NEW
TIME AND LOCATION for Exam1!
Mid-term exam 1
on Feb. 19 (FRIDAY!!) from 9am-11am (location: MH 150)! The exam will cover all
the knowledge points from Week1 to Week4
Week 6 (Java
introduction and Java class)
Main goal: get familiar with the
class definition and usage in Java; understand and know how to use inheritance
in java
Knowledge points that you should
know:
- Background of Java. Do you know who created
Java?
- You need to install the eclipse IDE, it provides
both windows and Mac versions. Do you know how to compile and run a java
program in eclipse?
- Make sure you know how to add and delete a class
in eclipse. Do you know how to compile and run a java program?
- Is jave an OOP language? (yes, everything is in
certain class)
- Why do we say jave code is portable or platform
independent? (the compiler does not produce executable file but rather a
so-called "java byte code" which can be further interpreted by JVM)
- Why do we need to install a java virtual machine
(or JVM)? How does it work? (see above question)
- What is the entrance of a java program and where
should we place it? (main() function, it needs to be defined as a
"
static
" function of a class!)
- In what forms your java programs can be run?
(either stand alone program or java applet)
- How did you define a class in Java? any
difference from what we did in C++? (everything is class. Be careful the
placement of the access modifier)
- Can you have a global variable or function in
Java? Why or why not? (No, every variable and function has to be associated
with a class, even the basic types of variables)
- How do you create an object using the defined
class? (use "new" operator to create an object!)
- Note that there is NO POINTER in
Java!
- Do we need constructor for the classes defined in
Java? Is it different from C++? (We need constructors. They have the same
name to the class name. Their definitions in Java are similar to those in
C++)
- When should we add a no-argument constructor?
(Note that it is not always automatically added for you! It is recommended
to add the no-argument constructor for every class you define.)
- Is there a copy constructor in Java? (Yes)
- Is there a destructor for Java class? (No, Java
will collect the released memory for you. However, this still needs to be
done carefully.)
- What is the alternative way to initialize a
member variable? (You can assign the initial value for each member variable
when you declare it, e.g. "
private int var = 0;
")
- What is the restriction of different access
modifier, including "public", "private", and "protected"? (Similar to C++,
but need to consider package)
- What is the "package access"? Do you know its
difference from "protected"?
- How to use static member variables and functions
in Java? (Similar to C++. NOTE that static member function cannot invoke a
non-static function!)
- Dose Java support function overloading? How to
achieve that? (Yes, Java supports function overloading. You need to have
different signatures for the overloading function)
- Does Java support operator overloading? (NO!)
- Do you know the meaning of "equal(Classname
objectname)" and "toString()"? Where are they from? (From the root class
Object!) Why do we need to override them some time?
- Why do we need inheritance? (Go back to see the
benefit in C++!)
- How to achieve inheritance in Java? (the
"extends" keyword, i.e. newClass extends oldClass)
- What can be inherited from in the new class? (all
public/protected members; and private member variables although you cannot
directly access them by name)
- How to override or redefine an inherited
function? (Similar to C++. They need to have exactly the same
signature)
- Can one change the return type of an overridden
method? (No, except the return type is changed from a base class type to a
derived class type)
- Can one change the access modifier of an
overridden method? (Yes, but can only make it more accessible, e.g. private
-> public, but not public->private)
- How to hide a member variable? (Same in C++)
- Do you know the meaning of "final" members or a
"final" class? (They cannot be overridden or used to derive new
classes!)
Additional materials:
Java passes by value example (.zip)
Jave reference example(.zip)
Week 7 ( Ch.4, Ch.5,
Ch.7, Java inheritance, continued; Java Polymorphism)
Main goal: This week, we will
continue learning Java inheritance mechansim and get started to learn Java
Polymorphism.
Knowledge points that you should
know:
- How to call the constructor in the base class to
initialize the member variables inherited from the base class? (Use "super"
keyword!)
- When and where should you use "super" (It should
be the first call in the constructor of the derived class. It can be used
to call the base version of a overridden method.)
- Do you know the meaning of "this" keyword? (It is
used to call the other constructor in the same class. It also has to be in
the first line of the constructor.)
- Do you know the purpose of "Object" class in
Java? Can you draw a class diagram of Java classes?
- What are the benefits of using "Object" as the
root of all classes in Java?
- Do you know the difference between "is a" and
"has a" relation between objects? (Think about inheritance and object
composition)
- What is the purpose of the "getClass()"
method?
- How to use "instanceof" operator and how is it
different from the "getClass()" method?What is the restriction of different
access modifier, including "public", "private", and "protected"? (Similar
to C++, but need to consider package)
- What is the "package access"? Do you know its
difference from "protected"?
- What is polymorphism? Can you explain it based on
what we learned in C++?
- What is late binding or dynamic binding? Why it
is useful?
- What binding scheme is typically used by Java?
- How to achieve polymorphism in Java? How is it
different from C++?
- What is an abstract method and an abstract
class?
- What is an interface and why is it useful? (An
interface is not a class, but rather useful for polymorphism.)
- How to define an interface? (Use keyword
"interface", all methods do not have
definition. Therefore, an interface does not have any real
functionality)
- How to use interface? (Used as the parameter in
the argument list of a function. Use to define class)
- How to implement an interface? (Define a class to
"implement" the interface)
- Can we derive a new interface from an existing
interface? (Yes! We use "
interface B extends A1, A2, A3
{}
")
- How many interfaces can one class implement? (As
many as needed! We use "
public class class_name implements inf1,
inf2,... {}
")
Additional materials:
Jave inheritance examples (.zip1,
.zip2)
Jave access modifier (.zip)
Figure example (polymorphism) (.zip)
Private methods
cannot be overridden in Java! (see this
example and try to play with the access modifier of the print() function in
the Base class)
Abstract class example (.zip)
In class exercise (Dog
class)
Java extra exercis 2 (.docx)
Week 8 (Ch.8, Ch. 10,
Jave File I/O)
Main goal: Understand the concept
of Abstract class and interface and their usage. We will learn (or revisit in
some sense) how to perform File I/O in java, especially text file processing.
We will also have a quick review for Exam 2!
Knowledge points that you should
know:
- What is a stream? What is an input stream and an
output stream?
- What types of files does a program will typically
handle? (ASCII text files and binary files)?
- Do you know how to write to a text file? (Use
PrintWriter object, and print() and println()
methods)
- How to append to an existing text file? (Use
"
PrintWriter obj = new PrintWriter (Filename, true);
" to
open)
- How to close a file? Why do we need to do that? (
writer_or_reader_obj.close();
)
- What exception can be thrown during opening a
file to write or read? (
FileNotFoundException
)
- How to read from a text file using
Scanner object? (Use "
Scanner StreamObject =new
Scanner(new FileInputStream(FileName));
" to open a text file to
read. )
- What methods can you use to read with a Scanner
object? (
nextInt(), nextDouble(), nextFloat(), nextLine(),
...)
- How to determine the end of a text file using a
Scanner object? (using one of the functions:
hasNextInt(),
hasNextDouble(), hasNextLine()
,...)
- How to read from a text file using a
BufferedReader object? (Use "
BufferedReader
readerObject = new BufferedReader(new FileReader(FileName));
" to
open a text file to read.)
- What methods can you use to read with a
BufferedReader object? (
read()
or readLine()
)
- How to determine the end of a text file using a
BufferedReader object? (using
read()
: if return
-1
; or using readLine()
: if return
null
)
- What is a file path name? When should we
explicitly specify the file path?
- Do not forget to import the proper classes to
your program.
Additional materials:
lecture examples (Ex1,
Ex2)
Challenge: Can you re-implement assignment 2 using
Java?
Mid-term exam 2
on Mar. 11 (FRIDAY!!) from 9am-11am (location: MH 170)! The exam will cover all
the knowledge points after mid-term exam1.
Spring
break (Mar.14~Mar.20)
Main goal: Continue file I/O in
java, read/write binary files, object serialization;
Knowledge points that you should
know:
- Do you still remember what is a stream? Do you
know the hierarchies of different stream classes in Java?
- What is the difference between byte stream and
character stream?
- What is the difference between a binary file and
a text file? How can you tell whether a file is a text file or not?
- How to write to a binary file? (Use
"
ObjectOutputStream outputStreamName = new ObjectOutputStream(new
FileOutputStream(FileName));
" to open a binary file to write)
- What methods can you use to write using a
ObjectOutputStream object? (
writeInt, writeDouble, writeChar,
writeBoolean, writeUTF
, ...)
- How to read from a binary file? (Use
"
ObjectInputStream inStreamName = new ObjectInputStream(new
FileInputStream(FileName));
" to open a binary file to read)
- What methods can you use to read using a
ObjectInputStream object? (
readInt, readDouble, readChar,
readBoolean, readUTF,
...)
- How to determine the end of a binary file? (Use
the "
EOFException
" exception to end a reading loop)
- Do you know the other way to determine the end of
a binary file? (look at the examples provided in the class)
- What is object serialization? Why it is
useful?
- How to achieve object serialization? How to
output and input the serialized objects to and from binary files?
- Do not forget to import the proper classes to
your program.
Additional materials:
Binary file examples (zip)
Object serialization examples (zip)
In class project (docx)
Main goal: Exception handling in
Java; revisit Java Interface and a number of useful interfaces that Java
provides to the programmer; learn how to utilize the mechanism of inner
class
Knowledge points that you should
know:
- What is an exception? Why handling them is
important?
- What is the difference between an error and an
exception?
- What is the format of exception handling?
(
try-throw-catch
trio)
- What can be thrown? (Only the objects of certain
exception class)
- Do you know the hierarchy of the exception class?
How to define a new exception class?
- What need to be paid attention to when defining a
new exception class? (must be a derived class of an existing exception
class. typically have at least two constructors. the
getMessage()
method)
- How many exception can each
catch
block catch? (Exactly one)
- If we have multiple
catch
blocks,
what need to be paid attention to? (Their ordering)
- How to handle the exceptions that are thrown
within a method?
- Do you know the two different ways to handle the
exceptions being thrown in a method?
- What is a
throw
s clause and when we
need to specify that? (specify in the heading of a method that may throw an
exception)
- What need to be aware of when overriding a method
that throws some exceptions? (We need to keep the list of exceptions in the
throw
clause or at least a subset of it)
- What is a java interface? How is it related to
java class?
- What is the benefit of using interfaces?
- An interface is NOT a class but is a type.
- An interface can extend (or inherit from) the
other interface.
- All the methods in an interface are
public
.
- A class can "implements" one or more interfaces
at the same time.
- The class that implements some interfaces will be
an abstract class if it does not provide the function bodies for the
methods of those interfaces.
- An interface cannot have instance variables, but
it can have constant variables.
- Why do we need the
Serializable
interface? How about the Cloneable
interface?
- What is an inner class and an outer class?
- How many
.class
files will be
generated when compiling a class that contains some inner classes?
- How to access the
public
inner
classes?
Additional materials:
Exception handling examples (zip)
Inner class and anonymous class examples (zip)
Week 11 (Swing
I)
Main goal: Understand the mechanism
of GUI program, even-driven-programming, and the basic model of GUI programming
using Swing.
Knowledge points that you should
know:
- What is a GUI program? Why we need it?
- What is the programming style for GUI programs?
(event-driven: objects/components fire events; the associated listener
objects catch and handle the events.)
- What is the difference between event driven
programming and normal programming? (the order of the flow of the program
is determined by the events not the logics of the algorithms)
- What is the relation between
swing
and awt packages? (Swing
is built on top of awt
but not replace awt. Some classes in awt are useful.)
- What is the basic step of creating a GUI program
using
Swing
? (Create container, set layout manager, add
components, associate listeners (need to customize the event handlers),
launch the interface)
- What is a container? Can you name a few examples
of container object from Swing? (All the classes provided in swing package
are derived from the base class
Container
. So they are
containers themselves.)
- What is the out most container of a GUI program
using swing? (a custermized
JFrame
object)
- What is a component? How is it different from the
container? (Container can host components, components can be put in a
container)
- How to achieve hierarchical interface design?
(Containers inside containers, and so on)
- What is a panel? How to create a panel object?
(using
JPanel
object)
- What are the three layout managers that swing
provides? (
FlowLayout
, BorderLayout
, and
GridLayout
)
- What is the default layout manager for a
JFrame
object? (BorderLayout
)
- What is the default layout manager for a
JPanel
object? (FlowLayout
)
- What is the general pipeline for implement a GUI
program using Java Swing? (Design the "look" of the interface, implement
the "action" of the interface, show the interface)
Additional materials:
Many examples shown in the class are from the
textbook (Absolute Java)!!
Extra example for the grid layout with borderlines
(.java)
Animation example (.zip)
A good place to find other swing example [A java
swing tutorial (http://zetcode.com/tutorials/javaswingtutorial/)]
Main goal: continue the
introduction of other standard usage of swing package. If you master the basic
steps of creating a hierarchical GUI program from the previous lectures, the
following will be trivial.
Knowledge points that you should
know:
- Given a desired interface, do you know how to
choose the appropriate layout manager to achieve the desired layout?
- How to implement the event handler for specific
events fired by some interface objects? (Implement the
ActionListener
interface!)
- Which function in the ActionListerner interface
must be implemented? (
public void actionPerformed(ActionEvent
e)
)
- How to distinguish the events fired by different
interface objects? (using the action command string, which can be retrieved
using
String getActionCommand()
function)
- Can you change the action command string for an
interface object? How? (using
setActionCommand(String s)
function)
- How can we add a menu bar to the interface?
(using
JMenuBar
class) How to position the menu bar in the
interface?
- How to create a menu and how to add different
choices to the menu? (using
JMenu
class to create a menu
object, then create and add different menu items using
JMenuItem
class)
- Do you know how to create nested menu? (The menu
object itself can be a menu item and added to another menu object!)
- What is a text field and how to insert it to the
interface? (using
JTextField
class)
- Do you know how to set the width of the text
field? (
em
space!)
- How to convert the using input to the
JTextField
object to its corresponding type, e.g., integer,
float, double, etc. ? (Use their corresponding wrapper classes!)
- What is a text area and how is it different from
a text field? (it enables the input of multiple lines, using
JTextArea
class)
- How to use scroll bar in combination with
JTextArea
objects?
- Do you know how to use
JScrolPane
with a JTextArea
object? (First create a
JTextArea
object, then use it to create a
JScrollPane
object.)
- What is an icon? (a small picture associated with
an
ImageIcon
object)
- Do you know how to insert icons to your
interface? (You can use the image icon to create button, label and menu
item objects! Or, you can add the icons to them after their creation!)
- What is a window listener? (an
object that can handle window events)
- Do you know how to use
WindowListener
?
- How many methods the
WindowListener
interface has? What should be
taken carefully if one wants to implements WindowListener
?
- How to draw user-customized geometry in the
interface? (Use or override the corresponding
paint
function
associated with the components and contains.)
- To draw in a
JFrame
object, override
the paint (Graphics g)
function. The first line of the
overridden function should be super.paint(g);
- To draw in a
JPanel
or other
JComponent
object, override the paintComponent (Graphics
g)
function. The first line of the overridden function should be
super.paintComponent(g);
The Graphics
class has
a number of functions allowing the user to draw different geometry shapes.
They are typically invoked inside the overrided paint function while the
Graphics
object (e.g., g
).
- Please play with the following examples and
go through the additional slides on the blackboard to learn more about the
Graphics class and its usage!
- What is a mouse event? (It is a
MouseEvent
object, e.g., click, press, release, drag, move,
etc.) Why is its handling useful? (To handle user interaction via
mouse!)
- How does Java handle different mouse events? (
via
MouseListener
, MouseMotionListener
, and
MouseWheelListener
interfaces)
- How to develop your customized mouse event
handler? (similarly, define a class that implements the above mouse event
interfaces! Make sure you provide the implementations of all methods!)
- What is a mouse adapter class,
MouseAdapter
? (It is a class that provides the default
implementations of the MouseListener
and
MouseMotionListener
interfaces!)
- Can you handle other events of the GUI program
(e.g., keyboard event, text event, component/container event, etc.)?
Additional materials:
The examples of JTextField, JTexaArea, ImageIcon,
and WindowListener are all from the textbook (Absolute Java)!
Example of Graphics (.zip).
Please do not forget the above animation example!
Example of JOGL (.zip)
Examples of MouseListener (.zip1,
.java,
.zip2)
Week 13 (Final
reviews and final projects)
Main goal: we will review what we
have learned about Java and give you time to work on your final projects.
Week 14 (Term Porject
Presentation)
Guidelines: We will do a final
project presentation for each group. Each group will do a 8~10 minutes
presentation with live demo of their developed systems. All students will
participate to the grading of other groups' projects. Be fair and be critical.
Let us choose our champion!
Final exam will be on
May 6 (Friday!!) from 8:00am-11:00am (location: SEC 100)! The exam will cover
all the material of Java (NO C++ content).
Links to valuable C++ programming resources:
- C++ .com: A great online reference cite for all of your C++ needs.
- www.learcpp.com; with a lot of examples and explanations.
- http://www.cppreference.com/wiki/: Another handy C++ reference.
- C++ FAQ: A great source of answers to frequently asked C+ questions
- cppreference.com
- cplusplus.com
- A simple way to start C++: https://developers.google.com/edu/c++/
Links to valuable Java programming
resources:
- Java coding examples (http://www.java-examples.com/)
- A java swing tutorial
(http://zetcode.com/tutorials/javaswingtutorial/)
- painting in java
(http://zetcode.com/tutorials/javaswingtutorial/painting/)
- Using OpenGL with java (http://en.wikipedia.org/wiki/Java_OpenGL;
http://jogamp.org/jogl/www/)