1 creating classes & applications chapter six. 2 defining classes l class myclassname { // new...

25
Creating Classes & Creating Classes & Applications Applications Chapter Six

Upload: blaise-stone

Post on 20-Jan-2018

213 views

Category:

Documents


0 download

DESCRIPTION

3 Defining Instance Variables l class Bicycle extends PersonPowerVechicle { // this was an idea from earlier chapter l String bikeType; l int chainGear; rearCogs; l int currentGearFront; current GearRear; } –the { } surround four instance variables

TRANSCRIPT

Page 1: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

1

Creating Classes & Creating Classes & ApplicationsApplications

Chapter Six

Page 2: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

2

Defining ClassesDefining Classes

class MyClassName { // new class

class myClassName extends SuperClass {– // your class is a subclass – class MyRunClass implements

Runnable { .

Page 3: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

3

Defining Instance Defining Instance VariablesVariables 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

Page 4: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

4

Constants - What’s Your Constants - What’s Your “Pi”“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 = “*”;

Page 5: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

5

Class VariablesClass Variables

Use static Examples:

– static int sum;– static final int maxObjects = 10;

Class variables “global…” good throughout class

Page 6: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

6

Creating Methods - Basic Creating Methods - Basic PartsParts 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

Page 7: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

7

Example - method Example - method definitiondefinition returntype methodname (type1

arg1, ...) } int[] makeRange (int lower, int

upper) {...} Example The RangeClass class Whats important? Why is the output The array

[12345678910]

Page 8: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

8

class RangeClass {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

Page 9: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

9

Continued...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 ]

Page 10: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

10

The this Keyword - refers The this Keyword - refers to the current objectto 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”

Page 11: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

11

scope , rules of (not the scope , rules of (not the mouthwash)mouthwash) When can a variable be referenced

(used)? Example class ScopeTest { int test = 10; void printTest ( ) { int test = 20; System.out.println(“Test = “ + test); } }

Page 12: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

12

Passing arguements -Passing arguements -parameters to Methodsparameters 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

Page 13: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

13

class PassByReference {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;

– } }

Page 14: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

14

public static void (String arg[]) { int arr[] = { 1, 3, 4, 5, 1, 1, 7};

//any nums PassByReference test = new

PassByReference( ); int numOnes;

Page 15: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

15

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: [ “);

Page 16: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

16

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 ]

Page 17: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

17

Class and Instance Class and Instance Variables and Variables and Now....MethodsNow....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 or y

Page 18: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

18

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.

Page 19: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

19

Java Applications 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

Page 20: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

20

Java Applications are Java Applications are stand alone programs...stand alone programs...

Page 21: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

21

Passing Arguments to Java Passing Arguments to Java ProgramsPrograms 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”

Page 22: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

22

EchoArgsEchoArgs

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

Page 23: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

23

Last application of ChapterLast application of Chapter.....Wake up it’s almost .....Wake up it’s almost time to go home....time to go home....class SumAverage {class SumAverage {public static void main public static void main (String args[]) {(String args[]) {

int Sum = 0;int Sum = 0;

Page 24: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

24

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);

} }

Page 25: 1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class…

25

ERROR: so sum += Integer.parseInt(args[i]); and all is well

java SumAverage 1 2 3 = = Sum 6 Avg 2