Wednesday, October 29, 2014

Java Program



A Revised Version of Hello World

import java.io.*;
class MyFirstProgram
{
           public static void main(String[] args)
{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String name = "Instructor";
    System.out.print("Give your name: ");
    try
    {
          name = in.readLine();
    }
          catch(Exception e)
          {
             System.out.println("Caught an exception!");
        }
             System.out.println("Hello " + name + "!");
  }
}

A Java Program with Looping

class Fibonacci
{
public static void main(String[] args)
{
       int lo = 1;
       int hi = 1;
       System.out.println(lo);
       while (hi < 50)
{
      System.out.print(hi);
      hi = lo + hi;
      lo = hi - lo;

   }
}

A Java Class

class Point
{
      public double x, y;
      public static Point origin = new Point(0,0);
      Point(double x_value, double y_value)
 {
        x = x_value;
        y = y_value;
   }
        public void clear()
{
        this.x = 0;
        this.y = 0;
   }
        public double distance(Point that)
 {
        double xDiff = x - that.x;
        double yDiff = y - that.y;
        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
   }
}

Extending a Class: Inheritance
class Pixel extends Point
{
     Color color;

     public void clear()
{
       super.clear();
       color = null;
  }
}

Interfaces

interface Lookup
{
  
    Object find(String name);
}

void processValues(String[] names, Lookup table)
{
        for (int i = 0; i ! names.length; i++)
{
        Object value = table.find(names[i]);
        if (value != null)
        processValue(names[i], value);
   }
}

class SimpleLookup implements Lookup
{
       private String[] Names;
       private Object[] Values;

 public Object find(String name)
{
      for (int i = 0; i < Names.length; i++)
{
             if (Names[i].equals(name))
             return Values[i];
      }
             return null;
   }
  
}


Creating Threads in Java

public class PingPONG extends Thread
{
      private String word;
      private int delay;  
      public PingPONG(String whatToSay, int delayTime)
{
        word = whatToSay; 
        delay = delayTime; 
    }
    public void run()
{
        try
{
            for (;;)
{
                System.out.print(word + " "); 
                sleep(delay);
            }
        }
catch (InterruptedException e)
{
            return;
        }
    }
 public static void main(String[] args)
{
           new PingPONG("Ping", 33).start();
           new PingPONG("PONG",100).start();
    }
}

Two Synchronization Methods

class Account
{
    private double balance;
    Public Account(double initialDeposit)
{
        balance = initialDeposit;
    }
 public synchronized double getBalance()
{
       return balance;
    }
 public synchronized viod deposit(double amount)
{
       balance += amont;
    }
}

public static void abs(int[] values)
{
         synchronized (values)
{
         for (int i = 0; i < values.length; i++)
{
          if (values[i] < 0)
          values[i] = -values[i];
       }
    }
}

UseArgument.java

public class UseArgument
{
            public static void main(String[] args)
    {
        System.out.print("Hi, ");
        System.out.print(args[0]);
        System.out.println(". How are you?");
     }
              }

No comments:

Post a Comment