JAVA Programming - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

JAVA Programming

Description:

'This one thing is a type of this other thing.' Keyword extends ... following are legal constructors that can be added to the Beagle subclass of Dog? ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 33
Provided by: tem3
Category:

less

Transcript and Presenter's Notes

Title: JAVA Programming


1
JAVA Programming
  • Part II - Review

2
Is A Relationship
  • Relies on inheritance
  • This one thing is a type of this other thing.
  • Keyword extends
  • A class is tightly encapsulated if it protects
    the objects data by forcing calling code to use
    methods instead of directly accessing instance
    variables.
  • public class Car
  • public class Subaru extends Car

3
OO Terms
  • public class Vehicle
  • public class Car extends Vehicle
  • public class Subaru extends Car
  • Vehicle is the superclass of Car
  • Car is the subclass of Vehicle
  • Car is the superclass of Subaru
  • Subaru is the subclass of Vehicle
  • Vehicle is the parent class of Car
  • Car is the child class of Vehicle
  • Car is the parent of Subaru
  • Subaru is the child of Car

4
Overridden Methods
  • Benefit is to get behavior specific to a
    particular subclass type
  • Abstract methods must be overridden unless the
    parent class is also abstract.
  • The argument list must match exactly.
  • The return type must match exactly.
  • You cant override a method marked final().
  • You cant override a method that cant be
    inherited. If the subclass does not have access
    to the superclass method because of access
    modifier restrictions.
  • Invoke the superclass version of a particular
    method by using the keyword super.

5
  • public class Animal
  • public void PrintYourself( )
  • public class Dog extends Animal
  • public void PrintYourself( )
  • super.PrintYourself( )

6
Overloaded Methods
  • Reuse the same method name in a class but use
    different arguments
  • Code takes on the burden of coping with different
    arguments rather than forcing the caller to have
    all the information needed for the method in a
    particular format
  • Must change the argument list (by order, number,
    and/or type)
  • The return types can vary.

7
  • public class Dog
  • int size
  • String name
  • int numOfSpots
  • public void change(int size, String name)
  • this.size size
  • this.name name
  • public void change(int size, String name, int
    numOfSpots)
  • this.size size
  • this.name name
  • this.numOfSpots numOfSpots

8
Varying Return Types
  • class Adder
  • public int addNums(int x, int y)
  • return xy
  • public double addNums(double x, double y)
  • return xy

9
Constructors
  • Invoked whenever you say new
  • Main purpose is to let you initialize the
    objects instance variables
  • A constructors name must exactly match the name
    of the class.
  • Constructors must not have a return type.
  • Methods can have the same name as the constructor
    as long as the method has a return type.
  • A call to a super constructor can be no-arg or
    include arguments passed to the super
    constructor.
  • Overloading a constructor means typing in
    multiple versions of the constructor, each having
    different lists.

10
  • class Dog
  • Dog( )
  • super(Fido)
  • Dog(String name)
  • System.out.println(name)

11
  • Which statements identify that a pancake is a
    food?
  • Class Pancake extends class Food
  • Class Food extends class Pancake
  • Class Pancake is in the package Food
  • Class Pancake has a reference to a Food
  • Class Food has a reference to a Pancake
  • Identify whether the following methods would be
    legal in a subclass of Foo.
  • class Foo
  • String doStuff(int x)
  • String doStuff(int x)
  • int doStuff(int x)
  • public String doStuff(int x)
  • String doStuff(String s)
  • What if the doStuff method was modified to a
    public class?
  • public String doStuff(int x)

12
  • Which of the following are legal constructors
    that can be added to the Beagle subclass of Dog?
  • No constructor, allow the compiler default
  • Beagle()super()
  • Beagle()super(fido)
  • Beagle()
  • Would the following code compile? Why or why
    not?
  • public class Animal
  • private Animal( )
  • public class Dog extends Animal

13
  • What will be the result of the following program?
  • public class Demo
  • public static void main(String args)
  • int i 1
  • if (i 0)
  • if (i 2)
  • if (i 4)
  • System.out.println(big number)
  • else
  • System.out.println(not so big number)
  • else
  • System.out.println(small number)

14
  • What will be the result of the following program?
  • public class Demo
  • public static void main(String args)
  • char test 3
  • switch (test)
  • case 3
  • System.out.println(third case)
  • default
  • System.out.println(default)
  • case 2
  • System.out.println(second case)
  • break
  • case 1
  • System.out.println(first case)

15
JComponent class
  • Basis of the GUI in Java application
  • Anything that can be seen or used in an
    application is a subclass of JComponent
  • Base class for all classes in the AWT with
    graphical representations
  • Use methods setVisible(false) and
    setVisible(true) to control visibility.
  • Use method setEnabled(false) grays out a
    component and makes it not functional
  • Use setFont method to set the name, style, and
    the size.
  • Styles Font.Plain, Font.Bold, Font.Italic,
    Font.BoldFont.Italic
  • Font myFont new Font(SansSerif,
    Font.Bold,28)
  • myComponent.setFont(myFont)

16
JTextField
  • Input field with only one line
  • JTextField textField1 new JTextField(20)
  • Blank field of 20 columns
  • JTextField textField2 new JTextField()
  • Blank text field
  • JTextField textField3 new JTextField(Yes)
  • Predefined text displayed
  • JTextField textField4 new JTextField(Both,30)
  • Predefined text in 30 columns

17
JTextArea
  • Multiple lines of text that allow the setting of
    the width and the height of the viewing area of
    the text
  • JTextArea textArea
  • JTextArea textArea new JTextArea(5, 20)
  • textArea.setEditable(false)
  • JScrollPane scrollPane new
    JScrollPane(textArea,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZON
    TAL_SCROLLBAR_ALWAYS)
  • JTextArea textArea2 new JTextArea(Once upon
    a time)
  • JTextArea textArea3 new JTextArea(Once upon
    a time, 4, 60)
  • JTextArea textArea4 new JTextArea(Once upon
    a time, 4, 60, JTextArea.SCROLLBARS_BOTH)

18
JLabel
  • public LabelDemo()
  • ImageIcon icon new ImageIcon("images/mid
    dle.gif",
  • "a pretty
    but meaningless splat")
  • setLayout(new GridLayout(3,1)) //3
    rows, 1 column
  • JLabel label1 new JLabel("Image and Text",
    icon, JLabel.CENTER)
  • label1.setVerticalTextPosition(JLabel.BOTTOM)
  • label1.setHorizontalTextPosition(JLabel.CENTE
    R)
  • JLabel label2 new JLabel("Text-Only Label")
  • JLabel label3 new JLabel(icon)
  • add(label1)
  • add(label2)
  • add(label3)

19
JCheckbox
  • JCheckbox chinButton new JCheckbox("Chin")
    chinButton.setMnemonic(KeyEvent.VK_C)
    chinButton.setSelected(true)
  • JCheckbox glassesButton new
    JCheckBox("Glasses") glassesButton.setMnemonic(Ke
    yEvent.VK_G)
  • glassesButton.setSelected(true)
  • JCheckbox hairButton new JCheckBox("Hair,
    true) hairButton.setMnemonic(KeyEvent.VK_H)
    hairButton.setSelected(true)

20
  • JList list new JList(imageList)
    list.setSelectionMode(ListSelectionModel.SINGLE_SE
    LECTION)
  • SINGLE_SELECTION
  • Only one item can be selected at a time. When the
    user selects an item, any previously selected
    item is deselected first.
  • SINGLE_INTERVAL_SELECTION
  • Multiple, contiguous items can be selected. When
    the user begins a new selection range, any
    previously selected items are deselected first.
  • MULTIPLE_INTERVAL_SELECTION
  • The default. Any combination of items can be
    selected. The user must explicitly deselect
    items.
  • listModel.addElement("Alison Huml")
  • listModel.addElement("Kathy Walrath")
  • listModel.addElement("Lisa Friendly")

21
Events
  • Package java.awt.event
  • When interfacing with a user at a keyboard, there
    is a method detected by the o/s to determine what
    the user is doing.creating an event
  • The AWTEvent holds the info concerning the type
    of event.
  • Any object can be notified of the event. All it
    has to do is implement the appropriate interface
    and be registered as an event listener on the
    appropriate event source.
  • If the interaction was performed with a mouse, a
    MouseEvent is created.
  • If the interaction was performed with a keyboard,
    the KeyEvent is created.
  • If the interaction was performed on a window, a
    WindowEvent is created.

22
  • Action Event and Associated Listener
  • User clicks a button
  • ActionListener
  • User closes a frame (main window)
  • WindowListener
  • User presses a mouse button while the cursor is
    over a component
  • MouseListener
  • User moves the mouse over a component
  • MouseMotionListener
  • Component becomes visible
  • ComponentListener
  • Component gets the keyboard focus
  • FocusListener
  • Table or list selection changes
  • ListSelectionListener

23
How To Implement an Event Handler
  • Every event handler requires three bits of code
  • In the declaration for the event handler class,
    code that specifies that the class either
    implements a listener interface or extends a
    class that implements a listener interface.
  • public class MyClass implements ActionListener
  • Code that registers an instance of the event
    handler class as a listener upon one or more
    components. someComponent.addActionListener(instan
    ceOfMyClass)
  • Code that implements the methods in the listener
    interface. public void actionPerformed(ActionEvent
    e) ...//code that reacts to the action...

24
JButton Event Example
  • To detect when the user clicks an on-screen
    button (or does the keyboard equivalent), a
    program must have an object that implements the
    ActionListener interface.
  • The program must register this object as an
    action listener on the button (the event source),
    using the addActionListener method.
  • When the user clicks the on-screen button, the
    button fires an action event. This results in the
    invocation of the action listener's
    actionPerformed method (the only method in the
    ActionListener interface).
  • The single argument to the method is an
    ActionEvent object that gives information about
    the event and its source.

25
Multiple Listeners
  • It is possible to have one listener and add that
    listener to lots of components and let that
    listener determine which component it is dealing
    with.
  • Listener needs to know which component fired the
    event.
  • Listener uses the getSource method to determine
    which component fired the event.

26
Multiple Button Listener Example
  • import java.awt.
  • import java.awt.event.
  • class Test extends Frame implements
    ActionListener
  • JButton b1 new JButton(Close)
  • JButton b2 new JButton(Write to console)
  • public Test()
  • this.setLayout(new FlowLayout())
  • b1.addActionListener(this)
  • b2.addActionListener(this)
  • this.add(b1)
  • this.add(b2)
  • this.setSize(300, 100)
  • this.setVisible(true)

public void actionPerformed(ActionEvent e)
if (e.getSource( ) b1)
System.exit(0) if (e.getSource() b2)
System.out.println(Button b2 was
pushed) public static void main(String
args) Test t new Test()
27
KeyEvent Constants
  • Set of constants built-in to relate to the
    KeyCode for all the possible inputs on a
    keyboard.
  • KeyTyped of a capital A requires a keyPressed of
    SHIFT and a keyPressed of a

28
  • import java.awt.event.
  • class MyListener implements KeyListener
  • public void keyPressed(KeyEvent e)
  • System.out.println(A key was pressed)
  • public void keyRelease(KeyEvent e)
  • System.out.println(A key was release.)
  • public void keyTyped(KeyEvent e)
  • System.out.println(A key was Typed)

29
  • import java.awt.
  • import java.awt.event.
  • class Test
  • public static void main(String args)
  • Jframe f new Jframe(test)
  • JTextField t new JTextField(This is the
    default)
  • MyListener lis new MyListener()
  • t.addKeyListener(lis)
  • f.add(t)
  • f.setSize(400,300)
  • f.setVisible(true)

30
  • What happens when the following code is executed?
  • import java.awt.
  • import java.swing.
  • class Test extends JFrame
  • JButton b1, b2, b3
  • Test()
  • JButton b1 new JButton(b1)
  • JButton b2 new JButton(b2)
  • JButton b3 new JButton(b3)
  • add(b1)
  • add(b2)
  • add(b3)
  • this.setSize(400, 200)
  • this.setVisible(true)
  • public static void main(String args)
  • Test t new Test()

31
  • Write the syntax to add a Checkbox named Maple to
    a Checkboxgroup named Trees and set it to a
    default to checked. There are 2 ways to
    accomplish this.
  • What happens if you create a JTextField on a
    Panel using new JTextField(Enter values) and
    the user enters a long String?

32
  • What is the output of the following code?
  • import java.awt.
  • import java.awt.event.
  • class Test extends Frame
  • MyListener lis new MyListener()
  • public Test()
  • this.addKeyListener(lis)
  • this.setSize(400, 400)
  • this.setVisible(true)
  • public static void main(String args)
  • Test t new Test()
  • class MyListener extends KeyAdapter
  • public void KeyPressed(KeyEvent e)
  • System.out.println(got e.getKeyChar())
Write a Comment
User Comments (0)
About PowerShow.com