CODERECTANGLE
  • Home
  • React.js
  • Java Tutorial
    • Introduction
    • Setting up the environment : Netbeans
    • Basics
    • FirstApp
    • Classes and Objects
    • Commonly Used Packages
    • Celsius to Fahrenheit Converter App
    • Celsius to Fahrenheit Using JOptionPane
    • Exception Handling
    • TestGradeApp
  • Objective-C
  • Python-Django
  • Contact

How to Write a CLass

TO CODE A CLASS MEANS TO WRITE THE INSTANCE VARIABLES. DEFINING THE INSTANCE VARIABLES IS       TO DECIDE WHAT VARIABLES (ACCESS IDENTIFIER, DATA TYPE, AND NAME) SHOULD BE INCLUDED IN THE     CLASS FOR WHICH ALL OBJECTS OF THE CLASS CAN ACCESS THOSE DATA. 

General Syntax while writing a class:



 accessModifier returnType methodName([argumentList]) { 
                              //the body of the statements 
     } 


Here: 
 accessModifier – can be public, private, protected, or package.  (Discussed Later)



Though there is no hard rule to follow when writing code, but there are some convention which is highly recommended to follow.


• Access modifier of an instance variable normally is private; 
• Access modifier of a method normally is public; 
• For each instance variable, there are setXxx() and getXxx() methods;   
• setXxx() is also called setter.  It assigns the value of the parameter to the instance variable it sets.; 
• getXxx() is also called getter. It returns the value of an instance variable; and
 • Other method(s) will carry out a specified task.  A method can access the instance variables directly.  Normally its return type is void and the argument list is empty. 


Demo Class : 



public class Payment {  //define Payment class   

 private double price, //define an instance variable and a const      
total;   private int quantity;    
private final double INTEREST_RATE = 0.0875; //define a const  

 public void setPrice(double cost)    // setter for price    {
                                                      price = cost; 
                                                                     }  
public double getPrice()    //getter for price   {

                                                  return price;
                                                                    }  
 public void setTotal (double amount)   { 

                                                 total = amount; 
                                                                    } 
 public double getTotal()    //getter for total   { 

                                                    return total; 
                                                                  }  
 public void setQuantity(int item)  //setter for quantity   

                                                                  {
                                                              quantity = item;
                                                                    }   
  public int getQuantity()    //getter for quantity   { 

                                                      return quantity; 
                                                                 }  
 public void bill()     //define the method   {

                                total = quantity * price + quantity * price * INTEREST_RATE;
                                                                                        } 
 } // end of Payment
 

test a class

A programmer-defined class must be tested to make sure it reaches the objective without any error.  To do so, we usually write a driver program to perform the testing.  To simplify testing, the driver is normally written in as a Java application class.   
The driver has a main() method that will be the starting point code for execution by the Java Virtual Machine (JVM).  The driver is also called as executable code.  The way to code a main() method is basically the same for all Java application drivers:

 
 public static void main(String[] args) { 
  //body of the main() 
  } //end of the main() 


 Here, a driver class and its main() methods are public.  The main() method must be declared as a static method to be executed by the JVM.  The important feature of a static method is that it can be called without creating an object of the class.  That means the JVM can execute a main() method without instantiating an object of the driver class. 
The argument in a main() method must be an array of String type.  Although the args parameter may be rarely used in the code, we must follow this required syntax and specify it in the parameter list. 
Powered by Create your own unique website with customizable templates.
  • Home
  • React.js
  • Java Tutorial
    • Introduction
    • Setting up the environment : Netbeans
    • Basics
    • FirstApp
    • Classes and Objects
    • Commonly Used Packages
    • Celsius to Fahrenheit Converter App
    • Celsius to Fahrenheit Using JOptionPane
    • Exception Handling
    • TestGradeApp
  • Objective-C
  • Python-Django
  • Contact