1 / 21

Numerical types ….

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

apollo
Download Presentation

Numerical types ….

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. Variable can be declared using these types …. int value; short number; double total; value number total

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

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

  5. Class Purse

  6. 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; } 

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

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

  9. Assignment

  10. Increment operator … nickels++ ; is the same as nickels = nickels + 1

  11. Decrement operator … nickels-- ; decrements the contents of the variable.

  12. Implementing the getTotal Method public double getTotal(){ return nickels * 0.05 + dimes * 0.1 + quarters * 0.25; }}

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

  14. 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;

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

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

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

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

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

  20. Mathematical Functions

  21. Analyzing an Expression

More Related