how to use methods from other classes in java code example
Example 1: java use method in another class
class A {
public void a1 ( ) {
}
private void a2 ( ) {
}
}
class B {
private A objA = new A ( );
public void b1 ( ) {
objA.a1 ();
}
void b2 ( ) {
objA.a2 ();
}
}
Example 2: java classes and methods
import java.lang.Math;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
class ResultNumberShape {
double number;
public boolean isSquare(double number) {
double result = Math.sqrt(number);
return ((result - Math.floor(result)) == 0);
}
}
ResultNumberShape checkNumber = new ResultNumberShape();
System.out.println("Enter any number:");
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();
scanner.close();
if (checkNumber.isSquare(num)) {
System.out.println(num + " is a sqaure number!");
} else {
System.out.println(num + " is not a square!");
}
}
}