Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CSC Programming for Science Lecture 7: Math Functions.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Introduction to Java Primitive Types Operators Basic input and output.
Chapter 2 Variables.
Week 15 - Monday.  What did we talk about last time?  File I/O.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
COMP Primitive and Class Types Yi Hong May 14, 2015.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
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.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
Variables, Operators, and Expressions
Chapter 2 Variables.
Week 3 - Wednesday CS 121.
Chapter 2 Basic Computation
Chapter 2 More on Math More on Input
Building Java Programs
Multiple variables can be created in one declaration
Building Java Programs
Introduction to Programming
Type Conversion, Constants, and the String Object
Week 3: Basic Operations
Chapter 2 Basic Computation
Chapter 2 Variables.
Elementary Programming (C++)
The keyboard is the standard input device.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Building Java Programs
Presentation transcript:

Week 2 - Friday

 What did we talk about last time?  Using Scanner to get input  Basic math operations

 For Project 1, the easiest way to print out data with 2 decimal places is put "%.2f" in the formatting string for System.out.format()  If you want, you can include other things in the formatting string double x = ; System.out.format("%.2f", x); //prints 5.75 double x = ; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", ); //prints Total = $15.78

There are three parts to using Scanner for input 1. Include the appropriate import statement so that your program knows what a Scanner object is 2. Create a specific Scanner object with a name you choose 3. Use the object you create to read in data

 How complex can expressions get?  What’s the value of a ?  18! int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d; int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d;

 Order of operations holds like in math  You can use parentheses to clarify or change the precedence  Now a is 16 int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d; int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d;

 You cannot directly store a double value into an int variable  However, you can cast the double value to convert it into an int  Casting tells the compiler that you want the loss of precision to happen  You can always store an int into a double int a = 2.6;// fails! int a = (int)2.6;// succeeds! (a = 2)

 In Java, the conversion of a double into an int does not use rounding  As in the case of integer division, the value is always rounded down  You can think of this as using the floor function from math  If you want to round normally, you can simply add 0.5 before the cast double x = 2.6; int a = (int)(x + 0.5);// rounds double x = 2.6; int a = (int)(x + 0.5);// rounds

 There are operations beyond +, -, *, /, and % that you probably want to do with numbers  Java has those built-in because the computer can do those directly  A number of other operations can be done by calling methods  Methods will end up being very important to us later

 A method is a piece of Java code that has been packaged up so that you can use it over and over  Usually, a method will take some input and give some output  System.out.println() is an example of a method  Using a method (calling a method) always requires parentheses

 The sin() method allows you to find the sine of an angle (in radians)  This method is inside the Math class  The answer that it gives back is of type double  To use it, you might type the following: double value = Math.sin( 2.4 );

If your method takes input, you put it inside the parentheses, if not, you leave them empty Next, you must give the method name that you are calling Unless the method is inside your class, you must supply a class name and a dot You can store the result of the method, as long as the variable matches the type that the method gives back result = class.method( input );

Return typeNameJob doublesin( double theta ) Find the sine of angle theta doublecos( double theta ) Find the cosine of angle theta doubletan( double theta ) Find the tangent of angle theta doubleexp( double a ) Raise e to the power of a (e a ) doublelog( double a ) Find the natural log of a doublepow( double a, double b ) Raise a to the power of b (a b ) longround( double a ) Round a to the nearest integer doublerandom() Create a random number in [0, 1) doublesqrt( double a ) Find the square root of a doubletoDegrees( double radians ) Convert radians to degrees doubletoRadians( double degrees ) Convert degrees to radians

 Compute the hypotenuse of a triangle  a 2 + b 2 = c 2 b a c

 Next time we will talk about operations on:  boolean values  char values  String values

 Keep reading Chapter 3 of the textbook  Get started on Project 1