1 / 5

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

siran
Download Presentation

Ch4 Data 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. 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: intidnum; // integer values double salary; // real numbers char gender; // single characters ‘M’ or ‘F’ boolean done; // 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!. Ch4 Data Types

  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. • (new Type) expression Examples: • (int)3.14159 • (BankAcct)checkingAcctObject • FYI • BigInteger ADT p137. <optional – working with big numbers> • 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; • 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! double circumference = Math.PI * diameter; // in client code

  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 • Basic: +, -, *, /, % // Be careful of integer division • Advanced ++, --, +=, -=, *=, /= • Resulting Type • int <op> int -> int • int<op>double -> double • Exs: • 3*8 = • 15/3 = //Integer division! • 3/15 = • 15%3 = // Modulus ->Remainder! • 3%2 = • (double)3/15 = • (double)(3/15) = • double result = 3/15; //<eoln>

  4. Static Methods 4.5 • Math class methods (p150): ans = Math.sqrt(x); • Static methods belong to the class – not an object! • 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 • deposit( ) is invoked by an object • 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 public static final double TAX_RATE = 0.03; public static void main(){ final double TWO_DIGITS_PI = 3.14; int x = 5; double radius = 3.35; char gender = 'M'; boolean m359IsCool = true; BankAcctjon = new BankAcct(1000); jon.withdraw(jon.getBalance() * TAX_RATE); System.out.println("jons balance: " +jon.getBalance()); System.out.println("Area of circle with radius = " +radius +"\t: " +(Math.PI * Math.pow(radius,2)) +"\n\t\t<or> less accurately \t: " +(TWO_DIGITS_PI * Math.pow(radius,2))); System.out.println("gender = " +gender); System.out.println("m359IsCool = " +m359IsCool); } } Homework p174 P4.3, P4.5

More Related