Title: CS242 Advanced Programming Concepts in Java
1CS242Advanced Programming Concepts in Java
Prof. Searleman jets_at_clarkson.edu
2Announcements
- Clarkson Student Chapter of the ACM
- interest meeting next week
- Clarkson ACM SIGGRAPH IDEA
- first meeting Monday, 9/3/07
3Outline
- Java Basics
- public methods constructors, accessors,
mutators, etc. - instance variable vs. argument vs. local variable
- Class variables/constants static
- Javadoc
- Numbers literals
- Design Issues
- HW1 posted due Thursday, 9/6/07, in class
- Read OODP textbook, Chapter 1
4Recap
- In Java
- Everything (except a primitive) is an object
- An object has an interface
5A simple Circle class
- representation
- center Point
- radius float (primitive type)
- public interface
- constructor(s) Circle(x,y) unit circle
- Circle(radius,x,y)
- float getRadius() Point getCenter()
- void setRadius(float) void setCenter(Point)
- float computeArea()
6Unified Modeling LanguageUML description is a
contract
Type name
Public interface
7- Circle c1 new Circle(1.58f, 10,20)
- float z c1.getRadius()
- Circle c2 new Circle(z, 57, 42)
- float y c2.computeArea()
- Circle c3
- c2 c1
- What is c1? c1 is a reference to an
- instance of the class Circle so is c2
- c3 is a null reference
8Implementing Circle
- private instance variables
- private float radius
- private Point center
- public methods
- constructor(s)
- accessor methods (get)
- mutator methods (set)
- etc.
note must repeat keyword private for each
instance variable
9Class Circle
- Implementing constructors a common problem
occurs when a parameter is the same name as an
instance variable e.g. - public Circle(float radius, int x, int y)
-
- radius ???
- solution use keyword this
10Constructors
- public Circle(float radius, int x, int y)
-
- this.radius radius
- center new Point(x,y)
-
- Its typical to have multiple ways to create an
object the constructors are distinguished by
their signatures
11Accessor methods
- Retrieves the current value of an instance
variable - public float getRadius()
-
- return radius
-
12Mutator methods
- changes the value of an instance variable
- public void setRadius(float newRadius)
-
- this.radius newRadius
-
13Other Methods
- Implementing computeArea()
- need to calculate
- how? Make good use of Javadoc
14PI
- javadoc used to find builtin value for PI
- public static final double PI
- class constant
- (1 per class, not per instance)
- Math.PI note Math is a classname
- Theres a problem in computeArea() with
- return radius radius Math.PI
- Why?
15computeArea method
- public float computeArea()
-
- return radiusradius((float)Math.PI)
-
Java is strongly typed the return type must be
compatible with the computed expression
16Instance Variables vs.local variables
vs.arguments
- public class Circle
-
- public float radius // instance variables
- public Point center
- public void draw(int x, int y) // x,y args
-
- int temp // local variable
17Good OOP Design
- The following rules of thumb are essential to a
good design - "Program to an interface, not to an
implementation." - Hide and abstract as much of your implementation
as possible. - "Favor object composition over inheritance."
- Minimize relationships among objects and organize
related objects in packages.
18Die Class
- constructor Die()
- int roll()
- int topFace()
- Consider implementing roll(). Need a
- random number generator, so
19Class Random
- java.util.Random is a class
- constructors Random(), Random(long),
- methods int next(int), int nextInt(), int
nextInt(int), - so, we can write
- Random rng new Random()
- System.out.println( rng.nextInt() )
20- import java.util.
- public main()
- Random rng new Random()
- / print random int between 1 and 6 /
- System.out.println( rng.nextInt(6) 1 )
- constants in Java
- final int MAX 6
- System.out.println( rng.nextInt(MAX) 1 )
21Design question Should there be 1 RNG used by
every Die instance, or 1 per die?
- public class Die
-
- // instance vars numSides topFace
- private Random rng new Random()
-
22- public static void main(String args)
-
- System.out.println("Roll the first die")
- Die die1 new Die()
- for(int i 1 i lt 10 i)
- System.out.println(die1.rollDie())
- System.out.println("Roll the second die")
- Die die2 new Die()
- for(int i 1 i lt 10 i)
- System.out.println(die2.rollDie())
-
23Alternative class variable
- public class Die
- // instance vars for numSides topFace
- static Random rng new Random()
- A static (or class) variable is created and
initialized when a class is first loaded (at
runtime). There is just one, and it is referred
to by its class name - examples of class variables
- Math.PI approximates PI
- System.out standard output stream
24Some design issues for Die class
- Should there be 1 RNG used by every Die instance,
or 1 RNG for the Die class shared by all
instances? - private Random rng new Random()
- or
- static Random RNG new Random()
25Some design issues for Die class
- What is the initial state of a die?
- same number on top face every time
- or random number
- How to test the class?
- separate test program
- main() as a test program
- JUnit?
26Testing
- be thorough
- test boundary conditions
- test all methods
- test both typical and unlikely combinations
27Coding conventions
- Make it easier for class designers to modify the
source - Part of the grade on your programs will be for
good programming style, coding conventions,
testing, comments, etc.
28Comments
- Write javadoc comments to an applications
programmer (client) - Write // and // comments to people who will be
reading your source code (explain why, not how)
29 Need / for javadoc, not /
- /
- Represents a circle by a center point a
radius - _at_author Janice T. Searleman
- /
- public class Circle
-
- private float radius
- private Point center
- /
- Constructs a circle with radius "aRadius"
- and whose center is given by the point (x,y)
- _at_param aRadius length of the radius
- _at_param x x-coordinate of the center
- _at_param y y-coordinate of the center
- /
- public Circle(float aRadius, int x, int y)
-
-
Must immediately precede the class heading
Must immediately precede the method heading
30Grading Criteria Style
- Incorrect file organization
- Identifiers have misleading or meaningless names
- Missing or inadequate source file comment
- Missing class/interface comment
- Implementation comments are inadequate or
redundant - Naming conventions not followed (file, class,
method, etc.) - Poor indentation/formatting/use of white space
- Code is overly complex or redundant
- Magic numbers used instead of constants
- Unnecessary import statements
31Grading Criteria OO Design
- Did not implement given interface
- Unnecessary classes
- Incorrect use of inheritance/composition
- Improper access modifiers
- Insufficient code reuse
- Missing or incorrect methods (including
constructors) - Nonportable code
- Poor use of static method or variable
32Grading Criteria Correctness Testing
- Die Class
- Does not model 6-sided die adequately, Random
number generator missing or inadequate, etc. - TestDie Class
- Inadequate testing for Die class, Test runs
missing - General
- Poor or missing javadoc comments
- Source files missing in /afs/cu/class/cs242
33Things to remember
- You manipulate objects with references
- When all references to an object are gone, it is
a candidate for garbage collection - You must create an object before it can be used
- You never need to destroy an object