Wednesday, December 25, 2019

Find shortest word in a string

public class MyClass {
    public static void main(String args[]) {
        String str = "I am Ankit Verma";
        String[] s = str.split(" ");
        String shortest = s[0] ;
        for(String s1:s)
        {
            if(s1.length()<shortest.length())
            {
                shortest = s1;
            }
        }
        System.out.println(shortest);
     }
}

Tuesday, October 3, 2017

10 WHOLE-PART PATTERN


Car.java

public class Car
{
   public CarParts[] parts=new CarParts[10];
   int c=0;
   /**
   @roseuid 56553BC001D7
    */
    public Car()
    {
    System.out.println("Car is initialised and will get assembled now...\n");   
  }
 
   /**
   @roseuid 56553AFA020A
    */
  public void add(CarParts cp)
    {
    parts[c++]=cp;   
    }
   /**
   @roseuid 56553B060252
    */
  public void assemble()
    {
    for(int i=0;i<c;i++)
parts[i].display();
System.out.println("\nCar has been prepared...");   
  }
}


CarParts.java

public interface CarParts 
{
   
  /**
  @roseuid 56553B6E02AD
    */
    public void display();
}
//Source file: C:\\Program Files\\Java\\jdk1.7.0_80\\bin\\Engine.java


public class Engine implements CarParts
{
   
  /**
    @roseuid 56553BC002A4
    */
  public Engine() 
    {
    System.out.println("Engine created");
    }
   
    /**
  @roseuid 56553B980173
  */
  public void display() 
  {
    System.out.println("Engine added");
  }
}

Wheels.java

public class Wheels implements CarParts
   /**
   @roseuid 56553BC003C9
   */
   public Wheels() 
   {
    System.out.println("Wheels created");
   }
   /**
   @roseuid 56553B9D017F
   */
   public void display() 
   {
    System.out.println("Wheels added");
   }
}


WindShield.java

public class WindShield implements CarParts
{
   /**
   @roseuid 56553BC0032F
   */
   public WindShield() 
   {
    System.out.println("Wind-shield created");
   }
   /**
   @roseuid 56553B910256
   */
   public void display() 
   {
    System.out.println("Wind-shield added");
   }
}


Factory.java


public class Factory
{
   
   /**
   @roseuid 56553BC00113
    */
   public Factory() 
   {
    
   }
   
   /**
   @roseuid 56553AD60084
    */
   public static void main(String[] args) 
   {
    Car car=new Car();
Engine e=new Engine();
WindShield ws=new WindShield();
Wheels w=new Wheels();
car.add(e);
car.add(ws);
car.add(w);
car.assemble();
   }
}

11 MASTER-SLAVE PATTERN









Master.java

public class Master
{
private Resource resource;
private int sk=2;
private Resource res=new Resource();
private Slave[] slave=new Slave[sk];

public Resource getResource() {

return resource;
}

public void setResource(Resource theResource) {
resource = theResource;
}
public void slave()
{
}
        public void run()
{
for(int i=0;i<sk;i++)
slave[i]=new Slave(res);

for(int i=0;i<sk;i++)
slave[i].start();

for(int i=0;i<sk;i++)
{
try
{
slave[i].join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println(slave[i].getName()+"--has died");
}
}
System.out.println("Master Exiting now");
}
public void start() {
}
}


Slave.java

public class Slave extends Thread
{
private Resource resource;
private Resource sharedResource;
private Boolean done=false;
public void halt()
{
done=true;
}
public Slave(Resource res)
{
sharedResource=res;
}
protected boolean task()
{
int status=sharedResource.innerStatus();
return(status>7);
}
public Resource getResource() 
{
return resource;
}
public void setResource(Resource theResource) 
{
resource = theResource;
}
public void run() 
{
while(done!=true)
{
done=task();
try
{
Thread.sleep(500);
}
catch(Exception e)
{
}
}
}
}


Resource.java

public class Resource 
{
private TestMaster testmaster;
private int status=0;
public synchronized int innerStatus()
{
int local=status;
System.out.println("Ststus="+local);
local++;
try
{
Thread.sleep(500);
}
catch(Exception e)
{
}
status=local;
System.out.println("New Status="+local);
return status;
}
public TestMaster getTestmaster() 
{
return testmaster;
}

public void setTestmaster(TestMaster theTestmaster) 
{
testmaster = theTestmaster;
}

private Master master;
public Master getMaster() 
{
return master;
}
public void setMaster(Master theMaster) 
{
master = theMaster;
}
private Slave slave;
public Slave getSlave() 
{
return slave;
}
}

TestMaster.java

public class TestMaster 
{
private Resource resource;
public Resource getResource() 
{
return resource;
}
public void setResource(Resource theResource) 
{
resource = theResource;
}
public static void main(String args[]) 
{
Master ms=new Master();
ms.run();
}
}

Tuesday, September 26, 2017

10. Demonstrate arrays of interface types (for runtime polymorphism) with a C# program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IntrDemo {
    public interface Shape
    {
        void area();
    }
        public class circle : Shape
        {
            public void area()
            {
            Console.WriteLine("------Calculating area of CIRCLE--------");
            Console.Write("Enter the radius : ");
            float r = float.Parse(Console.ReadLine());
            Console.WriteLine("Area of Circle" + (3.142) * r * r);
            }
        }
        public class Square : Shape
        {
            public void area()
            {
            Console.WriteLine("------Calculating area of Square--------");
            Console.Write("Enter the length : ");
            float side = float.Parse(Console.ReadLine());
            Console.WriteLine("Area of square" +(side*side));
        }
     }
        class program
        {
            static void Main(string[] args)
            {
            Console.WriteLine("------Calculating area of Square--------");
            Shape[] s = new Shape[2];
            s[0] = new circle();
            s[1] = new Square();
                for (int i = 0; i < s.Length; i++)
                {
                s[i].area();
                Console.ReadLine();
                }
            }
        }
}

9 . WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Properties
{
class point
{
int getx, gety;
public int x
{
get { return getx; } set {
getx = value; }
}
public int y
{
get { return gety; } set {
gety = value; }
}
}
class Program
{
static void Main(string[] args)
{
point start = new point();
point end = new point();
start.x = 10;
start.y = 20;
end.x = 100;
end.y = 200;
Console.Write( "\npoint 1 : "+ start.x + " " + end.x);
Console.Write("\npoint 2 :"  + start.y + " " + end.y);
Console.ReadLine();
}
}
}

8 WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND ABSTRACT METHODS IN C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab8
 {
abstract class person
  {
protected string fname;
protected string lname;
public person(string fn, string ln)
{
fname = fn;
lname = ln;
}
public abstract void display() ;
}

class emp : person
{
public ushort year;
public emp(string fn, string ln, ushort yr): base(fn, ln)
{
year = yr;
}
public override void display()
  {
Console.WriteLine("Employee :" + fname + " " + lname + " " + year);
}
}
class worker : person
 {
    public String company;
             public worker(string fn, string ln, string c) : base(fn, ln)
{
company = c;   
}   
public override void display() { 
Console.WriteLine("Worker :" + fname + " " + lname + " " + company); 
}   
}   
class Program   
{   
         static void Main(string[] args) 
{
Console.WriteLine("**ABSTRACT CLASS  AND ABSTRACT METHODS DEMO **"); 
person p2 = new emp("RAM", "KUMAR", 2012); 
person p3 = new worker("RAM", "KUMAR", "ABC TECH SOLS"); 
p2.display();   
p3.display();   
Console.ReadLine();   
}   
}   
}    

7 Write a program to demonstrate delegates.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _6
{
    public delegate int DelegateSample(int a,int b);
    public class sampleclass
    {
        public int add(int x, int y)
        {
            return x + y;
        }
        public int sub(int x, int y)
        {
            return x - y;
        }
        class program {
            static void Main(String[] args)
            {
                sampleclass s = new sampleclass();
                DelegateSample del = s.add;
                int i = del(10, 20);
                Console.WriteLine("Add result is"+i);
                DelegateSample del1 = s.sub;
                int j = del1(10, 2);
                Console.WriteLine("Sub result is" + j);
                Console.ReadLine();

            }
        }
    }
}