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

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

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

Topics. The Development Process

Full file at

Number Representation & Conversion

Programming with Java

double float char In a method: final typename variablename = expression ;

The Math Class. Using various math class methods. Formatting the values.

Formatting Output & Enumerated Types & Wrapper Classes

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

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Introduction to Programming Using Java (98-388)

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

AP CS Unit 3: Control Structures Notes

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Primitive Data Types: Intro

CIS133J. Working with Numbers in Java

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

AP Computer Science Unit 1. Programs

Chapter 2 Elementary Programming

CEN 414 Java Programming

What did we talk about last time? Examples switch statements

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Introduction to Computer Science and Object-Oriented Programming

Getting started with Java

Welcome to the Primitives and Expressions Lab!

1001ICT Introduction To Programming Lecture Notes

Computational Expression

Pace University. Fundamental Concepts of CS121 1

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

COMP6700/2140 Std. Lib., I/O

Lecture Set 4: More About Methods and More About Operators

CT 229 Java Syntax Continued

Chapter 4 Classes in the Java Class Libraries

Fall 2017 CISC124 9/16/2017

Full file at

Lecture Set 4: More About Methods and More About Operators

Advanced Object Concepts

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

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

OBJECT ORIENTED PROGRAMMING IN JAVA

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

Computer Science 145 Midterm 1 Fall 2016

Chapter 02: Using Data

Methods CSC 121 Fall 2016 Howard Rosenthal


Chapter 2 Primitive Data Types and Operations. Objectives

Loops. Repeat after me

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

AP Computer Science A

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter 2: Using Data

CSC Java Programming, Fall Java Data Types and Control Constructs

Chapter 2 ELEMENTARY PROGRAMMING

1 Short Answer (5 Points Each)

CS111: PROGRAMMING LANGUAGE II

Full file at

Elementary Programming

Methods CSC 121 Spring 2017 Howard Rosenthal

Chapter 2. Elementary Programming

LAB 13: ARRAYS (ONE DIMINSION)

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

Java Primer 1: Types, Classes and Operators

CS Programming I: Primitives and Expressions

PROGRAMMING FUNDAMENTALS

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

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.

1 Short Answer (15 Points Each)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

( &% class MyClass { }

Chapter 3: Operators, Expressions and Type Conversion

Computer Components. Software{ User Programs. Operating System. Hardware

JAVA WRAPPER CLASSES

Methods: A Deeper Look

Important Java terminology

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Data Types, Literals, Operators

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

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Object-Based Programming. Programming with Objects

Java Classes: Random, Character A C S L E C T U R E 6

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Chapter 2 Primitive Data Types and Operations

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

Chapter 2: Basic Elements of Java

Control Structures: if and while A C S L E C T U R E 4

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Lesson 4 Utility classes: Math, String, I/O

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Calculations, Formatting and Conversions

3. Java - Language Constructs I

Object Oriented Programming. Java-Lecture 6 - Arrays

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Transcription:

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

Math class Math class is a utility class You cannot create an instance of Math All references to constants and methods will use the prefix Math. Contains constants, π (PI) and e (E) Java convention is to name constants using capital letters.

Math class Some methods: pow( ) Raise the first argument to the power specified in second argument e.g. Math.pow(x,3) abs( ) Returns absolute value of its argument max( ) Returns the larger of two int or double arguments min( ) Returns the smaller of two int or double arguments many more random( ) Returns a double value greater than or equal to 0.0 and less than 1.0. round( ) floor( ) ceil( ) Returns the closest int to the argument Returns the largest double value that is less than or equal to the argument and is equal to an int Returns the smallest double value that is greater than or equal to the argument and is equal to an int

Example public class FindMax The larger of 3 integers is determined. The max method is used twice. Scanner methods used: Math.max() returns the larger of two values passed in as arguments Math is a utility class with static methods. Consider the statement: Math.max(j,k) The method max The Math class Two arguments, j and k, passed in to max

Example public class FindMax { public static void main(string[] args){ Scanner kb = new Scanner(System.in); System.out.println( "Please enter 3 int values"); int i = kb.nextint(); int j = kb.nextint(); int k = kb.nextint(); int mx = Math.max(i, Math.max(j,k)); System.out.println("largest is " +mx); } } FindMax.java

Casting Casting is an operation that allows us to change the type of a value. We can take a value of one type and cast it into an equivalent value of another type. There are two forms of casting in Java: explicit casting and implicit casting Recall mixed mode expressions: widening conversions

Explicit casting Java supports an explicit casting syntax with the following form: (type) exp E.g. double d1 = 3.2; double d2 = 3.99; int i1 = (int) d1; i1 value is 3 int i2 = (int) d2; i2 value is 3 double d3 = (double) i2; d3 value is 3.0

Integer class Integer class is a utility class Many methods are static: you do not need an object of type Integer. The prefix Integer. is used for these. Contains constants, MAX_VALUE and MIN_VALUE Re: Java convention is to name constants using capital letters.

Integer class Some methods max( ) min( ) parseint( ) Returns the larger of two int arguments Returns smaller Parses the string argument expecting that argument to be a valid decimal integer. Correction to notes: E.g. parseint(" 23 ") Should be: E.g. parseint("23")

Example 1 Read lines of text from System.in Each line is parsed according to the expected format: <name of an item><comma><quantity as integer> Examples of such lines: monitor,45 laptop,55

Example public class TotalQuantity Integer methods used: parseint( ) returns the integer represented by a character string Integer is a utility class with static methods. Consider the statement: int qty = Integer.parseInt(qtyAsString); int variable The Integer class String containing an integer The method parseint

Example public class TotalQuantity { public static void main(string[] args){ Scanner kb = new Scanner(System.in); int totalqty = 0; for (int i = 0; i < 4; i++){ System.out.print("Enter next line: "); String line = kb.nextline(); int commaat = line.indexof(","); String qtyasstring = line.substring(commaat+1); int qty = Integer.parseInt(qtyAsString); totalqty += qty; } System.out.println("total = " +totalqty); } } TotalQuantity.java

Wrapper classes With similarity to the Integer class, there are classes for other types these types of classes are called wrapper classes. These are called wrapper classes because you instantiate an object and wrap a primitive value inside Double Boolean Byte Character Float Long Short