Bifalgorithm post · Computer science one · Void Methods

How do you use methods defined in another Java class?

How do you use methods defined in another Java class? (Computer science one · Void Methods) — "Once a method has been defined in a class, you can make use of t…

Ideas in this flowchart

  • Using a method defined in a class
  • The class may need to be imported, depending on its location.
  • The example class is Utils in the package unl.cse.
  • Import Utils before the class declaration with import unl.cse.Utils;
  • Invoke a method by referencing its class and using the dot operator.
  • Examples of invoking methods from Utils
  • Utils.sum(a, b) returns 30 when a is 10 and b is 20.
  • Utils.getDistance(0.0, 0.0, 10.0, 20.0) uses literal values.
  • Utils.getMonthlyPayment(p, r, 60) combines variables and a literal value.
Feed/

How do you use methods defined in another Java class?

No cover

Channel

Computer science one

1 page

"Once a method has been defined in a class, you can make use of the method as follows. First, you may need to import the class itself depending on where it is. For example, suppose that the examples we’ve presented so far are contained in a class named Utils (short for “utilities”) which is in a package named unl.cse . Then in the class in which we want to call some of these functions we would import it using import unl.cse.Utils; prior to the class declaration. Once the class has been imported, we can invoke a method in the class by first referencing the class and using the dot operator to access one of its methods. For example,\n\nint a = 10, b = 20;\n\nint c = Utils.sum(a, b); //c contains the value 30\n\n//invoke a method with literal values:\n\ndouble dist = Utils.getDistance(0.0, 0.0, 10.0, 20.0);\n\n//invoke a method with a combination:\n\ndouble p = 1500.0;\n\ndouble r = 0.05;\n\ndouble monthlyPayment = Utils.getMonthlyPayment(p, r, 60);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.