200 likes | 325 Views
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 that class is a realization of that concept. An object has states (attributes) and behaviours (operations)
E N D
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 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.
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 Structure of Class Instance method javac Hello.java java Hello Hello there…
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 Structure of Class - Static Class method javac Hello2.java java Hello2 Hello there…
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 Structure of Class - Static Instance variable javac Hello3.java java Hello3 ??????????????
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 Structure of Class - Static Instance variable javac Hello3.java java Hello3 Morning1 Morning2 Bonjour1 Bonjour2
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 Structure of Class - Static Class variable javac Hello4.java java Hello4 ??????????????
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 Structure of Class - Static Class variable javac Hello4.java java Hello4 Morning1 Morning2 Bonjour3 Bonjour4
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 Structure of Class - Constructor Method overloading javac Game.java java Game Value of first die: 1 Value of second die: 3
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 Structure of Class - Encapsulation Access modifier Mutator Accessor javac Game.java java Game Value of first die: 6 Value of second die: 3
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
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 } }
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.
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
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); } }
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); } }
Activities • 10 minutes planning • Independent • 10 minutes discussion • Exchange ideas with peers • 20 minutes pitching • Selected pairs/groups • Implementation
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
References • Understanding Instance and Class Members http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html