Arithmetic, Class Variables and Class Methods Week 11

Slides:



Advertisements
Similar presentations
Variables and Operators
Advertisements

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Developing Software Applications Introduction to Variables & Data Types in VB State Transition Diagrams.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
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;
1 CSC241: Object Oriented Programming Lecture No 07.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C++ Programming: Basic Elements of C++.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Programming Process Programming in Java is an exercise in using pre-defined classes and writing new classes to fill in the gaps A procedure for determining.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Mathematical Calculations in Java Mrs. C. Furman.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Windows Programming Lecture 03. Pointers and Arrays.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
BASIC ELEMENTS OF A COMPUTER PROGRAM
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Arithmetic Operator Operation Example + addition x + y
Method Mark and Lyubo.
OPERATORS (1) CSC 111.
Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Numbers.
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Documentation and Style
Chapter 2: Introduction to C++.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Variables and Operators
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Presentation transcript:

Arithmetic, Class Variables and Class Methods Week 11

CONCEPTS COVERED THIS WEEK Arithmetic Class Variables Class Constants Class Methods

Variables A variable is a location in the computer’s memory that is used to store data A variable has a name, a type and a value associated with it int mark = 10; mark – its name int – its type – what we can store in it, in this case integers value - 10 - what is currently stored in it The statement can be broken down into two separate statements int mark; // declaration mark = 10; // assignment

Variable Declarations Variables must be declared before they are used. To declare a variable we give it a name and a type i.e. what type of data can be stored in it e.g. int mark; double price; String name;

Assignment To put a value in a variable we use the assignment operator = mark = 10; price = 3.64; name = “Fred”;

Arithmetic Operators Addition + Subtraction - Multiplication * Division / For example: int result = 2 + 3 * 4; What value is assigned to result?

Order of Operations int result = 2 + 3 * 4; result is assigned a value of 14 multiplication and division take precedence over addition and subtraction brackets can be used e.g. int result = (2 + 3) * 4; result is now assigned a value of 20

Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number?

Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. public class Triangle { // instance variables private int number; ... // Triangle constructor public Triangle() { number++; System.out.println("Triangle object " + number + " created"); }

Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. This does not work as number is an instance variable and therefore re-initialised to zero each time a Triangle is instantiated/created

Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: We need to create a non-instance count that can be incremented after each new Triangle, and assign its current value to the instance variable number. This is precisely the purpose behind the creation of so-called static, or Class, variables Class variables hold state that is shared in common among all objects of the class.

Programming with Classes CLASS VARIABLES public class Triangle { // class variable private static int count; // instance variables private int number; ... // Triangle constructor public Triangle() { count++; number = count; System.out.println("Triangle object " + number + " created"); }

Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: Create a static variable called count and add 1 to it each time the constructor is called. Assign the value of count to an instance variable called number in the constructor. This does work as count is not re-initialised to zero each time a new Triangle object is created

Programming with Classes CLASS VARIABLES Triangle count 3 is instance of… is instance of… is instance of… triangle1: Triangle number 1 height 30 width 40 color “green” triangle2: Triangle number 2 height 30 width 40 color “green” triangle3: Triangle number 3 height 30 width 40 color “green”

Programming with Classes CLASS CONSTANTS Class constants are class variables that are given a fixed value using the keyword final. They should be written in UPPERCASE with underscores between the words that make up compound variable names. For example: In a class that represents a bouncing ball it might be useful to declare a constant as follows: private static final double GRAVITATIONAL_ACCELERATION = 9.8; There is a Flash demo of bouncing balls at: http://funwithstuff.com/dswmedia/bouncing_ball_v3.html

Programming with Classes CLASS METHODS How can we access the value of a class variable? We could use an object method but this would require that an object had already been created and we had its reference. Also, why should we have to create an object to reference a class variable? It seems rather odd to create a BouncingBall object in order to find the value of gravity that will effect ALL bouncing balls.

Programming with Classes CLASS METHODS How can we access the value of a class variable? A much better solution would be to have a method that related to the BouncingBall class, rather than to a specific BouncingBall object, as this method has no interest in any specific object’s state. Such methods are called Class Methods (they are also often referred to as Static Methods), and they are defined as part of a class definition in the same way as object methods.

Programming with Classes CLASS METHODS public class Triangle { private static int count; // class variable private int number; // instance variable // Triangle constructor public Triangle() { count++; number = count; } // get number of Triangle objects created public static int getCount() { return count;

A look at class methods/variables in the Triangle class BlueJ Demonstration A look at class methods/variables in the Triangle class

CLASS VARIABLES AND METHODS Programming with Classes CLASS VARIABLES AND METHODS Class variables hold state that is shared in common among all objects of class Class methods act on attributes of the whole class through class variables Class methods can’t access instance variables