public class yourClass {
private String yourField;
//Constructor
public yourClass() {
}
}
public class Multiplication{ //Class name declaration
int x = 5; //Variable declaration
int y= 15;
public void multi(){ //Method declaration
int z = x*y;
}
}
public class HelloWorld {
public static void main(String[] args) {
class Number{
int number;
public boolean isPos(){
if(number > 0){
return true;
} else {
return false;
}
}
}
Number myNumber = new Number();
myNumber.number = 7;
if(myNumber.isPos()){
System.out.println(myNumber.number + " is positive!!!");
} else {
System.out.println(myNumber.number + " is not positive!!!");
}
}
}
class Number {
int number;
// square numbers
public boolean isSquare(){
double squareRoot = Math.sqrt(number);
if(squareRoot == Math.floor(squareRoot))
{
return true;
} else {
return false;
}
}
// triangular numbers
public boolean isTriangular(){
int x = 1;
int triangularNumber = 1;
while(triangularNumber < number){
x++;
triangularNumber = triangularNumber + x;
}
if(triangularNumber == number){
return true;
} else {
return false;
}
}
}
// testing
Number myNumber = new Number();
myNumber.number = 16;
System.out.println(myNumber.isSquare());
A class
— is a template used to create objects and to define object data types and methods.
Classes are categories, and objects are items within each category.
All class objects should have the basic class properties.
class <className>
{
public:
(public member data and functions go here)
private:
(private member data and functions go here)
};