COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
More About Objects and Methods
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Numerical Data Recitation – 01/30/2009
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Applications in Java Towson University *Ref:
COMP 110 Computer Basics Luv Kohli August 25, 2008 MWF 2-2:50 pm Sitterson
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 6, page 1 Sun Certified Java 1.4 Programmer Chapter 6 Notes Gary Lance
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Lecturer: Dr. AJ Bieszczad Chapter 5 COMP 150: Introduction to Object-Oriented Programming 5-1 l Programming with Methods l Static Methods and Static Variables.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Java Data Types.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
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.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
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.
Chapter 4 Mathematical Functions, Characters, and Strings
Static Members and Methods
Computer Programming Methodology Input and While Loop
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
Static and non-Static Chapter 5.
CMSC 202 Static Methods.
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Classes and Objects Miscellany: Statics, Wrappers & Packages
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 3, cont Sept 20, 2004.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Questions? Math Class Wrapper Classes Writing / Testing Methods.
Chapter 5 Methods.
Variables, Types, Operations on Numbers
Objects and Classes Creating Objects and Object Reference Variables
Variables, Types, Operations on Numbers
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
Classes and Objects Static Methods
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Using java libraries CGS3416 spring 2019.
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Program 3 due Friday, October 31, 2pm 2

Questions? 3

Today in COMP 110 Some notes about documentation Static variables and methods 4

Instance variables and methods Instance variables private int age; private String name; Methods public int getAge() { return age; } Calling methods on objects Student std = new Student(); std.setAge(20); System.out.println(std.getAge()); 5

static static 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

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

static, some examples static 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

static version of pow method public 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

static Static variables and methods can be accessed using the class name itself: ◦DimensionConverter.FEET_PER_YARD ◦int z = MathUtilities.pow(2, 4); 10

Will this code compile? public class SomeClass { public static final double PI = ; private boolean sunny = true; public static double area(double radius) { sunny = false; return PI * (radius * radius); } } Code will not compile static methods are invoked without an object ◦ no access to instance variables or non-static methods 11 ERROR!

Will this code compile? public class SomeClass { public static final double PI = ; public int data = 12; private void printData() { System.out.println(data); } public static double area(double radius) { printData(); return PI * (radius * radius); } 12 ERROR!

Will this code compile? public class SomeClass { public static final double PI = ; 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

Calling a nonstatic method from a static method public class SomeClass { public static final double PI = ; 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

Self-test questions Can 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

main is a static method import 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

static helper methods import 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

The 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

Using Math’s methods and constants public 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

Math.round Math.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

Math.floor Math.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

Math.ceil Math.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

What if you want an int? Math.ceil returns a double ◦ Math.ceil(5.6) returns 6.0 int num = (int) Math.ceil(5.6); 23

Wrapper classes All primitive types have an associated wrapper class We have seen some of these before (where?): ◦ Byte ◦ Short ◦ Integer ◦ Long ◦ Float ◦ Double ◦ Character ◦ Boolean 24

Wrapper classes int num = Integer.parseInt(“36”); Integer.parseInt, Double.parseDouble, etc. are all static methods These wrapper classes also have nonstatic methods 25

Wrapper classes You 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

Wrapper classes Useful static constants and methods Examples: ◦ Integer.MAX_VALUE ◦ Double.MIN_VALUE ◦ Float.parseFloat(“23.7”); ◦ Long.toString(368); 27

class Character Character.toUpperCase Character.toLowerCase Character.isUpperCase Character.isLowerCase Character.isWhiteSpace Character.isLetter Character.isDigit 28

Friday Program 3 due, 2pm Lab practice 29