Week 4
Week 4
Class
Java is completely Object Oriented. This means that everything must be a class An object is an instance of a class A class has attributes and behavior (properties and methods) Lets discuss the Car class
Class members
Visibility (public, private, protected) static (optional): class-wide variable, dont need an object final (optional): means it cant be changed Type (int, String, Object, whatever)
Methods
public static int getEngineType(int carType) { Visibility (public, private) static (or not) Return type Method name Input parameters
Get/Set Methods
It is best to utilize getters and setters for your properties, rather than making them public. They enable you to validate the value being assigned to the attribute Some values might be read-only. Therefore, you may only want a getter.
Constructors
They are used for initialization You can overload constructors, as long as the signature is different between each constructor. Note Car
Inheritance (extends)
java.lang.object is the root of all classes Every class extends (inherits) a superclass (parent) If you dont designate extends, then you are simply inheriting java.lang.object extends can be chained. Inheriting one class will inherit all non-private members up the hierarchy to java.lang.object How might you use inheritance on CarObject?
Interfaces (implements)
If two classes share many public members, it is a good idea to use interfaces In the car example, tuneUpPinto() and tuneUpPorche() could each be called tuneUp(). You can create an interface with tuneUp(). public interface Servicable { The Porche and Pinto would both implement it:
Exception Handling
java.lang.Exception Exceptions are throwable Throwing an exception can be done by the systems code or your code Catching an exception allows you to trap it and handle it.
import java.util.Calendar;
try { //all the code } catch (NumberFormatException ex) { //user typed in bad ints } catch (ArrayIndexOutOfBoundsException ex) { //not a valid date } } finally { //Want to play again? }
Java Applets
Java applets have many methods called automatically by the web browser:
public void init() public void start() public void paint(Graphics g) public void stop() public void destroy()
Applets loaded from the internet are considered untrusted and therefore cant
Read or write files Open a socket connection Start a program on the machine Call non-Java code
Applets running locally are trusted, so dont run them from your email
java.awt.Graphics
public void paint(Graphics g) You can paint a rectangle, arc, circle, or oval It can be filled or outlined You can also paint images and strings See Page 128 Table 3.5
javax.swing.JFrame
A JFrame is a canvas for your paint object that is not run in a browser Ensure your class extends JFrame public static void main(String[] args) is how it is invoked (if necessary) In the constructor, set the size and title for the window You can do what you want, or if another object will instantiate your class, youre done
javax.swing
Note that all of these must run on something like a JApplet or JFrame