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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
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.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
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.
FUNDAMENTAL DATA TYPES CSC 171 LECTURE 4. How do Computers represent information? Computers are switches Switches are “on” or “off” Suppose we want to.
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.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
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.
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.
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.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
Java Programming: From Problem Analysis to Program Design, 4e 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.
CPS120: Introduction to Computer Science Operations Lecture 9.
Java Data Types Assignment and Simple Arithmetic.
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.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Alice in Action with Java Chapter 8 Types and Expressions.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Today’s topic: Arithmetic expressions.
Chapter 4 – Fundamental Data Types
Multiple variables can be created in one declaration
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Elementary Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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 or an ADT:  Abstract Data Types(ADT): The class is the mechanism for creating user-defined types. These types represent some entity by encapsulating the entities data, instance fields, and methods that manipulate that data. public class BankAccount{ // declarations for this class }  Primitive Data Types (p135):  These are the simple primitive data types we will use: int idnum;// integer values doublesalary;// real numbers chargender;// single characters ‘M’ or ‘F’ booleandone;// true or false value  Be aware that manipulating real numbers (aka doubles) can result in round-off errors!  Consider:double f = 4.35; System.out.print(100*f);//prints This occurs because computers represent numbers in binary, and there is no exact representation of the fraction 1/10 in the binary system. So there are limitations!.

Type Cast – Conversion between types in Java is common. The following is how to type cast in Java, and it usually results in information loss. Type Cast – Conversion between types in Java is common. The following is how to type cast in Java, and it usually results in information loss.  (new Type) expression Examples:  (int)  (BankAcct)checkingAcctObject FYI FYI  BigInteger ADT p137.  BigInteger ADT p137. Constants 4.2 // Caps by convention -> static or non-static tbd later! Constants 4.2 // Caps by convention -> static or non-static tbd later!  Use named constants to make programs easier to read and maintain.  Constants are declared as final, and its value cannot be changed. public class TaxSchedule{ private static final double TAX_RATE = 0.075; private static final double TAX_RATE = 0.075;  Mathematical Constants: //declared in the Math class in the standard library public class Math{ public static final double E = ; public static final double PI = ; Static – Something that is static belongs to the class – Not an object! Static – Something that is static belongs to the class – Not an object! double circumference = Math.PI * diameter; // in client code double circumference = Math.PI * diameter; // in client code

Assignments, Increment & Decrement Operators 4.3 Assignments, Increment & Decrement Operators 4.3  Common Operation - Incrementing & Decrementing by 1 count = count +1;// increment by 1 count + = 1;// Same as above count + + ;// count - - ; decrement by 1  Common Operation – Accumulating a sum sum + = score;// sum = sum + score; myBalance + = amt; Arithmetic Operations & Mathematical Functions 4.4 Arithmetic Operations & Mathematical Functions 4.4  Basic:+, -, *, /, % // Be careful of integer division  Advanced++, --, +=, -=, *=, /=  Resulting Type int int->intint int->int int double->doubleint double->double  Exs: 3*8 =3*8 = 15/3 =//Integer division!15/3 =//Integer division! 3/15 =3/15 = 15%3 =// Modulus ->Remainder!15%3 =// Modulus ->Remainder! 3%2 =3%2 = (double)3/15 =(double)3/15 = (double)(3/15) =(double)(3/15) = double result = 3/15;// double result = 3/15;//

Static Methods 4.5 Math class methods (p150): Math class methods (p150): ans = Math.sqrt(x); Static methods belong to the class – not an object! Static methods belong to the class – not an object! Consider the following: Consider the following:  answer = Math.sqrt(x);// static method  davesAcct.deposit(amt);// non-static method  sqrt( ) is invoked by the Math class –> it belongs to the class –> it belongs to the class  deposit( ) is invoked by an object Static Methods: Static Methods:  Belong to the class  Are invoked by the class  DO NOT operate on object

Lets open Bluej and input the following: public class Ch4IntroEx{ // Leave room for a class constant // Leave room for a class constant public static final double TAX_RATE = 0.03; public static final double TAX_RATE = 0.03; public static void main(){ public static void main(){ final double TWO_DIGITS_PI = 3.14; final double TWO_DIGITS_PI = 3.14; int x = 5; int x = 5; double radius = 3.35; double radius = 3.35; char gender = 'M'; char gender = 'M'; boolean m359IsCool = true; boolean m359IsCool = true; BankAcct jon = new BankAcct(1000); BankAcct jon = new BankAcct(1000); jon.withdraw(jon.getBalance() * TAX_RATE); jon.withdraw(jon.getBalance() * TAX_RATE); System.out.println("jons balance: " +jon.getBalance()); System.out.println("jons balance: " +jon.getBalance()); System.out.println("Area of circle with radius = " +radius +"\t: " System.out.println("Area of circle with radius = " +radius +"\t: " +(Math.PI * Math.pow(radius,2)) +(Math.PI * Math.pow(radius,2)) +"\n\t\t less accurately \t: " +"\n\t\t less accurately \t: " +(TWO_DIGITS_PI * Math.pow(radius,2))); +(TWO_DIGITS_PI * Math.pow(radius,2))); System.out.println("gender = " +gender); System.out.println("gender = " +gender); System.out.println("m359IsCool = " +m359IsCool); System.out.println("m359IsCool = " +m359IsCool); }} Homework p174 P4.3, P4.5