Static Variables ICS 111: Introduction to Computer Science I

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Object Oriented Programming (OOP) Lecture No. 11.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
User-Written Functions
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
Data types, Expressions and assignment, Input from User
Program Style Console Input and Output
Arrays ICS 111: Introduction to Computer Science I
String Input ICS 111: Introduction to Computer Science I
String Output ICS 111: Introduction to Computer Science I
Set & Get Methods ICS 111: Introduction to Computer Science I
Defining Your Own Classes
Command Line Arguments
Introduction to Classes and Objects
Implementing Classes Chapter 3.
Variables, Types, Operations on Numbers
Workshop for Programming And Systems Management Teachers
Variables, Types, Operations on Numbers
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
elementary programming
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Primitives
Method exercises Without IF
CS 144 Advanced C++ Programming January 31 Class Meeting
Unit 3: Variables in Java
Classes and Objects Systems Programming.
Presentation transcript:

Static Variables ICS 111: Introduction to Computer Science I William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "I think there is a world market for maybe five computers.“ Thomas Watson, Chairman of IBM, 1943 11/14/2018 © 2007 William Albritton

Class Feedback On an anonymous (don’t write your name) sheet of paper, answer the following questions Average time working on each assignment Total time studying for the exam List several things that help you learn List several things that make learning difficult List several suggestions for improving the class 11/14/2018 © 2007 William Albritton

Using Static Variables Static variables, or “class variables” DO NOT belong to a particular object, but are variables for the entire class Syntax ClassName.variableName Examples Static variable “out”: System.out.println(“hey”); Static variable “in”: Scanner input = new Scanner(System.in); 11/14/2018 © 2007 William Albritton

Defining Static Variables Typically, static variables of a class are constants (variables that store values that do not change), so the keyword “final” is also used in their declaration Declaration for static variables in & out public class System{ public static final InputStream in; public static final PrintStream out; . . . } 11/14/2018 © 2007 William Albritton

Static Variables of Class Math Class Math has useful mathematical constants (variables that store values that do not change) Two static variables of class math Math.E; //2.718281828459045 Math.PI; //3.141592653589793 Initialization (declaration & instantiation) for static variables E & PI public class Math{ public static final Double E = new Double(2.718281828459045); public static final Double PI = new Double(3.141592653589793); . . . } 11/14/2018 © 2007 William Albritton

Pinball Price Increase The price of pinball games just went up to 50¢! We need to change our program already! In order to make our program more adaptable, we can add constants, so that it is easier to change & maintain the program See PinballGames.java & MyCalculator.java 11/14/2018 © 2007 William Albritton

Terminology Constant Variables that store values (data) that do not change Makes the program more maintainable (easier to change, find & fix bugs, etc.) Compiler will complain if try to assign a constant to another value public class MyCalculator { public static final Integer GAME_PRICE = new Integer(50); ...} MyCalculator.GAME_PRICE = 100; //ERROR! 11/14/2018 © 2007 William Albritton

Defining Static Variables Syntax for initializing (declaring & instantiating) a static variable Note that static variables are declared inside a class, but outside all methods public class ClassName1 { public static final ClassName2 VARIABLE_NAME = new ClassName2(parameters); public static void method1(...){...} public static void method2(...){...} public static void method3(...){...} } 11/14/2018 © 2007 William Albritton

Scope of Static Variables The scope of a variable is where a variable is visible (where it can be used) Scope refers to a program’s line numbers in a text file For static variables, the scope begins after the opening delimiter (curly bracket) of a class & ends at the closing delimiter (curly bracket) of a class Scope of static variables GAME_PRICE, FREEZING, C2F_RATIO, F2C_RATIO are lines 3-47, as they can be used within any method within the MyCalculator.java program 11/14/2018 © 2007 William Albritton

Case Sensitive Note that Java is case sensitive Different variables or constants for a primitive data type or object can have the same name, but different cases Integer dollars = new Integer(10); Integer Dollars = new Integer(20); Integer dOlLaRs = new Integer(30); Integer DOLLARS = new Integer(40); 11/14/2018 © 2007 William Albritton

Java Coding Standard Don’t forget to include comments, especially above methods The comments will describe what the method does, the parameters, & the return value See link to Java Coding Standard for details /**Calculate The Number Of Pinball Games Can Play. * @param dollarParameter Is The Number Of Dollars. * @param centParameter Is The Number Of Cents. * @return The Number Of Games */ public static Integer calculateGameCount(Integer dollarParameter, Integer centParameter) {...} 11/14/2018 © 2007 William Albritton

Class Exercise 1 Trace through the following Java program & write the output See Exercise1.java 11/14/2018 © 2007 William Albritton

Terminology Class definition Client (driver) A program that defines the static variables & static methods for a class For example, MyCalculator.java Client (driver) A program that uses (tests) the class’s static variables & static methods In other words, this program uses static variables & calls static methods & displays appropriate output For example, PinballGames.java & TemperatureConverter.java 11/14/2018 © 2007 William Albritton

Class Definition & Client Contents of a class definition Class name at top Static variables definitions Static method definitions Contents of the client (driver) Client name at top In main() method, use static variables In main() method, call the static methods & display appropriate output 11/14/2018

Client & Class Definition Basic outline of your assignment code Contents of the client Put this class at the top of your program This class has the “public” modifier main() method goes here Name of class is LastNameFirstNameX Contents of a class definition This class does NOT have the “public” modifier Client tells Class Definition what to do 11/14/2018 © 2007 William Albritton

Class Exercise 2 Write 2 Java classes For class #1 (class definition) Store the decimal number 4.0 as a static variable Write a Java static method that has 4 double (decimal) parameters that returns the average of the 4 doubles For class #2 (client) Test the method of class #1 11/14/2018 © 2007 William Albritton

Temperature Converter Here is another example for declaring & using static variables Also, check out the comments above each method formatted according to the Java Coding Standard See TemperatureConverter.java & MyCalculator.java 11/14/2018 © 2007 William Albritton