Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.

Similar presentations


Presentation on theme: "Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type."— Presentation transcript:

1 Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type or an ADT:  Abstract Data Types(ADT): The class is the mechanism for creating user-defined types. These types represent some entity by encapsulating the entities data, instance fields, and methods that manipulate that data. public class BankAccount{ // declarations for this class }  Primitive Data Types (p135):  These are the simple primitive data types we will use: int idnum;// integer values doublesalary;// real numbers chargender;// single characters ‘M’ or ‘F’ booleandone;// true or false value  Be aware that manipulating real numbers (aka doubles) can result in round-off errors!  Consider:double f = 4.35; System.out.print(100*f);//prints 434.99999999999994 This occurs because computers represent numbers in binary, and there is no exact representation of the fraction 1/10 in the binary system. So there are limitations!.

2 Type Cast – Conversion between types in Java is common. The following is how to type cast in Java, and it usually results in information loss. Type Cast – Conversion between types in Java is common. The following is how to type cast in Java, and it usually results in information loss.  (new Type) expression Examples:  (int)3.14159  (BankAcct)checkingAcctObject FYI FYI  BigInteger ADT p137.  BigInteger ADT p137. Constants 4.2 // Caps by convention -> static or non-static tbd later! Constants 4.2 // Caps by convention -> static or non-static tbd later!  Use named constants to make programs easier to read and maintain.  Constants are declared as final, and its value cannot be changed. public class TaxSchedule{ private static final double TAX_RATE = 0.075; private static final double TAX_RATE = 0.075;  Mathematical Constants: //declared in the Math class in the standard library public class Math{ public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846; Static – Something that is static belongs to the class – Not an object! Static – Something that is static belongs to the class – Not an object! double circumference = Math.PI * diameter; // in client code double circumference = Math.PI * diameter; // in client code

3 Assignments, Increment & Decrement Operators 4.3 Assignments, Increment & Decrement Operators 4.3  Common Operation - Incrementing & Decrementing by 1 count = count +1;// increment by 1 count + = 1;// Same as above count + + ;// count - - ; decrement by 1  Common Operation – Accumulating a sum sum + = score;// sum = sum + score; myBalance + = amt; Arithmetic Operations & Mathematical Functions 4.4 Arithmetic Operations & Mathematical Functions 4.4  Basic:+, -, *, /, % // Be careful of integer division  Advanced++, --, +=, -=, *=, /=  Resulting Type int int->intint int->int int double->doubleint double->double  Exs: 3*8 =3*8 = 15/3 =//Integer division!15/3 =//Integer division! 3/15 =3/15 = 15%3 =// Modulus ->Remainder!15%3 =// Modulus ->Remainder! 3%2 =3%2 = (double)3/15 =(double)3/15 = (double)(3/15) =(double)(3/15) = double result = 3/15;// double result = 3/15;//

4 Static Methods 4.5 Math class methods (p150): Math class methods (p150): ans = Math.sqrt(x); Static methods belong to the class – not an object! Static methods belong to the class – not an object! Consider the following: Consider the following:  answer = Math.sqrt(x);// static method  davesAcct.deposit(amt);// non-static method  sqrt( ) is invoked by the Math class –> it belongs to the class –> it belongs to the class  deposit( ) is invoked by an object Static Methods: Static Methods:  Belong to the class  Are invoked by the class  DO NOT operate on object

5 Lets open Bluej and input the following: public class Ch4IntroEx{ // Leave room for a class constant // Leave room for a class constant public static final double TAX_RATE = 0.03; public static final double TAX_RATE = 0.03; public static void main(){ public static void main(){ final double TWO_DIGITS_PI = 3.14; final double TWO_DIGITS_PI = 3.14; int x = 5; int x = 5; double radius = 3.35; double radius = 3.35; char gender = 'M'; char gender = 'M'; boolean m359IsCool = true; boolean m359IsCool = true; BankAcct jon = new BankAcct(1000); BankAcct jon = new BankAcct(1000); jon.withdraw(jon.getBalance() * TAX_RATE); jon.withdraw(jon.getBalance() * TAX_RATE); System.out.println("jons balance: " +jon.getBalance()); System.out.println("jons balance: " +jon.getBalance()); System.out.println("Area of circle with radius = " +radius +"\t: " System.out.println("Area of circle with radius = " +radius +"\t: " +(Math.PI * Math.pow(radius,2)) +(Math.PI * Math.pow(radius,2)) +"\n\t\t less accurately \t: " +"\n\t\t less accurately \t: " +(TWO_DIGITS_PI * Math.pow(radius,2))); +(TWO_DIGITS_PI * Math.pow(radius,2))); System.out.println("gender = " +gender); System.out.println("gender = " +gender); System.out.println("m359IsCool = " +m359IsCool); System.out.println("m359IsCool = " +m359IsCool); }} Homework p174 P4.3, P4.5


Download ppt "Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type."

Similar presentations


Ads by Google