Numerical types ….

21
Numerical types …. Numerical types …. int: int: integers, no fractional part integers, no fractional part 1, -4, 0 1, -4, 0 (also byte, short and long types) (also byte, short and long types) double: double: numbers with decimal part numbers with decimal part (double precision) 0.5, -3.11111, (double precision) 0.5, -3.11111, 4.3E24, 1E-14 4.3E24, 1E-14 (also float types for smaller (also float types for smaller values) values)

description

Numerical types …. int: integers, no fractional part 1, -4, 0 (also byte, short and long types) double: numbers with decimal part (double precision) 0.5, -3.11111, 4.3E24, 1E-14 (also float types for smaller values). Variable can be declared using these types …. - PowerPoint PPT Presentation

Transcript of Numerical types ….

Page 1: Numerical types ….

Numerical types ….Numerical types ….

► int:int: integers, no fractional part 1, -4, integers, no fractional part 1, -4, 00

(also byte, short and long types)(also byte, short and long types)

►double:double: numbers with decimal part numbers with decimal part (double precision) 0.5, -3.11111, (double precision) 0.5, -3.11111, 4.3E24, 1E-144.3E24, 1E-14

(also float types for smaller values)(also float types for smaller values)

Page 2: Numerical types ….

Variable can be declared using these Variable can be declared using these types ….types ….

int value; int value; short number;short number; double total; valuedouble total; value

numbernumber

total total

4555545555

3232

34.88834.888

Page 3: Numerical types ….

Values can be stored in these Values can be stored in these variables using the assignment variables using the assignment statement…statement… variable name = value OR variable namevariable name = value OR variable name OR arithmetic expression ;OR arithmetic expression ; Examples:Examples: int value, sum, product;int value, sum, product; 4555545555 value = 45555;value = 45555;

4555545555 sum = value;sum = value;

4553545535 sum = value – 10 * 2; //note precedencesum = value – 10 * 2; //note precedence

Page 4: Numerical types ….

Variables declarations and Variables declarations and assignments may be used…assignments may be used… * for instance variables of a class* for instance variables of a class

* for local variable a class method * for local variable a class method

* in a main method* in a main method

let’s look……….let’s look……….

Page 5: Numerical types ….

+Purse ()+Purse ()+ void addNickels(int count) + void addNickels(int count) + void addDimes(int count) + void addDimes(int count) + void addQuarters(int count) + void addQuarters(int count) + double getTotal(int count) + double getTotal(int count)

Class Purse

Page 6: Numerical types ….

Assignment operatorAssignment operator

public class Purse{public class Purse{

private int nickels; private int nickels; //amount of nickels in purse//amount of nickels in purse private int dimes; private int dimes; //amount of dimes in purse//amount of dimes in purse

private int quarters; private int quarters; //amount of quarters in //amount of quarters in pursepurse

* note variable names begin w/ lower case letters* note variable names begin w/ lower case letters

public Purse(){ public Purse(){ // a new purse object is empty// a new purse object is empty             nickels = 0;nickels = 0;

      dimes = 0;      dimes = 0;      quarters = 0;      quarters = 0;

} }    

Page 7: Numerical types ….

Assignment operatorAssignment operator

public class Purse{public class Purse{

private int nickels = 0; private int nickels = 0; //amount of nickels in //amount of nickels in pursepurse private int dimes = 0; private int dimes = 0; //amount of dimes in //amount of dimes in pursepurse

private int quarters = 0; private int quarters = 0; //amount of quarters in //amount of quarters in pursepurse

public Purse(){ } public Purse(){ }    

Does same thing as previous slide ….

Page 8: Numerical types ….

Assignment operatorAssignment operator

public class Purse{public class Purse{

private int nickels; private int nickels; private int dimes; private int dimes; private int quarters; private int quarters;

public Purse(){public Purse(){ nickels = 0;nickels = 0;

     dimes = 0;     dimes = 0;     quarters = 0;     quarters = 0;

} }     public void addNickels(int count){public void addNickels(int count){

            nickels = nickels + count;nickels = nickels + count;}}** addDimes and addQuarters would be written in a similar manner** addDimes and addQuarters would be written in a similar manner

Page 9: Numerical types ….

AssignmentAssignment

Page 10: Numerical types ….

Increment operator …Increment operator …

nickels++ ;nickels++ ; is the same as nickels = nickels is the same as nickels = nickels

+ 1+ 1

Page 11: Numerical types ….

Decrement operator …Decrement operator …

nickels-- ;nickels-- ; decrements the contents of the variable.decrements the contents of the variable.

Page 12: Numerical types ….

Implementing the getTotal MethodImplementing the getTotal Method

public double getTotal(){ public double getTotal(){ return nickels * 0.05 return nickels * 0.05 + dimes * 0.1 + quarters * 0.25;+ dimes * 0.1 + quarters * 0.25;

}}}}

Page 13: Numerical types ….

ConstantsConstants

public double getTotal()public double getTotal(){{

final double NICKEL_VAL = 0.05;final double NICKEL_VAL = 0.05;final double DIME_VAL = 0.1;final double DIME_VAL = 0.1;final double QUARTER_VAL = 0.25;final double QUARTER_VAL = 0.25;return nickels * NICKEL_VAL + dimes * DIME_VAL +return nickels * NICKEL_VAL + dimes * DIME_VAL +

quarters * QUARTER_VAL; quarters * QUARTER_VAL; }}

** constants make code more readable … no more ** constants make code more readable … no more ‘unclear’ numbers in the code‘unclear’ numbers in the code

Page 14: Numerical types ….

Class Constants make a bit more sense Class Constants make a bit more sense here…here…

public class Purse{ public class Purse{

private int nickels;private int nickels; private int dimes;private int dimes; private int quarters;private int quarters;

public double getTotal(){public double getTotal(){ return nickels * NICKEL_VALUE return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; + dimes * DIME_VALUE + quarters * QUARTER_VALUE; }}

}}// these constants are shared by ALL OBJECTS of type // these constants are shared by ALL OBJECTS of type

PursePurse

private static final double NICKEL_VALUE = 0.05;private final double NICKEL_VALUE = 0.05;private final double DIME_VALUE = 0.1;

private final double QUARTER_VALUE = 0.25;

private static final double DIME_VALUE = 0.1;

private static final double QUARTER_VALUE = 0.25;

Page 15: Numerical types ….

IF these constants were publicIF these constants were publicpublic class Purse{ public class Purse{

publicpublic staticstatic final double NICKEL_VALUE = 0.05; final double NICKEL_VALUE = 0.05;publicpublic staticstatic final double DIME_VALUE = 0.1; final double DIME_VALUE = 0.1;publicpublic staticstatic final double QUARTER_VALUE = final double QUARTER_VALUE = 0.25;0.25;

private int nickels;private int nickels; private int dimes;private int dimes; private int quarters;;private int quarters;; // purse class methods// purse class methods}}

In other classes, these constant can be used:In other classes, these constant can be used: Purse.DIME_VALUE Purse.DIME_VALUE

Page 16: Numerical types ….

let’s write an application program let’s write an application program which uses the class Purse…which uses the class Purse…

Page 17: Numerical types ….

File PurseTest.javaFile PurseTest.java

//This program tests the Purse class.//This program tests the Purse class.

public class PurseTest {public class PurseTest {       public static void main(String[] args) {public static void main(String[] args) { double total; //total cashdouble total; //total cash               Purse myPurse = new Purse();Purse myPurse = new Purse();           myPurse.addNickels(3);myPurse.addNickels(3);              myPurse.addDimes(1);myPurse.addDimes(1);    myPurse.addQuarters(2);myPurse.addQuarters(2);            totalValue = myPurse.getTotal();totalValue = myPurse.getTotal();        System.out.print("The total is “ + totalValue);System.out.print("The total is “ + totalValue);        }}}} //notice indentation//notice indentation

Page 18: Numerical types ….

Division and RemainderDivision and Remainder► / is the division operator/ is the division operator

► If both operands are integers, the result is an integer. The If both operands are integers, the result is an integer. The remainder is discarded remainder is discarded

► total = 7.0 / 4;total = 7.0 / 4; //total must be decimal type, assigns 1.75 //total must be decimal type, assigns 1.75

► sum = 7 / 4;sum = 7 / 4; // assigns 1 to total (1.0 if sum is declared // assigns 1 to total (1.0 if sum is declared as double or float)as double or float)

► value = 7 % 4;value = 7 % 4; // remainder with % (pronounced "modulo") // remainder with % (pronounced "modulo") // 3 assigned to value // 3 assigned to value

Page 19: Numerical types ….

What about other mathematical What about other mathematical functions??functions??

► aab b ?? square root ?? ?? square root ??

In Java, these are provided by the In Java, these are provided by the Math class .Math class .

The Math class provides many methods that can be called The Math class provides many methods that can be called from other classes.from other classes.

http://http://www.java.sun.comwww.java.sun.com

The Math class methods are The Math class methods are staticstatic (they belong to the (they belong to the class),class),

and can be called and can be called without without creating an object of class type creating an object of class type Math.Math.

Page 20: Numerical types ….

Mathematical FunctionsMathematical FunctionsMath.sqrt(x)Math.sqrt(x) square rootsquare root

Math.pow(x, y)Math.pow(x, y) power power xxyy

Math.exp(x)Math.exp(x) eexx

Math.log(x)Math.log(x) natural lognatural log

Math.sin(x), Math.sin(x), Math.cos(x), Math.cos(x), Math.tan(x)Math.tan(x)

sine, cosine, tangent (sine, cosine, tangent (xx in radian)in radian)

Math.round(x)Math.round(x) closest integer to closest integer to xx

Page 21: Numerical types ….

Analyzing an ExpressionAnalyzing an Expression