Block 1 Unit 2 Basic Constructs in Java

Block 1 Unit 2 Basic Constructs in Java Khalid Ali 12/10/2006 M 301 - Khalid Ali

Call Back n n n n n What is Object Orientation? What is Class and what is Object? What is the Deference between Protected and Private? What is the meaning of STATIC? What is ADT? What is Actual Parameter? Why the method main is used? The ‘wrapper’ class Integer. Inner classes. 12/10/2006 M 301 - Khalid Ali 2

Reminder n n All the coming up Announcement will be on http: //kuwait. arabcampus. org even if it is announced in the Section. Office Hours are • Sunday • Thursday n 16: 00 – 18: 00 12: 00 – 14: 00 My Email Address • ksaaifan@hotmail. com n 12/10/2006 Start the Subject with M 301 -103. M 301 - Khalid Ali 3

Aims n n n n Create Objects. Send Messages. Develop Windows Application. Inheritance. Java’s Event. Exceptions. Try and Catch Arrays in JAVA. 12/10/2006 M 301 - Khalid Ali 4

Basic Constructs in Java n n n Abstract Windowing Toolkit (AWT) Swing packages to simulate movement in a window based on the Java graphics model. Use several classes and Inheritance. What is Class constructors. Constants. 12/10/2006 M 301 - Khalid Ali 5

Ball. World Program import java. awt. *; import javax. swing. JFrame; public class Ball. World extends JFrame { public static void main (String [ ] args) { Ball. World world = new Ball. World (Color. red); world. show (); for (int i = 0 ; i < 1000 ; i++) world. run(); System. exit(0); // halt program } Public static final int Frame. Width = 600; Public static final int Frame. Height = 400; Private Ball a. Ball = new Ball (new Point(50, 50), 20); 12/10/2006 M 301 - Khalid Ali 6

Ball. World Program cont… private Ball. World (Color ball. Color) { set. Size (Frame. Width, Frame. Height); set. Title ("Ball World"); a. Ball. set. Color (ball. Color); a. Ball. set. Motion (3. 0, 6. 0); } public void paint (Graphics g) { super. paint(g); a. Ball. paint (g); } 12/10/2006 M 301 - Khalid Ali 7

Ball. World Program cont… Public void run() { a. Ball. move(); Point pos = a. Ball. location(); if ((pos. x < a. Ball. radius()) || (pos. x > Frame. Width – a. Ball. radius())) a. Ball. reflect. Horz(); if ((pos. y < a. Ball. radius()) || (pos. y > Frame. Height – a. Ball. radius())) a. Ball. reflect. Vert(); repaint(); Try { Thread. sleep(50); } catch (Interrupted. Exception e) { System. exit(0); } 12/10/2006 M 301 - Khalid Ali 8

Constructor and Method n What is the deference between constructor and method? • The name of the constructor matches the name of the class in which it appears. • The constructor does not specify a return type. • The user can never execute the constructor except as part of creating a new object. 12/10/2006 M 301 - Khalid Ali 9

Constructor and Method cont… n What are the similarity between constructors and methods? • Both can have arguments. • Their body consists of a sequence of statements. 12/10/2006 M 301 - Khalid Ali 10

Please Note n Please Note that: • The sleep method can raise an exception. Hence it must be surrounded by a try block. 12/10/2006 M 301 - Khalid Ali 11
![Java and Arrays n private Ball [] ball. Array = new Ball [Ball. Array. Java and Arrays n private Ball [] ball. Array = new Ball [Ball. Array.](http://slidetodoc.com/presentation_image_h2/4957a391653a3969706a9af80f88c915/image-12.jpg)
Java and Arrays n private Ball [] ball. Array = new Ball [Ball. Array. Size]; 12/10/2006 M 301 - Khalid Ali 12

Interfaces vs. Inheritance n n Using inheritance, methods and data fields that are declared in the parent class may be used in the child class. Hence, the child class inherits the structure of the parent class. Using an interface, there is no implementation of the methods in the "parent interface“ at all. Instead, the parent simply defines the names of the methods which must then be implemented in the child class. Hence, a child class inherits a specification of methods from an interface, but no structure, no data fields, and no member functions. 12/10/2006 M 301 - Khalid Ali 13

Terms to remember actual argument initialize public catch interrupt redefine (a method) coordinate invoke (a method) repaint constructor loop sleep data field loop control variable static event model main subclass final paint super for pixel superclass graphics context Point Thread inheritance private try protected 12/10/2006 M 301 - Khalid Ali 14

The class Integer and the primitive data type int n n int • a primitive data type. • its operations are not represented by methods and are confined to the normal arithmetic operations such as add and multiply. Integer • an Object which contains an int value that can be manipulated by methods. • Instances of the class Integer are objects such that each Integer object contains (or ‘wraps up’) a data field of type int. 12/10/2006 M 301 - Khalid Ali 15

Casts n In the paint method of class Cannon there are the following expressions: • • n n int int lv = (int) (barrel. Length * Math. sin(radian. Angle)); lh = (int) (barrel. Length * Math. cos(radian. Angle)); sh = (int) (barrel. Width * Math. sin(radian. Angle)); sv = (int) (barrel. Width * Math. cos(radian. Angle)); radian. Angle is of type double and (barrel. Length * Math. sin(radian. Angle)) is also of type double Putting (int) in front of a double simply changes the value to an integer by ignoring anything that comes after the decimal. This is usually known as “Casting”. 12/10/2006 M 301 - Khalid Ali 16

Homework n n It is Not an TMA. Experiment with JBuilder and try to comprehend how Ball. World and Cannon. Game execute and perform. Study all of Block 1 - Unit 2. Please Prepare Unit 3 and Unit 4 12/10/2006 M 301 - Khalid Ali 17
- Slides: 17