Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
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.
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.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
Slides by Donald W. Smith
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 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. 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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers 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.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 4 – Fundamental Data Types.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
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.
Chapter Goals To understand integer and floating-point numbers
Chapter Goals To understand integer and floating-point numbers
Chapter 4 – Fundamental Data Types
2.5 Another Java Application: Adding Integers
Lecture Notes – Basics (Ch1-6)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 4 – Fundamental Data Types
Chapter 2: Basic Elements of Java
Wednesday 09/23/13.
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Principles of Computer Science I
In this class, we will cover:
4.3 Arithmetic Operators and Mathematical Functions
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Chapter 3 Fundamental Data Types

Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types and the overflow and round off errors that can result To write arithmetic expressions in Java To use the String type to define and manipulate character strings To learn about the char data type To learn how to read program input To understand the copy behavior of primitive types and object references

Purse Class Interface public class Purse { public void addNickels(int count)... public void addDimes(int count)... public void addQuarters(int count)... public double getTotal(int count) }

Number types int: integers, no fractional part 1, -4, 0 double: floating-point numbers (double precision) 0.5, , 4.3E24, 1E-14

Implementing the getTotal Method public class Purse { public double getTotal(){ return nickels * dimes * quarters * 0.25; } private int nickels; private int dimes; private int quarters; } * = multiplication

Assignment operator public Purse() { nickels = 0; dimes = 0; quarters = 0; } public void addNickels(int count) { nickels = nickels + count; }

Assignment

Increment/Decrement nickels++ is the same as nickels = nickels + 1 nickels-- decrements the contents of the variable.

Constants public double getTotal() { final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; }

Class Constants public class Purse{... public double getTotal(){ return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; } private static final double NICKEL_VALUE = 0.05; private static final double DIME_VALUE = 0.1; private static final double QUARTER_VALUE = 0.25;... } In methods of other classes, the constant is Purse.DIME_VALUE

Syntax 3.1: Constant Definition Example: Purpose: In a method: final typeName variableName= expression ; In a class: accessSpecifier static final typeName variableName = expression; Example: final double NICKEL_VALUE =0.05; public static final double LITERS_PER_GALLON =3.785; Purpose: To define a constant of a particular type

File Purse.java 1/** 2 A purse computes the total value of a collection of coins. 3*/ 4public class Purse 5{ 6 /** 7 Constructs an empty purse. 8 */ 9 public Purse() 10 { 11 nickels = 0; 12 dimes = 0; 13 quarters = 0; 14 } 15…Continue

16 /** 17 Add nickels to the purse. count the number of nickels to add 19 */ 20 public void addNickels(int count) 21 { 22 nickels = nickels + count; 23 } 2425 /** 26 Add dimes to the purse. count the number of dimes to add 28 */ Continue…

29 public void addDimes(int count) 30 { 31 dimes = dimes + count; 32 } 3334 /** 35 Add quarters to the purse. count the number of quarters to add 37 */ 38 public void addQuarters(int count) 39 { 40 quarters = quarters + count; 41 } 42 …Continue

43 /** 44 Get the total value of the coins in the purse. the sum of all coin values 46 */ 47 public double getTotal() 48 { 49 return nickels * NICKEL_VALUE 50 + dimes * DIME_VALUE + quarters * QUARTE R_VALUE; 51 } 52 …Continue

53 private static final double NICKEL_VALUE = 0.05; 54 private static final double DIME_VALUE = 0.1; 55 private static final double QUARTER_VALUE = 0.25; private int nickels; 58 private int dimes; 59 private int quarters; 60} 61

File PurseTest.java 1/** 2 This program tests the Purse class. 3*/ 4public class PurseTest 5{ 6 public static void main(String[] args) 7 { 8 Purse myPurse = new Purse(); 9 10 myPurse.addNickels(3); 11 myPurse.addDimes(1); …Continue

12 myPurse.addQuarters(2); double totalValue = myPurse.getTotal(); 15 System.out.print("The total is "); 16 System.out.println(totalValue); 17 } 18}

Division and Remainder / is the division operator If both arguments are integers, the result is an integer. The remainder is discarded 7.0 / 4 = / 4 = 1 Get the remainder with % (pronounced "modulo") 7 % 4 = 3

Mathematical Functions Math.sqrt(x)square root Math.pow(x, y)power x y Math.exp(x)exex Math.log(x)natural log Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radian) Math.round(x)closest integer to x

Analyzing an Expression

Syntax 3.2: Static Method Call ClassName. methodName ( Tparameters) Example: Math.sqrt(4) Purpose: To invoke a static method (a method that doesn't operate on an object) and supply its parameters.

Type Conversion In assignment, types must match. double total = "a lot"; // no Use “cast” (int) to convert floating-point values to integer values: int pennies = (int)(total * 100); Cast discards fractional part. Use Math.round for rounding: int dollar = (int)Math.round(total);

Syntax 3.3 : Cast (typeName)expression Example: (int)(x + 0.5) (int)Math.round(100 * f) Purpose: To convert an expression to a different type

Strings String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length();

Concatenation String fname = "Harry"; String lname = "Hacker"; String name = fname + lname; name is "HarryHacker" If one operand of + is a string, the other is converted to a string: String a = "Agent"; String name = a + 7; name is "Agent7"

Converting between Strings and Numbers Convert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); Convert to string: String str = "" + n; str = Integer.toString(n);

Substrings String greeting = "Clown"; String sub = greeting.substring(1, 4); Supply start and “past the end” position First position is at 0 0 C 1 l 2 o 3 w 4 n substring length = “past the end” - start

Reading Input String input = JOptionPane.showInputDialog(prompt) Convert strings to numbers if necessary: int count = Integer.parseInt(input); Conversion throws an exception if user doesn't supply a number--see chapter 15 Add System.exit(0) to the main method of any program that uses JOptionPane

An Input Dialog

File InputTest.java 1import javax.swing.JOptionPane; 2 3/** 4 This program tests input from an input dialog. 5*/ 6public class InputTest 7{ 8 public static void main(String[] args) 9 { 10 Purse myPurse = new Purse(); …Continue

11 12 String input = JOptionPane.showInputDialog( "How many nickels do you have?"); 13 int count = Integer.parseInt(input); 14 myPurse.addNickels(count); input = JOptionPane.showInputDialog("How many dimes do you have?"); 17 count = Integer.parseInt(input); 18 myPurse.addDimes(count); input = JOptionPane.showInputDialog("How many quarters do you have?"); …Continue

21 count = Integer.parseInt(input); 22 myPurse.addQuarters(count); double totalValue = myPurse.getTotal(); 25 System.out.println("The total is " + totalValue); System.exit(0); 28 } 29}

Characters char: character type—a single Unicode character Character constants use single quotes: 'A', '\n', '\u00E9' 'A'is not the same as "A" charAt method gets character from a string "Hello".charAt(0) is 'H'

Copying Numbers double balance1 = 1000; double balance2 = balance1; balance2 = balance ; Change in balance2 does not affect balance1

Copying Object References BankAccount account1 = new BankAccount(1000); BankAccount account2 = account1; account2.deposit(500); Change through account2 is also visible through account1 Object variables hold references, not objects

Copying Object References