Advice on Methods: Useful Items - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Advice on Methods: Useful Items

Description:

Like Math.pow( a, b ); 1. Math.round(x) double x = 47.67; Int y = 0; ... Math.pow (double a, double b) ... Math.max( a, b ) and Math.min( a, b ) 8. JOptionPane ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 18
Provided by: msresearch
Category:
Tags: advice | items | methods | useful

less

Transcript and Presenter's Notes

Title: Advice on Methods: Useful Items


1
Advice on Methods Useful Items
  • Using static classes

2
So whats a static class?
  • Its a class that doesnt have to be instantiated
    to be used
  • Like System.exit(0)
  • Like Math.pow( a, b )

3
1. Math.round(x)
  • double x 47.67
  • Int y 0
  • y Math.round( x ) returns an int, rounded to
    48
  • Use (int) x to truncate (casting)

4
2. String.valueOf( any number )
  • creates a String when you need it

5
3. Integer.parseInt( any String )
  • creates an int from a String

6
4. Double.parseDouble( any String )
  • creates a double from a String

7
5. x.substring( start, end )
  • String x Have a nice summer
  • x.substring(7, 10) yields nice
  • Start at 0.
  • x.substring( 12 ) yields summer from 12 to
    end
  • Also useful
  • x.equalsIgnoreCase( anotherString )
  • returns a boolean true if the same

8
6. Math.random( )
  • Returns a double value with a positive sign,
    greater than or equal to 0.0 and less than 1.0.

9
7. some other Math methods
  • Math.pow (double a, double b)           Returns
    the value of the first argument raised to the
    power of the second argument.
  • Math.sqrt( double a )           Returns the
    positive square root of a double value.
  • Math.max( a, b ) and Math.min( a, b )

10
8. JOptionPane
  • Here are three useful static methods from
    javax.swing.JOptionPane that allow you to easily
    create dialog boxes for input and output. The
    Java API documentation has many more JOptionPane
    options, but these are sufficient for many uses.
    In the code below userInput and text are Strings,
    button is an int.
  • String userInput  JOptionPane.showInputDialog(
    text )
  • JOptionPane.showMessageDialog( component, text )
  • int button JOptionPane.showConfirmDialog(
    component, text )
  • Use null for the component parameter if you don't
    have a window
  • The dialog box will be centered over the
    component given in the first parameter. Typically
    you would give the window over which it should be
    centered. If your program doesn't have a window,
    you may simply write null, in which case the
    dialog box will be centered on the screen.

11
9. Shortcuts
  • x is the same as x x 1
  • String userInput Hello
  • userInput how are you? yields
  • Hello how are you?
  • System.out.println(\nHello)
  • when printed, \n skips a line
  • System.out.print(Hello)
  • does not go to next line

12
10. A website
  • http//leepoint.net/notes-java/
  • I have no idea where this is from.

13
11. What if you need a date?
  • import java.util.Date
  • public class testpublic static void
    main(String args )
    System.out.println("Starting test program")
    Date myDate new Date() System.out.println(
    myDate )

14
Use JFrame methods
  • import javax.swing.JFrame
  • public class JFrameDemo
  • public static void main (String args )
  • JFrame myWindow new JFrame("Title")
  • myWindow.setDefaultCloseOperation(
    myWindow.EXIT_ON_CLOSE )
  • myWindow.setLocation( 50, 200 )
  • myWindow.setSize( 400, 400)
  • myWindow.setVisible(true)

15
Using pop-up windows
X0, Y0
X800, Y0
X0, Y600
X800, Y600
16
try / catch
  • try
  • (x Integer.parseInt( stringToConvert ))
  • catch (Exception myError)
  • System.out.println(Bad Format)
  • System.exit(0)

17
Methods in the Exception Java Native Class
  • try
  • x Integer.parseInt(stringToConvert)
  • catch(Exception e)
  • System.out.println( e.getMessage() ) // what
    user typed
  • System.out.println( e.toString()) // error
    message
  • e.printStackTrace( ) // where the program
    failed
  • System.exit(0)
Write a Comment
User Comments (0)
About PowerShow.com