• We just launched and are currently in beta. Join us as we build and grow the community.

How to use Predefined Method

zudi

Sales Funnel Architect
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
76
Likes
176
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn on how to use Predefined Method. In java, predefined methods are organized as a collection of classes, called class libraries. For example, the class Math contains mathematical methods. The method type is the data type of the value returned by the method. The class Math is contained in the package java.lang

. I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code.

  1. import

    static

    java.lang

    .Math

    .*;
  2. import

    static

    java.lang

    .Character

    .*;

  3. public

    class

    PredefinedMethod
  4. {
  5. public

    static

    void

    main(

    String

    [

    ]

    args)
  6. {
  7. int

    x;
  8. double

    u, v;

  9. System

    .out

    .println

    (

    "Uppercase a is "
  10. +

    toUpperCase(

    'a'

    )

    )

    ;

  11. u =

    4.2

    ;
  12. v =

    3.0

    ;

  13. System

    .out

    .printf

    (

    "%.1f to the power of "
  14. +

    "%.1f = %.2f%n"

    , u, v, pow(

    u,v)

    )

    ;

  15. System

    .out

    .printf

    (

    "5 to the power of 4 = "
  16. +

    "%.2f%n"

    , pow(

    5

    , 4

    )

    )

    ;

  17. u =

    u +

    Math

    .pow

    (

    3

    , 3

    )

    ;
  18. System

    .out

    .printf

    (

    "u = %.2f%n"

    , u)

    ;

  19. x =

    -

    15

    ;
  20. System

    .out

    .printf

    (

    "The absoloute value of %d = "
  21. +

    "%d%n"

    , x, abs(

    x)

    )

    ;
  22. }


  23. }

Sample run:

Uppercase is A
4.2 to the power of 3.0 = 74.09
5 to the power of 4 = 625.00
u = 31.20
The absolute value of -15=15

This program works as follows:

The statement
  1. System

    .out

    .println

    (

    "Uppercase a is "
  2. +

    toUpperCase(

    'a'

    )

    )

    ;

outputs the uppercase letter that corresponds to ‘a’, which is ‘A’.

In the statement
  1. System

    .out

    .printf

    (

    "%.1f to the power of "
  2. +

    "%.1f = %.2f%n"

    , u, v, pow(

    u,v)

    )

    ;

, the method pow (of the class Math) is used to output u. The method pow

is called with the parameters u and v. in this case, the values of u and v are passed to the method pow

The statement
  1. System

    .out

    .printf

    (

    "5 to the power of 4 = "
  2. +

    "%.2f%n"

    , pow(

    5

    , 4

    )

    )

    ;

uses the method pow

to output 5 to the power of 4.

The statement u =

u +

Math

.pow

(

3

, 3

)

;

uses the methodpow

to determine 3 to the power of 3, adds this value to the value of u, and then stores the new value into u.

The statement System

.out

.printf

(

"u = %.2f%n"

, u)

;

outputs the value of u.

The statement x =

-

15

;

stores -15 to x, and the statement
  1. System

    .out

    .printf

    (

    "The absoloute value of %d = "
  2. +

    "%d%n"

    , x, abs(

    x)

    )

    ;

outputs the value of x.

 

452,292

323,526

323,535

Top