32datatype

32datatype - 3.2 Creating Data Types Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3.2 Creating Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 18, 2008 2:46 PM 2 Data Types Data type. Set of values and operations on those values. Basic types. Last time. Write programs that use data types. Today. Write programs to create our own data types. Some Operations Set of Values Data Type not, and, or, xor true , false boolean String int concatenate, compare sequence of Unicode characters add, subtract, multiply -2 31 to 2 31 - 1 3 Defining Data Types in Java To define a data type, specify: ! Set of values. ! Operations defined on those values. Java class. Defines a data type by specifying: ! Instance variables. (set of values) ! Methods. (operations defined on those values) ! Constructors. (create and initialize new objects) 4 Point Charge Data Type Goal. Create a data type to manipulate point charges. Set of values. Three real numbers. [position and electrical charge] Operations. ! Create a new point charge at ( r x , r y ) with electric charge q . ! Determine electric potential V at ( x , y ) due to point charge. ! Convert to string. V = k q r r = distance between ( x , y ) and ( r x , r y ) k = electrostatic constant = 8.99 ! 10 9 N " m 2 / C 2 q r = ( r x " x ) 2 + ( r y " y ) 2 ( x , y ) ( r x , r y ) 5 Point Charge Data Type Goal. Create a data type to manipulate point charges. Set of values. Three real numbers. [position and electrical charge] API. 6 Charge Data Type: A Simple Client Client program. Uses data type operations to calculate something. public static void main ( String args ) { double x = Double . parseDouble ( args [ ]); double y = Double . parseDouble ( args [ 1 ]); Charge c1 = new Charge ( .51 , .63 , 21.3 ); Charge c2 = new Charge ( .13 , .94 , 81.9 ); double v1 = c1 . potentialAt ( x , y ); double v2 = c2 . potentialAt ( x , y ); StdOut . println ( c1 ); StdOut . println ( c2 ); StdOut . println ( v1 + v2 ); } % java Charge .50 .50 21.3 at (0.51, 0.63) 81.9 at (0.13, 0.94) 2.74936907085912e12 automagically invokes the toString() method 7 Anatomy of Instance Variables Instance variables. Specifies the set of values. ! Declare outside any method. ! Always use access modifier private . ! Use modifier final with instance variables that never change. stay tuned 8 Anatomy of a Constructor Constructor. Specifies what happens when you create a new object....
View Full Document

This note was uploaded on 12/16/2008 for the course COS 126 taught by Professor Douglasw.clark during the Fall '08 term at Princeton.

Page1 / 8

32datatype - 3.2 Creating Data Types Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online