1. Trang chủ
  2. » Giáo Dục - Đào Tạo

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

46 12 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 46
Dung lượng 313,62 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Test Posn ConstructorTest: Create a posn import junit.framework.*; public class TestPosn extends TestCase { public void testConstrutor{ new Posn5, 12; Posn aPosn1= new Posn6,8; Posn aPo

Trang 1

Section 01

From Scheme to Java …

A2 Programming Course

Week 1,2

Trang 2

Introduce to Java

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

– Set Path: %JAVA_HOME%\bin

– Set CLASSPATH: %JAVA_HOME%\lib;.

• IDE: JBulider, Eclipse

• Compiler:

– javac FileName.java

• Interpreter:

– java FileName

Trang 3

• Computes how far some pixel is from the origin

• Computes the distance between 2 pixels

Trang 4

Class diagram

Posn

- int x

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

Trang 6

Test Posn Constructor

Test: Create a posn

import junit.framework.*;

public class TestPosn extends TestCase { public void testConstrutor(){

new Posn(5, 12);

Posn aPosn1= new Posn(6,8);

Posn aPosn2 = new Posn(3,4);

}}

Test: Create a posn

(make-posn 5 12)

(define aPosn1 (make-posn 6 8))

(define aPosn2 (make-posn 3 4))

Instance of class

Trang 7

public ??? distanceTo0(???){

…this.x…

…this.y… }

}

Add a contract,

a purpose statement

METHOD SIGNATURE

Trang 8

distanceTo0 function body:

(define (distanceTo0 aPosn)

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

(posn-x aPosn))

(* (posn-y aPosn)

(posn-y aPosn)))))

distanceTo0 method implementation

public class Posn { private int x;

return Math.sqrt(this.x * this.x + this.y * this.y); }

}

Trang 9

Posn aPosn1= new Posn(6,8);

Posn aPosn2 = new Posn(3,4);

} public void testDistanceTo0(){

(define aPosn1 (make-posn 6 8))

(define aPosn2 (make-posn 3 4))

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

(= (distanceTo0 aPosn1) 10)

(= (distanceTo0 aPosn2) 5)

Trang 10

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

Trang 11

Class diagram

Posn

- int x

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

Trang 12

public double distanceTo0(){

return Math.sqrt(this.x * this.x + this.y * this.y);

…that.y… }

Add a contract,

a purpose statement

METHOD SIGNATURE

Trang 13

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

distanceTo0 function:

(define (distanceTo aPosn1 aPosn2)

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

Trang 14

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

}

}

Test distanceTo

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

(define aPosn1 (make-posn 6 8))

(define aPos2 (make-posn 3 4))

(= (distanceTo aPosn1 aPosn2) 5)

Trang 15

- int x

- int y

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

Class diagram

Property or field

Method Data type

Class

Trang 16

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.

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

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

Trang 18

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 19

Test Employee Constructor

Test: Create an employee

}

Test: Create an employee

(make-Employee "Nam" 40)

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

(define aEmployee2 (make-Employee "Minh"

100))

Trang 20

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 21

private int hours;

public Employee(String name, int hours) { this.name = name;

this.hours = hours;

} public int wage(){

return this.hours * 12;

}

}

Trang 22

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

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

(= (wage (make-Employee "Nam" 40)) 480)

(= (wage aEmployee1) 360)

(= (wage aEmployee2) 1200)

Trang 24

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 25

tax body

tax method:

public class Employee {

private String name;

private int hours;

public Employee(String name, int hours) {

Trang 26

assertEquals(new Employee("Nam",40).tax(),72.0,0.001); Employee aEmployee1 = new Employee("Mai",30); Employee aEmployee2 = new Employee("Minh",100); Assert.assertEquals(aEmployee1.tax(),54.0,0.01); Assert.assertEquals(aEmployee2.tax(),180.0,0.01); }

}

Test tax function

(make-Employee "Nam" 40)

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

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

((tax (make-Employee "Nam" 40)) 72)

(= (tax aEmployee1) 54)

(= (tax aEmployee2) 180)

Trang 28

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 29

netpay function:

(define (netpay aEmployee)

(-(wage aEmployee) (tax aEmployee)))

netpay method

public class Employee { 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(){

return ((double)this.wage()) * 0.15;

} public double netpay(){

//this.hours *12 – this.hours*12* 0.15 return this.wage() - this.tax();

Trang 30

Employee aEmployee2 = new Employee("Minh",100); Assert.assertEquals(aEmployee1.netpay(),306.0,0.01); Assert.assertEquals(aEmployee2.netpay(),1020.0,0.01); }

}

Test netpay function

(make-Employee "Nam" 40)

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

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

(=(netpay (make-Employee "Nam" 40)) 408)

(= (netpay aEmployee1) 306)

(= (netpay aEmployee2) 1020)

Trang 32

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 33

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(){

return ((double)this.wage()) * 0.15; }

public double netpay(){

return ((double)this.wage()) - this.tax(); }

public int raiseWage(){

//this.hours * 12 +14;

Trang 34

Employee aEmployee2 = new Employee("Minh",100); assertEquals(aEmployee1.raiseWage(),374.0,0.01);

assertEquals(aEmployee2.raiseWage(),1214.0,0.01); }

}

Test netpay function

(make-Employee "Nam" 40)

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

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

(raiseWage (make-Employee "Nam" 40))

(= (raiseWage aEmployee1) 374)

(= (raiseWage aEmployee2) 1214 )

Trang 36

private int hours;

public Employee(String name, int hours) { this.name = name;

Trang 37

private int hours;

public Employee(String name, int hours) { this.name = name;

this.hours = hours;

}

… public boolean check(){

return this.hours < 100;

}

}

Trang 38

public void testCheck(){

assertTrue( new Employee("Nam",40).check());

Employee aEmployee1 = new Employee("Mai",30);

Employee aEmployee2 = new Employee("Minh",101);

assertTrue(aEmployee1.check()); assertFalse(aEmployee2.check(); }

}

Test check function

(make-Employee "Nam" 40)

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

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

(check (make-Employee "Nam" 40))

(check aEmployee1)

(not (netpay aEmployee2) )

Trang 39

Property or field

Method Class

Data type

Trang 40

Exercise 4.4.2 (HTDP)

• Develop the function tax, which consumes the gross pay and produces the amount of tax owed For a gross pay of $240 or less, the tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any pay over $480, the tax rate is 28%.

• Also develop netpay The function determines the net pay of an employee from the number

of hours worked The net pay is the gross pay minus the tax Assume the hourly pay rate is

Solutions

Trang 41

Scheme gives us these structure definitions:

(define-struct entry (name zip phone))

(define-struct movie (title producer))

(define-struct boyfriend (name hair eyes phone))

(define-struct cheerleader (name number))

(define-struct CD (artist title price))

(define-struct sweater (material size producer))

(define-struct personnel-record (name salary dob ssn))

(define-struct child (parents dob))

(define-struct ball (x y speed-x speed-y))

Translate them to Java class, write constructor and testContructor

Trang 43

Prepare for next week

How to design class hierarchy

I The Varietes of Data   

    1 Primitive Forms of Data   

III Fun Methods   

1

    Methods

3 Methods for Classes   

Trang 44

& Do Exerci se

Trang 45

Solution 4.4.2(HTDP): taxWithRate()

• Method implementation

public double taxWithRate(){

double money = this.wage();

Trang 46

Solution 4.4.2(HTDP): netpayWithRate()

• Method implementation

public double netpayWithRate(){

return this.wage() - this.taxWithRate();

Ngày đăng: 29/03/2021, 10:41

TỪ KHÓA LIÊN QUAN