Download class IntCell - Valdosta State University

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
Chapter 3
Objects and Classes
Objects
Object – a data type with structure,
state, and operations to access and
manipulate state - an instance of a class
Inheritance – allows us to extend the
functionality of an object.
The class construct
Information hiding – no direct access to the data
parts of an object
Encapsulation – grouping of data (fields) and
operations (methods) with implementation details
hidden
Data and operations are accessed using
the dot operator
Constructor
controls how an object is initialized
same name as the class – no return type
may be overloaded
if not provided, a default is provided that
initializes all data to its default
this reference
a reference to the current object
used to access class members while the
class is currently executing
instance variables
methods
pass current object as a parameter
1.
2.
3.
4.
5.
6.
7.
class IntCell
{
/**
* Get the stored value
* @return the stored value
*/
private int storedValue ;
8.
public IntCell(int n)
{ storedValue = n; }
9.
10.
public int read()
{ return storedValue; }
public void write(int x)
{ storedValue = x ; }
11.
12.
13.
14.
15.
}
1.
public static void main(String [] args)
2.
{
IntCell m = new IntCell();
System.out.println("Initial Value = "+m.read());
3.
4.
5.
}
6.
class IntCell
{
/**
* Get the stored value
* @return the stored value
*/
7.
private int storedValue ;
8.
public IntCell()
{
// this.write(0);
this(10);
}
1.
2.
3.
4.
5.
9.
10.
11.
12.
public IntCell(int n)
{ storedValue = n; }
…………………………..
13.
14.
15.
16.
}
Accessibility of Class
Members
public Protected Package Private
Class itself
Yes
Yes
Yes
Yes
Classes in the
same package
Yes
Yes
Yes
No
Subclasses in a
different package
Yes
Yes
No
No
Non-subclasses in
a different package
Yes
No
No
No
static methods
also called class methods
not associated with an object
used by sending a message to the class
main is an example – used by the
interpreter.
other examples include
Integer.parseInt and Math.abs
static data
shared by all members of a class
only one copy exists and is accessible to
all instances
used for constants – example: MAX_VALUE
in the Integer class
initialized when the class is loaded
static initializer – runs during class loading
static method/data
1.
2.
3.
4.
class A
{
public static double myValue = 0 ;
public double yourValue;
public static void show1()
{
System.out.println(“myValue = “+myValue);
}
public void show2()
{
System.out.println(“yourValue = “+yourValue);
}
5.
6.
7.
8.
9.
10.
11.
12.
13.
}


Note that a static method can access only static variables.
Non-static method can access both static and non-static variables.
static method/data
public static void main()
{
double x ;
A.show1();
//
x=A.myValue;
//
A.show2();
//
x=A.yourValue ; //
}
Initialization Block
class Square
2. {
3.
private static double [] squareRoots = new double [100];
4.
private double [] squareRts = new double[100];
1.
5.
6.
7.
8.
9.
10.
11.
12.
13.
{
System.out.println("Hello 1 ");
for ( int i= 0 ; i< squareRoots.length ; i++ )
squareRoots[i] = Math.sqrt(i);
for ( int i= 0 ; i< squareRts.length ; i++ )
squareRts[i] = Math.sqrt(i);
}
public Square()
{
14.
System.out.println(“Hello 2 ”);
15. }
16. }
Static Initialization Block
1.
2.
3.
4.
class Square
{
private static double [] squareRoots = new double [100];
private double [] squareRts = new double[100];
5.
6.
7.
8.
9.
10.
static
{
System.out.println("Hello 1 ");
for ( int i= 0 ; i< squareRoots.length ; i++ )
squareRoots[i] = Math.sqrt(i);
}
11.
12.
13.
14.
15.
16.
17.
public Square()
{
System.out.println(“Hello 2 ”);
for ( int i= 0 ; i< squareRoots.length ; i++ )
squareRoots[i] = Math.sqrt(i);
}
}
The instanceof Operator

The instanceof operator performs a
run-time test. For example,
if ( obj instanceof Truck)
do something;
else
do something different;

The instanceof operator is typically
used prior to performing a type
conversion.
“Standard” Methods
toString
should format the object data into a String
suitable for printing
equals
the prototype is always public boolean
equals (Object rhs)
default is to compare the reference
locally defined – it should compare the state
Packages



Used to organize similar classes
Examples:
java.io
java.applet
Class C in the package p is specified as
p.C.
Java.util.Date today = new java.util.Date()

To avoid using a full package name, use
the import directive
Packages
Two forms of import directive:
import java.util.Date;
import java.util.*;
In the first form, Date may be used as a
shorthand for a fully qualified class name. In
the second form, all classes in the package
may be abbreviated with the corresponding
class name.
Packages
With the following import directives,
import java.util.Date;
import java.io.*;
we may have
Date today = new Date();
FileReader fr = new FileReader(name);
Package Syntax
the package heading
all package classes should be in the same
directory
the directory has the same name as the
package
compilation is done outside the package.
Final Notes on Packages
The CLASSPATH variable – sets the path
to look at for packages that are imported.
friendly access – default alternative to
public and private – access to
others in the same package
How to Use Javadoc.
How to Use Javadoc.
How to Use Javadoc.
1.
import java.io.*;
2.
/**
* This is the solution of the HW3B
* <p>Title: HW3 Part II </p>
* <p>Description: Answer for HW3 Part II</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Valdosta State University</p>
* @author Jaehoon Seol
* @version 1.0
*/
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
public class CS1302HW3B {
/**
* The main starting point
* @param n hour no input parameter
* @see Your Textbook
* @return none no return value
* @deprecated This method is deprecated due to some reason.
*/
public void show(int n)
{
System.out.println("Test");
}
public static void main(String [] args)


All javadoc comments before import statement will be ignored
The first string after @param should match the variable name of
the parameter. That is
@param h hour information
public void showTime( int h)
{…………..}

@author paragraph will not be generated automatically.
Use:
>javadoc –author filename.java

@version paragraph will not be generated automatically.
Use:
>javadoc –version filename.java