Vous êtes sur la page 1sur 42

JAVA - recap -

Read it , do it , know it !

Requirements
If you are not prepared to recap C programming language object oriented programming You can stop reading now. Otherwise :

Incoming crash course on Java !


See the demo project here: Java tutorial

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

JAVA
All Java code runs on the Java Virtual Machine, so you have to install it (in the labs it is already installed). A virtual machine is like a software computer in a host operating system .

Development Environment IDE


In the labs at the faculty we have Eclipse installed. You need to create a new Java project and learn how to create new classes. You can handle this one yourself. You might want to take a look at NetBeans as well.

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Classes and Objects


Please revise the object oriented programming course.

A class is a type . For example a class of cars can be Ford. An object is an instance of a class, a materialization of the type. For example: Ford DJ_69_SEX ; // DJ_69_SEX is an object of type Ford

Entry point - first thing to execute in a Java application


package ro.ucv.main; /** this is where you describe your class. * note the " ** " */ public class MainClass { //any name will do
make a habit out of writing these. or your coworkers might skin you alive

/** this is where you describe your method * @param args arguments received when application is executed */ public static void main(String[] args) { // name type and parameters of the main method are predefined // TODO Auto-generated method stub //"TODO" comments usually mean you should write some code here //this is where you start to implement your application } }

Classes - Please revise the object oriented programming course.


public class MainClass { //methods and varibles } a java class source code is usually found in a text file with the *.java extension. it has a body where you define methods (functions) and variables. These are called generically "members" of the class.

Packages

a way of organizing classes

think of packages as folders for classes: - in the source *.java file it looks like this:
package ro.ucv.main; // ... class definition

- in the IDE it might look like this:

- on the hard drive packages are actual folders:

Constructor - also from the object oriented programming course.


A constructor is a method with no type that has the same name as the class.
class MyClass { public MyClass(){ // whatever code you want will be run once when this class is instantiated } }

The constructor will run once when the class is instantiated.


MyClass object=new MyClass(); //this is when the constructor is executed

Destructor
In C programming language these were special methods called when an object of the class is explicitly cleared from memory. Java has no destructor. Instead it has a garbage collector that will destroy objects for you. Static objects will not be destroyed until the end of the execution of the application.

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Classes extend each other


(inheritance in object oriented programming)

public class Boss extends Worker { ... } In this case class Boss can do everything Worker can do plus whatever you will implement especially for Boss.

For your information : In Java all classes implicitly extend the Object class.

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{
class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{

These are called members

class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e) { } public void setLayout( LayoutManager l) {

} }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{

Members have access modifiers

class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{

variables WIDTH and HEIGHT are private in class JFrame

class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{

variables WIDTH and HEIGHT will only be accessible in class JFrame

class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{

protected members are private members that are also accessible from classes that extend this class class JFrame{
private int WIDTH=0, HEIGHT=0; public JFrame(){ }

protected void processWindowEvent (WindowEvent e){ // this method overrides the analogous // method in JFrame }

protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{
public JFrame(){ // this method overrides the analogous // method in JFrame } protected void processWindowEvent (WindowEvent e){ // this method overrides the analogous // method in JFrame }

class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ }

public members are protected members that are also accessible directly from objects like so : JFrame obj=new JFrame(); obj.setLayout(null);

protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }

public void setLayout( LayoutManager l){ // this method overrides the analogous // method in JFrame }

Extending classes in Java


- example, not actual code class MyGUI extends JFrame{
public JFrame(){
public JFrame(){ super(); //calls the constructor in JFrame // this method overrides the analogous // method in JFrame } protected void processWindowEvent (WindowEvent e){ super.processWindowEvent(e); // this method overrides the analogous // method in JFrame } public void setLayout( LayoutManager l){ super.setLayout(l); // this method overrides the analogous // method in JFrame }

class JFrame{ private int WIDTH=0, HEIGHT=0;

} protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }

if in the override methods (in MyGUI) you want to call the initial, overridden method as well use super = a pointer to the extended class (superclass)

Reasons for extending


avoid repeating yourself. If you duplicate code you duplicate errors. It is much easier to debug when you extend. modular architecture. Have someone build the basics and others use them. Learn how to use your access modifiers for this one. stop reinventing the wheel. If it's been done you are wasting precious time reimplementing it. consequences of the above mentioned: re-usability, maintainability, extensibility, modifiability, simplicity.

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Abstract Classes
Example: an action at left click. You are the one creating the class that detects the left click allowing others to extend it with their own functionality. An abstract method has no body, it is meant to be overridden. When a left click event is detected just call an abstract method and allow the extending class to implement it. A class that contains abstract methods is an abstract class. It cannot be instantiated since it is not yet complete. Someone could, however, extend it and implement all abstract methods.

Abstract Classes in Java


- example, not actual code class MouseHandler extends MouseListener{ void onLeftClick(MouseEvent e){ //code for action to be executed }

you cannot instantiate MouseListener MouseListener l=new MouseListener();

abstract class MouseListener{ //some code to detect left mouse //event and call the abstract method //onLeftClick abstract void onLeftClick(MouseEvent e);

} you CAN instantiate MouseHandler MouseHandler m= new MouseHandler(); }

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Interfaces
An abstract type containing only method signatures (no bodies) and constants.
interface Predator{ static final int aprxWeight; public Stats getStatistics(); }

since no implementation is done in an interface it is implemented by a class


//Predator is an interface class Lion implements Predator{ ... } class Tiger implements Predator{ ... } //see how nicely they fall in line?

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Transferring objects over the network


sending objects over a network is no problem reconstructing objects at the receiver side is a problem All classes of objects that will be sent over a network communication must implement Serializable in order for Java to serialize and deserialize the objects. At the receiver end you will get an object of type Object (the base class in Java). This needs to be cast to the actual type of the received object.
//declaration cast MessageObject obj = (MessageObject) receiveObject();

where receiveObject() is a generic fictitious method that receives an object over the network.

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Creating GUIs in Java


public class MainGUI extends JFrame { public MainGUI(){ super("GUI title"); this.setVisible(true); this.setBounds(100, 100, 800, 600); this.setLayout(null); JButton b = new JButton("Interactive Gui"); b.setBounds(10, 10, 150, 25); add(b); //other additional controls repaint(); b.addActionListener(new ButtonInteactiveAction(this)); // other action listeners } }

Creating an Action Listener


- example, not actual code -

public class ButtonMapsAction implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // do something when the button is pushed } }

Inputs from mouse - example, not actual code public class PictureGUI extends JFrame implements MouseListener { public PictureGUI() { // create the GUI } @Override public void mouseClicked(MouseEvent e) { //do something on left click } @Override public void mouseEntered(MouseEvent arg0) { //do something when mouse pointer enters the frame } @Override public void mouseExited(MouseEvent arg0) { // do something when mouse pointer exits the frame } @Override public void mousePressed(MouseEvent arg0) { } @Override public void mouseReleased(MouseEvent arg0) { } }

When in doubt : Google it


1. There are a lot of Java examples on the internet just type in Google: <what you want to do> Java example 2. Test the code. hint: click the errors. The IDE might have some suggestions. 3. Repeat step 1 if you can't make it work 4. If it works move on to the next challenge

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Java Archive
A java archive (JAR) is a compact format for a java project. You can include jars implemented by others in your own project: copy them in your project and include them. In Eclipse it looks like this:

copy

include

Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example

Running a java source code in cmd line


java [ options ] class [ argument ... ] java [ options ] -jar file.jar [argument... ] options Command-line optionsclass Name of the class to be invoked.file.jar Name of the jar file to be invoked. Used only with -jar.argument Argument passed to the main function. (remember public static void main(String args[]) in slide 7 ? args[] are the arguments we are talking about here)

Running java source code in the IDE


You should find a way to run the project or the main class as an application. You are on your own on this one. Run the test application (developed for you in Eclipse): - several GUIs with mouse listeners - an interactive map component that captures the actual global coordinates of the clicked location and marks it on the map . this component needs an internet connection to work.

Go forth and do something cool with this knowledge


enjoy the vast open source Java community. People are just waiting around for your questions. enjoy the multitude of free jars and apps already implemented for you by experienced programmers. Mix and match to build your dream app. so many people asked so many questions that your problem has probably already been solved over and over again online. Just Google it. The solution is a copy paste away.

Prepare yourself ! You are about to make all your dreams come true.

Vous aimerez peut-être aussi