06-OOP-More

06-OOP-More - 2006 Marty Hall Object-Oriented Programming...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
2 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall Object-Oriented Programming in Java: More Capabilities 3 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 J2EE training: http://courses.coreservlets.com Agenda Overloading Designing “real” classes Inheritance Advanced topics Abstract classes Interfaces Understanding polymorphism Setting CLASSPATH and using packages Visibility modifiers Creating on-line documentation using JavaDoc 5 J2EE training: http://courses.coreservlets.com Example 4: Overloading class Ship4 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name; public Ship4(double x, double y, double speed, double direction, String name) { this.x = x; this.y = y; this.speed = speed; this.direction = direction; this.name = name; } public Ship4(String name) { this.name = name; } private double degreesToRadians(double degrees) { return(degrees * Math.PI / 180.0); } ...
Background image of page 2
6 J2EE training: http://courses.coreservlets.com Overloading (Continued) ... public void move() { move(1); } public void move(int steps) { double angle = degreesToRadians(direction); x = x + (double)steps * speed * Math.cos(angle); y = y + (double)steps * speed * Math.sin(angle); } public void printLocation() { System.out.println(name + " is at (" + x + "," + y + ")."); } } 7 J2EE training: http://courses.coreservlets.com Overloading: Testing and Results public class Test4 { public static void main(String[] args) { Ship4 s1 = new Ship4("Ship1"); Ship4 s2 = new Ship4(0.0, 0.0, 2.0, 135.0, "Ship2"); s1.move(); s2.move(3); s1.printLocation(); s2.printLocation(); } } Compiling and Running: javac Test4.java java Test4 Output: Ship1 is at (1,0). Ship2 is at (-4.24264,4.24264).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 J2EE training: http://courses.coreservlets.com Overloading: Major Points Idea Allows you to define more than one function or constructor with the same name Overloaded functions or constructors must differ in the number or types of their arguments (or both), so that Java can always tell which one you mean Simple examples: Here are two square methods that differ only in the type of the argument; they would both be permitted inside the same class definition. // square(4) is 16 public int square(int x) { return(x*x); } // square("four") is "four four" public String square(String s) { return(s + " " + s); } 9 J2EE training: http://courses.coreservlets.com Example 5: OOP Design and Usage /** Ship example to demonstrate OOP in Java. */ public class Ship { private double x=0.0, y=0.0, speed=1.0, direction=0.0; private String name; /** Get current X location. */ public double getX () { return(x); } /** Set current X location. */ public void setX (double x) { this.x = x; }
Background image of page 4
10 J2EE training: http://courses.coreservlets.com Example 5: Major Points Encapsulation Lets you change internal representation and data structures without users of your class changing their code
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/16/2009 for the course CSCI 571 taught by Professor Papa during the Summer '07 term at USC.

Page1 / 22

06-OOP-More - 2006 Marty Hall Object-Oriented Programming...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online