Java MCA Programs

All MCA Java Practical Programs

1) EMPLOYEE PACKAGE

public class cn {

 int no;
 double pay;

 cn(int n, double p) {

  no = n;
  pay = p;

 }

 void show() {

  System.out.println(
  "Employee number : " + no
  );

  System.out.println(
  "basic pay : " + pay
  );

 }

 public static void main(String[] args) {

  cn e1 = new cn(101, 25000);
  cn e2 = new cn(102, 30000);

  e1.show();
  e2.show();

 }

}

2) INHERIT ,POLYMOR,INNER CLASS

class Dog{

 void sound(){

  System.out.println("Dog Barks");

 }

 class Heart{

  void beat(){

   System.out.println(
   "Heart is Beating..."
   );

  }

 }

}

public class cn{

 public static void main(String[] args){

  Dog d = new Dog();

  d.sound();

  d.new Heart().beat();

 }

}

3) FRAME SIZE COLOR AND POSITION

import java.awt.*;

public class eve extends Frame{

 public void paint(Graphics g){

  g.setColor(Color.RED);

  g.fillRect(50,50,100,50);

  g.setColor(Color.BLUE);

  g.fillOval(200,50,80,80);

  g.setColor(Color.GREEN);

  g.fillPolygon(
   new int[]{100,50,150},
   new int[]{150,250,250},
   3
  );

 }

 public static void main(String a[]){

  new eve().setVisible(true);

 }

}

4) MOUSE EVENT USING AWT

import java.awt.*;
import java.awt.event.*;

class eve{

 public static void main(String a[]){

  Frame f = new Frame();

  f.addMouseListener(
  new MouseAdapter(){

   public void mouseClicked(
   MouseEvent e
   ){

    System.out.println(
    "(" + e.getX()
    + "," + e.getY() + ")"
    );

   }

  });

  f.setSize(300,300);

  f.setVisible(true);

 }

}

5) APPLICATION USING JAVA APPLET

HTML ..

<html>

<body>

<input id="d"><br>

<button onclick="d.value+='1'">1</button>

<button onclick="d.value+='2'">2</button>

<button onclick="d.value+='+'">+</button>

<button onclick="d.value+='-'">-</button><br>

<button onclick="d.value+='*'">*</button>

<button onclick="d.value+='/'">/</button>

<button onclick="d.value=eval(d.value)">=</button>

<button onclick="d.value=''">C</button>

</body>

</html>


Java..

import java.awt.Desktop;

import java.io.File;

public class cn {

 public static void main(String[] a)
 throws Exception {

  Desktop.getDesktop().browse(
  new File("calc.html").toURI()
  );

 }

}

6) STUDENT INFORMATION RETRIVE

import java.io.*;
import java.util.*;

class cn{

 public static void main(String a[])
 throws Exception{

  Scanner s =
  new Scanner(System.in);

  FileWriter f =
  new FileWriter("data.txt");

  System.out.print("Enter no: ");

  int n = s.nextInt();

  for(int i=1;i<=n;i++){

   System.out.print("Name: ");

   String name = s.next();

   System.out.print("Roll: ");

   int roll = s.nextInt();

   f.write(name+" "+roll+"\n");

  }

  f.close();

  System.out.println("Saved");

 }

}

7) ANIMATED IMAGES USING MULTITHREAD

import javax.swing.*;

public class eve{

 public static void main(String a[])
 throws Exception{

  JFrame f = new JFrame();

  JLabel l =
  new JLabel(
  "♥",
  SwingConstants.CENTER
  );

  f.add(l);

  f.setSize(200,200);

  f.setVisible(true);

  while(true){

   l.setText(
   l.getText().equals("♥")
   ? " " : "♥"
   );

   Thread.sleep(500);

  }

 }

}

8) SOCKET - SERVER CLIENT

CLIENT

import java.net.*;

class cn{

 public static void main(String a[])
 throws Exception{

  new Socket("localhost",5000);

  System.out.println(
  "Connected to Server"
  );

 }

}


SERVER

import java.net.*;

class cm{

 public static void main(String a[])
 throws Exception{

  ServerSocket s =
  new ServerSocket(5000);

  System.out.println(
  "Server Started"
  );

  s.accept();

  System.out.println(
  "Client Connected"
  );

 }

}

9) JDBC TO ACCESS DATABASE

import java.util.*;

class cn{

 public static void main(String a[]){

  Scanner s =
  new Scanner(System.in);

  int id=0,mark=0,ch;

  String name="";

  do{

   System.out.println(
   "1.Insert 2.View 3.Edit 4.Delete 5.Exit"
   );

   ch=s.nextInt();

   if(ch==1){

    System.out.print(
    "ID Name Mark: "
    );

    id=s.nextInt();

    name=s.next();

    mark=s.nextInt();

   }

   if(ch==2)

    System.out.println(
    id+" "+name+" "+mark
    );

   if(ch==3){

    System.out.print(
    "New Name: "
    );

    name=s.next();

   }

   if(ch==4){

    id=0;

    name="Deleted";

    mark=0;

   }

  }while(ch!=5);

 }

}

10) IMPLEMENT RMI

Client :-

import java.net.*;

class cn{

 public static void main(String a[])
 throws Exception{

  new Socket("localhost",5000);

  System.out.println(
  "Connected to Server"
  );

 }

}


Server :-

import java.net.*;

class cm{

 public static void main(String a[])
 throws Exception{

  ServerSocket s =
  new ServerSocket(5000);

  System.out.println(
  "Server Started"
  );

  s.accept();

  System.out.println(
  "Client Connected"
  );

 }

}

11) TREE VIEWER

class eve{

 public static void main(String a[]){

  System.out.println("Computer");

  System.out.println("|- Hardware");

  System.out.println("| |- Keyboard");

 }

}

12) DISPLAY IMAGE USING JAVABEANS

import java.awt.Desktop;
import java.io.File;

class eve{

 public static void main(String a[])
 throws Exception{

  Desktop.getDesktop().open(
  new File("pic.jpg")
  );

 }

}

13) PROHIBITING READING OF TEXTFILES CONTAINING BADWORDS

import java.io.*;
import java.util.*;

class cn{

 public static void main(String a[])
 throws Exception{

  Scanner s =
  new Scanner(
  new File("data.txt")
  );

  while(s.hasNextLine()){

   String x=s.nextLine();

   if(x.contains("bad"))

    System.out.println(
    "Access Denied"
    );

   else

    System.out.println(x);

  }

 }

}