Search This Blog

Thursday 24 January 2019

3. Write a java program to find area of geometric figures (atleast 3) using method overloading.


import java.util.*;
import java.io.*;
import java.lang.*;
class geoFig
{
              double area(double r)
              {
              return(3.14*r*r); //Cicle
              }

              float area(float s)
              {
              return(s*s); //Square
              }
            
double area(double b,double h)
    {
           return(0.5*b*h); //Triangle
    }
}
class geoMain
{
        public static  void main(String arg[]) throws IOException
        {
            DataInputStream ob=new DataInputStream(System.in);
             geoFig g = new geoFig();
              System.out.println("enter double value for radius of circle");
             double r=Double.valueOf(ob.readLine()).doubleValue();
             System.out.println("area of circle="+g.area(r));
              System.out.println("enter float value for side of a square");
             float s=Float.valueOf(ob.readLine()).floatValue();
             System.out.println("area of square="+g.area(s));
     System.out.println("enter double value for base & height of triangle");
             double b1=Double.valueOf(ob.readLine()).doubleValue();
             double h=Double.valueOf(ob.readLine()).doubleValue();
             System.out.println("area of triangle="+g.area(b1,h));
    }
}

2. Write a Java program to create a vector, add elements at the end, at specified location onto the vector and display the elements. Write an option driven program using switch…case.


import java.util.*;
import java.io.*;
import java.lang.String;

class VectorPro
{
    public static void main(String args[])
    {
        try
        {
       
            int length = args.length;
            int i,ch;
            String str1,str2;

            Vector list = new Vector();

            for( i=0;i<length;i++)
            {
                list.addElement(args[i]);
            }

            DataInputStream in= new DataInputStream(System.in);
           
            for(;;)
            {
                System.out.println("1.Add element at end");
                System.out.println("2.Add element at specific location");
                System.out.println("3.Display");
                System.out.println("4.Exit");       
                System.out.println("Enter your choice:");
   
                str1=in.readLine();
                ch=Integer.parseInt(str1);

                switch(ch)
                {
                   
             case 1: System.out.print("Enter an element to insert at end:");
                            str2=in.readLine();
                            list.addElement(str2);
                            length++;   
                            break;
                       
             case 2: System.out.print("Enter an element & position:");
                            str1=in.readLine();
                            str2=in.readLine();
                            int pos;
                            pos=Integer.parseInt(str2);
                            if(pos<=length && pos>0)
                            {
                                list.insertElementAt(str1,pos-1);
                                length++;
                                break;
                            }
                            else
                            {
                                System.out.println("Invalid Position!");
                            }
                   
            case 3:
                            System.out.println("List of elements:");
                            for(i=0;i<length;i++)
                            System.out.println(list.elementAt(i));
                            break;
                   
            default:       System.exit(0);
   
                }
                              
            }
        }
        catch(Exception e){}
    }
}

1. Java program to generate first odd numbers then pick and display prime numbers among them.



import java.io.*;

public class OddNumber

{

     public static void main(String args[]) throws IOException

     {

           int limit;

           int b=0;

           limit = Integer.parseInt(args[0]);

           int a[]= new int[50];

           System.out.println("Odd numbers between 1 and " + limit);

           for(int i=1; i <= limit; i++)

           {

                boolean isPrime = true;

                           for(int j=2; j < i ; j++)

                           {                       

                                 if(i % j == 0)

                                 {

                                      isPrime = false;

                                      break;

                                 }

                           }

                //if the number is not divisible by 2 then it is odd

                           if( i % 2 != 0)

                           {

                           System.out.print(i + " ");

                           }

                           if(isPrime)

                           {

                                 if(i>2)   //1 is not prime and 2 is even.

                                 {

                                      a[b] =i;

                                      b++;

                                 }

                           }

           }

           System.out.println("\nPrime numbers are as follows:");

                for(int k=0;k<=b-1;k++)

                           System.out.println(a[k]);

     }

}
FREE Hit Counters