Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Numerical Data Recitation – 01/30/2009
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Expressions, Data Conversion, and Input
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
CSC Programming I Lecture 5 August 30, 2002.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mathematical Calculations in Java Mrs. G. Chapman.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSC 107 – Programming For Science. The Week’s Goal.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Math Class Methods Utilizing the Important Math Operations of Java!
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
CompSci 230 S Programming Techniques
Chapter 2 Variables.
Chapter 2 Basic Computation
Lecture 3 Java Operators.
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Chapter 4: Mathematical Functions, Characters, and Strings
Introduction to C++ Programming
Chapter 2 Variables.
Unit 3: Variables in Java
Chapter 2 Variables.
Just Enough Java 17-May-19.
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables may also be initialized during multiple declarations  Ex. double hours = 35.5, rate = 8.0, total;

Variable and Constants  It is possible to give a variable a starting value when it is declared. Ex. int x=7; float y=2.345; char initial = ‘K’;  Another variation is to create a constant (i.e. a value that cannot change throughout a program) Ex.final int MAXAGE = 65;

Arithmetic Operators AddSubtractMultiplyDivide Modulus (remainder) + - * / %

Order of Operations  All operators within parentheses are performed first  If there are nested parentheses (parentheses within parentheses) the innermost operators are performed  The *,/,% operators are performed next, from left to right  The + and - are performed last from left to right.

Assignment Statement  An assignment statement is used to assign a value on the left hand side of an equation to a variable on the right.  The command used to create an assignment statement is the equals sign(=).  The general form of an assignment statement is: = ; = ;

Compound Assignment Operations Simple Assignment Compound Addition Compound Subtraction Compound Multiplication Compound Division Compound Remainder =+=-=*=/=%= (integers only)

Compound Assignment Equalities x = x + 10 x = x – 10 x = x * 10 x = x / 10 x = x % 10 x += 10 x –= 10 x *= 10 x /= 10 x %= 10

Increment and Decrement Operators  Increment and Decrement operators add one or subtract one to the value of the variable.  Can be applied to integer, floating point, or character variables.  INCREMENT ++  DECREMENT --

Numeric Type Conversion(TypeCasting)  When you perform arithmetic operations with operands of unlike types, the Java language chooses a unifying type for the result.  The Java programming language then converts nonconforming operands to the unifying type. This unifying type is the type of the involved operand that appears first in the following list: 1.double 2.float 3.long 4.int 5.short 6.byte

Typecasting (cont.)  If you do not want this to happen you have to purposely override the unifying type (typecasting) Ex: int balance= 189; double weeklyBudget = (double)balance/4; //weeklyBudget will be int dollars = (int) weeklyBudget; //dollars will be 47

Strings and Object- Oriented Types  Simple data types are used when simple calculations need to be done.  For more advanced applications, there is a need for strings and object-oriented types.  These types can give the application the ability to (for example) “turn the power on”, “signal if a seat belt is not attached”, “regulate the fuel mixture in an engine”  These data type classes are called class wrappers or object wrappers.

String Data Type  String is used to contain more than one character.  The stored characters must be placed in double quotes.  Many times, processing all input as strings improves reliability of a program because the computer can take in a series of characters, test them to see if they are numbers, and then work with them. Ex: String name=“Rusty”;

String Concatenation  String concatenation is used to combine strings and strings or strings and variables together. Ex. String firstName, lastName, lastFirst; int age = 65; firstName = “John”; lastName = “Doe; lastFirst = lastName + “ “ + firstName + “ is age “ + age; System.out.println (lastFirst); System.out.println(firstName + “ is: \n” + age + “ years old”); Output: Doe John is age 65 John is 65 years old

Methods in class KeyboardReader SignatureDescription char readChar()Returns the first character in the input line, even if it is a space. double readDouble()Returns the first double in the input line. Leading and trailing spaces will be ignored. int readInt()Returns the first integer in the input line. Leading and trailing spaces are ignored String readLine()Returns the input line, including leading and trailing spaces void pause()Returns once the user presses Enter.

Errors  There are three types of errors in programming: syntax, run-time, and logical errors.  Syntax errors occur when you violate a syntax rule (i.e using system.Out.println instead of System.out.println).  Run-time errors occur when you ask a computer to do something it cannot do (i.e divide by zero).  Logic errors occur when you fail to express yourself accurately (i.e using greater than instead of less than)

Example Program public class Example2 { public static void main (String[] args) { int oneint=10; int twoint=15; int sum,difference,product, modulus; float quotient; System.out.print("The first int is "); System.out.println(oneint); System.out.println("The second int is " + twoint); sum = oneint + twoint; difference = oneint - twoint; product = oneint * twoint; modulus = oneint % twoint; quotient = oneint / (float)twoint; System.out.println("The sum is " + sum); System.out.println("The difference is " + difference); System.out.println("The product is " + product); System.out.println("The quotient is " + quotient); System.out.println("The modulus is " + modulus); }

Math Class  The math class is quite extensive but we will concentrate a just a few of it’s properties: abs(int x) Returns the absolute value of x pow(double base, double exponent) Returns the base raised to the exponent. round(double x) Returns x rounded to the nearest whole number. max(int a, int b) Returns the greater of a and b min(int a, int b) Returns the lesser of a and b random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. sqrt(double x) Returns the square root of x.

Examples of Math Class Methods int m; double x; m = Math.abs(-7) // m equals 7 x = Math.abs(-7.5)// x equals 7.5 x = Math.pow(3.0,2.0)// x equals 3.0^2.0 = 9.0 x = Math.pow(16.0,.25)// x equals 16.0 ^.25 = 2.0 m = Math.max(20,40)// m equals 40 m = Math.min(20,40)// m equals 20 m = (int) Math.round(4.27)// m equals 4

Math.random  In order to generate a random number between a certain range use the following formula.  X = Math.random() * (high # - low #) + low #  If you want the number to be an integer, you must use the round function.  X = Math.round(Math.random() * (high # - low #) + low #)  Or typecast the formula + 1 to an int  X = (int)(Math.random() * (high # - low # + 1) + low #)

Random Class  Must include  import java.util.Random;  Must then create an object from Random class  Ex: Random name = new Random();  Use object to call methods  Ex: name.method();

Random Class Methods  nextInt(integer);  Generates a random whole number between 0-(integer -1)  nextDouble();  Generates a random decimal between 0.0 – – 1.0