Wednesday, 13 August 2014

Demonstrate the Method Overloading feature of Object Oriented 


Programming using example of “calculate Volume of given objects” program.

Create a Volume class called by main class which contains method volume that 


calculate the volume of cube or cuboid or cylinder, depending on number of 


parameters. Read all the inputs from user. Result has to be printed in main class.






(CODE)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Volume
{
   double volume(double l)
   {
      return l*l*l;
   }
   double volume(double r,double h)
   {
      return 3.14*r*r*h;
   }
   double volume(double l,double w,double h)
   {
      return l*w*h;
   }
   void printMenu()
   {
     System.out.println("Select One to Calculate Volume or Exit");
     System.out.println("1) Cube");
     System.out.println("2) Cuboid");
     System.out.println("3) Cylinder");
     System.out.println("4) Exit");
   }
}

public class lab3A1
{
    public static void main(String args[]) throws NumberFormatException, IOException
    {
       Volume v=new Volume();
       int option;
       boolean exit=false;
       BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
       double answer,l,w,h,r;
       do
       {
          v.printMenu();
          try
          {
             option=Integer.parseInt(br.readLine().toString());
          }
          catch(Exception e)
          {
             option=5;
             System.out.println(e);
          }
          if(option==1)
          {
             System.out.println("Enter the Cube Length:");
             answer=v.volume(Double.parseDouble(br.readLine().toString()));
             System.out.println("The Volume of Cube is:"+answer);
          }
          else if(option==2)
          {
             System.out.println("Enter the Cuboid Length:");
             l=Double.parseDouble(br.readLine().toString());
             System.out.println("Enter the Cuboid Width:");
             w=Double.parseDouble(br.readLine().toString());
             System.out.println("Enter the Cuboid Height:");
             h=Double.parseDouble(br.readLine().toString());
             answer=v.volume(l,w,h);
             System.out.println("The Volume of Cuboid is:"+answer);
           }
           else if(option==3)
           {
             System.out.println("Enter the Cylinder Radius:");
             r=Double.parseDouble(br.readLine().toString());
             System.out.println("Enter the Cylinder Height:");
             h=Double.parseDouble(br.readLine().toString());

             answer=v.volume(r,h);
             System.out.println("The Volume of Cylinder is:"+answer);
            }
            else if(option==4)
            {
              exit=true;
              System.out.println("Closing Program");
            }
            else
            {
              System.out.println("Invalid Option");
             }
         }while(!exit);
   }

}










Write a program to demonstrate the inheritance feature of object oriented prgraming.
Create a class Circle and write methods to calculate area of a circle. Declare required fields. Write another class Cylinder which inherite the Circle class and write method to calculate volume of Cylinder useing it.

(CODE)

class Circle
{
   float radius;
   float pie;

   Circle()
   {
      this.pie=3.14f;
   }

   float area()
   {
      float ans;
      ans=this.pie*this.radius*this.radius;
      return ans;
   }
}

class Cylinder extends Circle
{
   float height;
    
   float volume()
   {
     float ans=area()*this.height;
     return ans;
   }
   
   Cylinder()
   {
     this.pie=3.14f;
   }
}

public class lab3A3
{

   public static void main(String args[])
   {
      Cylinder c= new Cylinder();
      c.radius= 4.5f;
      c.height=2.5f;
      System.out.println("Answer: "+c.volume());
   }
}

Tuesday, 1 April 2014

Giraph Code to find maximum Value of node

Giraph Version:1.1.0
Hadoop Version: 0.20.203

Input Graph:
store it it tiny_graph.txt and upload it on HDFS
[0,5,[[1,1],[3,3]]]
[2,66,[[1,2],[4,4]]]
[1,25,[[0,1],[2,2],[3,1]]]
[3,85,[[0,3],[1,1],[4,4]]]
[4,125,[[3,4],[2,4]]]

Input Graph Pattern:  [VertexId,VertexValue,[EdgeDestinationVertexId,EdgeValue]....]


code:

package Success;
import java.io.IOException;

import org.apache.giraph.graph.BasicComputation;
import org.apache.giraph.graph.Vertex;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;


public class MaxVal extends BasicComputation<LongWritable,DoubleWritable,FloatWritable,DoubleWritable>
 {

    @Override
    public void compute(
            Vertex<LongWritable, DoubleWritable, FloatWritable> v,
            Iterable<DoubleWritable> msg) throws IOException {
        // TODO Auto-generated method stub
        boolean changed=false;
       
        for(DoubleWritable messages :msg)
        {
           
           
            if(v.getValue().get() < messages.get())
            {
                v.setValue(messages);
                changed=true;
            }
        }
       
        if(getSuperstep()==0 | changed)
        {
            sendMessageToAllEdges(v,v.getValue());
        }
       
        v.voteToHalt();
    }

}

How to Compile:

copy this java file to:
$cp your_program.java /usr/local/giraph/giraph-examples/src/main/java/org/apache/giraph/examples/your_program.java

$cd /usr/local/giraph/giraph-examples/
$mvn compile


How to run:

hadoop jar /usr/local/giraph/giraph-examples/target/giraph-examples-1.1.0-SNAPSHOT-for-hadoop-1.2.1-jar-with-dependencies.jar org.apache.giraph.GiraphRunner MaxVal  -vif org.apache.giraph.io.formats.JsonLongDoubleFloatDoubleVertexInputFormat -vof org.apache.giraph.io.formats.JsonLongDoubleFloatDoubleVertexOutputFormat -vip /input/tiny_graph.txt -op /output/MaxVal -w 1

What are arguments?
-vif    :    Graph input format
-vof   :    Graph output format
-vip    :    Graph input file path on HDFS
-op      :    Graph outout path on HDFS


Giraph sendMessageToAllEdges() API

 sendMessageToAllEdges(vertex,data)

This Giraph API send messages to all the vertices directly connected with given vertex.
Tested for undirected graph.

Monday, 31 March 2014

Check HDFS storage/memory status

command:

hadoop   dfsadmin  -report

Kill Hadoop Job

To list hadoop job:

hadoop job -list

To kill Hadoop Job:

hadoop job -kill <jobID>

Datanode not runnning..

Don't configure Hadoop to run as ROOT.
Instead use <hduser> or someother standard user.(created with useradd command)

I tried to configured with root and got following error and my datanode did not start. so finally
I created user with name:hduser and now I am up and running...

Best place to learn your first hadoop configuration is following page. Ignore Giraph configuration and take hadoop configuration part.

https://giraph.apache.org/quick_start.html

localhost: starting datanode, logging to /usr/local/hadoop/bin/../logs/hadoop-root-datanode-vm1.out
localhost: Unrecognized option: -jvm
localhost: Error: Could not create the Java Virtual Machine.
localhost: Error: A fatal exception has occurred. Program will exit.