xxxxxxxxxx
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());
xxxxxxxxxx
public class Multiplication{ //Class name declaration
int x = 5; //Variable declaration
int y= 15;
public void multi(){ //Method declaration
int z = x*y;
}
}
xxxxxxxxxx
class Dog {
int age = 5;
public static void main(String[]args) {
Dog myObj = new Dog();
System.out.println(myObj.age);
}
}
xxxxxxxxxx
Remember from the Java Syntax chapter
that a class should always start with an uppercase first letter,
and that the name of the java file should match the class name.
xxxxxxxxxx
access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}
xxxxxxxxxx
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.
xxxxxxxxxx
class <className>
{
public:
(public member data and functions go here)
private:
(private member data and functions go here)
};