Download presentation
Presentation is loading. Please wait.
Published byLisa Hamilton Modified over 6 years ago
1
Robot Class name Attributes Constructor Services, methods
int street int avenue Direction direction ThingBag backbag ... Robot(City aCity, int aStreet, int aAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() Class name Attributes Constructor Services, methods UML class diagram for Robot 1/23/2008 ITK 168
2
Modeling what? Modeling Robots in a City with Software Classes. Robot
int street int avenue Direction direction ThingBag backbag ... Robot(City aCity, int aStreet, int aAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() City String name int stree_No int ave_No ... .... City( ) 1/23/2008 ITK 168
3
Task: deliver X from (1,2) to (3,1) and step away
import becker.robot.*; public class DeliverX{ 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.turnLeft(); karel.turnLeft(); karel.turnLeft(); karel.pickThing(); karek.move(); karel.move(); karel.move(); karel.putThing(); } 1 2 3 4 X 1 2 3 4 named Karel 1/23/2008 ITK 168
4
Task: deliver X from (1,2) to (3,1) and step away
import becker.robot.*; public class DeliverX{ 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.turnLeft(); karel.turnLeft(); karel.turnLeft(); karel.move(); karel.pickThing(); karek.move(); karel.putThing(); } 1 2 3 4 X 1 2 3 4 named Karel 1/23/2008 ITK 168
5
Anatomy of a Java Program
import becker.robot.*; public class DeliverX{ 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.turnLeft(); karel.turnLeft(); karel.turnLeft(); karel.pickThing(); karek.move(); karel.move(); karel.move(); karel.putThing(); } 1/23/2008 ITK 168
6
Package: collection of classes
// Java API: Java Application Programming Interface // (java., javax.) // GUI: Graphical User Interface // // using swing package, JOptionPane class // showMessageDialog method import javax.swing.JOptionPane; public class TestPane{ public static void main(String args[]){ JOptionPane.showMessageDialog( null, "Welcome\nTo\nswing\nPackage"); System.exit(0); } 1/23/2008 ITK 168
7
class defined in the package
A Java program package import javax.swing.JOptionPane; public class time{ public static void main(String args[]){ int x,y,z; String X,Y; X = JOptionPane.showInputDialog("Input x"); Y = JOptionPane.showInputDialog("Input y"); x = Integer.parseInt(X); y = Integer.parseInt(Y); z = x*y; JOptionPane.showMessageDialog( null, x + " * " + y + " = " + z, "The product of " + x + " and " + y, JOptionPane.PLAIN_MESSAGE ); System.exit(0); } class defined in the package 1/23/2008 ITK 168
8
A Java Program: // Text-printing program. public class Welcome1 {
// 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 Welcome1 /********************************************************* *(C) Copyright by * * * **********************************************************/ 1/23/2008 ITK 168
9
Reusing Codes Composition A class has another class, “has a” relation
Inheritance A class has another class, “has a” relation A car has an engine A class inherits another class, “is a” relation An SUV is a car 1/23/2008 ITK 168
10
Composition: has a A DeliverX has a main,
The main has a City, Thing, and a Robot import becker.robot.*; public class DeliverX{ 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
11
Composition: has a A BankAccount has a Client, has a List of BankTransaction public class BankAccount { private Client client; private List<BankTransaction> transactions; // other fields ... } 1/23/2008 ITK 168
12
Classes: A class is a concept of something Vehicle
4 wheels, seats, engine, windows, color…… accelerate, start, stop, turn, lock, Truck ………… Sedan ………… Focus ………… Matrix ………… SUV ………… 1/23/2008 ITK 168
13
Inheritance: From membership point of view
A SUV is a vehicle, but a vehicle may not be a SUV Any SUV is a Vehicle Vehicle SUV Base concept Honda Pilot Derived concept Java Object Number Base class Integer Double Derived class 1/23/2008 ITK 168
14
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
15
UML (Unified Modeling Language) Diagram
Vehicle SUV Truck Sedan Honda Pilot Focus Matrix Matrix XRS 1/23/2008 ITK 168
16
my_a is invisible to B = A - my_a: int +get_a():int B B - my_b: int
+get_b():int B - my_b: int +get_a():int +get_b():int = 1/23/2008 ITK 168
17
Base class (superclass) & Derived class (extended subclass)
// A is a base class public class A { private int my_a; Public A(int a) { my_a = a; } public int get_a() { return my_a; // B is a derived class Public class B extends A { private int b; public B(int a, int b) { super(a); this.b = b; } public int get_b() { return b; A a = new A(2); B b = new B(3,7); I = a.get_a(); J = b.get_a(); K = b.get_b(); B inherits A’s functions and variables. 1/23/2008 ITK 168
18
An enhanced robot = Robot ExperimentRobot ExperimentRobot int street
int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() An enhanced robot ExperimentRobot int street int avenue Direction direction ThingBag backback ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void move() void turnLeft() void pickThing() void putThing() void turnAround(); void turnRight(); void move3(); ExperimentRobot ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir) void turnAround(); void turnRight(); void move3(); = 1/23/2008 ITK 168
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.