What is a computer

Download Report

Transcript What is a computer

IDE (Integrated Development Environment)
1.
2.
3.
4.
A customized plain text editor
Compiler
Loader
Debugging tool
1.
2.
3.
4.
vi (or emacs)
javac hello.java
java hello
......
JDK (Java Development Kit) = IDE+JRE
Eclipse = IDE
3/28/2016
IT 179
1
ProblemsSolutions cycle
Customers
System analyst, Project leader
Problems
Senior Programmers
Algorithms
Programmers
Syntax
Semantics
(human)
Programs in JAVA
(or any high level Programming Language)
compiler
Assembly
IDE
Assembler (computers)
VM
Machine code
linker
3/28/2016
Results
IT 179
2
How much effort in
solving a problem
After
ITK 168
This is a basic requirement.
We need to reduce this portion
Java's Features
Solving Problem
After
ITK 179
This is more important
and difficult;
data structures,
algorithm analysis
Java's Features
3/28/2016
IT 179
Solving Problem
3
Procedure- v. Object- oriented
To solve a program is:
To find a way to manipulate data -•
•
We design procedures
Procedure-Oriented Programming
Statements + functions  programs
To find objects to model the problem –
•
•
We choose data (object)
Object-Oriented Programming
Classes + Objects  programs
(interfaces, methods, attributes…)
3/28/2016
IT 179
4
Robot
Class name
int street
int avenue
Direction direction
ThingBag backbag
...
...
Attributes
Constructor
Robot(City aCity, int aStreet, int aAvenue, Direction aDir)
void move()
void turnLeft()
void pickThing()
void putThing()
...
...
Services, methods
UML class diagram for Robot
3/28/2016
IT 179
5
Modeling what?
Modeling Robots in a City with Software Classes.
Robot
City
int street
int avenue
Direction direction
ThingBag backbag
...
...
String name
int stree_No
int ave_No
...
...
....
Robot(City aCity, int aStreet, int aAvenue, Direction aDir)
void move()
void turnLeft()
void pickThing()
void putThing()
...
City(...........)
....
....
....
....
3/28/2016
IT 179
6
package
Anatomy of a Java Program
import javax.swing.JOptionPane;
class defined
public class time{
public static void main(String args[]){
int x,y,z;
String X,Y;
classes
X
Y
x
y
=
=
=
=
in the package
Variable declaration
JOptionPane.showInputDialog("Input x");
JOptionPane.showInputDialog("Input y");
Integer.parseInt(X);
Integer.parseInt(Y);
methods
z = x*y;
JOptionPane.showMessageDialog(
null,
x + " * " + y + " = " + z,
"The product of " + x + " and " + y,
JOptionPane.PLAIN_MESSAGE );
System.exit(0);
}
}
IT 179
arguments
3/28/2016
7
Task: deliver X from (1,2) to (3,1) and step away
0 1 2 3 4
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.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();
}
X
named Karel
}
3/28/2016
IT 179
8
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.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();
}
}
3/28/2016
IT 179
9
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);
}
}
3/28/2016
IT 179
10
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 ......
*
*
*
**********************************************************/
3/28/2016
IT 179
11
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
3/28/2016
IT 179
12
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);
......
}
}
3/28/2016
IT 179
13
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
...
}
3/28/2016
IT 179
14
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
…………
3/28/2016
IT 179
15
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
Double
3/28/2016
Base class
Integer
Derived class
IT 179
16
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
3/28/2016
IT 179
17
UML (Unified Modeling Language) Diagram
Vehicle
SUV
Truck
Sedan
Honda Pilot
Focus
Matrix
Matrix XRS
3/28/2016
IT 179
18
A
- my_a: int
my_a is invisible to B
+get_a():int
B
B
- my_b: int
+get_b():int
3/28/2016
=
IT 179
- my_b: int
+get_a():int
+get_b():int
19
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();
3/28/2016
{
IT 179
20
An enhanced
robot
Robot
int street
int avenue
Direction direction
ThingBag backback
ExperimentRobot
Robot(City aCity, int aStreet, int anAvenue,
Direction aDir)
void move()
void turnLeft()
void pickThing()
void putThing()
ExperimentRobot
int street
int avenue
Direction direction
ThingBag backback
=
ExperimentRobot(City aCity, int aStreet,
int anAvenue, Direction aDir)
void turnAround();
void turnRight();
void move3();
3/28/2016
IT 179
ExperimentRobot(City aCity, int aStreet,
int anAvenue, Direction aDir)
void move()
void turnLeft()
void pickThing()
void putThing()
void turnAround();
void turnRight();
void move3();
21
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
3/28/2016
IT 179
22