The essence of Java is to write useful classes - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

The essence of Java is to write useful classes

Description:

The essence of Java is to write useful classes ... does most of what you need, and absorb it entirely into your class (i.e. you ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 32
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: The essence of Java is to write useful classes


1
The essence of Java is to write useful classes
  • You can use library classes to add complicated
    functions to your class (like JOptionPane)
  • You can instantiate another class to use locally.
  • You can use Java Native (Wrapper) classes to
    accomplish very common functions, like
    String.equals(exit) and
  • Integer.parseInt(userInput )
  • You can find a class that already does most of
    what you need, and absorb it entirely into your
    class (i.e. you can inherit all of the
    capabilities of another class)

2
Extension, Hierarchy, and Inheritance
3
Extension, Hierarchy, and Inheritance
  • Extends in the class statement, tells which
    class serves as a parent.
  • Inheritance the child class inherits public
    methods and variables from the parent class
  • Hierarchy - parent to child relationship

4
Extend keyword
  • public class myClass extends someOtherClass
  • // I can use all the public methods and
  • // variables from someOtherClass in myClass.

5
Other terms
  • Parent / Child
  • Base class / Sub class
  • Super Class / Sub class
  • Any class (not just an imported library class)
    can be a parent or base class.
  • Find or Write classes then write new inherited
    classes for targeted capabilities.

6
Java common inherited classes
  • public class myClass extends JFrame
  • - myClass gets a whole bunch of methods for
    displaying windows on-screen
  • public class myClass extends Applet
  • - applet and browser graphics
  • public class myClass extends Object
  • the parent of ALL classes, usually omitted

7
Child is more powerful than the Parent
  • The Parent has methods that are useful (i.e. the
    JFrame class handles mouse input)
  • The Child can write new methods that enhance
    those capabilities (a class that extends JFrame
    can apply mouse handling to joysticks, game
    controllers, etc.)

8
An example using counterClass
  • public class counterClass
  • public static int intervalSummer(int a, int b)
  • int x
  • int total 0
  • for ( x a x lt b x)
  • total total x
  • return(total)
  • // end intervalSummer method
  • // end counterClass

9
new class for average
  • public class averagerClass extends counterClass
  • public static double averager(int a, int b)
  • return(ab/2.0)
  • // end averager method
  • // end averagerClass

10
averagerClass
  • has a new method averager
  • still has an old method intervalSummer from
    counterClass.
  • extends means everything in both classes

11
in main
  • averagerClass mathObject new averagerClass( )
  • int x,y
  • x mathObject.intervalSummer( 4, 10 ) // legal
  • y mathObject.averager(10,90)

12
Currently
  • If you dont specify an extend source, a class
    extends the Java base class Object
  • Only mildly capable
  • Better to extend a useful class

13
  • import javax.swing.JFrame
  • public class MainClass extends JFrame
  • public MainClass()
  • .....
  • public static void main (String args )
  • MainClass application new MainClass( )

14
What is this?
  • Main method instantiates the class that its in?
    Why?
  • Because main is not considered a member of the
    class that contains it.
  • The Main class serves a place-holding function
    for the computer, by keeping main in a usable
    place.
  • Instantiating the Main class causes the
    constructor (if there is one) to be run.

15
JFrame
  • Puts graphics-capable windows on screen.
  • JOptionPane uses JFrame
  • Colors
  • Fonts
  • Drawings
  • A frame for games

16
the computer screen
(600, 0)
  • (0, 0)

made up of tiny pixels, each with an (x, y)
location
(600, 800)
(0, 800)
17
  • import javax.swing.JFrame
  • public class MainClass extends JFrame
  • public MainClass ( )
  • setLocation(100,200)
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass application new MainClass( )
  • // end main method
  • // end class

18
  • import javax.swing.JFrame
  • public class MainClass extends JFrame
  • public MainClass ( int x, int y )
  • setLocation( x, y )
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass application new MainClass(100,
    200)
  • // end main method
  • // end class

19
  • import javax.swing.JFrame
  • public class MainClass extends JFrame
  • public MainClass ( int x, int y )
  • setLocation( x, y )
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass appl1 new MainClass(100, 200)
  • MainClass appl2 new MainClass(200, 300)
  • // end main method
  • // end class

20
Demo
21
The JFrame method paint( )
  • looks for, and runs, a special method that YOU
    write, called
  • paint( Graphics g )

22
  • import javax.swing.
  • import java.awt.
  • public class PaintTest extends JFrame
  • public PaintTest()
  • setSize( 500,500 )
  • setVisible( true )
  • repaint()
  • public void paint( Graphics g )
  • g.drawRect(50,50,200,200) // x, y, width,
    height
  • g.drawOval(50,50,100,100) // x, y, width,
    height
  • public static void main( String args )
  • PaintTest app new PaintTest()
  • // end main

23
helper classes
  • remember
  • try
  • catch ( Exception e )
  • the Exception class acted on information behind
    the scenes to help handle run-time errors.
  • well, now we have
  • paint ( Graphics g )

24
Objects As Containers of Information
25
Objects dont always DO things
  • Sometimes they just HOLD things (information)
  • The Color class can hold color information
  • The Point class can hold cartesian coordinate
    information
  • The Container class, well

26
Color class
  • Like the String class, and Integer class, does
    not use the new keyword.
  • holds a color value (nothing more)
  • e.g Color boxColor Color.blue
  • then boxColor can be used to set System
    properties in Classes/Objects that need color
    (more later).

27
JColorChooser returns a Color object to the
caller
28
Returns an Object?
  • JColorChooser fills in all of the information in
    a blank object of the Color class, and copies it
    to the Color object in the calling statement
  • import javax.swing.
  • myClr JColorChooser.showDialog(
  • null, Greeting, default color )

29
JFrame methods
  • set the window up
  • requires import javax.swing.JFrame
  • or import javax.swing .
  • setLocation( 10, 10 )
  • setSize( 500,500 )
  • setVisible( true )
  • paint (Graphics g)

30
another swing method
  • colorObject JColorChooser.showDialog( null,
    greeting, default )

31
Graphics methods
  • draw in the window
  • requires import java.awt .
  • g.setColor( Color.blue )
  • g.drawRect(50,50,200,200)
  • g.fillOval(50,50,100,100)
  • // x, y, width, height
Write a Comment
User Comments (0)
About PowerShow.com