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

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

Đ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

A2 Programming Course Section 01 From Scheme to Java … Week 1,2 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 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 pixels Class diagram Posn - int x - int y + ??? distanceTo0(???) + ??? distanceTo(???) 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; } } Test Posn Constructor Test: Create a posn (make-posn 12) Test: Create a posn import junit.framework.*; public class TestPosn extends TestCase { public void testConstrutor(){ (define aPosn1 (make-posn 8)) new Posn(5, 12); Posn aPosn1= new Posn(6,8); (define aPosn2 (make-posn 4)) Posn aPosn2 = new Posn(3,4); } } Instance of class 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 } 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); } } Test distanceTo0 Test distanceTo0 function (make-posn 12) Test distanceTo0 method import junit.framework.*; (define aPosn1 (make-posn 8)) (define aPosn2 (make-posn 4)) public class TestPosn extends TestCase { public void testConstrutor(){ new Posn(5,12); (= (distanceTo0 (make-posn 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); } } 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 raiseWage template raiseWage function: netpay method (define (raiseWage 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; } // Raise the wage of employee with 14 $ public ??? raiseWage(???){ …this.name… …this.hours … } Add a contract, a purpose statement } METHOD SIGNATURE 32 raiseWage raiseWage method raiseWage function: public class Employee { (define (raiseWage aEmployee) private String name; private int hours; (+(wage aEmployee) 14)) 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; return this.wage() + 14; } 33 } Test raiseWage Test netpay function Test raiseWage 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 testRaiseWage(){ assertEquals(new Employee("Nam",40).raiseWage(),494,0.001); (raiseWage (make-Employee "Nam" 40)) (= (raiseWage aEmployee1) 374) Employee aEmployee1 = new Employee("Mai",30); (= (raiseWage aEmployee2) 1214) Employee aEmployee2 = new Employee("Minh",100); assertEquals(aEmployee1.raiseWage(),374.0,0.01); assertEquals(aEmployee2.raiseWage(),1214.0,0.01); } } 34 Class diagram Employee - String name - int hours + + + + + int wage() double tax() double netpay() double raiseWage() ??? check(???) 35 check template check function: check method (define (check 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 whether the number of hours of work exceeds 100 public ??? check(???){ …this.name… …this.hours … } Add a contract, a purpose statement } METHOD SIGNATURE 36 check netpay function: check method public class Employee { (define (check aEmployee) private String name; private int hours; (cond public Employee(String name, int [(< (Employee-hours aEmployee) 100) true] this.name = name; [else false])) this.hours = hours; } … public boolean check(){ return this.hours < 100; } } 37 hours) { Test check Test check function Test check method (make-Employee "Nam" 40) import junit.framework.*; (define aEmployee1 (make-Employee"Mai"30)) public class TestEmployee extends TestCase { (define aEmployee2 (make-Employee "Minh" 100)) (check (make-Employee "Nam" 40)) public void testCheck(){ assertTrue( new Employee("Nam",40).check()); (check aEmployee1) (not (netpay aEmployee2)) Employee aEmployee1 = new Employee("Mai",30); Employee aEmployee2 = new Employee("Minh",101); assertTrue(aEmployee1.check()); assertFalse(aEmployee2.check(); } } 38 Class diagram Class Employee - String name - int hours Property or field Data type + + + + + int wage() double tax() double netpay() double raiseWage() boolean check() Method 39 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 $12 Solutions 40 Exercises 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 41 Exercises (cont) • Exercise 6.4.2(HTDP).   Write Java class, constructor and test constructor for representing points in time since midnight A point in time consists of three numbers: hours, minutes, and seconds.   42 Prepare for next week How to design class hierarchy I  The Varietes of Data     1  Primitive Forms of Data     2  Compound Data: Classes III  Fun Methods  1  Methods 2  Computing with Primitive Types 3  Methods for Classes 43 Relax… & Do Exercise 44 Solution 4.4.2(HTDP): taxWithRate() • Method implementation public double taxWithRate(){ double money = this.wage(); if (money < 240) return 0.0; if (money < 480) return money * 0.15; return money * 0.28; } • Unit testing public void testTaxWithRate(){ assertEquals(new Employee("Nam",10).taxWithRate(),0.0,0.001); Employee aEmployee1 = new Employee("Mai",30); assertEquals(aEmployee1.taxWithRate(),54.0,0.001); Employee aEmployee2 = new Employee("Minh",100); assertEquals(aEmployee2.taxWithRate(),336.0,0.001); 45 Back Solution 4.4.2(HTDP): netpayWithRate() • Method implementation public double netpayWithRate(){ return this.wage() - this.taxWithRate(); } • Unit testing public void testNetpayWithRate(){ assertEquals(new Employee("Nam",10).netpayWithRate(),120.0,0.001); Employee aEmployee1 = new Employee("Mai",30); assertEquals(aEmployee1.netpayWithRate(),306.0,0.001); Employee aEmployee2 = new Employee("Minh",100); assertEquals(aEmployee2.netpayWithRate(),864.0,0.001); } 46 Back ...Introduce to Java • JDK – – – • • Set Path: %JAVA_ HOME%in Set CLASSPATH: %JAVA_ HOME%lib; IDE: JBulider, Eclipse Compiler: – • Set JAVA_ HOME: D:Program Files Java jdk1.5.0_04 javac FileName .java. .. speed-y)) Translate them to Java class, write constructor and testContructor 41 Exercises (cont) • Exercise 6.4.2(HTDP).   Write Java class, constructor and test constructor for representing points... int x - int y + double distanceTo0() + ??? distanceTo(???) 11 distanceTo template distanceTo0 function: (define (distanceTo aPosn1 distanceTo0 method public class Posn { private double x; aPosn2)

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

Mục lục

    Prepare for next week

Tài liệu cùng người dùng

Tài liệu liên quan