Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.

Similar presentations


Presentation on theme: "CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1."— Presentation transcript:

1 CPSC 233 Tutorial 12 March 4/5 th, 2015

2 TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1

3 TopHat Quiz int[] a = {0}; int[] b = {0}; What is the result of (a == b) i)True ii)False

4 Static Variables Static variables belongs to the class Class variables Shared by all objects of the class Usually private Use keyword “static” private static int mystatic;

5 Example-1 MyStatic.java class keeps track of the total number of method calls across all of its objects. Refer MyStatic.java Objective: Demonstrate use of static variables in communicating across objects of the same class

6 What will be O/P? public class MyStaticTest { public static void main(String[] args) { MyStatic obj1 = new MyStatic(); MyStatic obj2 = new MyStatic(); System.out.printl(obj1.getCount()); System.out.println(obj2.getCount()); }

7 Static Defined Constants Declaration includes keywords static final Public static final double mystatic_constant = 3.14; Cannot change value Can be referred outside the class using class name, provided variable is public

8 Example-2 Write a java class (say Circle ) which computes the area of the circle, for a given radius. Use pi=3.14 as a static constant in the class. Your class should have radius as an instance variable and a constructor for the initialization. Also write a test class with main method. The main method should print the area and the pi value used for the computation. Objective: Demonstrate use of static constants and how it can be referred from another class using class name rather than through object

9 Static Methods Like static variables, static methods belongs to the class public static int getInterestrate() Can be invoked using class name Not associated with any calling object. Hence, within a static method: Cannot refer any non-static instance variable Cannot call any non-static method Can refer static variables and call static methods

10 Example-3 Modify Circle class such that it now has a static variable that keeps track of the number of circles created and a static get method that returns this count value. Everything else remain the same. Objective: Demonstrate mixing of static and non-static methods; accessing static method using class name

11 Math Class Collection of static methods that do standard mathematical operations Math.max(25, 56) // returns the maximum value Math.round(6.8) // returns 7 Math.ceil(3.2) // returns 4.0 Math.pow(2.0,3.0) // returns 8.0 No object of Math class is created Two static constants PI and E No import statement required

12 Example-4 Modify example-3 to use static constant PI. Also, use pow method defined in the Math class to compute the area. Objective: Demonstrate use of math class methods and accessing static methods/variables using class names

13 Example-5 Write your own math class (say ‘mymath’) that has static methods to compute the following LCM of two numbers Maximum Minimum Test the class Assume that inputs are whole numbers greater than zero and are always valid (no error checking)


Download ppt "CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1."

Similar presentations


Ads by Google