Java class and objects

in this example, we create a java class and then show how we create an object from it.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;

/**
*
* @author DKenga
*/
public class Calculator {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int sum,difference,multiplication;
double division;

//lets create an object from the Cal class; we use the keyword new
Calc calc = new Calc();
//the Calc() is a default constructor
//Now we use the object to access the functions
sum = calc.sum(3,4);
difference = calc.sub(12, 6);
multiplication = calc.multiplication(3, 4);
division = calc.division(12, 5);

System.out.println("Sum is " + sum);
System.out.println("The difference is " + difference);
System.out.println("Multiplicaion is " + multiplication);
System.out.println("Divisuion is " + division);

}

}

We then create another class called Calc.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;

/**
*
* @author DKenga
*/
public class Calc {

//We create our data memmers here
//we have created Instance variables
int first_number;
int second_number;

int sum (int num1, int num2){
int result = num1+num2;
/*the variable result is a local variable and is only visible within
this single function. it is different from first_number, which is visible
throughout the class.
*/
return result;
}

int sub(int num1, int num2){
int result = num1- num2;
return result;
}
int multiplication (int num1, int num2){
int result = num1*num2;
return result;
}

double division(int num1, int num2){
double result =0.00;
result= (num1/num2);
return result;
}

}

note that we have not used the variables

  int first_number;
    int second_number;

Our objective was to show the components of a simple class. we will explain the use of these items in our next post.

 

Another eaxmple

Class SafaricomBongaPoints.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package safaricombongapoints;

/**
*
* @author dkenga
*/
public class SafaricomBongaPoints {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Points point = new Points();
System.out.println(point.points);
Points point1 = new Points("+25473646867", 0.0);
System.out.println("the phone number is:" + point1.phone_number);
//Points point1 = new Points();
//Points point2 = new oints();
/*Now you see we can create as many objects from a
single class.
the Points() is a constructor; in fact a default
constructor.
*/
//We can acquire new point, then check our new balance

point.acquireBongaPoints(100);
System.out.println(point.checkBongaBalance());
}

}

 

add another class Points.java

 

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package safaricombongapoints;

/**
*
* @author dkenga
*/
public class Points {
//data members
double air_time;
String name;
String phone_number;
int points;

//these are constructors

Points(){
// this is a default constructor
System.out.println("A default constructor executes on object creation");
points =10;

}
//we now create a paramerrised constructor
Points (String phone, double air){
air_time = air;
phone_number = phone;

}

//methods
int redeemFreeMinutes (){
//we assume that 10 points redeems 1 minute of talk time
int minutes =  points/10;
return minutes;
}

int checkBongaBalance (){
return points;
}

void acquireBongaPoints (int new_airtime){
int p = new_airtime/10;

points = points+p;

}
}

 

4 Comments

Add a Comment

Your email address will not be published. Required fields are marked *