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