Friday, 28 September 2018

JAVA RECORD PROGRAMS


// 1. JAVA PROGRAM FOR COMMANDLINE ARGUMENTS
class CommandLine{ 
public static void main(String args[]){ 
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println("Sum of a and b is: "+c);
  } 
} 

// 2.  java program used to caliculate the Compound Interest
import java.lang.*;
class compound
{
                public static void main(String []args)
                {
                                int roa=Integer.parseInt(args[0]);
                                int noy=Integer.parseInt(args[1]);
                                int amt=Integer.parseInt(args[2]);
                                float pamt=(float)(amt*Math.pow((1+(float)roa/100),noy));
                                System.out.println("Compound Interest is.."+pamt);
                }
}
// 3.  Java program to demonstrate primitive data types in Java
class Datatypes
{
    public static void main(String args[])
    {
        // declaring character
         char a = 'G';
          // Integer data type is generally
        // used for numeric values
        int i=89;
         // use byte and short if memory is a constraint
        byte b = 4;
        short s = 56;
         // by default fraction value is double in java
        double d = 4.355453532;
         // for float use 'f' as suffix
        float f = 4.7333434f;
        System.out.println("char: " + a);
        System.out.println("integer: " + i);
        System.out.println("byte: " + b);
        System.out.println("short: " + s);
        System.out.println("float: " + f);
        System.out.println("double: " + d);
    }   
}
// 4.  Float data type example in java
class Exp
 {
    public static void main(String args[])
     {
         float c=17.2f,d=2;
         float z;
          z = c / d;
         System.out.println("C="+z);
      }
}
// 5. SIMPLE IF EXAMPLE
class testif{
public static void main(String args[]){
float marks=29;
if(marks>=35){
System.out.println("PASS");
}
}
}
// 6.  BANK TRANSACTION EXAMPLE
interface bank
{
                int accno=101;
                String accname="GVP";
                int amt=10000;
}
interface acctype extends bank
{
                void calc_interest(int rof);
                void put_details();
}
abstract class members implements acctype
{
                float interest=0.0f;
                float bal=0.0f;
                public void put_details()
                {
                                System.out.println("Account Number="+accno);
                                System.out.println("Account Name="+accname);
                                System.out.println("Amount ="+amt);
                                System.out.println("Interest="+interest);
                                System.out.println("Balance="+bal);
                }
}
class current_accnt  extends members implements acctype
{             
                public void calc_interest(int rof)
                {
                                interest=(rof*amt)/100;
                                bal=amt+interest;
                }
}
class savings_accnt extends members implements acctype
{
                public void calc_interest(int rof)
                {
                                interest=(rof*amt)/100;
                                bal=amt+interest;
                }
}
class interest
{
                public static void main(String []args)
                {
                                current_accnt ca=new current_accnt();
                                savings_accnt sa=new savings_accnt();
                                if(args[0].equals("current"))
                                {
                                                ca.calc_interest(10);
                                                ca.put_details();
                                }
                                else if(args[0].equals("savings"))
                           {
                                                sa.calc_interest(5);
                                                sa.put_details();
                                }             
                                else
                                                System.exit(0);
                }
}
//  7. LOOPS IN JAVA
class loop{
public static void main(String args[]){
  System.out.println("URSING FOR LOOP:");
  for(int i=1;i<11;i++){
  System.out.println(i);
  }
  System.out.println("URSING WHILE LOOP:");
  int j=0;
  while(j<10){
  j=j+1;
  System.out.println(j);
  }
  System.out.println("URSING do WHILE LOOP:");
  int k=0;
  do{
  k=k+1;
  System.out.println(k);
  }while(k<10);
 }
}
// 8. IF ELSE IN JAVA
class nestedif{
public static void main(String args[]){
int a=30,b=45,c=65;

if(a>b && a>c)
{
System.out.println("A IS BIG");
}
 else if(b>a && b>c)
{
System.out.println("B IS BIG");
}
else
{
System.out.println("C IS BIG");
}
}
}
// 9. NUMBER NAME IN JAVA
class NumberName
{
static void PrintName(char c)
{
switch(c)
{
case '0' :
System.out.print(" Zero ");
break;
case '1' :
System.out.print(" One ");
break;
case '2' :
System.out.print(" Two ");
break;
case '3' :
System.out.print(" Three ");
break;
case '4' :
System.out.print(" Four ");
break;
case '5' :
System.out.print(" Five ");
break;
case '6' :
System.out.print(" Six ");
break;
case '7' :
System.out.print(" Seven ");
break;
case '8' :
System.out.print(" Eight ");
break;
case '9' :
System.out.print(" nine ");
break;
default :
System.out.print(" Not a Valid Number ");
}
}
     public static void main(String args[])
       {
               String str;
               str = args[0];
               int l = str.length();
              for(int i=0; i < l ; i++)
                  {
                       PrintName(str.charAt(i));
                  }
        }
} 
// 10. Multithreading using java
class odd extends Thread
{
                int n=20;
                public void  run()
                {
                                for(int i=1;i<=n;i++)
                                {
                                if (i%2!=0)
                                System.out.println("Odd nos:"+i);
                                try
                                {
                                                Thread.sleep(1000);
                                }
                                catch(InterruptedException e) {}
                                }
                }
}
class even extends Thread
{
                int n=20;
                public void run()
                {
                for(int i=1;i<=n;i++)
                {
                                if(i%2==0)
                                System.out.println("Even nos:"+i);
                                try
                                {
                                                Thread.sleep(1000);
                                }
                                catch(InterruptedException e) {}
                }
                }
               
}
class oddeven
{
public static void main(String args[])
{
                odd t1=new odd();
                even t2=new even();
                t1.start();
                t2.start();
}
}
// 11. Java Program to print Prime Numbers up to N
class prime
{
                public static void main(String args[])
                {
                                int num=Integer.parseInt(args[0]);
                               
                                for(int n=2;n<=num;n++)
                                {
                                                int count=0;
                                                for(int i=1;i<=n;i++)
                                                {
                                                                if((n%i)==0)
                                                                                count++;
                                                }
                                                if(count==2)
                                                                System.out.println(n);
                                }
                }
}
// 12. java program to calculate sum of series 1 + ½ + 1/3 + ¼ + ……..+1/n 

class series
{
                public static void main(String args[])
                {
                                int n=Integer.parseInt(args[0]);
                                float sum = 1.0f;
                                                for(int i=1; i<n; i++)
                                                {
                                                   sum += (float) 1 / (i+1);
                                                }
                                System.out.println("Sum of the series  is.."+ sum);
                }
}
// 13. Single Inheritance in Java
class Dept
{
                int dno;
                String dname;
                String loc;
                void setDetails(int x,String y,String z)
                {
                                dno=x;
                                dname=y;
                                loc=z;
                }
}
class emp extends Dept
{
                int eno;
                String ename;
                String job;
                void setemp(int x,String y,String z)
                {
                                eno=x;
                                ename=y;
                                job=z;
                }
                void showdetails()
                {
                                System.out.println("Dept Details="+dno+"\t\t"+dname+"\t\t"+loc);
                                System.out.println("Emp Details="+eno+"\t\t"+ename+"\t\t"+job);
                }
}
class single
{
                public static void main(String []args)
                {
                                emp e=new emp();
                                e.setDetails(10,"sales","vizag");
                                e.setemp(101,"Smith","manager");
                                e.showdetails();
                }
}
// 14. String manipulations in java
class StringManip
{
     public static void main(String args[])
     {
           String s1 = "I am a student of B.Sc Final year.";
           String s2 = " ";
           s2 = s1.replace('a','z');
           System.out.println(s1);
          System.out.println(s2);
          int n1,n2,n3;
          n1 = s1.indexOf('a');
          n2 = s1.indexOf('a',5);
          n3 = s1.indexOf("stud");
          System.out.println(n1);
          System.out.println(n2);
          System.out.println(n3);
          String s3 = "";
          String s4 = "";
          s3 = s1.substring(4);
          s4 = s1.substring(4,12);
           System.out.println(s3);
          System.out.println(s4);
       }
}
// 15. Ternary operator in java(?:)
class testif{
public static void main(String args[]){
int c=34;
String d=(c>34)?"PASS":"FAIL";
System.out.println(d);
}
}
// 16. Switch case in java
class testswitch{
public static void main(String args[]){

int day=7;
switch(day)
{
case 1: System.out.println("Sunday");
break;
case 2: System.out.println("Monday");
break;
case 3: System.out.println("Tuesday");
break;
case 4: System.out.println("wednesday");
break;
case 5: System.out.println("Thursday");
break;
case 6: System.out.println("Friday");
break;
case 7:System.out.println("Saturday");
break;
default: System.out.println("Sorry! Invalid choice!");
break;
}
}
}
// 17. Java Applet program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class textinput extends Applet implements ActionListener
{
                Font f=new Font("TimesRoman",Font.BOLD,30);
                TextField t1,t2;
                Button b1;
                Label l1;
                public void init()
                {
                                setBackground(Color.pink);
                                setForeground(Color.black);
                                t1=new TextField(8);
                                t2=new TextField(8);
                                b1=new Button("Ok");
                                setFont(f);
                                l1=new Label("Enter a Number");
                                add(l1);
                                add(t1);
                                add(t2);
                                add(b1);
                                b1.addActionListener(this);
                }
                public void actionPerformed(ActionEvent e)
                {
                                if(e.getSource()==b1)
                                {
                                                int n, r,rev=0;
                                                String s1;
                                                s1=t1.getText();
                                                n=Integer.parseInt(s1);
                                                while(n!=0)
                                                {
                                                                r=n%10;
                                                                rev=rev*10+r;
                                                                n=n/10;
                                                }
                                                String x=String.valueOf(rev);
                                                t2.setText(x);
                                }
                }
}
// 18. Vector Demo
import java.io.*;
import java.util.*;
class VectorDemo1
{
public static void main(String args[]) throws IOException
{

Vector v = new Vector( );
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("1.Creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.Display");
System.out.println("5.exit");
System.out.println("Enter Ur Choice:");
int n=Integer.parseInt(br.readLine());
switch(n)
{
                case 1:
                                System.out.println("Enter the Element:");
                                v.addElement(new Integer(br.readLine()));
                                break;
                case 2:  System.out.println("Enter the position to be inserted:");
                                int x=Integer.parseInt(br.readLine());
                                v.insertElementAt(new Integer(br.readLine()),x);
                                break;
                case 3:  System.out.println("Enter the position to be deleted:");
                                int y=Integer.parseInt(br.readLine());
                                v.removeElementAt(y);
                                break;
                case 4: System.out.println("Elements are:");
                                for(int i=0;i<v.size();i++)
                                System.out.println(v.get(i)+" ");                              
                                break;
                case 5:  System.exit(0);
   }
  }
 }
}
// 19. PACKAGE PROGRAM IN JAVA
// 1. CREATE A FOLDER arithmetic and save the following file calculations.java
package arithmetic;
public class calculations
{
                public int add(int a,int b)
                {
                                return a+b;
                }
                public int sub(int a,int b)
                {
                                return a-b;
                }
                public int mul(int a,int b)
                {
                                return a*b;
                }
                public int div(int a,int b)
                {
                                return a/b;
                }
}
/*2. Create another file outside arithmetic folder with the name performoperations.java and run the file */
//importing package
import arithmetic.calculations;
class performoperations
{
                public static void main(String []args)
                {
                                calculations c=new calculations();
                                int a=Integer.parseInt(args[0]);
                                int b=Integer.parseInt(args[1]);
                                int z=c.mul(a,b);
                                int v=c.sub(a,b);
                                System.out.println("Addition is:"+c.add(a,b));
                                System.out.println("Division is:"+c.div(a,b));
                                System.out.println("Multiplication is:"+z);
                                System.out.println("Subtraction is:"+v);               
                }
}

No comments: