©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data 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

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
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.
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
Slides by Donald W. Smith
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
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 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
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
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.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Review 2.
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.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
Counting Coins. The Basics Quarter 25 cents Dime 10 cents.
Mixing integer and floating point numbers in an arithmetic operation.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 4 – Fundamental Data Types.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
A final variable is a constant
Chapter Goals To understand integer and floating-point numbers
Chapter Goals To understand integer and floating-point numbers
Chapter 4 – Fundamental Data Types
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 4 – Fundamental Data Types
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Wednesday 09/23/13.
Lecture 10 Strings CSE /26/2018.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Principles of Computer Science I
In this class, we will cover:
Presentation transcript:

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 2 Program Coins1.java public class Coins1 { public static void main(String[] args) { int pennies = 8; // the purse contains 8 pennies, int dimes = 4; // four dimes, int quarters = 3 ; // and three quarters // compute total value of the coins double total = pennies * dimes * quarters * 0.25; // print result System.out.print("Total value = "); System.out.println(total); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 3 Number types int : integer double : double-precision floating-point numbers Variable declaration: int n; double total = 0.5; Quality tip: Use descriptive variable names int nickels;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 4 Program Coins2.java public class Coins2 { public static void main(String[] args) { int pennies = 8; // eight pennies in the purse double total = pennies * 0.01; int dimes = 4; // four dimes in the purse // add value of dimes total = total + dimes * 0.10; int quarters = 3; // three quarters in the purse // add value of quarters total = total + quarters * 0.25; System.out.print("Total value = "); System.out.println(total); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 5 Assignment Assign a new value to a variable variableName = expression ; total = total + dimes * 0.1;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 6 Figure 1 Assignment

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 7 Increment and decrement month++; month--; Shortcuts for month = month + 1; month = month - 1;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 8 Figure 2 Incrementing a Variable

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 9 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); Use Math.round for rounding: int dollar = (int)Math.round(total);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 10 Static method calls ClassName. MethodName ( parameters ) Invoke a method that doesn't operate on an object Example: Math.round(3.14)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 11 Program Volume.java public class Volume { public static void main(String[] args) { final double BOTTLE_VOLUME = 2.0; final double CAN_VOLUME = 0.355;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 12 Constants final TypeName VariableName = Expression ; Defines a constant and assign its value Example: final double CAN_VOLUME = 0.355; Useful constants: Math.PI, Math.E Quality tip: No magic numbers

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 13 int bottles = 4; // we have four bottles int cans = 10; // and ten cans // compute total volume double total = bottles * BOTTLE_VOLUME + cans * CAN_VOLUME; // print result System.out.print("The total volume is "); System.out.print(total); System.out.println(” liters"); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 14 Program Coins3.java public class Coins3 { public static void main(String[] args) { final int PENNY_VALUE = 1; final int NICKEL_VALUE = 5; final int DIME_VALUE = 10; final int QUARTER_VALUE = 25; final int DOLLAR_VALUE = 100; int pennies = 8; // the purse contains 8 pennies, int nickels = 0; // no nickels, int dimes = 4; // four dimes, int quarters = 3; // and three quarters // compute total value in pennies int total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE;

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 15 // use integer division to convert to dollars, cents int dollar = total / DOLLAR_VALUE; int cents = total % DOLLAR_VALUE; System.out.print("Total value = "); System.out.print(dollar); System.out.print(” dollars and "); System.out.print(cents); System.out.println(" cents"); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 16 Figure 3 Analyzing an Expression

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 17 Arithmetic Operators + - * / Integer division 9 / 4 is 2 and not 2.25 ! 9 % 4 is 1 Common functions Math.pow(x,y) Math.sqrt(x)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 18 Figure 4 On-Line Help

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 19 Program MakePassword.java public class MakePassword { public static void main(String[] args) { String firstName = "Harold"; String middleName = "Joseph"; String lastName = "Hacker"; // extract initials String initials = firstName.substring(0, 1) + middleName.substring(0, 1) + lastName.substring(0, 1); // append age int age = 19; // the age of the user String password = initials.toLowerCase() + age; System.out.println("Your password is ” + password); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 20 Strings String constants: "Carl" String variables: String name = "Carl"; String length: int n = name.length();

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 21 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

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 22 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"

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 23 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);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 24 Formatting numbers NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(2); formatter.setMinimumFractionDi gits(2); formatter.format(tax); prints 0.30

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 25 Program Coins4.java import java.text.NumberFormat; public class Coins4 { public static void main(String[] args) { final double PENNY_VALUE = 0.01; final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; ConsoleReader console = new ConsoleReader(System.in); System.out.println("How many pennies do you have?"); int pennies = console.readInt(); System.out.println("How many nickels do you have?"); int nickels = console.readInt(); System.out.println("How many dimes do you have?"); int dimes = console.readInt(); System.out.println("How many quarters do you have?"); int quarters = console.readInt();

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 26 double total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; // total value of the coins NumberFormat formatter = NumberFormat.getCurrencyInstance(); System.out.println("Total value = " + formatter.format(total)); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 27 Reading input ConsoleReader console = new ConsoleReader(System.in); int pennies = console.readInt(); Also readDouble, readLine Not a standard Java class. Include ConsoleReader.java in same directory, or paste into source file

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 28 Program Coins5.java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Coins5 { public static void main(String[] args) { try { final double PENNY_VALUE = 0.01; final double NICKEL_VALUE = 0.05; final double DIME_VALUE = 0.1; final double QUARTER_VALUE = 0.25; InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 29 System.out.println("How many pennies do you have?"); String input = console.readLine(); int pennies = Integer.parseInt(input); System.out.println("How many nickels do you have?"); input = console.readLine(); int nickels = Integer.parseInt(input); System.out.println("How many dimes do you have?"); input = console.readLine(); int dimes = Integer.parseInt(input); System.out.println("How many quarters do you have?"); input = console.readLine(); int quarters = Integer.parseInt(input);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 30 double total = pennies * PENNY_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; // total value of the coins System.out.println("Total value = ” + total); } catch(IOException e) { System.out.println(e); System.exit(1); }