05-OOP-Basics

05-OOP-Basics - 2006 Marty Hall Basic Object-Oriented...

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

View Full Document Right Arrow Icon
2 J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, AJAX, Java 5, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2006 Marty Hall Basic Object-Oriented Programming in Java 3 Developed and taught by well-known author and developer. At public venues or onsite at your location. © 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 Similarities and differences between Java and C++ Object-oriented nomenclature and conventions Instance variables (fields) Methods (member functions) Constructors 5 J2EE training: http://courses.coreservlets.com Object-Oriented Programming in Java Similarities with C++ User-defined classes can be used the same way as built-in types. Basic syntax Differences from C++ Methods (member functions) are the only function type Object is the topmost ancestor for all classes All methods use the run-time, not compile-time, types (i.e. all Java methods are like C++ virtual functions) The types of all objects are known at run-time All objects are allocated on the heap (always safe to return objects from methods) Single inheritance only
Background image of page 2
6 J2EE training: http://courses.coreservlets.com Object-Oriented Nomenclature “Class” means a category of things A class name can be used in Java as the type of a field or local variable or as the return type of a function (method) “Object” means a particular item that belongs to a class Also called an “instance” For example, consider the following line: String s1 = "Hello"; Here, String is the class, and the variable s1 and the value "Hello" are objects (or “instances of the String class”) 7 J2EE training: http://courses.coreservlets.com Example 1: Instance Variables (“Fields” or “Data Members”) class Ship1 { public double x, y, speed, direction; public String name; } public class Test1 { public static void main(String[] args) { Ship1 s1 = new Ship1(); s1.x = 0.0; s1.y = 0.0; s1.speed = 1.0; s1.direction = 0.0; // East s1.name = "Ship1"; Ship1 s2 = new Ship1(); s2.x = 0.0; s2.y = 0.0; s2.speed = 2.0; s2.direction = 135.0; // Northwest s2.name = "Ship2"; ...
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 Instance Variables: Example (Continued) ... s1.x = s1.x + s1.speed * Math.cos(s1.direction * Math.PI / 180.0); s1.y = s1.y + s1.speed * Math.sin(s1.direction * Math.PI / 180.0); s2.x = s2.x + s2.speed * Math.cos(s2.direction * Math.PI / 180.0); s2.y = s2.y + s2.speed * Math.sin(s2.direction * Math.PI / 180.0); System.out.println(s1.name + " is at (" + s1.x + "," + s1.y + ")."); System.out.println(s2.name + " is at (" + s2.x + "," + s2.y + ")."); } } 9 J2EE training: http://courses.coreservlets.com Instance Variables: Results Compiling and Running: javac Test1.java java Test1 Output: Ship1 is at (1,0).
Background image of page 4
Image of page 5
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 / 17

05-OOP-Basics - 2006 Marty Hall Basic Object-Oriented...

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

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