1 / 16

Week 5 : Defining Classes and Methods

Week 5 : Defining Classes and Methods. Review. Loop statements while do-while for. Objective. Learn how to define classes Learn how to define and use methods Learn difference between class and object Learn private variable – get, set method Learn call by reference. Class (Design).

ardice
Download Presentation

Week 5 : Defining Classes and Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 5: Defining Classes and Methods

  2. Review Loop statements while do-while for

  3. Objective • Learn how to define classes • Learn how to define and use methods • Learn difference between class and object • Learn private variable – get, set method • Learn call by reference

  4. Class (Design) amount of fuel __10__ speed __55__ license plate _135XJK_ amount of fuel __14__ speed __2__ license plate _Sues car_ Data: amount of fuel ______ speed ______ license plate _______ Functions: increaseSpeed: stop: amount of fuel __2__ speed __75__ license plate _351WLF_ • Not a real product • Just a design Class definition vs. Object Object (Product) • Real product based on a design (class) • Each product (object) can have own data

  5. Define class & Create object OK! Here you are Class name Instance Variables Class Definition Memory Address public class Point { public double x; public double y; ….. public void increaseX(int increment ) { x = x + increment; } public void decreaseX(int decrement ) { x = x – decrement; } } ... public class Point { public double x; public double y; ….. public void increaseX(int increment ) { x = x + increment; } public void decreaseX(int decrement ) { x = x – decrement; } } p1 300 ... Give me an object p2 600 ... 300 300 x = 0.0 y = 0.0 public void increaseX(int increment) public void decreaseX(int decrement) x = 5.0 Define methods ... 600 Object create & use x = 0.0 y = 0.0 public void increaseX(int increment) public void decreaseX(int decrement) x = -10.0 public class TestPoint { public static void main( String[] args ) { Point p1 Point p2 = new Point(); p1.increaseX(5); p2.decreaseX(10); p1.decreaseX(3); } } Create Object new Point(); = ... Use methods

  6. Define methods (1/3) Methods without return value public class Point { ….. public void printPoint( ) { System.out.print("The Point is :"); System.out.println("("+x+" , "+y+ ") "); } ..... } // There is no return value When the method has no return value, you should write ‘void’ <Memory> <Address> ... p3 700 ... public class TestPoint { public static void main( String[] args ) { ...... Point p3 = new Point(); p3.printPoint(); ...... } } 700 x = 0.0 y = 0.0 printPoint() { ... } ...

  7. Define methods (2/3) Methods with a return value public class Point { ….. public doubleaddXAndY( ) { return x+y; } // There is a return value You should write the type of return value (int, double, …) here The type of return value is “double” <Memory> <Address> 3.0+2.0=5.0 5.0 ... p4 800 ... public class TestPoint { public static void main( String[] args ) { ...... Point p4 = new Point(); p4.x=3.0; p4.y=2.0; double z = p4.addXAndY(); ...... } } 800 x = 0.0 y = 0.0 addXAndY() { ... } x = 3.0 y = 2.0 ... z 5.0 ...

  8. Define methods (3/3) Methods with parameters public int sum( int a, int b) { return a+b; } public void printSum() { int c = sum(3, 8); } parameter list 3+8 Type of parameters should be matched 11

  9. p2 p1 … … … … … 1200 300 Console x y x y Memory Address … … … 1200 300 Practice– Class define & use • Look into “Point.java” in the “lab4.zip” and execute the “TestPoint.java”. • “Point.java” and “TestPoint.java” must be in the same directory. • public class Point { • public double x; • public double y; • public void printPoint() { • System.out.println ("("+x+" , "+y+")"); • } • public void increaseX(int increment) { • x = x+increment; • } • public void decreaseX(int decrement) { • x = x-decrement; • } • } • public class TestPoint { • public static void main(String[] args) { • Point p1 = new Point(); • Point p2 = new Point(); • p1.x = 0; • p1.y = 0; • p2.x = 1; • p2.y = 2; • p1.increaseX(5); • p2.decreaseX(10); • p1.decreaseX(3); • p1.printPoint(); • p2.printPoint(); • } • } : 5.0 : 0.0 : 2.0 : 1.0 : -9.0 (2.0 , 0.0) : 0.0 : 2.0 (-9.0 , 2.0)

  10. public vs. private • private • Keyword to make instance variablewhich cannot be accessed from outside of the class • Need accessor method, mutator method to access private variable <Practice> Modify “Point.java” like the below, and run “TestPoint.java” public class Point { public double x; public double y; … } public class Point { private double x; private double y; … } • p1.x = 0; • p1.y = 0; • p2.x = 1; • p2.y = 2; <Error!> *Because variable x and y are private The field Point.x is not visible. // It is private variable The field Point.y is not visible. // It is private variable

  11. p2 p1 … … … … … 500 100 x y x y Memory Address … … … 500 100 set, get method public void setX (double newX) { x = newX; } public void setY (double newY) { y = newY; } public void setXY (double newX, double newY) { x = newX; y = newY; } Method to change the Point’s private variable x and y into other value - newX and newY (mutator method) Method to access private variable of Point class – x, y (accessor method) public int getX() { return x; } public int getY() { return y; } x 10 <Example> Point p1= new Point(); Point p2= new Point(); p1.setX(10); p1.setY(5); P2.setXY(0, 0); int x = p1.getX(); : 10.0 : 0.0 : 5.0 : 0.0

  12. The type of return value can be a class The type of parameter can be a class p 500 x y 500 Call by reference public Point findCenter(Point p1, Point p2) { Point p = new Point(); double x = (p1.getX() + p2.getX()) / 2; double y = (p1.getY() + p2.getY()) / 2; p.setX(x); p.setY(y); return p; } 30 301 (2.0+3.0)/2 (3.0+4.0)/2 500 p2 p3 p1 x y Point p1, p2, p3; p1 = new Point(); p2 = new Point(); p1.setXY(2,3); p2.setXY(3,4); p3 = findCenter(p1,p2) … 30 ? 2.5 … … 301 ? 500 ? 3.5 … … x y : 2.0 x y : 3.0 : 2.5 … … … : 3.0 : 4.0 : 3.5 30 301 Memory Address 500 30, 301 p3.getY(); 3.5

  13. Practice (1/2) • Define SimpleElevatorclass

  14. Practice (2/2) • SimpleElevator class • Instance variables • private int numOfFloor – from 1st floor to numOfFloorth floor • private int currentFloor – the elevator is currently at currentFloorth floor • private boolean doorOpened – whether the door of the elevator is opened • private int travelLength – total distance that the elevator has traveled. • public methods • SimpleElevator(int numFloor, int initCurrFloor) • Constructor. Use parameter to initialize field. Also set the doorOpened=false, travelLength =0 • void closeDoor() -Assign false to doorOpened • If the door is already closed, print “"** Error: The door is already closed." • void openDoor() - Assign true to doorOpened • If the door is already opened, print “"** Error: The door is already opened." • boolean isDoorOpened() – whether the door is opened. • int getCurrentFloor() - returns currentFloor • int getTravelLength() – returns travelLength • void resetTravelLength() – set travelLength to 0 • boolean goFloor(int destinFloor) • 1 <= destinFloor <= numOfFloor • If not, print “** Error: Invalid destination” and return false • doorOpened == false • if not, print “** Error: Please close the door first” and return false • else • increment travelLength by Math.abs(currentFloor – destinFloor) • set currentFloor to destinFloor, and return true • boolean isUsedMore(SimpleElevator e)- Compare travelLength

  15. Run : TestSimpleElevator class public class TestSimpleElevator { public static void main(String[] args) { SimpleElevator elevator1 = new SimpleElevator(63, 1); SimpleElevator elevator2 = new SimpleElevator(15, 1); System.out.println("elevator1 is at " + elevator1.getCurrentFloor() + "th floor"); elevator1.goFloor(105); System.out.println("elevator1 is at " + elevator1.getCurrentFloor() + "th floor"); elevator1.openDoor(); elevator1.goFloor(63); System.out.println("elevator1 is at " + elevator1.getCurrentFloor() + "th floor"); elevator1.closeDoor(); elevator1.closeDoor(); elevator1.goFloor(63); System.out.println("elevator1 is at " + elevator1.getCurrentFloor() + "th floor"); System.out.println("elevator1's door is " + (elevator1.isDoorOpened() ? "opened" : "closed")); elevator1.goFloor(40); elevator1.goFloor(60); elevator1.goFloor(60); elevator1.goFloor(10); System.out.println("elevator1's travel length is " + elevator1.getTravelLength()); System.out.println("elevator1's is " + (elevator1.isUsedMore(elevator2) ? " " : " not ") + "used more"); elevator1.resetTravelLength(); System.out.println("After resetting, elevator1's travel length is " + elevator1.getTravelLength()); } <Execution Result> elevator1 is at 1th floor ** Error: Invalid destination elevator1 is at 1th floor ** Error: Please close the door first. elevator1 is at 1th floor ** Error: The door is already closed. elevator1 is at 63th floor elevator1's door is closed elevator1's travel length is 155 elevator1's is used more After resetting, elevator1's travel length is 0

  16. Template: SimpleElevator class public class SimpleElevator { private int numOfFloor; private int currentFloor; boolean doorOpened; private int travelLength; public SimpleElevator(int numFloor, int initPosition) { numOfFloor = numFloor; if (initPosition > numFloor || initPosition < 1) currentFloor = 1; else currentFloor = initPosition; doorOpened = false; travelLength = 0; } public void openDoor() { } public void closeDoor() { } public boolean isDoorOpened() {} public int getCurrentFloor() { } public int getTravelLength() { } public void resetTravelLength() { } public boolean goFloor(int destinFloor) { } public boolean isUsedMore(SimpleElevator e) {} }

More Related