1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.

Slides:



Advertisements
Similar presentations
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.
Advertisements

©2004 Brooks/Cole Chapter 7 Strings and Characters.
1 COMP 110 Inheritance Tabitha Peck M.S. April 16, 2008 MWF 3-3:50 pm Philips 367.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
More About Objects and Methods
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
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 Class Members Wrapper Classes Autoboxing Unboxing.
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Applications in Java Towson University *Ref:
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
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
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.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
A: A: double “4” A: “34” 4.
1 CSE 373 Data Types and Manipulation; Arrays slides created by Marty Stepp © University.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Chapter 4 Mathematical Functions, Characters, and Strings
Strings, StringBuilder, and Character
Static Members and Methods
Static and non-Static Chapter 5.
CMSC 202 Static Methods.
Type Conversion, Constants, and the String Object
Chapter 4: Mathematical Functions, Characters, and Strings
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
Variables, Types, Operations on Numbers
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Classes and Objects Static Methods
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Variables and Constants
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367

2 Announcements No Homework… yet

3 Questions?

4 Today in COMP 110 Static Methods A little review

5 Methods Actions performed by objects –smiley.drawFace(); –this.setColor(“green”); –circumference? –area?

6 Static Methods Methods that do not require an object of any kind –Where have you seen static before? –Invoke method with class name –SmileyClass.circumference(3);

7 Why no object? What data is in the object? When should you create a static method?

8 Writing a static method Add static to the heading public static final double PI = ; public static double circumference(double radius) { return (PI*(radius + radius)); }

9 Writing a static method Cannot do anything that refers to a calling object –Why? public static final double PI = ; private boolean smile = true; public static double circumference(double radius) { smile = false; return (PI*(radius + radius)); } NO

10 Accessing instance variables or methods Create an object public static final double PI = ; private boolean smile = true; public static double circumference(double radius) { Smiley s = new Smiley(); s.drawMouth(false); return (PI*(radius + radius)); }

11 Self-Test Questions Can you invoke a nonstatic method within a static method? Can you invoke a static method within a nonstatic method? Can you reference an instance variable within a static method?

12 The Math Class All methods in this class are static –Don’t create a Math object –Invoke methods using class name Math Math.abs Math.max Math.min Math.round –Predefined constants Math.PI Math.E

13 Math.round(2.3) Math.round(2.7) Math.floor(2.3) Math.floor(2.7) Math.ceil(2.3) Math.ceil(2.7)

14 Type Casting with Math Ceil returns a double –Math.ceil(5.6) return 6.0 What do you do if you want an int? –int num = (int)Math.ceil(5.6);

15 Primitive vs. Class Types Primitive –int, float, char, double –Stored in memory as value Class –String, classes you write –Stored in memory as address

16 Objects in Memory Name: Tom Age: 20 int num float num2 Person p1 Person p2 Name: Jane Age: 23 p2 = p1; 2078 p1.setName(“Joe”); Name: Joe Age: 23 p2.print(); p1.print();

17 Methods with objects as arguments How is an object stored in memory? What is passed as the argument? What happens if you modify an object in your method? What about primitive types?

18 Wrapper Classes What if you want a primitive type to be modified in your method? Convert a primitive type to an “equivalent” class type –Integer n = new Integer(42); –Integer n = 42; EVERY primitive type has a wrapper class

19 Wrapper Classes Constants and Static Methods Numeric Wrapper Classes –Integer.MAX_VALUE; –Double.MIN_VALUE; –Float.parseFloat(“23.4”); –Long.toString(368);

20 Class Character toUpperCase toLowerCase isUpperCase isLowerCase isWhiteSpace isLetter isDigit

21 Algebra Review / Writing code Write static methods –abs(double n), returns double –ceil(float n), returns double –floor(double n), returns double –max(int n1, int n2), returns int –min(float n1, float n2), returns float –round(float n), returns int

22 Wednesday Designing and overloading methods Read 5.3 and 5.4

23 Wednesday