A final variable is a constant

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Interfaces and Polymorphism.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter Goals To learn how to choose appropriate classes to implement
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
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 © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 4 – Fundamental Data Types.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 3 – Fundamental Data Types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To recognize.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Review 2.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Static. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static method does not operate on an object double dY.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Penny Nickel Dime Penny Nickel Dime Quarter Half Dollar.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types.
12 OBJECT-ORIENTED DESIGN CHAPTER
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 4 – Fundamental Data Types.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Slides by Evan Gallagher
Slides by Evan Gallagher
John Woodward A Simple Program – Hello world
Lecture 3 John Woodward.
Chapter Goals To understand integer and floating-point numbers
Chapter Goals To understand integer and floating-point numbers
5.3 Multiple Alternatives (Part 1)
Chapter 4 – Fundamental Data Types
Chapter Three - Implementing Classes
Chapter Three - Implementing Classes
Chapter 4 – Fundamental Data Types
Chapter 6 Methods: A Deeper Look
Introduction to Computer Science and Object-Oriented Programming
Chapter 5 – Decisions Big Java by Cay Horstmann
Principles of Computer Science I
Use a variable to store a value that you want to use at a later time
4.3 Arithmetic Operators and Mathematical Functions
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
Money Math Review.
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
Presentation transcript:

A final variable is a constant 4.2 Constants A final variable is a constant Once its value has been set, it cannot be changed Named constants make programs easier to read and maintain Convention: Use all-uppercase names for constants final double QUARTER_VALUE = 0.25; final double DIME_VALUE = 0.1; final double NICKEL_VALUE = 0.05; final double PENNY_VALUE = 0.01; payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Constants: static final If constant values are needed in several methods, declare them together with the instance fields of a class and tag them as static and final Give static final constants public access to enable other classes to use them public class Math { . . . public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846; } double circumference = Math.PI * diameter; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 4.1 Constant Definition Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

ch04/cashregister/CashRegister.java Continued 1 /** 1 /** 2 A cash register totals up sales and computes change due. 3 */ 4 public class CashRegister { 5 public static final double QUARTER_VALUE = 0.25; 6 public static final double DIME_VALUE = 0.1; 7 public static final double NICKEL_VALUE = 0.05; 8 public static final double PENNY_VALUE = 0.01; 9 10 private double purchase; 11 private double payment; 12 13 /** 14 Constructs a cash register with no money in it. 15 */ 16 public CashRegister() { 17 purchase = 0; 18 payment = 0; 19 } 20 Continued Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

ch04/cashregister/CashRegister.java (cont.) 21 /** 22 Records the purchase price of an item. 23 @param amount the price of the purchased item 24 */ 25 public void recordPurchase(double amount) { 26 purchase = purchase + amount; 27 } 28 29 /** 30 Enters the payment received from the customer. 31 @param dollars the number of dollars in the payment 32 @param quarters the number of quarters in the payment 33 @param dimes the number of dimes in the payment 34 @param nickels the number of nickels in the payment 35 @param pennies the number of pennies in the payment 36 */ 37 public void enterPayment(int dollars, int quarters, 38 int dimes, int nickels, int pennies) { 39 payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE 40 + nickels * NICKEL_VALUE + pennies * PENNY_VALUE; 41 } 42 Continued Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

ch04/cashregister/CashRegister.java (cont.) 43 /** 44 Computes the change due and resets the machine for the next customer. 45 @return the change due to the customer 46 */ 47 public double giveChange() { 48 double change = payment - purchase; 49 purchase = 0; 50 payment = 0; 51 return change; 52 } 53 } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

ch04/cashregister/CashRegisterTester.java 1 /** 1 /** 2 This class tests the CashRegister class. 3 */ 4 public class CashRegisterTester { 5 public static void main(String[] args) { 6 CashRegister register = new CashRegister(); 7 8 register.recordPurchase(0.75); 9 register.recordPurchase(1.50); 10 register.enterPayment(2, 0, 5, 0, 0); 11 System.out.print("Change: "); 12 System.out.println(register.giveChange()); 13 System.out.println("Expected: 0.25"); 14 15 register.recordPurchase(2.25); 16 register.recordPurchase(19.25); 17 register.enterPayment(23, 2, 0, 0, 0); 18 System.out.print("Change: "); 19 System.out.println(register.giveChange()); 20 System.out.println("Expected: 2.0"); 21 } 22 } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

ch04/cashregister/CashRegisterTester.java (cont.) Program Run: Change: 0.25 Expected: 0.25 Change: 2.0 Expected: 2.0 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 4.4 What is the difference between the following two statements? final double CM_PER_INCH = 2.54; and public static final double CM_PER_INCH = 2.54; Answer: The first definition is used inside a method, the second inside a class.

Self Check 4.5 What is wrong with the following statement sequence? double diameter = . . .; double circumference = 3.14 * diameter; Answer: You should use a named constant, not the “magic number” 3.14. 3.14 is not an accurate representation of π.