Monday 21 April 2014

JAVA OOP DEMONSTRATION

I'm gonna show you how Java OOP(Object Oriented Programming)  works. The program i write is about the propagation of sound waves.
I believe you know this formula of sound v = 2x/t .
It's used to find

  • v which is speed of the sound in meters per second m/s.
  • x which is the distance of the sound in meters m,
  • t which is time in seconds s/
First we create a class named Sound.java which will contain all our formula variables and the method that will calculate them/ You should notice that the class doesn't contain a main method, therefore when you try to run it, you will get an error. Below is the source code for the Sound.java file

/*
 * This class just construct the formulae for solving
 * speed of sound 
 * distance of sounde
 * and time sound take to reach back it's
 * Notice that the class doesn't contain a main method
 */

public class Sound 

{
    // Inatialise our varibles
  public double v;
  public double x;
  public double t;
  
  // method for calculating speed of sound
  public double speed(double x, double t)
    {
      v = (2*x)/t;
      return v;
    }
  
  // method for calculatin distance of sound
  public double distance(double v, double t)
    {
       x =(v*t)/2;
       return x;
    }
  
  // method for calculating time 
  public double time(double v, double x)
    {
       t = (2*x)/v; 
       return t;
    }
  
}


Next, we create the file SoundDrive.java which will contain the main method. You must also save the two files(i.e Sound.java and SoundDrive,java) in the same directory. 
In the SoundDrive.java you will notice how we create and object calc using our Sound class, after creating the object we are able to access it;s methods and variables. 
Below is the Source code for the SoundDrive.java

import java.util.Scanner;

public class SoundDrive 


    public static void main(String[] args) 
    {
         Scanner input = new Scanner(System.in); // construct input
         Sound calc = new Sound(); //create obkect of class sound
         String get;
         System.out.println("This program calculate Speed of sound, distance of the"
                + " sound and the time sound take to echo.\n How to work with it, enter "
                + "v to calculate sound speed, enter x to calculate distance of sound\n or enter t"
                + " to calculate time.");
      
        System.out.print("Enter V, X or T or enter any key to exit: "); // prompt user for selection
      get = input.nextLine(); //
      if (get.equalsIgnoreCase("v"))
      {
          System.out.print("Enter Distance: ");
          double dst,tm;
          dst = input.nextDouble();
          System.out.print("Enter Time: ");
          tm = input.nextDouble();
          System.out.println("The speed of sound = "+calc.speed(dst, tm)+"m/s"); 
      }
      else if (get.equalsIgnoreCase("x"))
      { 
          System.out.print("Enter Speed: ");
          double spd,tm;
          spd = input.nextDouble();
          System.out.print("Enter Time: ");
          tm = input.nextDouble();
          System.out.println("The distance X = "+calc.distance(spd, tm)+"m");
      }
      else if (get.equalsIgnoreCase("t"))
      {
          System.out.print("Enter Speed: ");
          double spd,dst;
          spd = input.nextDouble();
          System.out.print("Enter Distance: ");
          dst = input.nextDouble();
          System.out.println("The time T = " +calc.distance(spd, dst)+"s");
      }
    
}
    
}

This is small example about the concept of OOP in java. Have fun with it.

Saturday 12 April 2014

A SIMPLE JAVA GUI CALCULATOR

Below is the source code of a simple Java GUI Calculator that I programmed couple of days ago.
This is the Snapshot of the calculator

The calculator works with two text fields the (i.e for first number and second number), I also used uneditable third text field to display the result. Entering a given numbers in the two text fields and clicking an arithmetic operator button will display the result. 

This is the Source Code: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Calculator extends JFrame
    public JTextField text = new JTextField(10);
    public JTextField text2 = new JTextField(10);
    public JTextField rslt = new JTextField(10);
    public int num = 11;
    public int number;
    public Calculator()
 {
  
      JFrame cover = new JFrame("My App");
      cover.setLayout(new FlowLayout());
      cover.setSize(260, 200);
      cover.setTitle("Maitalata's Calc.");
      cover.setVisible(true);
      
      cover.add(new JLabel("First Numbber   "));
      cover.add(text);
      cover.add(new JLabel("Second Number"));
      cover.add(text2);
      cover.add(new JLabel("Result"));
      cover.add(rslt);
      rslt.setEditable(false);
      JButton b = new JButton("/");
      cover.add(b);
      b.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
           first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a / b;
          rslt.setText(c+"");
      }
      } ); 
      JButton c = new JButton("+");
      cover.add(c);
      c.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
          first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a + b;
          rslt.setText(c+"");
      }
      } );
      JButton d = new JButton("-");
      cover.add(d);
      d.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
          first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a - b;
          rslt.setText(c+"");
      }
      } );
      
      JButton st = new JButton("x");
      cover.add(st);
      st.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
           first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a * b;
          rslt.setText(c+"");
      }
      } ); 
      JButton e = new JButton("Clear");
      cover.add(e);
      e.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          text.setText(null);
          text2.setText(null);
          rslt.setText(null);
      }
      } );
      
      
 }
 public static void main(String args[])
 {        
   Calculator nw = new Calculator();
 } 
}