Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Intro to Computer Science I Chapter 2 Fundamental Data Types Java scripting with BeanShell.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
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.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
Java Data Types Assignment and Simple Arithmetic.
Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
Mathematical Calculations in Java Mrs. C. Furman.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Primitive and Reference Data Values
Computing with C# and the .NET Framework
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
OPERATORS (2) CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Chapter 2: Basic Elements of Java
Introduction to Computer Science and Object-Oriented Programming
Primitive Types and Expressions
Data Types and Expressions
Introduction to Computer Science and Object-Oriented Programming
Data Types and Expressions
Presentation transcript:

Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 5 Topics Categories of Variables Implicit and Explicit Method Parameters Number Types Constants Assignment, Increment, and Decrement Arithmetic Operations and Mathematical Functions Calling Static Methods

5.1.1 Categories of Variables Instance fields Local variables Parameter variables All hold values. Difference is lifetime. Instance field will exist so long as there is a reference to the object it belongs to. Parameter and local variables come to life when method is called, and die after call.

5.1.1 Categories of Variables Cont. public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; }... balance: instance field amount: parameter variable newBalance: local variable Example: harrysChecking.deposit(500); Method is called, amount is created and set to 500, newBalance is created and set to balance + amount, balance is set to newBalance. After method call, amount and newBalance dies, balance still lives.

5.1.2 Implicit and Explicit Method Parameters public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = this.balance + amount; this.balance = newBalance; }... harrysChecking.deposit(500); The amount parameter is an explicit parameter. An instance field in a class method can be denoted as this.instanceFieldName. this.balance is equivalent for this example to “harrysChecking.balance”, that is to say this refers to the implicit object parameter (harrysChecking object). The this keyword is not required, but is a good programming style.

5.2.1 Number Types Primitive Types TypeDescriptionSize byteSmallest numeric type1 byte shortShort integer2 bytes intInteger (~ + or – 2 billion)4 bytes longLong integer8 bytes floatFloating point type4 bytes doubleDouble precision floating point 8 bytes charSingle character type2 bytes booleanTrue or false type1 bit

5.2.1 Number Types Cont. int n = ; System.out.println(n * n); // Overflow double f = 4.35; System.out.println(100 * f); // Rounding error (due to binary to decimal conversion, will print …) int dollars = 100; double balance = dollars; // OK double balance = 13.75; int dollars = balance; // Error int dollars = (int) balance; // OK because of cast double d = 5/2; // d is 2 double d = 5/2.0; // d is 2.5

5.2.2 Constants A constant is a numeric value that does not change and is used for computations final denotes that a variable is a constant static means that the constant belongs to the class (not to an object like an instance field) OK to make class constants public since the value cannot be changed Good style to make a constant all caps Example of a method constant: final double QUARTER_VALUE = 0.25; Example of a class constant: public static final double QUARTER_VALUE = 0.25;

5.2.2 Constants Cont. // Example of using a constant in a computation double coins1 = numQuarters * QUARTER_VALUE; // A public static constant can be used by methods // in other classes using the syntax: // className.constantName double coins1 = numQuarters * Coins.QUARTER_VALUE; double circumference = Math.PI * diameter; In above examples Coins is assumed to be a user-defined class and Math is a Java API class

5.2.3 Assignment, Increment, and Decrement = is the assignment operator ++ is the increment operator myVar = myVar + 1; can be expressed as myVar++; -- is the decrement operator ++myVar; is a pre-increment myVar++; is a post-increment --myVar; is a pre-decrement myVar--; is a post-decrement

5.2.3 Assignment, Increment, and Decrement Cont. Assuming that myVar equals 10 at the outset of each example statement below: System.out.println(myVar++); // displays 10 System.out.println(++myVar); // displays 11 System.out.println(myVar--); // displays 10 System.out.println(--myVar); // displays 9 Note that the final value of incremented myVar is 11 and decremented myVar is 9. Make sure to understand the “pre vs. post” distinction when doing an increment or decrement as part of an assignment statement! Example: x = myVar++; // stores 10 in x, NOT 11

5.2.4 Arithmetic Operations and Mathematical Functions OperatorDescription + Addition - Subtraction * Multiplication / Division % Modulus A modulus B is the remainder of A divided by B. Example: 5%2 resolves to 1. Use parentheses to change the operator precedence. Example: * 10 resolves to 24 whereas (4 + 2) * 10 = 60

5.2.4 Arithmetic Operations and Mathematical Functions Cont. TypeReturns Math.sqrt(x)Square root of x Math.pow(x,y)x to the y power Math.round(x)Closest integer to x Math.ceil(x)Smallest integer greater than or equal to x Math.floor(x)Largest integer less than or equal to x Math.abs()Absolute value of x Math.max(x,y)The larger of x and y Math.min(x,y)The smaller of x and y

5.2.4 Arithmetic Operations and Mathematical Functions Cont. 5 to the 3 rd power can be expressed in Java as: int x = Math.pow(5, 3); // x equals 125 The square root of 25 can be expressed in Java as: double x = Math.sqrt(25); // x equals 5 Reminder from a previous slide (worth repeating), integer division truncates the remainder, so use a floating point literal value or cast to a double to avoid this pitfall: double x = 5/2; // x equals 2

5.2.5 Calling Static Methods The Math class methods per the previous section are examples of calling static methods A static method call uses the class name rather than an object name Format is: ClassName.methodName(parameters) Example Math class static method definition: public static double sqrt(double a) Classes with static methods only, such as the Java API Math class, are not used to create objects. Think of such classes as general purpose utility classes.

Reference: Big Java 2 nd Edition by Cay Horstmann Categories of Variables (section 3.7 in Big Java) Implicit and Explicit Method Parameters (section 3.8 in Big Java) Number Types (section 4.1 in Big Java) Constants (section 4.2 in Big Java) Assignment, Increment, and Decrement (section 4.3 in Big Java) Arithmetic Operations and Mathematical Functions (section 4.4 in Big Java) Calling Static Methods (section 4.5 in Big Java)