Robot Class name int street int avenue Direction
Robot Class name int street int avenue Direction direction Thing. Bag backbag. . . Attributes Constructor Robot(City a. City, int a. Street, int a. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing(). . . Services, methods UML class diagram for Robot 1/23/2008 ITK 168 1
Modeling what? Modeling Robots in a City with Software Classes. Robot City int street int avenue Direction direction Thing. Bag backbag. . . String name int stree_No int ave_No. . Robot(City a. City, int a. Street, int a. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing(). . . City(. . . ). . . . 1/23/2008 ITK 168 2
Task: deliver X from (1, 2) to (3, 1) and step away 0 1 2 3 4 import becker. robot. *; public class Deliver. X{ public static void main(String args[]){ // set up Initial situation City A = new City(); Thing X = new Thing(A, 1, 2); Robot karel = new Robot(A, 0, 0, Direction. East); // direct the robot karel. move(); karel. turn. Left(); karel. move(); karel. pick. Thing(); karek. move(); karel. turn. Left(); karel. move(); karel. put. Thing(); karel. move(); } X named Karel 1/23/2008 } ITK 168 3
Task: deliver X from (1, 2) to (3, 1) and step away 0 1 2 3 4 X named Karel 1/23/2008 import becker. robot. *; public class Deliver. X{ public static void main(String args[]){ // set up Initial situation City A = new City(); Thing X = new Thing(A, 1, 2); Robot karel = new Robot(A, 0, 0, Direction. East); // direct the robot karel. turn. Left(); karel. move(); karel. pick. Thing(); karel. turn. Left(); karek. move(); karel. turn. Left(); karel. move(); karel. put. Thing(); karel. turn. Left(); karel. move(); } ITK 168 4 }
Anatomy of a Java Program import becker. robot. *; public class Deliver. X{ public static void main(String args[]){ // set up Initial situation City A = new City(); Thing X = new Thing(A, 1, 2); Robot karel = new Robot(A, 0, 0, Direction. East); // direct the robot karel. move(); karel. turn. Left(); karel. move(); karel. pick. Thing(); karek. move(); karel. turn. Left(); karel. move(); karel. put. Thing(); karel. move(); } } 1/23/2008 ITK 168 5
Package: collection of classes // Java API: Java Application Programming Interface // (java. , javax. ) // GUI: Graphical User Interface // // using swing package, JOption. Pane class // show. Message. Dialog method import javax. swing. JOption. Pane; public class Test. Pane{ public static void main(String args[]){ JOption. Pane. show. Message. Dialog( null, "Welcomen. Tonswingn. Package"); System. exit(0); } } 1/23/2008 ITK 168 6
package A Java program import javax. swing. JOption. Pane; class defined in the package public class time{ public static void main(String args[]){ int x, y, z; String X, Y; X Y x y = = JOption. Pane. show. Input. Dialog("Input x"); JOption. Pane. show. Input. Dialog("Input y"); Integer. parse. Int(X); Integer. parse. Int(Y); z = x*y; JOption. Pane. show. Message. Dialog( null, x + " * " + y + " = " + z, "The product of " + x + " and " + y, JOption. Pane. PLAIN_MESSAGE ); System. exit(0); } } 1/23/2008 ITK 168 7
A Java Program: // Text-printing program. public class Welcome 1 { // main method begins execution of Java application public static void main( String args[] ) { System. out. println( "Welcome to Java Programming!" ); } // end method main } // end class Welcome 1 /***************************** *(C) Copyright 1992 -2003 by. . . * * * *****************************/ 1/23/2008 ITK 168 8
Reusing Codes 1. Composition A class has another class, “has a” relation A car has an engine 2. Inheritance A class inherits another class, “is a” relation An SUV is a car 1/23/2008 ITK 168 9
Composition: has a A Deliver. X has a main, The main has a City, Thing, and a Robot import becker. robot. *; public class Deliver. X{ public static void main(String args[]){ // set up Initial situation City A = new City(); Thing X = new Thing(A, 1, 2); Robot karel = new Robot(A, 0, 0, Direction. East); . . . } } 1/23/2008 ITK 168 10
Composition: has a A Bank. Account has a Client, has a List of Bank. Transaction public class Bank. Account { private Client client; private List<Bank. Transaction> transactions; // other fields. . . } 1/23/2008 ITK 168 11
Classes: A class is a concept of something Vehicle 4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock, . . . . Truck ………… Sedan Matrix ………… Focus ………… SUV ………… 1/23/2008 ITK 168 12
Inheritance: From membership point of view A SUV is a vehicle, but a vehicle may not be a SUV Any SUV is a Vehicle SUV Base concept Honda Pilot Derived concept Java Object Number Double 1/23/2008 Base class Integer ITK 168 Derived class 13
Inheritance: From functionality point of view All vehicle can do, SUV can too The SUV has every properties and function of the Vehicle SUV Vehicle Derived class Base class 1/23/2008 ITK 168 14
UML (Unified Modeling Language) Diagram Vehicle SUV Honda Pilot Truck Sedan Focus Matrix XRS 1/23/2008 ITK 168 15
A - my_a: int +get_a(): int my_a is invisible to B B B - my_b: int +get_b(): int 1/23/2008 = ITK 168 - my_b: int +get_a(): int +get_b(): int 16
Base class (superclass) & Derived class (extended subclass) // A is a base class public class A { // B is a derived class Public class B extends A { private int b; private int my_a; Public A(int a) { my_a = a; } public B(int a, int b) { super(a); this. b = b; } public int get_a() { return my_a; } } public int get_b() return b; } } A a = new A(2); B b = new B(3, 7); B inherits A’s functions and variables. I = a. get_a(); J = b. get_a(); K = b. get_b(); 1/23/2008 { ITK 168 17
An enhanced robot Robot int street int avenue Direction direction Thing. Bag back Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing() Experiment. Robot int street int avenue Direction direction Thing. Bag back = Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void turn. Around(); void turn. Right(); void move 3(); 1/23/2008 ITK 168 Experiment. Robot(City a. City, int a. Street, int an. Avenue, Direction a. Dir) void move() void turn. Left() void pick. Thing() void put. Thing() void turn. Around(); void turn. Right(); void move 3(); 18
- Slides: 18