describing similar things
play

Describing Similar Things In English, we commonly describe things by - PowerPoint PPT Presentation

Describing Similar Things In English, we commonly describe things by saying it is like x except... For example: Mt. Holyoke is like other colleges except it only admits women. Java is like Flash except you create graphics by


  1. Describing Similar Things In English, we commonly describe things by saying “it is like x except... ” For example: Mt. Holyoke is like other colleges except it only admits women. Java is like Flash except you create graphics by programming. 1 Types of Variation Extra behaviors: A strobe light is a light that flashes. Behavior has a different effect: A poisonous snake is a snake that delivers venom when it bites. Extra properties: A textbook is a book that includes exercises. 2 Is-A Hierarchies Animal Vertebrate Invertebrate Mammals Birds Fish Insects Worms 3 Tuesday, February 5, 13

  2. Java Class Hierarchies Person UML Notation meaning is-a Student K12Student CollegeStudent 4 Java Class Hierachy public class Person { Superclass private String name; public Person (String theName) {…} public String getName() {…} } Subclass public class Student extends Person { public Student (String theName) {…} } 5 Inheritance Student “inherits” the public class Person { getName method. private String name; public Person (String theName) {…} public String getName() {…} } public class Student extends Person { public Student (String theName) {…} } Student s = new Student(“Alice”); System.out.println (s.getName()); 6 Tuesday, February 5, 13

  3. Adding Behavior to a Subclass public class Student extends Person { private double gpa; public Student (string theName) {…} public void setGPA (double newGPA){…} } Student s = new Student(“Alice”); System.out.println(s.getName()); s.setGPA (3.6); Person p = new Person(“Bob”); System.out.println(p.getName()); p.setGPA (3.6); 7 Overriding Methods public class Person { public Person (String theName) {…} public String toString() { return name; } public String getName() {…} private String name; } A subclass can provide its own public class Student extends Person { definition of an private double gpa; public Student (String theName) {…} inherited method. public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } } 8 public class Person { Overridden public Person (String theName) {…} public String toString() { Methods return name; } public String getName() {…} private String name; } public class Student extends Person { private double gpa; public Student (String theName) {…} public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } } Person p = new Person(“Lucy”); System.out.println (p.toString()); 9 Tuesday, February 5, 13

  4. public class Person { Overridden public Person (String theName) {…} public String toString() { return name; Methods } public String getName() {…} private String name; } public class Student extends Person { private double gpa; public Student (String theName) {…} public void setGPA (double newGPA){…} public String toString() { return getName() + “ has gpa “ + gpa; } } Student s = new Student(“Linus”); s.setGPA(3.1); System.out.println (s.toString()); 10 What is a Student? It is like a Person except that: It has a GPA The GPA can be set Since it is like a Person, it must have a name How does the name get set? 11 Constructing a Student public Student (String theName) { super (theName); gpa = 0.0; } The Student constructor calls the Person constructor. This must be the first statement in the subclass’ s constructor. 12 Tuesday, February 5, 13

  5. extends JComponent public class HelloFromVenus extends JComponent { public void paintComponent (Graphics g) { super.paintComponent(g); int width = getWidth(); … } public static void main (String[] args) { … contentPane.add(new HelloFromVenus(), BorderLayout.CENTER); } 13 What is a JComponent? Something we can paint by overriding: public void paintComponent (Graphics g) Something that has a width, which we can find out by calling: public int getWidth () Something we can add to a panel, the content pane, … public Component add(Component comp) 14 JComponent Component Hierarchy Container means “extends” can also be read from bottom to top as “is a”. A JComponent JComponent “is a” Container. HelloFromVenus 15 Tuesday, February 5, 13

  6. extends JPanel public class PizzaOrder extends JPanel { public PizzaOrder () { add (new JLabel (“Name”)); … } public static void main (String[] args) { … contentPane.add(new PizzaOrder(), BorderLayout.CENTER); } 16 What is a JPanel? Something we can add Swing components to: public Component add(Component comp) Something we can add to a panel, the content pane, … public Component add(Component comp) 17 Component Container JPanel Hierarchy JComponent JPanel PizzaOrder 18 Tuesday, February 5, 13

  7. Animation Animation Works the same way as a movie - scene changes every 30 milliseconds To program an animation: Say that your class “extends Animation” Define the nextFrame method to update the scene. This is called every 30 milliseconds: protected void nextFrame() Call the start method to start the animation public void start() 19 Tuesday, February 5, 13

Recommend


More recommend