Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Types and Arithmetic Operators
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Chapter 2: Using Data.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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,
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Expressions, Data Conversion, and Input
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Input, Output, and Processing
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Programming Process Programming in Java is an exercise in using pre-defined classes and writing new classes to fill in the gaps A procedure for determining.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topic 2 Elementary Programming
Engineering Problem Solving With C An Object Based Approach
Data Types and Conversions, Input from the Keyboard
Yanal Alahmad Java Workshop Yanal Alahmad
Nahla Abuel-ola / WIT.
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Variables, Expressions, and IO
Type Conversion, Constants, and the String Object
INPUT STATEMENTS GC 201.
Introduction to CS Your First C Programs
Computers & Programming Languages
Presentation transcript:

Using Java's Math & Scanner class

Java's Mathematical functions (methods) (1)

Java's Mathematical functions (methods) (2) Unlike a calculator that always shows the result of a computation on its display.... A computer will only show the result of a computation when it is told !!!

Java's Mathematical functions (methods) (3) A computed value is not printed: If you want to print a computed value, use a print statement A computed value is not stored: If you want to store (save) a computed value, use an assignment statement

Reading input from the console input (1) The console is the terminal window that is running the Java program. When a Java program starts running, the Java runtime system will initialize many variables in support for the running program. Java system variable: System.in represents the console input A Java program can obtains inputs from the console through the keyboard

Reading input from the console input (2) Java's Scanner library class The class path of the Scanner class inside the Java library is: java.util.Scanner Before a Java program can read input from the keyboard, the program must " construct a Scanner object (explain later) Example:

Reading input from the console input (3) After having constructed the Scanner object named in, you can use the following expression to read a floating point number from the keyboard: You must save (store) the number read in by "in.nextDouble()" in a double typed variable with an assignment statement

Reading input from the console input (4) the method call in.nextDouble() will invoke (run) a method in Java's library. The task performed by in.nextDouble() is to read a floating point number from the keyboard:

Reading input from the console input (5) The return value will replace the method call: The input value 3.5 is then stored in the variable a !!!

Reading input from the console input (6) steps to read in a floating point number

Reading input from the console input (7) Good programming courtesy: When the program needs the user to enter input from the keyboard, it must print out a (short) prompt message

Different Types of Numerical Information

Recall Recall that the computer can combine adjacent bytes in the RAM memory to form larger memory cells To obtain a higher accuracy (= more significant digit of accuracy), we need to combine more memory cells

Trade-offs Memory usage Speed Arithmetic expressions that uses higher accuracy will take longer time to compute When a Java program needs to use higher accurate numbers, it will not only use more memory, but it will also take a longer time to complete.

Different kinds of floating point numbers in Java Java provides 2 different sizes of floating point numbers (and variables): 1. Single precision floating numbers (has lower accuracy and uses less memory) uses 4 consecutive bytes of memory as a single 32 bit memory cell 2. Double precision floating numbers (has more accuracy and uses more memory) uses 8 consecutive bytes of memory as a single 64 bit memory cell

Defining single and double precision floating point variables define double precision floating point variables: define single precision floating point variables: float and double are considered as different types

Converting (casting) to a single or a double precision representation single and double precision floating point numbers uses different encoding methods The computer has built-in machine instructions to convert between different encodings

Very important phenomenon in computer programming: lost of accuracy

Safe and unsafe conversion operations Safe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is no (or very little) loss in accuracy Unsafe conversion = a conversion from one representation (encoding) to another representation (encoding) where there is significant loss in accuracy

Expressions containing values of different types A computer can only operate on data of the same data type In order to perform any operation on two values of differing types, the computer must: 1. convert one of the types into the other type 2. Perform the operation on the value

Automatic conversions: 1.During a calculation of a arithmetic expressions Arithmetic promotion of float to double: 2. Storing the result to a variable by an assignment operator If float value is assigned to a double variable, the float value is converted to double.

The general rule for automatic type conversion in the assignment operation If type1 is a higher accuracy type than type2, then: The type2 value is automatically converted to type1 before the assignment statement is executed. If type1 is a lower accuracy type than type2, then: The assignment statement is not allowed. You must use an casting operator to make the assignment statement valid.