Tải bản đầy đủ (.pptx) (46 trang)

From scheme to java (lập TRÌNH cơ bản SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (219.47 KB, 46 trang )

A2 Programming Course

Section 01

From Scheme to Java …
Week 1,2

1


Introduce to Java



JDK








Set Path: %JAVA_HOME%\bin
Set CLASSPATH: %JAVA_HOME%\lib;.

IDE: JBulider, Eclipse
Compiler:






Set JAVA_HOME: D:\Program Files\Java\jdk1.5.0_04

javac FileName.java

Interpreter:



java FileName

2


Posn example



Suppose we wish to represent the pixels (colored dots) on our computer monitors. A pixel is very
much like a Cartesian point. It has an x coordinate, which tells us where the pixel is in the
horizontal direction, and it has a y coordinate, which tells us where the pixel is located in the
downwards vertical direction. Given the two numbers, we can locate a pixel on the monitor




Computes how far some pixel is from the origin
Computes the distance between 2 pixels


3


Class diagram

Posn
- int x
- int y
+ ??? distanceTo0(???)
+ ??? distanceTo(???)

4


Define Class
Define structure

Define class

(define-struct posn (x y))

public class Posn{
private int x;
private int y;
}

Constructor:
(make-posn

Constructor:

public class Posn{
private int x;
private int y;

public Posn(int x, int y){
this.x = x;
this.y = y;
}
}
5


Test Posn Constructor
Test: Create a posn
(make-posn 5 12)

Test: Create a posn
import junit.framework.*;
public class TestPosn extends TestCase {
public void testConstrutor(){

(define aPosn1 (make-posn 6 8))

new Posn(5, 12);
Posn aPosn1= new Posn(6,8);

(define aPosn2 (make-posn 3 4))

Posn aPosn2 = new Posn(3,4);
}

}

Instance of class

6


distanceTo0 template
distanceTo0 function:
(define (distanceTo0 aPosn)

distanceTo0 method
public class Posn {
private int x;

...(posn-x aPosn)...

private int y;

...(posn-y aPosn)...)

public Posn(int x, int y) {
this.x = x;
this.y = y;
}
// Computes how far this pixel is from the origin
public ??? distanceTo0(???){
…this.x…
Add a contract,


…this.y…

a purpose statement

}
METHOD SIGNATURE

}

7


distanceTo0
distanceTo0 function body:
(define (distanceTo0 aPosn)
(sqrt(+ (* (posn-x aPosn)

distanceTo0 method implementation
public class Posn {

(posn-x aPosn))

private int x;

(* (posn-y aPosn)

private int y;

(posn-y aPosn)))))


public Posn(int x, int y) {
this.x = x;
this.y = y;
}
public double distanceTo0(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}

8


Test distanceTo0
Test distanceTo0 function
(make-posn 5 12)

Test distanceTo0 method
import junit.framework.*;

(define aPosn1 (make-posn 6 8))
(define aPosn2 (make-posn 3 4))

public class TestPosn extends TestCase {
public void testConstrutor(){
new Posn(5,12);

(= (distanceTo0 (make-posn 5 12)) 13)

Posn aPosn1= new Posn(6,8);
Posn aPosn2 = new Posn(3,4);


(= (distanceTo0 aPosn1) 10)

}

(= (distanceTo0 aPosn2) 5)

public void testDistanceTo0(){
assertEquals(new Posn(5,12).distanceTo0(),13.0,0.001);

Posn aPosn1 = new Posn(6,8);
assertEquals(aPosn1.distanceTo0(),10.0,0.001);

Posn aPosn2 = new Posn(3,4);
assertEquals(aPosn2.distanceTo0(),5.0,0.001);
}
}

9


Take note



Java Methods are roughly like Scheme functions. Like a function, a method consumes data
and produces data. Unlike a function, however, a method is always associated with a class of
data.




Instances of this class are the method's main argument. Because of that, a Java programmer
does not speak of calling functions for some arguments, but instead speaks of INVOKING an
object's method

10


Class diagram

Posn
- int x
- int y
+ double distanceTo0()
+ ??? distanceTo(???)

11


distanceTo template
distanceTo0 function:
(define (distanceTo aPosn1

distanceTo0 method
public class Posn {
private double x;

aPosn2)

private double y;


...(posn-x aPosn1)...

public Posn(int x, int y) {

...(posn-y aPosn1)...

this.x = x;

...(posn-x aPosn2)...

this.y = y;

...(posn-y aPosn2)...)

}
public double distanceTo0(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}

// Computes distance from this posn to another

posn

public ??? distanceTo(???){
…this.x…
…this.y…
Add a contract,

…that.x...


a purpose statement

…that.y…
METHOD SIGNATURE

}
12

}


distanceTo
distanceTo0 function:

distanceTo0 method:

(define (distanceTo aPosn1 aPosn2)

public class Posn {

(sqrt(+ (* (-(posn-x aPosn2)

(posn-x private int x;
private int y;

aPosn1))
(- (posn-x aPosn2)
(posn-x aPosn1)))


public Posn(int x, int y) {

(* (- (posn-y aPosn2)

this.x = x;

(posn-y aPosn1))

this.y = y;

(- (posn-y aPosn2)

}

(posn-y aPosn1))))))

public double distanceTo0(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public double distanceTo(Posn that){
return Math.sqrt((that.x - this.x)*(that.x - this.x) +(that.y - this.y)*(that.y - this.y));
}
}
13


Test distanceTo
Test distanceTo

Test distanceTo


(= (distanceTo (make-posn 6 8) (make-posn 3 4)) 5)

import junit.framework.*;
public class TestPosn extends TestCase {

(define aPosn1 (make-posn 6 8))

public void testConstrutor(){

(define aPos2 (make-posn 3 4))



(= (distanceTo aPosn1 aPosn2) 5)

}
public void testDistanceTo0(){

}
public void testDistanceTo(){
assertEquals(new Posn(6,8).distanceTo(new Posn(3,4)),5.0,0.001);

Posn aPosn1 = new Posn(6,8);
Posn aPosn2 = new Posn(3,4);
assertEquals(aPosn1.distanceTo(aPosn2),5.0,0.001);
}
}
14



Class diagram
Class

Posn
- int x
- int y
Data type

Property or field

+ double distanceTo0()
+ double distanceTo(Posn that)

Method

15


Employee example
An employee has his name and the number of hours of work.




Determines the wage of an employee from the number of hours of work. Suppose 12 dollars per hour.
Exercise 2.3.1(HTDP). Utopia's tax accountants always use programs that compute income taxes even
though the tax rate is a solid, never-changing 15%. Determine the tax on the gross pay.






Also determine netpay of an employee from the number of hours worked base on gross pay and tax .
Exercise 10.1.2 (HTDP): Give everyone a raise to $14
Exercise 10.1.2 (HTDP): No employee could possibly work more than 100 hours per week. To protect the
company against fraud, the function should check that the hours doesn’t exceed 100. If it does, the function
returns false. Otherwise, it returns true

16


Class diagram

Employee
- String name
- int hours
+
+
+
+
+

???
???
???
???
???

wage(???)

tax(???)
netpay(???)
raiseWage(???)
check(???)

17


Define Class
Define structure

Define class

(define-struct Employee(name hours))

public class Employee {
private String name;
private int hours;
}

Constructor:
(make-Employee

Constructor:
public class Employee {
private String name;
private int hours;

public Employee(String name,


int hours) {

this.name = name;
this.hours = hours;
}
}
18


Test Employee Constructor
Test: Create an employee

Test: Create an employee

(make-Employee "Nam" 40)

import junit.framework.*;

(define aEmployee1 (make-Employee"Mai"30))

public class TestEmployee extends TestCase {

(define aEmployee2 (make-Employee "Minh"

public void testContructor(){

100))

new Employee("Nam",40);
Employee aEmployee1 = new


Employee("Mai",30);

Employee aEmployee2 = new

Employee("Minh",102);

}
}

19


wage template
wage function:

wage method

(define (wage aEmployee)

public class Employee {
private String name;

…(Employee-name aEmployee)…

private int hours;

…(Employee-hours aEmployee)…

public Employee(String name, int


)

hours) {

this.name = name;
this.hours = hours;
}
// Determines the wage of an employee from the number of hours of work
public ??? wage(???){
…this.name…
…this.hours …
}

Add a contract,
a purpose statement

}

METHOD SIGNATURE

20


wage body
wage function:
(define (wage aEmployee)

wage method
public class Employee {

private String name;

(*(Employee-hours aEmployee) 12))

private int hours;
public Employee(String name, int
this.name = name;
this.hours = hours;
}
public int wage(){
return this.hours * 12;
}
}

21

hours) {


Test wage
Test wage function

Test wage method

(make-Employee "Nam" 40)

import junit.framework.*;
public class TestEmployee extends TestCase {

(define aEmployee1 (make-Employee"Mai"30))

(define aEmployee2 (make-Employee "Minh" 100))


public void testWage(){
assertEquals(new Employee("Nam",40).wage(),480,0.01);

(= (wage (make-Employee "Nam" 40)) 480)
(= (wage aEmployee1) 360)
(= (wage aEmployee2) 1200)

Employee aEmployee1 = new

Employee("Mai",30);

Employee aEmployee2 = new

Employee("Minh",100);

assertEquals(aEmployee1.wage(),360,0.01);
assertEquals(aEmployee2.wage(),1200,0.01);
}
}

22


Class diagram
Employee
- String name
- int hours

+
+
+
+
+

int wage()
??? tax(???)
??? netpay(???)
??? raiseWage(???)
??? check(???)

23


tax template
tax function:

tax method

(define (tax aEmployee)

public class Employee {
private String name;

…(Employee-name aEmployee)…

private int hours;

…(Employee-hours aEmployee)…


public Employee(String name, int

)

hours) {

this.name = name;
this.hours = hours;
}
// Determines the tax of an employee from the wage
public ??? tax(???){
…this.name…
…this.hours …
}

Add a contract,
a purpose statement

}

METHOD SIGNATURE

24


tax body
tax function:
(define (tax aEmployee)


tax method:
public class Employee {

(*(wage aEmployee) 0.15))
private String name;
private int hours;
public Employee(String name, int hours) {
this.name = name;
this.hours = hours;
}
public int wage(){
return this.hours * 12;
}
public double tax(){
//this.hours * 12 * 0.15;
return this.wage() * 0.15;
}

25


×