***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.

Slides:



Advertisements
Similar presentations
1001ICT Programming 1 Semester 1, 2011 Lecture 6 Using Java Classes (Textbook, Chapter 3, Sections 3.2 to 3.7 ONLY)
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
LAB 10.
Computer Programming Lab(5).
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
CS1101: Programming Methodology Aaron Tan.
Introduction to Programming
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Using Java Class Library
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSE 1201 Object Oriented Programming Using Classes and Objects.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 posted – Due Feb 2PM Assignment 2 Posted – Due Feb 2 PM Finish Data and Expressions.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 CMPT 126 Java Basics Using Classes and Objects.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Introduction to programming in java
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Formatting Output & Enumerated Types & Wrapper Classes
Crash course in the Java Programming Language
Project 1.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Interactive Standard Input/output
Dialogues and Wrapper Classes
Using Classes and Objects (Chapter 3)
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Introduction to Classes and Methods
Chapter 3 Using Classes and Objects
Review: Simplify.
The Random Class The Random class is part of the java.util package
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Outline Boolean Expressions The if Statement Comparing Data
Math class services (functions)
Chapter 3 Using Classes and Objects
Presentation transcript:

***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically imported. A Math object does not have to be instantiated. See the chart below for a partial summary of available methods: Math FunctionMethodExample square rootsqrt(double a) Math.sqrt(value) =  value exponentiation (e a )exp(double a)Math.exp(value) = e value power function (x a )pow(double x, double a)Math.pow(value, 3) = value 3 value of PIPI() Math.PI() = 

***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class Ex int a, b, c; // ax^2 + bx + c double discriminant, root1, root2; Scanner scan = new Scanner (System.in); System.out.print ("Enter the coefficient of x squared: "); a = scan.nextInt(); System.out.print ("Enter the coefficient of x: "); b = scan.nextInt(); System.out.print ("Enter the constant: "); c = scan.nextInt(); Example from Program “Quadratic” (See text CDROM Chapter 3)

***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class Ex. 1 // Use the quadratic formula to compute the roots. // Assumes a positive discriminant. discriminant = Math.pow(b, 2) - (4 * a * c); root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a); root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a); System.out.println ("Root #1: " + root1); System.out.println ("Root #2: " + root2);... Enter the coefficient of x squared: 2 Enter the coefficient of x: 4 Enter the constant: 1 Root #1: Root #2: Example from Program “RandomNumbers” (See text CDROM Chapter 3) 2x 2 + 4x + 1

***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class The Random class provides a convenient way to generate random numbers. The Random class generates both floating point (float) and integer random numbers. Must be imported from “java.util” package See the chart below for a partial summary of available methods: Random Number TypeMethod floatnextFloat() Integer (range: all integers)nextInteger() Integer (range: 1 to num)nextInteger(int num)

***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class Ex.... Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1);... num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2); Example from Program “RandomNumbers” (See text CDROM Chapter 3)

***** SWTJC STEM ***** Chapter 3-1 cg 38 Random Class Ex. Run 1... A random integer: From 0 to 9: 9... A random float (between 0-1): Run 2... A random integer: From 0 to 9: 5... A random float (between 0-1): Example from Program “RandomNumbers” (See text CDROM Chapter 3)

***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class The DecimalFormat class provides a convenient way to format output values. DecimalFormat class has one method format. format uses a string pattern to control: Number of decimal places Leading zeros or not Show as percentage Show in scientific notation Plus others. See Java Reference. Must be imported from “java.text” package.

***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class See the chart below for a partial summary of available formats: FormatMethod Formatted standard notation - 2 decimal placesformat(“0.##”) standard notation - 3 decimal placesformat(“0.###”) scientific notation - 2 decimal placesformat(“0.##E0”)1.25E2

***** SWTJC STEM ***** Chapter 3-1 cg 40 DecimalFormat Class Ex. import java.text.DecimalFormat; public class FormatOutput { public static void main(String[] args) { double num = ; DecimalFormat fmt0 = new DecimalFormat ("0.##"); DecimalFormat fmt1 = new DecimalFormat ("0.###"); DecimalFormat fmt2 = new DecimalFormat ("0.###E0"); System.out.println(fmt0.format(num)); System.out.println(fmt1.format(num)); System.out.println(fmt2.format(num)); } E2