Download (UML) for Circle class( Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Professor Sana Odeh, New York University
UML for class Circle
Unified Modeling Language (UML)
for Circle class( Class with constructors )
See the code for Crcle.java and TestCircle.java on the next pages!
Class Name
List of object’s fields
(instance and static variables)
List of constructor’s,
instance and static
methods.
Circle
- radius: double
- numOfCircles:int
+ Circle()
+ Circle(r: double)
+ getArea():double
+ getRadius():double
+ getNumOfCircles():int
UML for objects of Class Circle:
In this example, we provided the UML for two objects (circle1 and circle2):
Object Name
(reference variable)
List of object’s fields (instance
and static variables)
Here you have to include the
values of these fields
circle1:Circle
radius: 1.0
numOfCircles: 1
circle2:Circle
radius: 4.5
numOfCircles: 2
Notes about UML notation used for fields and methods in this example:
1) Use + for public fields and methods
2) Use – for private fields and methods
3) Underline static fields/variables and static methods
Professor Sana Odeh, New York University
UML for class Circle
/* Circle.java: The circle class with private fields, accessor methods
(getters), setters and static variables and methods */
public class Circle
{
//The radius of the circle is private (can be accessed by this class only
private double radius;
private static int numOfCircles = 0;
/** constructors should be public. */
public Circle()
{
radius = 1.0;
numOfCircles++;
}
/** 2nd Constructor (take an argument) to construct a circle with a
specified radius */
public Circle(double r)
{
radius = r;
numOfCircles++;
}
/** Return radius using a getter or (accessor) */
public double getRadius()
{
return radius;
}
/** Set a new radius using a setter (Mutator)*/
public void setRadius(double newRadius)
{
radius = newRadius;
}
// Return number of objects created by this class (static method)
// can only manipulate static objects
public static int getNumOfCircles ()
{
return numOfCircles;
}
/** Return the area of this circle */
public double findArea()
{
return radius * radius * 3.14159;
}
} // end of class Circle
Professor Sana Odeh, New York University
UML for class Circle
// TestCircle.java: Demonstrate private modifier, getters, setters, static
public class TestCircle
{
/** Main method */
public static void main(String[] args)
{
// Create a Circle with radius 5.0
Circle circle1 = new Circle(5.0);
System.out.println("The area of the circle of radius "
+ circle1.getRadius() + " is " + circle1.findArea());
// Increase myCircle's radius by 10%
circle1.setRadius(circle1.getRadius() * 1.1);
System.out.println("The area of the circle of radius "
+ circle1.getRadius() + " is " + circle1.findArea());
Circle circle2 = new Circle(3.0);
// Print info for object circle2
System.out.println("The area of the circle of radius "
+ circle1.getRadius() + " is " + circle1.findArea());
System.out.println();
// Print number of objects (circles) instantiated so far
System.out.println("The number of objects (circles) instantiated/created
so far is: " + Circle.getNumOfCircles());
}
}// end of class TestCircle
Output:
The area of the circle of radius 5.0 is 78.53975
The area of the circle of radius 5.5 is 95.0330975
The area of the circle of radius 5.5 is 95.0330975
The number of objects (circles) instantiated/created so far is: 2
Related documents