classes and method in java code example
Example 1: 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!");
}
}
}
Example 2: class in java
Class is a blueprint or template which
you can create as many objects as you
like. Object is a member or instance
of a class.
Class is declared using class keyword,
Object is created through
new keyword mainly. A class is a template
for objects. A class defines
object properties including a valid range
of values, and a default value.
A class also describes object behavior.