CIS133J. Working with Numbers in Java

Similar documents
Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Introduction to Computer Science and Object-Oriented Programming

1.00 Lecture 4. Promotion

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Expressions and Casting

Program Fundamentals

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Zheng-Liang Lu Java Programming 45 / 79

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

LECTURE 3 C++ Basics Part 2

JAVA Programming Fundamentals

Object-Oriented Programming

Chapter 3: Operators, Expressions and Type Conversion

Basics of Java Programming

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CMPT 125: Lecture 3 Data and Expressions

JAVA OPERATORS GENERAL

CHAPTER 3 Expressions, Functions, Output

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

DATA TYPES AND EXPRESSIONS

Getting started with Java

MODULE 02: BASIC COMPUTATION IN JAVA

Information Science 1

3. Java - Language Constructs I

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Declaration and Memory

COMP 110 Introduction to Programming. What did we discuss?

Introduction to Computer Science Unit 2. Notes

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Chapter 2. Elementary Programming

Solving Equations with Inverse Operations

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Lecture Set 4: More About Methods and More About Operators

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Mr. Monroe s Guide to Mastering Java Syntax

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture Set 4: More About Methods and More About Operators

CEN 414 Java Programming

More Programming Constructs -- Introduction

Add Subtract Multiply Divide

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

ECE 122 Engineering Problem Solving with Java

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Types and Expressions. Chapter 3

Programming in C++ 5. Integral data types

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Reserved Words and Identifiers

Chapter 2 Elementary Programming

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Chapter 3 Structure of a C Program

Full file at

Values and Variables 1 / 30

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

UNIT- 3 Introduction to C++

Chapter 4: Basic C Operators

Information Science 1

COMP Primitive and Class Types. Yi Hong May 14, 2015

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Java Fall 2018 Margaret Reid-Miller

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Operators and Expressions

Operators. Java operators are classified into three categories:

Visual C# Instructor s Manual Table of Contents

Chapter 3. Numeric Types, Expressions, and Output

Computer System and programming in C

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 2: Data and Expressions

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS110: PROGRAMMING LANGUAGE I

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

AP Computer Science Unit 1. Programs

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Chapter 2: Using Data

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Ex: If you use a program to record sales, you will want to remember data:

Java Classes: Math, Integer A C S L E C T U R E 8

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

VARIABLES AND TYPES CITS1001

Elementary Programming

CS 106 Introduction to Computer Science I

Chapter 2: Using Data

Transcription:

CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables Saving and retrieving values using the variables Doing calculations Common calculations Trigonometric functions Numbers in Java fall into two categories: integral floating point Integral numbers are those that do not have a decimal point. They can hold whole number values only such as 100, -250, 4567 Floating point number have a decimal point and can hold fractional numbers such as 44.55, 3.14159, -56.44 Note that either type can store positive or negative numbers. Using variables with integral numbers The choice of variable types here are: byte short int long The only difference is the size of the variable. Normally, use int or long.

Using variables with floating point numbers The choice of variable type here are: float double The difference between these is the size of the variable and the precision. How to declare integral variables I will only show how to use int and long. To declare an int variable, use the int keyword and choose a name for the variable: int age; To declare a long variable, use the long keyword and choose a variable name: long distancetoplanet; How to declare floating point variables To declare a float variable, use the float keyword and name the variable. float taxrate; To declare a double variable, use the double keyword and name the variable: double budget; Saving and retrieving values using the variables To store a value into one of the variables, use an assignment statement: age = 45; budget = 245678.99; Numeric values that you use in your code, such as 45 above are called literals. A literal is a constant value.

A literal with a decimal point is assumed to be a double. For instance: float taxrate; taxrate = 55.66; will cause a compiler error because 55.66 is assumed to be a double and taxrate was declared as a float. To fix this problem, use the f suffix after any literal you want to be treated as a float. taxrate = 55.66f; Now 55.66 is considered a float and Java is happy. Warning! If you declare a variable of one type and try to store data of a different type in it, you will get a type mismatch error. Doing calculations Performing calculations is straightforward as long as all the numbers are of the same type. For example, double budget; double adjustment; double totalbudget; budget = 123456.88; adjustment = 545.66; totalbudget = budget + adjustment; Every variable and literal is the same type. There is trouble when you try to mix types in a calculation. For example, double budget; double adjustment; int totalbudget; budget = 123456.88; adjustment = 545.66; totalbudget = budget + adjustment;

Java doesn t like this because you are attempting to store budget + adjustment which is a double, into an int variable. These are called mixed calculations. Warning! If you try to store data of the wrong type into a variable, you will get a type mismatch error. The solution? The easiest is solution is: don t mix your types. Java will perform implicit casting in a mixed calculation. Here are the rules, performed in this order: 1. if either value is double, the other is converted to double 2. if either value is float, the other is converted to float 3. if either is long, the other is converted to long For example; public static void main(string[] args) double budget; float adjustment; double totalbudget; } budget = 123456.88; adjustment = 545.66f; totalbudget = budget + adjustment; Since budget is a double, and adjustment is a float, rule 2 applies and adjustment will be promoted to a double, in this calculation only. You can also force java to change the type of a variable using explicit casting, but that is dangerous if you don t know all the implications. We won t discuss that here. (Better to leave a rattlesnake alone.) I ll repeat: The easiest is solution is: don t mix your types. Common calculations Most of these are obvious and need no explanation. Addition

int x = 5; int y = 7; int total; total = x + y; Subtraction int x = 5; int y = 7; int result; result = x - y; Multiplication int x = 5; int y = 7; int result; result = x * y; Division int x = 5; int y = 7; int result; result = x / y; By the way, * and / are done before + and - in a calculation. For example, x + y * z y * z is done first, then add x to that result You can use ( ) to override this order, as in: (x + y) * z Now, x is added to y first, then the multiplication is performed. Use parentheses where needed to do the calculation correctly. Modulus Division (Integer Division) We all know that 12/2 = 6 with no remainder while 12/5 = 2 with a remainder of 2 The modulus operator (%) does a division and gives the remainder as a whole number. For example;

int x = 14; int y = 5; int result; result = x % y; result will contain 4 (because 14 % 5 = 2 with a remainder of 4) Two ways to add or subtract 1 from a variable You can do it the standard way: x = x + 1; or use the increment operator (++) x++; The same for the decrement (--) operator: or x = x - 1; x--; Find the maximum of two numbers You can do it with an if statement: int x = 6; int y = 8; if (x < y) or use the max method in the Math class: public static void main(string[] args) double budget = 44.55; double adjustment = 666.66; } double result = Math.max(budget, adjustment); System.out.println (result); Find the minimum of two numbers Use the min method in the Math class public static void main(string[] args) double budget = 44.55; double adjustment = 666.66;

} double result = Math.min(budget, adjustment); System.out.println (result); Alternatively, you could use a simple if statement. How to round a number Use the round method in the Math class public static void main(string[ ] args) double value = 66.53; } double roundedvalue = Math.round(value); System.out.println (roundedvalue); How to find the square root of a number Use the sqrt method in the Math class public static void main(string[] args) double value = 66.53; } double x = Math.sqrt(value); System.out.println (x); How to square a number Just multiply it by itself int x = 5; int square = x * x; Trigonometric functions These are all in the Math class. They aren t discussed here. Self test questions 1. What is a literal? Give an example. 2. What is the decrement operator? 3. Which is done first, addition or multiplication? 4. Java treats a literal such as 3.1435 as which data type?

5. Is it safe to use explicit casting? 6. How could you round a number?