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.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Final and Static Keywords. Creating constants  There will be occasions where data items in a program have values that do not change.  Maximum score.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
Enhancing classes Visibility modifiers and encapsulation revisited
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Static members Based on Java tutorial by Oracle: svars.html
Applications in Java Towson University *Ref:
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 5 August 30, 2002.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,
Classes: user-defined types. Organizing method with a class A class is used to organize methods * Methods that compute Mathematical functions * The Scanner.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
Arithmetic, Class Variables and Class Methods Week 11
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Exam tip! str.equals(), str.startsWith(), str.toUpperCase() kbd.next(), kbd.nextLine(), etc Exam tip! str.equals(), str.startsWith(), str.toUpperCase()
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Software Development I/O and Numbers
Interface.
Namespaces, Scopes, Access privileges
CS 302 Week 11 Jim Williams, PhD.
Static and non-Static Chapter 5.
Defining Classes II.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
CSC 113 Tutorial QUIZ I.
Interface.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Chapter 5 Methods.
Objects and Classes Creating Objects and Object Reference Variables
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Building Java Programs
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Classes and Objects Static Methods
In this class, we will cover:
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Final and Abstract Classes
Visibilities and Static-ness
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

CPSC 233 Tutorial 12 March 4/5 th, 2015

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

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

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;

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

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

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

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

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

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

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

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

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)