Escolar Documentos
Profissional Documentos
Cultura Documentos
radius: double
findArea(): double
new Circle()
new Circle()
circle1: Circle
radius = 2
circlen: Circle
...
radius = 5
Class Declaration
public class Circle {
double radius = 1.0;
public double findArea()
{
return radius * radius * 3.14159;
}//end of method
}//end of class
Object declaration
o Declaring object reference variable:
Class_Name objectReference;
Example:
Circle myCircle;
o
Creating object:
CSC238-OOP
Primitive type
int i = 1
Object type
Circle c
reference
c: Circle
Created using
new Circle()
radius = 1
Before:
After:
Before:
After:
c1
c1
c2
c2
c1: Circle
c2: Circle
radius = 5
radius = 9
Accessing object
o Referencing the objects data
objectReference.data
Example: myCircle.radius;
o
CSC238-OOP
Constructor
o A constructor with no parameters is referred to as a default constructor.
o A constructor with parameters is referred to as a parameterized/normal constructor.
o Constructors must have the same name as the class itself.
o Constructors do not have a return typenot even void.
o Constructors are invoked using the new operator when an object is created.
o Constructors play the role of initializing objects.
o Example:
public class Circle {
double radius;
public Circle() {
radius = 1.0;
}//default constructor
public Circle(double r){
radius = r;
}//parameterized constructor
public double findArea(){
return radius * radius * 3.14159;
}//end of method
}
o Example using constructor:
public class TestCircle {
public static void main(String[] args) {
Circle myCircle = new Circle(5.0);
System.out.println("The area of the circle of radius " +
myCircle.radius + " is " + myCircle.findArea());
Circle yourCircle = new Circle();
System.out.println("The area of the circle of radius " +
yourCircle.radius + " is " + yourCircle.findArea());
yourCircle.radius = 100; //modify circle radius
System.out.println("The area of the circle of radius " +
yourCircle.radius + " is " + yourCircle.findArea());
}
}
CSC238-OOP
CSC238-OOP
123.45789345
123.458
CSC238-OOP
Description
Natural number e raised to the power of a
Natural logarithm (base e) of a.
The largest whole number less than or equal to a.
The largest of a and b.
The number a raised to the power of b.
The square root of a.
The sine of a. (Note: all trigonometric functions are
computed in radians)
Table 3.8 page 113 in the textbook contains a list of class methods defined in the Math
class.
2.4 SimpleDateFormat
o The SimpleDateFormat class allows the Date information to be displayed with
various format.
o Table 2.1 page 64 shows the formatting options.
o Example:
Date today = new Date( );
SimpleDateFormat sdf1, sdf2;
sdf1 = new SimpleDateFormat( MM/dd/yy );
sdf2 = new SimpleDateFormat( MMMM dd, yyyy );
sdf1.format(today);
10/31/03
sdf2.format(today);
CSC238-OOP
= new GregorianCalendar();
This table shows the class constants for retrieving different pieces of calendar
information from Date.
name;
new String(Jon Java);
CSC238-OOP
substring
- Assume str is a String object and properly initialized to a String.
- str.substring( i, j ) will return a new string by extracting characters
of str from position i to j-1 where 0 i length of str, 0 j length of str, and
i j.
- If str is programming, then str.substring(3, 7) will create a new string whose
value is gram because g is at position 3 and m is at position 6.
- The original string str remains unchanged.
- Example:
String text = Espresso;
text.substring(6,8);
text.substring(0,8);
text.substring(1,5);
text.substring(3,3);
text.substring(4,2);
so
Espresso
spre
error
length
- Assume str is a String object and properly initialized to a string.
- str.length( ) will return the number of characters in str.
- If str is programming , then str.length( ) will return 11 because there are 11
characters in it.
- The original string str remains unchanged.
indexOf
- Assume str and substr are String objects and properly initialized.
- str.indexOf( substr ) will return the first position substr occurs in str.
- If str is programming and substr is gram , then str.indexOf(substr ) will
return 3 because the position of the first character of substr in str is 3.
- If substr does not occur in str, then 1 is returned.
- The search is case-sensitive.
- Example:
String str;
str = I Love Java and Java loves me. ;
str.indexOf( J )
str2.indexOf( love )
str3. indexOf( ove )
str4. indexOf( Me )
7
21
3
-1
charAt
- Individual characters in a String accessed with the charAt method.
- Example:
String name = "Sumatra";
name.charAt(3);
CSC238-OOP
equals
- Determines whether two String objects contain the same data.
- Example:
String
String
String
String
s1
s2
s3
s4
=
=
=
=
Hello;
hello;
new String(hello);
new String(hello);
s1.equals(s2);
s2.equals(s3);
s3.equals(s4);
o
false
true
true
equalsIgnoreCase
- Determines whether two String objects contain the same data, ignoring the case of
the letters in the String.
- Example:
String s1 = Hello;
String s2 = hello;
s1.equalsIgnoreCase(s2);
true
Method
compareTo
trim
valueOf
startsWith
endsWith
Meaning
Compares the two strings
str1.compareTo(str2)
Removes the leading and trailing spaces
str1.trim()
Converts a given primitive data value to a String
String.valueOf(123.4567)
Return true if a String starts with a specified prefix string.
str1.startsWith(str2)
Returns true if a string ends with a specified suffix string.
str1.endsWith( str2 )
Reference:
Liang, Y. Daniel, Introduction to Java Programming, 8 th Edition, Pearson, 2011.
Wu, C. Thomas, An Introduction to Object Oriented Programming with Java, 4th Edition, Mc Graw Hill, 2006.