Variable Declaration - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Variable Declaration

Description:

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 ... – PowerPoint PPT presentation

Number of Views:424
Avg rating:3.0/5.0
Slides: 22
Provided by: justin332
Category:

less

Transcript and Presenter's Notes

Title: Variable Declaration


1
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

2
Variable and Constants
  • It is possible to give a variable a starting
    value when it is declared.
  • Ex. int x7
  • float y2.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

3
Arithmetic Operators
  • Add
  • Subtract
  • Multiply
  • Divide
  • Modulus (remainder)
  • -
  • /

4
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.

5
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
  • ltvariablegt ltexpressiongt

6
Compound Assignment Operations
  • Simple Assignment
  • Compound Addition
  • Compound Subtraction
  • Compound Multiplication
  • Compound Division
  • Compound Remainder
  • -
  • /

(integers only)
7
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

8
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 --

9
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
  • double
  • float
  • long
  • int
  • short
  • byte

10
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 47.25
  • int dollars (int) weeklyBudget
  • //dollars will be 47

11
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.

12
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 nameRusty

13
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

14
Methods in class KeyboardReader
Signature Description
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.
15
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)

16
Example Program
public class Example2 public static void main
(String args) int oneint10 int
twoint15 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.printl
n("The product is " product) System.out.print
ln("The quotient is " quotient) System.out.pr
intln("The modulus is " modulus)
17
Math Class
  • The math class is quite extensive but we will
    concentrate a just a few of its 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.
18
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.02.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

19
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 )

20
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()

21
Random Class Methods
  • nextInt(integer)
  • Generates a random whole number between
    0-(integer -1)
  • nextDouble()
  • Generates a random decimal between
  • 0.0 1.0
Write a Comment
User Comments (0)
About PowerShow.com