class class methods in java code example
Example 1: java class
public class Lightsaber {
private boolean isOn;
private Color color;
public Lightsaber(Color color) {
this.isOn = false;
this.color = color;
}
public Color getColor() {
return color;
}
public boolean getOnStatus() {
return isOn;
}
public void turnOn() {
isOn = true;
}
public void turnOff() {
isOn = false;
}
}
public class test {
public static void main(String[] args) {
Lightsaber yoda = new Lightsaber(green);
yoda.turnOn();
}
}
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!");
}
}
}