function and class java code example
Example 1: how to call a function from a class java
public class Classroom {
private String teacherName;
public Classroom(String teacherName){
this.teacherName = teacherName;
}
public String getTeacherName() {
return teacherName;
}
}
public static void main(String[] args){
Classroom firstGrade = new Classroom("John");
firstGrade.getTeacherName();
}
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!");
}
}
}