31
ECP4136 Java Technology Tutorial 6

ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

  • View
    222

  • Download
    1

Embed Size (px)

Citation preview

Page 1: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

ECP4136 Java Technology

Tutorial 6

Page 2: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Activities

• Revision – Tutorial 4• Work on tutorial questions

– today’s target – part 1 of tutorial 6

• Try to figure out:– ways of implementing ActionListener– ways of passing information of the button

being pressed (hence the Color) to ButtonListener in Tutorial 4

• Last 45 minutes - Discussion

Page 3: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Discussion

a) Revisit static keyword

b) Use of this keyword

c) Ways of implementing ActionListener

d) Ways of passing information of the button being pressed (hence the Color) to ButtonListener in Tutorial 4

Page 4: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

int size, weight;char category;

Data declarations

Method declarations

public private

Variables

MethodsProvide services

to clients

Support othermethods in the

class

Enforceencapsulation

Violateencapsulation

Page 5: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• Examples:– class variables:

• Math.PI, JFrame.EXIT_ON_CLOSE

– class methods:• Math.sin( ), JOptionPane.showMessageDialog( )

static

Variables

MethodsInstancemethods

Class methods

Class variables

Instance variables

Page 6: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword• How can we identify whether it is an instance or not?

String str = new String(“Instantiate me”);JButton blueButton = new JButton(“Blue”);Random gen = new Random( );

• Recall how we called the class variables/methods…

int area = Math.PI*radius*radius;String age = JOptionPane.showInputDialog(“Enter your age:”);

static

Variables

MethodsInstancemethods

Class methods

Class variables

Instance variables

Page 7: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keywordpublic class Student {

public int count = 0;

private String name;

public Student(String stdName) {

name = stdName;

count++;

}

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Student Ali = new Student(“Ali”);

}

}

int count;String name;

int count;String name;

Ivan Ali

count = 1

name = “Ivan”

count = 1

name = “Ali”

Page 8: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keywordpublic class Student {

public static int count = 0;

private String name;

public Student(String stdName) {

name = stdName;

count++;

}

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Student Ali = new Student(“Ali”);

}

}

String name; String name;

Ivan Ali

count = 1

name = “Ivan”

count = 2

name = “Ali”

declared as static

int count;

Page 9: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• Under normal circumstances– Class variables may

/may not be encapsulated

– Instance variables are encapsulated

• Assessors & Mutators

String name; String name;

Ivan Ali

count = 1

name = “Ivan”

count = 2

name = “Ali”

int count;

Page 10: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• Recall that instance variables/methods are referenced through the objects (i.e. instances has to be created – new keyword)

• Example: JFrame frame = new JFrame(“Circle”);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

• You cannot refer to the instance variables if there is no instance created yet.

Page 11: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• Remember, you cannot refer to the instance variables if there is no instance created yet!

• Now, let’s say you have a accessor to get the mark of students…

public class Student {

private int mark;

private String name;

public Student(String stdName) {

name = stdName;

mark = 0;

}

public int getMark( ) { return mark; }

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Ivan.getMark( );

}

}

Page 12: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keywordpublic class Student {

private int mark;

private String name;

public Student(String stdName) {

name = stdName;

mark = 0;

}

public static int getMark( ) { return mark; }

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Ivan.getMark( );

}

}

Can we set this as class method?

Page 13: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• Compile-time error !!!

• Look’s familiar?

public class Student {

private int mark;

private String name;

public Student(String stdName) {

name = stdName;

mark = 0;

}

public static int getMark( ) { return mark; }

}

public class Tutorial {

public static void main(String[ ] args) {

Student Ivan = new Student(“Ivan”);

Ivan.getMark( );

|

}

javac Tutorial.java

Student.java:8: non-static variable mark cannot be reference from a static context

Page 14: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The static keyword

• That is what would happen if you try to initialise instances in the main method.

• Isn’t main method a static type?

import javax.swing.JPanel;//// This is a bad example, do not follow!//public class TestGUI {

private JPanel panel;

public static void main(String[] args) {

// Common wrong perception, // initialisation in main method panel = new JPanel(); // ...

}

}

javac TestGUI.java

TestGUI.java:19: non-static variable panel cannot be referenced from a static context

Page 15: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The this keyword

• “Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.”

• Keywords:– Instance, object

Page 16: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The this keyword

• Having hard times creating different names for similar variables?

public class Circle {

private int xPoint, yPoint, size; private Color circleColor;

public Circie(int xLocation, int yLocation, int diameter, Color shadeColor) {

xPoint = xLocation; yPoint = yLocation; size = diameter; circleColor = shadeColor;

}

Page 17: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The this keyword

• More elegant way to solve it– Simple & avoid confusion

public class Circle {

private int xPoint, yPoint, size; private Color circleColor;

public Circie(int xPoint, int yPoint, int size, Color circleColor) {

this.xPoint = xPoint; this.yPoint = yPoint; this.size = size; this.circleColor = circleColor;

}

Page 18: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

The this keyword

• It is also pretty useful for the overloading of the constructorpublic class Circle {

private int xPoint, yPoint, size; private Color circleColor;

public Circle(int xPoint, int yPoint) {

this(xPoint, yPoint, 10, Color.black);

}

public Circie(int xPoint, int yPoint, int size, Color circleColor) {

this.xPoint = xPoint; this.yPoint = yPoint; this.size = size; this.circleColor = circleColor;

}

Page 19: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

About the ActionListener

The are a few possible ways to implement the ActionListener in Tutorial 4

a) Define a private class in the same source file

b) Define class as the input parameter of the addActionListener method

c) Implement on the JPanel subclass itself

Page 20: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

About the ActionListenerimport java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

public TestPanel ( ) {

JButton blueButton = new JButton(“Blue”); blueButton.addActionListener( new ButtonListener() ); add(blueButton); }

private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { // do something here } } // inner class

}

a) Define a private class in the same source file

Must know !

Page 21: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

About the ActionListenerimport java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

public TestPanel ( ) {

JButton blueButton = new JButton(“Blue”); blueButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { //do something here } } ); add(blueButton); } }

b) Define class as the input parameter of the

addActionListener method

Page 22: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

About the ActionListenerimport java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel implements ActionListener {

public TestPanel ( ) {

JButton blueButton = new JButton(“Blue”); blueButton.addActionListener( this ); add(blueButton); }

public void actionPerformed(ActionEvent event) { // do something here } // it’s a method of TestPanel, not an inner class

}

c) Implement on the JPanel

subclass itself

Page 23: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

About the ActionListener

The are a few possible ways to implement the ActionListener in Tutorial 4

a) Define a private class in the same source file

b) Define class in the addActionListener method

c) Implement on the JPanel subclass itself

Page 24: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

• How to determine which button is pressed in Tutorial 4?

• Again, there are at least four ways of doing this:

a) Simplest method – redundant classes

b) ActionEvent -> getSource( )

c) Utilize setActionCommand and getActionCommand

d) Object oriented concept -> pass to constructor

Page 25: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

public TestPanel ( ) {

JButton blueButton = new JButton(“Blue”); blueButton.addActionListener( new BlueButtonListener() ); add(blueButton); }

private class BlueButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { // this is for sure a blue button ! } } // inner class

// private class GreenButtonListener implements ActionListener { … } // private class RedButtonListener implements ActionListener { … }

}

a) redundant classes

Page 26: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

public TestPanel ( ) {

JButton blueButton = new JButton(“Blue”); blueButton.addActionListener( new BlueButtonListener() ); add(blueButton); }

private class BlueButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { // this is for sure a blue button ! } } // inner class

// private class GreenButtonListener implements ActionListener { … } // private class RedButtonListener implements ActionListener { … }

}

a) redundant classes

Looks stupid, but it works !

Page 27: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

JButton blueButton;

public TestPanel ( ) {

blueButton = new JButton(“Blue”); blueButton.addActionListener( new BlueButtonListener() ); blueButt add(blueButton); }

private class BlueButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource()==blueButton) { // it’s blue, call the blue color object } else { // it’s other buttons } } } // inner class

}

b) ActionEvent -> getSource( )

Page 28: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;

public class TestPanel extends JPanel {

JButton blueButton;

public TestPanel ( ) {

blueButton = new JButton(“Blue”); blueButton.addActionListener( new BlueButtonListener() ); blueButton.setActionCommand( “blue”); add(blueButton); }

private class BlueButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if ( (event.getActionCommand( )).equals(“blue”) ) { // it’s blue, call the blue color object } else { // it’s other buttons } } } // inner class

}

c) Utilize setActionCommand and getActionCommand

Page 29: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Passing information to ActionListener

import java.awt.event.*;import javax.swing.*;import java.awt.*;public class TestPanel extends JPanel {

JButton blueButton;

public TestPanel ( ) {

blueButton = new JButton(“Blue”); blueButton.addActionListener( new ButtonListener( Color.blue ) ); add(blueButton); }

private class ButtonListener implements ActionListener {

Color buttonColor;

public ButtonListener(Color buttonColor) { this.buttonColor = buttonColor; }

public void actionPerformed(ActionEvent event) { // we can directly call the code we want } } // inner class

}

d) Object oriented concept -> pass to constructor

Page 30: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Conclusion

• Multiple ways of achieving a single target– Look for alternatives, judge for the best

method– Discuss with peers

• Homework? You may try to:– Modify your PlusPanel according to the

previously discussed method of implementation

Page 31: ECP4136 Java Technology Tutorial 6. Activities Revision – Tutorial 4 Work on tutorial questions –today’s target – part 1 of tutorial 6 Try to figure out:

Reference

• Using the this Keyword

(link:http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/javaOO/thiskey.html)