objects and classes java code example
Example 1: how to make a class in java
public class Main {
public static void main (String[] args) {
System.out.println("Hello World");
}
}
Example 2: java class
public class TestClass {
private int id = 111;
public TestClass(){
super();
}
public void test(){
}
}
Example 3: using class in java
class Number {
int number;
public boolean isSquare(){
double squareRoot = Math.sqrt(number);
if(squareRoot == Math.floor(squareRoot))
{
return true;
} else {
return false;
}
}
public boolean isTriangular(){
int x = 1;
int triangularNumber = 1;
while(triangularNumber < number){
x++;
triangularNumber = triangularNumber + x;
}
if(triangularNumber == number){
return true;
} else {
return false;
}
}
}
Number myNumber = new Number();
myNumber.number = 16;
System.out.println(myNumber.isSquare());