Using Static Methods ICS 111 Introduction to Computer

  • Slides: 18
Download presentation
Using Static Methods • ICS 111: Introduction to Computer Science I – William Albritton

Using Static Methods • ICS 111: Introduction to Computer Science I – William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa • “I have traveled the length and breadth of this country and talked with the best people, and I can assure you that data processing is a fad that won't last out the year. ” – The editor in charge of business books for Prentice Hall, 1957 12/23/2021 © 2007 William Albritton 1

Code Improvements • Let’s “spiff up” the user interface for our programs – Instead

Code Improvements • Let’s “spiff up” the user interface for our programs – Instead of text I/O (input & output), let’s use a GUI (Graphical User Interface) • See Temperature. Converter. GUI. java – See the comments with this program on how to use the class JOption. Pane to display simple GUI dialog windows for I/O (input & output) 12/23/2021 © 2007 William Albritton 2

Static Methods • Static methods, or “class methods” DO NOT belong to a particular

Static Methods • Static methods, or “class methods” DO NOT belong to a particular object, but are methods for the entire class – Syntax • Class. Name. static. Method() – Example • String five = "5"; • Integer number = Integer. parse. Int(five); //5 – Method parse. Int() code looks something like this • public static Integer parse. Int(String a. String. Variable){ //code } 12/23/2021 © 2007 William Albritton 3

Non-Static Methods • Non-Static methods, usually just called “methods” DO belong to a particular

Non-Static Methods • Non-Static methods, usually just called “methods” DO belong to a particular object, – Syntax • variable. Name. non. Static. Method() – Example • String name = "Nami"; • String letter = name. substring(1); // "ami" – Method substring() code looks something like this • public String substring(int an. Integer. Variable){ //code } 12/23/2021 © 2007 William Albritton 4

More Code Improvements • Let’s make the temperature converter program more accurate by using

More Code Improvements • Let’s make the temperature converter program more accurate by using decimal numbers – Instead of Integers (whole numbers), we will use Doubles, which are a way to store decimal numbers • Temperature. Converter. Floats. java 12/23/2021 © 2007 William Albritton 5

Decimal Numbers • A decimal number is known as a float – For “floating

Decimal Numbers • A decimal number is known as a float – For “floating point” • Referring to the decimal point • The class is a Double – For “double precision floating point” • A Double is a kind of float • Just like Integer is a kind of integer • Double is also restricted to a certain range – -1. 7 x 10308 to +1. 7 x 10308 12/23/2021 © 2007 William Albritton 6

Floating Point Numbers • You can initialize a double just like any other object

Floating Point Numbers • You can initialize a double just like any other object – Class. Name variable. Name = new Constructor(); • Double decimal = new Double(123. 456); • Floating point operators – Same as integers (=, +, -, *, /) • No modulus (%) 12/23/2021 © 2007 William Albritton 7

Two Kinds of Division • Integer division – “Cuts-off” digits after the decimal point

Two Kinds of Division • Integer division – “Cuts-off” digits after the decimal point (does not round up) • Integer whole = 1/2; // 0 • Floating point division – Returns a floating point (decimal point) value • Double decimal = 1. 0/2. 0; // 0. 5 – Returns a floating point (decimal point) value • decimal = 1/2. 0; // 0. 5 – This returns an integer and then converts to a float • decimal = 1/2; 12/23/2021 © 2007 William Albritton // 0. 0 8

Class Exercise 1 • What is the output of the Java program? – See

Class Exercise 1 • What is the output of the Java program? – See Exercise 1. java 12/23/2021 © 2007 William Albritton 9

Static Methods of Class Math • Class Math has many useful mathematical operations that

Static Methods of Class Math • Class Math has many useful mathematical operations that we can use – – – Math. abs(x); //absolute value Math. log 10(x); //log base 10 Math. max(x, y); //largest value Math. min(x, y); //smallest value Math. pow(x, y); //x to power of y Math. random(); //random number 0. 0 to 1. 0 • x & y are variables of type double • Most methods return type double 12/23/2021 © 2007 William Albritton 10

Static Variables • Static variables, or “class variables” DO NOT belong to a particular

Static Variables • Static variables, or “class variables” DO NOT belong to a particular object, but are variables for the entire class – Syntax • Class. Name. variable. Name – Examples • Static variable “out”: System. out. println(“hey”); • Static variable “in”: Scanner input = new Scanner(System. in); 12/23/2021 © 2007 William Albritton 11

Static Variables • Typically, static variables of a class are constants (variables that do

Static Variables • Typically, static variables of a class are constants (variables that do not change), so the keyword “final” is also used in their declaration – The declaration for static variables in & out looks something like this • public class System{ public static final Input. Stream in; public static final Print. Stream out; . . . } 12/23/2021 © 2007 William Albritton 12

Static Variables of Class Math • Class Math has useful mathematical constants (variables that

Static Variables of Class Math • Class Math has useful mathematical constants (variables that do not change) – Two static variables of class math • Math. E; //2. 71828459045 • Math. PI; //3. 141592653589793 • Example program using several static methods of class Math – See Largest. Logarithm. java 12/23/2021 © 2007 William Albritton 13

Class Exercise 2 • Write a Java program that asks the user to enter

Class Exercise 2 • Write a Java program that asks the user to enter the radius & calculate the area of a circle – Use JOption. Pane for I/O – Use Math. PI for π – Use Math. pow(x, y) for r 2 12/23/2021 © 2007 William Albritton 14

Class Exercise 3 • • What is the output of the program? Identify the

Class Exercise 3 • • What is the output of the program? Identify the category (static or non-static) of each method & variable in the program – See Exercise 3. java 12/23/2021 © 2007 William Albritton 15

Double & double • Double objects are automatically converted to & from double primitive

Double & double • Double objects are automatically converted to & from double primitive data types • Primitive data type – A built-in data type • • Closely models the computer’s memory Predefined by the Java language Manipulated by built-in operators (+, -, *, /) Do not have methods (very different than objects) • Initialization for primitive data types – data. Type variable. Name = value; • Example: double number = 5. 5; 12/23/2021 © 2007 William Albritton 16

Objects vs. Primitives • Objects 1. The blueprint for objects are the classes from

Objects vs. Primitives • Objects 1. The blueprint for objects are the classes from Java API (not part of Java language) 2. Create (instantiate) using constructors • String name = new String("Nami"); 3. Uses methods to access/manipulate data • first. Letter = name. substring(0, 1); 4. Variable contains an address that points to the object’s data • 12/23/2021 A box pointing to another box containing data © 2007 William Albritton 17

Objects vs. Primitives • Primitive data types 1. The blueprint for primitive data types

Objects vs. Primitives • Primitive data types 1. The blueprint for primitive data types is already built into the Java language (not in API) 2. Create using name of data type (no constructor) • double number = 1. 25; 3. Uses operators to access/manipulate data • number = number + 7. 5; 4. Variable contains the actual data (no address) • 12/23/2021 A box containing data © 2007 William Albritton 18