Download CH06revised

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
Creating Classes & Applications
Chapter Six
1
Defining Classes
class MyClassName { // new class
 class myClassName extends SuperClass
{

– // your class is a subclass
– class MyRunClass implements Runnable { .
2
Defining Instance Variables
class Bicycle extends
PersonPowerVechicle { // this was an
idea from earlier chapter
 String bikeType;
 int chainGear; rearCogs;
 int currentGearFront; current GearRear;
}

– the { } surround four instance variables
3
Constants - What’s Your “Pi”
final float pi = 3.141592; // NO
changes
 final boolean debug = false;
 final int maxsize = 4000;

– final is the keyword - no changes are
allowed to pi, debug or maxsize

final String star = “*”;
4
Class Variables
Use static
 Examples:

– static int sum;
– static final int maxObjects = 10;

Class variables “global…” good
throughout class
5
Creating Methods - Basic Parts
public private protected package
(default) not discussed today “all
modifiers”
 Name the method
 Type it (ie. int etc.)
 list parameters (arguments)
coord(10,10)
 body
 “method’s signiture” = all of the above

6
Example - method definition
returntype methodname (type1 arg1, ...)
}
 int[] makeRange (int lower, int upper)
{...}
 Example The RangeClass class
 Whats important?
 Why is the output The array
[12345678910]

7
class RangeClass {
int[] makeRange(int lower, int upper) {
 for(int i = 0; i < arr.length; i++) {

– arr[i] = lower++; } return arr; }
public static void main(String arg[]) {
 int theArray[];
 RangeClass theRange = new
 RangeClass( );
 //continued next slide

8
Continued...
theArray = theRangemakeRange(1,10);
 System.out.print (“The array: [ “);
 for (int i = 0; i < the Array.length; i++) {

– System.out.print(theArray[i] + “ “); }

}
}
 Book output is correct
 [1 2 3 4 5 6 7 8 9 10 ]

9
The this Keyword - refers to the
current object
t = this.x // the x instance variable for
this object... t has just been assigned the
current value of object x ...which had
better have already been defined,
assigned etc.
 return this; // return current object
 this,myMethod(this) // call mymethod,
defined in this class, and pass it the
current object... You should know what
is “CURRENT”

10
scope , rules of (not the
mouthwash)
When can a variable be referenced
(used)?
 Example
 class ScopeTest {
 int test = 10;
 void printTest ( ) {
 int test = 20;
 System.out.println(“Test = “ + test); } }

11
Passing arguements -parameters
to Methods
The PassByReference class
 Pass by value (a copy - original stays as
is)
 Pass by reference (address passed
anything goes) and usually does...
 Next Slide shows a listing to
demonstrate

12
class PassByReference {
int onetozero( int arg[]) {
 int count = 0;
 for(int i=0; i<arg.length;i++) {

– if (arg[i] = = 1) {
» count++;
» arg[i] = 0; } }
» return count;
–}

}
13
public static void (String arg[]) {
 int arr[] = { 1, 3, 4, 5, 1, 1, 7}; //any
nums
 PassByReference test = new
PassByReference( );
 int numOnes;

14
System.out.print(“Values of the array: [
“);
 for (int i = 0; i < arr.length; i++) {

– System.out.print(arr[i] + “ “); } // num +
space
System.out.println (“ ] “);
 numOnes = test.onetozero(arr);
 System.out.println(“Number of Ones =
“ + numOnes); System.out.print(“New
values of the array: [ “);

15

for (int i = 0; i < arr.length; i++) {
– System.out.print(arr[i] + “ “);
–}
System.out.println(“ ]” + “ “); }
}
 OUTPUT: [ 1 3 4 5 1 1 7 ]
 Number of Ones = 3
 “New” [ 0 3 4 5 0 0 7 ]

16
Class and Instance Variables and
Now....Methods
Class methods are available to any
instance of the class itself - and can be
available to other classes.
 Some class methods can be used
anywhere
 Java Math library as an example:
 float root = Math.sqrt(453.0);
 System.out.print(“Largest x vs. y = “ +
Math.max(x,y)); // returns Biggest of x

17
int count = Integer.parseInt(“42” , 10)
 Example of a wrapper “like a gum
wrapper”
 Not much between your gum and the
protective wrapper but it does keep it
clean.....sort of.

18
Java Applications
public static void main ( String args[] )
{...}
 What does all that stuff mean?

– public available to other classes & objects
– static = this is a class method
– void means the main( ) does NOT return
anything
– main( ) takes one parameter : an array of
strings
– body {...} would normally follow
19
Java Applications are stand alone
programs...
20
Passing Arguments to Java
Programs

java Myprogram arguementOne 2 three
– The space between argumentOne, the 2,
and three is important....
java myprogram Java is cool
 or java myprogram “ Java is waycool”

21
EchoArgs


class EchoArgs {
public static void main(String args[]) {
– for( int i = 0; i < args.length; i++) {
» System.out.println
» (“Argument “ + i + “ : “ + args[i]);} } }




java EchoArgs 1 2 3 jump
1 2 3 jump
java EchoArgs “foo bar” zap teaddle 5
foo bar zap teaddle 5
22
Last application of Chapter
.....Wake up it’s almost time to go
home....
class SumAverage {
public static void main (String
args[]) {
int Sum = 0;
23

for (int i =0; i < args.length; i++) {
– sum += Aargs[i]; }
System.out.println(“Sum is: “ + sum);
 System.out.printlm(“Average is: “ +
(float) sum / Args.length);
}
}

24
ERROR: so sum +=
Integer.parseInt(args[i]); and all is well
 java SumAverage 1 2 3 = = Sum 6 Avg
2

25