1 creating classes & applications chapter six. 2 defining classes l class myclassname { // new...
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 variablesTRANSCRIPT
1
Creating Classes & Creating Classes & ApplicationsApplications
Chapter Six
2
Defining ClassesDefining Classes
class MyClassName { // new class
class myClassName extends SuperClass {– // your class is a subclass – class MyRunClass implements
Runnable { .
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
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 = “*”;
5
Class VariablesClass Variables
Use static Examples:
– static int sum;– static final int maxObjects = 10;
Class variables “global…” good throughout 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
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]
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
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 ]
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”
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); } }
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
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;
– } }
14
public static void (String arg[]) { int arr[] = { 1, 3, 4, 5, 1, 1, 7};
//any nums PassByReference test = new
PassByReference( ); int numOnes;
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: [ “);
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 ]
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
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.
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
20
Java Applications are Java Applications are stand alone programs...stand alone programs...
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”
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
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;
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);
} }
25
ERROR: so sum += Integer.parseInt(args[i]); and all is well
java SumAverage 1 2 3 = = Sum 6 Avg 2