Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Computer Programming w/ Eng. Applications
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Expressions, Data Conversion, and Input
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
***** 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.
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.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Using Java Class Library
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
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.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
Input/Output.
Chapter 2 More on Math More on Input
Crash course in the Java Programming Language
Data Conversion & Scanner Class
Chapter 3 Java Input/Output.
Classes, Libraries & Packages
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
الكلية الجامعية للعلوم التطبيقية
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Computers & Programming Languages
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
A+ Computer Science INPUT.
IFS410 Advanced Analysis and Design
Chapter 3 Input/Output.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
A+ Computer Science INPUT.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Chapter 3 Introduction to Classes, Objects Methods and Strings
Happy October Exam Review 10/01/2014.
Math class services (functions)
Reading Input and Scanner Class
Presentation transcript:

Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Suppose you want to find the absolute value of a number. You could square it and then take the square root. OR You can ask Math for some help. Math is a class of services automatically provided to help us with a range of math services. To request Math’s help in this problem, we essentially say, Math, calculate the value of this number. In code, you would see something like this: double original; double result; original = ; result = Math.abs(original);

In code, you would see something like this: double original; double result; original = ; result = Math.abs(original); Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Name of class providing service Name of the function (static method) The argument to the method (value that the method will act upon) We say that this method, abs, returns a value. In this case that value is assigned to the variable result. Appendix G – Math functions Lab will include some play with math functions MathPlay.java

Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Primitives int byte long short float double char boolean 5 5 int num; num = 5; num Reference Types Reference types take up 2 chunks of memory, one that is the named variable that contains an address or referenceand the other that contains the data contents of the object. When a variable references an object, it contains the memory address of the object’s location. Then it is said that the variable references the object. String cityName = "Charleston "; Address to the object cityName The object that contains the character string “Charleston” Charleston

Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Decimal Formatter – Another class in Java Creating an object DecimalFormat twoDecimals; DecimalFormat threeDecimals; twoDecimals = new DecimalFormat(“##0.00”); threeDecimals = new DecimalFormat(“##0.000”); Data type (class name) variables We say that we are instantiating a new DecimalFormat object with the new operation. Below, we use those formats. double number1; number1 = ; System.out.println(twoDecimals.format(number1); System.out.println(threeDecimals.format(number1); Formatting.java Book Chap 3.10

Scanner keyboard; keyboard = new Scanner (System.in); Data type (class name)variable Standard input keyboard System.in We say that we are instantiating a new Scanner object with the new operation. Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class 23.4\n567.98\nThis little piggy\n

keyboard System.in Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class If I want to read in data, I will use keyboard ’s methods to do so, since I want to read from standard input and keyboard references the Scanner object which will read from standard input. Scanner methods int: nextInt() double: nextDouble() String: next() String: nextLine() Examples – assume all variables have been initialized to the appropriate types abc = keyboard.nextInt(); text = keyboard.nextLine(); MPG.java Book chapter 2.13

Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner class Scanner methods (except for nextLine() ) treat “whitespace” as a delimiter. For each next method, it will pluck off the next token. If the input is not correct, such as using nextInt and the user has entered “abc”, you will get a runtime error. If you try to read in a String after you have read in a number, you must “consume” the new line character prior to reading the String. See page 88 (purple) or 86 (green). InputProblem.java