* ITK 179 1 Robot int street

19
07/05/22 ITK 179 1 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() ... ... Services, methods Attributes UML class diagram for Robot Class name Constructor

Transcript of * ITK 179 1 Robot int street

Page 1: * ITK 179 1 Robot int street

05/01/23 ITK 179 1

Robotint streetint avenueDirection directionThingBag backbag......Robot(City aCity, int aStreet, int aAvenue, Direction aDir)void move()void turnLeft()void pickThing()void putThing()......

Services, methods

Attributes

UML class diagram for Robot

Class name

Constructor

Page 2: * ITK 179 1 Robot int street

05/01/23 ITK 179 2

Modeling what?

Modeling Robots in a City with Software Classes.

Robotint streetint avenueDirection directionThingBag backbag......

Robot(City aCity, int aStreet, int aAvenue, Direction aDir)void move()void turnLeft()void pickThing()void putThing()...

CityString nameint stree_Noint ave_No..........

City(...........)................

Page 3: * ITK 179 1 Robot int street

05/01/23 ITK 179 3

Task: deliver X from (1,2) to (3,1) and step away

X01234

0 1 2 3 4import 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.move();karel.turnLeft(); karel.turnLeft();

karel.turnLeft(); karel.move();karel.pickThing();

karek.move(); karel.move();karel.turnLeft(); karel.turnLeft();karel.turnLeft();karel.move(); karel.putThing();karel.move();

}}

named Karel

Page 4: * ITK 179 1 Robot int street

05/01/23 ITK 179 4

Task: deliver X from (1,2) to (3,1) and step away

X01234

0 1 2 3 4

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.turnLeft();karel.move();karel.move();karel.pickThing();karel.turnLeft(); karel.turnLeft();

karel.turnLeft(); karek.move();

karel.turnLeft(); karel.turnLeft(); karel.turnLeft();

karel.move();karel.turnLeft();karel.move();

karel.putThing();karel.turnLeft(); karel.turnLeft();karel.turnLeft();karel.move();

}}

named Karel

Page 5: * ITK 179 1 Robot int street

05/01/23 ITK 179 5

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.move();karel.turnLeft(); karel.turnLeft();

karel.turnLeft(); karel.move();karel.pickThing();

karek.move(); karel.move();karel.turnLeft(); karel.turnLeft();karel.turnLeft();karel.move(); karel.putThing();karel.move();

}}

Anatomy of a Java Program

Page 6: * ITK 179 1 Robot int street

05/01/23 ITK 179 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); }}

Page 7: * ITK 179 1 Robot int street

05/01/23 ITK 179 7

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 1992-2003 by ...... * * ***********************************************************/

Page 8: * ITK 179 1 Robot int street

05/01/23 ITK 179 8

Reusing Codes

1. Composition

2. Inheritance

A class has another class, “has a” relation

A class inherits another class, “is a” relation

A car has an engine

An SUV is a car

Page 9: * ITK 179 1 Robot int street

05/01/23 ITK 179 9

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); ...... }}

Page 10: * ITK 179 1 Robot int street

05/01/23 ITK 179 10

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 ...}

Page 11: * ITK 179 1 Robot int street

05/01/23 ITK 179 11

Classes:A class is a concept of something

Vehicle4 wheels, seats, engine, windows, color……

accelerate, start, stop, turn, lock, ...............

Truck…………

Sedan…………

SUV…………

Matrix…………

Focus…………

Page 12: * ITK 179 1 Robot int street

05/01/23 ITK 179 12

Inheritance: From membership point of view

Vehicle

SUV Honda Pilot

Object

NumberIntegerDouble

Base concept

Derived concept

JavaBase class

Derived class

A SUV is a vehicle, but a vehicle may not be a SUV

Any SUV is a Vehicle

Page 13: * ITK 179 1 Robot int street

05/01/23 ITK 179 13

Inheritance: From functionality point of view

SUV

Vehicle

Derived class

Base class

All vehicle can do, SUV can too

The SUV has every properties and function of the Vehicle

Page 14: * ITK 179 1 Robot int street

05/01/23 ITK 179 14

UML (Unified Modeling Language) Diagram

Vehicle

Truck SedanSUV

MatrixFocusHonda Pilot

Matrix XRS

Page 15: * ITK 179 1 Robot int street

05/01/23 ITK 179 15

A- my_a: int+get_a():int

B- my_b: int+get_b():int

B- my_b: int+get_a():int+get_b():int

=

my_a is invisible to B

Page 16: * ITK 179 1 Robot int street

05/01/23 ITK 179 16

Base class (superclass) & Derived class (extended subclass)

// A is a base classpublic class A { private int my_a;

Public A(int a) { my_a = a; }

public int get_a() { return my_a; }}

// B is a derived classPublic 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 andvariables.

Page 17: * ITK 179 1 Robot int street

05/01/23 ITK 179 17

An enhanced robot

Robotint streetint avenueDirection directionThingBag backback

Robot(City aCity, int aStreet, int anAvenue, Direction aDir)void move()void turnLeft()void pickThing()void putThing()

ExperimentRobot

ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir)void turnAround();void turnRight();void move3();

=

ExperimentRobot

int streetint avenueDirection directionThingBag backback

ExperimentRobot(City aCity, int aStreet, int anAvenue, Direction aDir)void move()void turnLeft()void pickThing()void putThing()void turnAround();void turnRight();void move3();

Page 18: * ITK 179 1 Robot int street

18

A Java program

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); }}

classes methods

package

arguments

Variable declaration

class defined in the package

Page 19: * ITK 179 1 Robot int street

05/01/23 ITK 168 19

Selection sort algorithm in Java

public static class SelectionSort {public void sort(int a[]) { for (int i = 0; i<a.length-1; i++) { // select one for a[i]

int j = min(a,i); exchange(a,i,j); } }

// select the minimum between a[s] to the end private int min(int a[], int s) {int m = s; for (int i=s; i<a.length;i++)

if (a[i] < a[m]) m=i; return m; }

private void exchange(int[] a, int i, int j) { int temp = a[i];

a[i] = a[j]; a[j] = temp;

}

} // end of SelectionSort