Advice on Methods: 10 Useful Items - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Advice on Methods: 10 Useful Items

Description:

Advice on Methods: 10 Useful Items. STATIC classes. So what's a static class? ... Math.round( x ) returns an int, rounded to 48. Use (int) x to truncate ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 13
Provided by: msresearch
Category:

less

Transcript and Presenter's Notes

Title: Advice on Methods: 10 Useful Items


1
Advice on Methods 10 Useful Items
  • 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.random()

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

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 Happy Holidays
  • x.substring(2, 4) yields ppy
  • Start at 0.
  • x.substring( 11 ) yields ays from 11 to end
  • Also useful
  • x.equalsIgnoreCase( another String )
  • 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(
    component, 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.
Write a Comment
User Comments (0)
About PowerShow.com