{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

13_creating_datatypes - Data Types 3.2 Creating Data Types...

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

View Full Document Right Arrow Icon
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 )
Background image of page 1

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

View Full Document Right Arrow Icon
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 [ 0 ]); 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
Background image of page 2
2/3/08 3 9 Anatomy of a Class 10 Potential Visualization Potential visualization. Read in N point charges from a file; compute total potential at each point in unit square. % more charges.txt 9 .51 .63 -100 .50 .50 40 .50 .72 10 .33 .33 5 .20 .20 -10 .70 .70 10 .82 .72 20 .85 .23 30 .90 .12 -50 % java Potential < charges.txt 11 Potential Visualization Arrays of objects. Allocate memory for the array; then allocate memory for each individual object. // read in the data int N = StdIn . readInt (); Charge [] a = new Charge [ N ]; for ( int i = 0 ; i < N ; i ++) { double x0 = StdIn . readDouble (); double y0 = StdIn . readDouble (); double q0 = StdIn . readDouble (); a [ i ] = new Charge ( x0 , y0 , q0 ); } 12 Potential Visualization // plot the data int SIZE = 512 ; Picture pic = new Picture ( SIZE , SIZE ); for ( int row = 0 ; row < SIZE ; row ++) { for ( int col = 0 ; col < SIZE ; col ++) { double V = 0.0 ; for ( int i = 0 ; i < N ; i ++) { double x = 1.0 * row / SIZE ; double y = 1.0 * col / SIZE ; V += a [ i ]. potentialAt ( x , y ); } pic . set ( row , SIZE - 1 - col , color ); } } pic . show (); (0, 0) is upper left V = k q i r i ( ) i compute color as a function of potential V
Background image of page 3

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

View Full Document Right Arrow Icon
2/3/08 4 Turtle Graphics 14 Turtle Graphics Goal. Create a data type to manipulate a turtle moving in the plane.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}