Transcript Slide 1

APCS-AB: Java
Miscellaneous Topics:
Snippets we missed in
Chapters 1-6 of book
November 11, 2005
week10
1
More about Classes & Design
• Need to be consistent with our models and
•
design of classes. One way to do this is to use
UML (Unified Modeling Language) diagrams to
visualize programs
Use box to represent class
 Top box - Class name
 Middle box - instance variables
 Bottom box - methods
Song
length : int
artist : Artist
album : Album
play() : void
toString(): String
week10
2
Encapsulation
• The instance data of an object should only be
modified by that object
 Keep the data private
• Make other objects use getter (accessor) and
•
setter (mutator) methods to access and change
data
This guarding of data is called encapsulation
week10
public
private
variables
Violate
encapsulation
Enforce
encapsulation
methods
Provide services
to clients
Support other
methods in class
3
Figure 4.5
Class Relationships
• Dependency (one class “uses” another)
 But how does a class gain access to another object?
• One class can instantiate (create) the other object
• One class can gain access to another by getting that object as
a parameter to a method
 The more classes depend on one another, the more
changes in one class can impact another (which can
be troublesome)
• Classes can depend upon objects of the same
class (ie one object of a class interacts with
another object of the same class)
week10
4
Class Relationships
• Composition (aggregation) - one object is
made up of other objects
 Can be described as a “has-a” relationship
 A special type of dependency
• Next week we will being talking about
inheritance and hierarchical class
relationships…
week10
5
Math Class
• Defined in the java.lang package
• All methods in the Math class are static methods (aka
class methods)
 This means that you do not need an object of the class to call the
methods - you invoke them directly through the name of the class
• Some useful methods:






week10
static double sqrt (double power)
static double pow (double num, double power)
static double random ()
static double cos (double num)
static double sin (double num)
static double tan (double num)
6
static
• Static Methods - invoked by class name, no object
needed
 Can be used when no object state is needed to do an action
 The Binary class that I gave you could have had static methods,
since there were no instance variables in that class
• Static Variables (also called Class Variables) are shared
among all instances of a class
 There is one copy of the variable for all objects in the class (if the
variable is public, then you can have one copy for all classes in
that project)
 This is different from an instance variable in which each instance
(object) has its own version of the variable
week10
7
One use of static
• For a project you may want to have the option of
printing debugging information
 You could put a final static boolean variable in one
class to indicate if debugging should be on or off
• Any other class in that project could then access
that variable (through the class) and use it as an
indication if it should print the information
 The final is so that it can’t be changed once the
program is running
• A control such as this is a called a Singleton
design pattern
 Exactly one item with global access to it
week10
8
enum
• Enumerated type, lists all the possible
values of a variable of the type
 The values given are identifiers
• enum is a special kind of class
 It can be declared either on its own (in BlueJ
when you create a new class, enum is an
option)
 It can be declared as part of a class (used like
an instance variable)
week10
9
enum examples
• Examples:
enum Season {winter, spring, summer, fall}
enum Grade {A, B, C, D, F}
enum STA-grade {Aplus, A, Bplus, B, Cplus, C, Dplus,
D, F}
• Using an enum type:
STA-grade myGrade = STA-grade.Aplus;
Season time = Season.fall;
week10
10
Full enum example
public class IceCream {
enum Flavor{ vanilla, chocolate, strawberry}
Flavor myFlavor;
public void chooseFlavor(Flavor choice){
if(choice.getName() == “vanilla”)
System.out.println(“Chocolate is better”);
else if(choice == Flavor.chocolate)
System.out.println(“You choose wisely”);
else
System.out.println(“Strawberry is ok.”);
}
}
week10
11
Method Overloading
• Having the same method name with
different types of parameters
 String convertToBinary(String s)
 String convertToBinary(char c)
• The compiler knows which method to call
based on the type of the argument that you
pass in
week10
12
Graphical User Interfaces
• To make a GUI, you need at least:
 Components - the object on the screen that displays
information or provides interactivity
• Like button, label, textfield, menu, etc
• (As opposed to the containers with hold and organize
components)
 Events - an object that represents something that has
happened which may be of interest (like user actions
of pressing or typing)
• Most GUI components generate events
 Listeners - an object that waits for an event to occur
and responds in some way (usually that the
programmer defines)
week10
13
To Create a Java program that
uses a GUI
• Steps as specified in book:
• Instantiate and set up the necessary components
• Implement listener classes that define what
•
happens when particular events occur
Establish the relationship between the listeners
and the components that generate the events of
interest
week10
14
ActionListener
• ActionListener is an interface (basically a
contract that says that you have defined all the
methods defined in the interface)
 The ActionListener interface specifies one method
• Any gui item that you create can set a listener:
addActionListener (new MyListener());
• Then you can make an internal class:
private class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e){}
}
• In that method you can do whatever you need to,
print the value, call another method, etc
week10
15
Sudoku and Files
• Scanner class can read in a File
 Scanner sc = new Scanner(new File("myFile"));
• We can get a line of the file and split it up in
tokens based on white space
 Will need to do hasNext() to make sure that there is
another token on the way before you try to actually
read it in
• Then we can look at each of those string tokens
 if it is a period, we know that is an empty cell
 if it is a number, we can make a non-changeable cell
with that number in it
week10
16
Sudoku Project
• Program the Sudoku game as designed in class
 Part 1: Due Monday/Tuesday - You write Sudoku.java
and Cell.java as specified in class
• Monday we will discuss any additional methods you may
discover you need while programming so that everyone has
the same class/method signatures (for ease of testing and
integration)
• Monday night you can debug your code and finish up
anything based on class discussion, Part 1 code due Tuesday
in class.
 Part 2: Due Thursday - I will provide skeleton gui files
early next week (CellPanel.java and SudokuGui.java)
for you to fill in and to make work with Part 1, all of this
will be due on Thursday
week10
17
• Additional specifications for part 2 will be provided next week.