210 likes | 334 Views
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 ….
E N D
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 …. int value; short number; double total; value number total
Values can be stored in these variables using the assignment statement… variable name = value OR variable name OR arithmetic expression ; Examples: int value, sum, product; 45555 value = 45555; 45555 sum = value; 45535 sum = value – 10 * 2; //note precedence
Variables declarations and assignments may be used… * for instance variables of a class * for local variable a class method * in a main method let’s look……….
Assignment operator public class Purse{ private int nickels; //amount of nickels in purse private int dimes; //amount of dimes in purse private int quarters; //amount of quarters in purse * note variable names begin w/ lower case letters public Purse(){ // a new purse object is empty nickels = 0; dimes = 0; quarters = 0; }
Assignment operator public class Purse{ private int nickels = 0; //amount of nickels in purse private int dimes = 0; //amount of dimes in purse private int quarters = 0; //amount of quarters in purse public Purse(){ } Does same thing as previous slide ….
Assignment operator public class Purse{ private int nickels; private int dimes; private int quarters; public Purse(){ nickels = 0; dimes = 0; quarters = 0; } public void addNickels(int count){ nickels = nickels + count; } ** addDimes and addQuarters would be written in a similar manner
Increment operator … nickels++ ; is the same as nickels = nickels + 1
Decrement operator … nickels-- ; decrements the contents of the variable.
Implementing the getTotal Method public double getTotal(){ return nickels * 0.05 + dimes * 0.1 + quarters * 0.25; }}
Constants public double getTotal() {final double NICKEL_VAL = 0.05;final double DIME_VAL = 0.1;final double QUARTER_VAL = 0.25;return nickels * NICKEL_VAL + dimes * DIME_VAL + quarters * QUARTER_VAL; } ** constants make code more readable … no more ‘unclear’ numbers in the code
Class Constants make a bit more sense here… private static final double NICKEL_VALUE = 0.05; private final double NICKEL_VALUE = 0.05; public class Purse{ private int nickels; private int dimes; private int quarters; public double getTotal(){ return nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; } } // these constants are shared by ALL OBJECTS of type Purse private static final double DIME_VALUE = 0.1;private static final double QUARTER_VALUE = 0.25; private final double DIME_VALUE = 0.1;private final double QUARTER_VALUE = 0.25;
IF these constants were public public class Purse{ publicstatic final double NICKEL_VALUE = 0.05;publicstatic final double DIME_VALUE = 0.1;publicstatic final double QUARTER_VALUE = 0.25; private int nickels; private int dimes; private int quarters;; // purse class methods } In other classes, these constant can be used: Purse.DIME_VALUE
let’s write an application program which uses the class Purse…
File PurseTest.java //This program tests the Purse class. public class PurseTest { public static void main(String[] args) { double total; //total cash Purse myPurse = new Purse(); myPurse.addNickels(3); myPurse.addDimes(1); myPurse.addQuarters(2); totalValue = myPurse.getTotal(); System.out.print("The total is “ + totalValue); } } //notice indentation
Division and Remainder • / is the division operator • If both operands are integers, the result is an integer. The remainder is discarded • total = 7.0 / 4; //total must be decimal type, assigns 1.75 • sum = 7 / 4; // assigns 1 to total (1.0 if sum is declared as double or float) • value = 7 % 4; // remainder with % (pronounced "modulo") // 3 assigned to value
What about other mathematical functions?? • ab ?? square root ?? In Java, these are provided by the Math class . The Math class provides many methods that can be called from other classes. http://www.java.sun.com The Math class methods are static (they belong to the class), and can be called without creating an object of class type Math.