Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECP4136 Java Technology Tutorial 4. Class A class is a blueprint of an object The class represents the concept of an object, and any object created from.

Similar presentations


Presentation on theme: "ECP4136 Java Technology Tutorial 4. Class A class is a blueprint of an object The class represents the concept of an object, and any object created from."— Presentation transcript:

1 ECP4136 Java Technology Tutorial 4

2 Class A class is a blueprint of an object The class represents the concept of an object, and any object created from that class is a realization of that concept. An object has states (attributes) and behaviours (operations) Every class can contain data declarations and method declarations. Collectively, the data and methods of a class are called the members of a class.

3 Structure of Class public class Hello { public static void main(String[] args) { Greeting greet = new Greeting(); greet.hello(); } // main method } // Hello class public class Greeting { public void hello() { System.out.println(“Hello there…”); } } // Greeting class javac Hello.java java Hello Hello there… Instance method

4 Structure of Class - Static public class Hello2 { public static void main(String[] args) { Greeting2.hello(); } // main method } // Hello2 class public class Greeting2 { public static void hello() { System.out.println(“Hello there…”); } } // Greeting2 class javac Hello2.java java Hello2 Hello there… Class method

5 Structure of Class - Static public class Hello3 { public static void main(String[] args) { Greeting3 Jon = new Greeting3(); Greeting3 Marry = new Greeting3(); Jon.English(); Jon.English(); Marry.French(); Marry.French(); } // main method } // Hello3 class public class Greeting3 { public int greetCount = 0; public void English() { greetCount++; System.out.print("Morning"+greetCount+'\t'); } public void French() { greetCount++; System.out.print("Bonjour"+greetCount+'\t'); } } // Greeting3 class javac Hello3.java java Hello3 ?????????????? Instance variable

6 Structure of Class - Static public class Hello3 { public static void main(String[] args) { Greeting3 Jon = new Greeting3(); Greeting3 Marry = new Greeting3(); Jon.English(); Jon.English(); Marry.French(); Marry.French(); } // main method } // Hello3 class public class Greeting3 { public int greetCount = 0; public void English() { greetCount++; System.out.print("Morning"+greetCount+'\t'); } public void French() { greetCount++; System.out.print("Bonjour"+greetCount+'\t'); } } // Greeting3 class javac Hello3.java java Hello3 Morning1Morning2Bonjour1Bonjour2 Instance variable

7 Structure of Class - Static public class Hello4 { public static void main(String[] args) { Greeting4 Jon = new Greeting4(); Greeting4 Marry = new Greeting4(); Jon.English(); Jon.English(); Marry.French(); Marry.French(); } // main method } // Hello4 class public class Greeting4 { public static int greetCount = 0; public void English() { greetCount++; System.out.print("Morning"+greetCount+'\t'); } public void French() { greetCount++; System.out.print("Bonjour"+greetCount+'\t'); } } // Greeting4 class javac Hello4.java java Hello4 ?????????????? Class variable

8 Structure of Class - Static public class Hello4 { public static void main(String[] args) { Greeting4 Jon = new Greeting4(); Greeting4 Marry = new Greeting4(); Jon.English(); Jon.English(); Marry.French(); Marry.French(); } // main method } // Hello4 class public class Greeting4 { public static int greetCount = 0; public void English() { greetCount++; System.out.print("Morning"+greetCount+'\t'); } public void French() { greetCount++; System.out.print("Bonjour"+greetCount+'\t'); } } // Greeting4 class javac Hello4.java java Hello4 Morning1Morning2Bonjour3Bonjour4 Class variable

9 Structure of Class - Constructor public class Game { public static void main(String[] args) { String text = new String(“Board game”); Die die1 = new Die(); Die die2 = new Die(3); System.out.println(“Value of first die: ” + die1.faceValue); System.out.println(“Value of second die: ” + die2.faceValue); } // main method } // Game class public class Die { public int faceValue; public Die() { faceValue = 1; } public Die(int value) { faceValue = value; } } // Die class javac Game.java java Game Value of first die: 1 Value of second die: 3 Method overloading

10 Structure of Class - Encapsulation public class Game { public static void main(String[] args) { String text = new String(“Board game”); Die die1 = new Die(); Die die2 = new Die(3); die1.setFaceValue(6); System.out.println(“Value of first die: ” + die1.getFaceValue()); System.out.println(“Value of second die: ” + die2.getFaceValue()); } // main method } // Game class public class Die { private int faceValue; public Die() { faceValue = 1; } public Die(int value) { faceValue = value; } public void setFaceValue(int value) { // Code for value checking may be added faceValue = value; } public int getFaceValue() { return faceValue; } } // Die class javac Game.java java Game Value of first die: 6 Value of second die: 3 Mutator Accessor Access modifier

11 Swing components import javax.swing.*; public class MyGUI { public static void main(String[ ] args) { JFrame frame = new JFrame(“Frame title”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); // arrange the GUI components here frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } // MyGUI class

12 Swing components import java.awt.Graphics; Import javax.swing.JPanel; public class MyPanel extends JPanel { public MyPanel( ) { } public void paintComponent(Graphics page) { super.paintComponent(page); // draw other components }

13 Graphics-based programs “A program that is oriented around a GUI, responding to events from the user, is called event-driven.” A listener is required to respond to the event. It “is an object that “waits” for an event to occur and responds in some way when it does” Listener classes are needed to perform actions we desire when events occur.

14 Listener class import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; public class TestListener { public static void main(String[ ] args) { JFrame frame = new JFrame("Test Listener"); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(100,50)); JButton closeButton = new JButton("Press to close"); closeButton.addActionListener(new ButtonListener()); panel.add(closeButton); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.exit(0); } } // inner class

15 Listener class import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; public class TestListener { public static void main(String[ ] args) { JFrame frame = new JFrame("Test Listener"); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(100,50)); JButton closeButton = new JButton("Press to close"); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } ); panel.add(closeButton); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }

16 Creating Panel subclass import javax.swing.JPanel; public class MyOwnPanel extends Panel { public MyOwnPanel( ) { JPanel leftPanel, rightPanel; leftPanel = new JPanel(); rightPanel = new JPanel(); // More codes here add(leftPanel); add(rightPanel); } public class PanelDriver { public static void main(String[] args) { JFrame myFrame = new JFrame(); MyOwnPanel p = new MyOwnPanel(); myFrame.getContentPane().add(p); myFrame.pack(); myFrame.setVisible(true); }

17 Activities 10 minutes planning –Independent 10 minutes discussion –Exchange ideas with peers 20 minutes pitching –Selected pairs/groups Implementation

18 Activities Plus class –Accessor and mutators PlusPanel class –Extend JPanel PlusDriver class –Create main frame –Can be used to test PlusPanel and Plus PlusDesign class –Extend JPanel

19 References Understanding Instance and Class Members http://java.sun.com/docs/books/tutorial/jav a/javaOO/classvars.html


Download ppt "ECP4136 Java Technology Tutorial 4. Class A class is a blueprint of an object The class represents the concept of an object, and any object created from."

Similar presentations


Ads by Google