comp 110 static variables and methods luv kohli october 29, 2008 mwf 2-2:50 pm sitterson 014
DESCRIPTION
Questions? 3TRANSCRIPT
![Page 1: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/1.jpg)
COMP 110COMP 110Static variables and methodsStatic variables and methods
Luv KohliOctober 29, 2008
MWF 2-2:50 pmSitterson 014
![Page 2: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsProgram 3 due Friday, October 31, 2pm
2
![Page 3: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/4.jpg)
Today in COMP 110Today in COMP 110Some notes about documentation
Static variables and methods
4
![Page 5: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/5.jpg)
Instance variables and methodsInstance variables and methodsInstance variables
private int age;private String name;
Methodspublic int getAge(){ return age;}
Calling methods on objectsStudent std = new Student();std.setAge(20);System.out.println(std.getAge());
5
![Page 6: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/6.jpg)
staticstaticstatic variables and methods belong to a class
as a whole, not to an individual object
Where have we seen static before?
When would you want a method that does not need an object to be called?
6
![Page 7: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/7.jpg)
What about a pow method?What about a pow method?// Returns x raised to the yth power, where y >= 0.public int pow(int x, int y){ int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result;}
Do we need an object to call this method?
7
![Page 8: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/8.jpg)
staticstatic, some examples, some examplesstatic constants and variables
◦ private static final int FACE_DIAMETER = 200;◦ public static final int FEET_PER_YARD = 3;◦ private static int numberOfInvocations;
static methods◦ public static void main(String[] args)◦ public static int pow(int x, int y)
8
![Page 9: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/9.jpg)
staticstatic version of pow method version of pow methodpublic class MathUtilities{ // Returns x raised to the yth power, where y >=
0. public static int pow(int x, int y) { int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result; }}
9
static keyword
![Page 10: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/10.jpg)
staticstaticStatic variables and methods can be
accessed using the class name itself:◦ DimensionConverter.FEET_PER_YARD◦ int z = MathUtilities.pow(2, 4);
10
![Page 11: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/11.jpg)
Will this code compile?Will this code compile?public class SomeClass{ public static final double PI = 3.14159; private boolean sunny = true;
public static double area(double radius) { sunny = false; return PI * (radius * radius); }}
Code will not compilestatic methods are invoked without an object◦ no access to instance variables or non-static methods
11
ERROR!
![Page 12: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/12.jpg)
Will this code compile?Will this code compile?public class SomeClass{ public static final double PI = 3.14159; public int data = 12;
private void printData() { System.out.println(data); }
public static double area(double radius) { printData(); return PI * (radius * radius); }}
12
ERROR!
![Page 13: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/13.jpg)
Will this code compile?Will this code compile?public class SomeClass{ public static final double PI = 3.14159;
private void printPi() { System.out.println(PI); System.out.println(area(3.0)); }
public static double area(double radius) { return PI * (radius * radius); }}
Nonstatic methods CAN call static methods and access static variables
13
![Page 14: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/14.jpg)
Calling a nonstatic method from a static methodCalling a nonstatic method from a static method
public class SomeClass{ public static final double PI = 3.14159;
private void printPi() { System.out.println(PI); System.out.println(area(3.0)); }
public static double area(double radius) { SomeClass sc = new SomeClass(); sc.printPi(); return PI * (radius * radius); }}
14
![Page 15: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/15.jpg)
Self-test questionsSelf-test questionsCan you call a nonstatic method from a
static method?
Can you call a static method from a nonstatic method?
Can you access an instance variable inside a static method?
15
![Page 16: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/16.jpg)
main is a main is a staticstatic method methodimport java.util.*;public class MyClass{ public static void main(String[] args) { System.out.println(“Give me a number, and I will ”
+ “tell you its square and its square’s
square.”); Scanner kb = new Scanner(System.in); int num = kb.nextInt(); int numSquared = num * num; System.out.println(“The square is ” + numSquared); int numSquaredSquared = numSquared * numSquared; System.out.println(“The square’s square is ” + numSquaredSquared); }}
16
![Page 17: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/17.jpg)
staticstatic helper methods helper methodsimport java.util.*;public class MyClass{ public static int square(int x) { return x * x; }
public static void main(String[] args) { System.out.println(“Give me a number, and I will ” + “tell you its square and its square’s square.”); Scanner kb = new Scanner(System.in); int num = kb.nextInt(); int numSquared = square(num); System.out.println(“The square is ” + numSquared); int numSquaredSquared = square(numSquared); System.out.println(“The square’s square is ” + numSquaredSquared); }}
17
![Page 18: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/18.jpg)
The Math classThe Math class Provides many standard mathematical methods, all static
◦ do not create an instance of the Math class to use its methods Call Math class’ methods using class name
◦ Math.abs◦ Math.max◦ Math.min◦ Math.pow◦ Math.round◦ Others
Predefined constants◦ Math.PI◦ Math.E
18
![Page 19: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/19.jpg)
Using Math’s methods and constantsUsing Math’s methods and constantspublic static double largeToSmallthPower(int a, int
b){ double small = Math.min(a, b); double large = Math.max(a, b); return Math.pow(large, small);}
public static double area(double radius){ return Math.PI * (radius * radius);}
19
![Page 20: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/20.jpg)
Math.roundMath.roundMath.round: returns closest long (or int,
if using a float) to argument◦Math.round(2.3)
Returns 2◦Math.round(2.7)
Returns 3
20
![Page 21: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/21.jpg)
Math.floorMath.floorMath.floor: returns largest double value
less than or equal to argument and equal to a mathematical integer◦Math.floor(2.3)
Returns 2.0◦Math.floor(2.7)
Returns 2.0
21
![Page 22: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/22.jpg)
Math.ceilMath.ceilMath.ceil: returns smallest double value
greater than or equal to argument and equal to a mathematical integer◦Math.ceil(2.3)
Returns 3.0◦Math.ceil(2.7)
Returns 3.0
22
![Page 23: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/23.jpg)
What if you want an What if you want an intint??Math.ceil returns a double◦Math.ceil(5.6) returns 6.0
int num = (int) Math.ceil(5.6);
23
![Page 24: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/24.jpg)
Wrapper classesWrapper classesAll primitive types have an associated wrapper
classWe have seen some of these before (where?):◦ Byte◦ Short◦ Integer◦ Long◦ Float◦Double◦ Character◦ Boolean
24
![Page 25: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/25.jpg)
Wrapper classesWrapper classesint num = Integer.parseInt(“36”);
Integer.parseInt, Double.parseDouble, etc. are all static methods
These wrapper classes also have nonstatic methods
25
![Page 26: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/26.jpg)
Wrapper classesWrapper classesYou can create an instance of a wrapper
class and use the instance to convert the value to different types.
Example:◦ Integer num = new Integer(36);◦ double numAsDouble = num.doubleValue();
26
![Page 27: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/27.jpg)
Wrapper classesWrapper classesUseful static constants and methodsExamples:◦ Integer.MAX_VALUE◦Double.MIN_VALUE◦Float.parseFloat(“23.7”);◦Long.toString(368);
27
![Page 28: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/28.jpg)
class Characterclass CharacterCharacter.toUpperCaseCharacter.toLowerCaseCharacter.isUpperCaseCharacter.isLowerCaseCharacter.isWhiteSpaceCharacter.isLetterCharacter.isDigit
28
![Page 29: COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.mx/reader035/viewer/2022062600/5a4d1b1e7f8b9ab05999442d/html5/thumbnails/29.jpg)
FridayFridayProgram 3 due, 2pm
Lab practice
29