13_creating_datatypes - 2/3/08 1 3.2 Creating Data Types 2...

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: 2/3/08 1 3.2 Creating Data Types 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, define: Set of values. Operations defined on them. Java class. Allows us to define data types by specifying: Instance variables. (set of values) Methods. (operations defined on them) 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 ) 2/3/08 2 5 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 automatically invokes the toString() method 6 Anatomy of Instance Variables Instance variables. Specifies the set of values. Declare outside any method. Always use access modifier private . stay tuned 7 Anatomy of a Constructor Constructor. Invoke with new to create new objects. invoking a constructor 8 Anatomy of a Data Type Method Method. Define operations on instance variables. invoking a method 2/3/08 2 5 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 automatically invokes the toString() method 6 Anatomy of Instance Variables Instance variables. Specifies the set of values....
View Full Document

Page1 / 15

13_creating_datatypes - 2/3/08 1 3.2 Creating Data Types 2...

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