Java Practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

JAVA PRACTICALS EXERCISE

PART-A

EXNO.1

PROGRAM

import java.util.Scanner;

public class Temper

public static void main(String args[])

int fahrenheit, celsius;

Scanner scan= new Scanner(System.in);

System.out.println("Enter Temperature in celsius");

celsius= scan.nextInt();

fahrenheit= ((celsius *9)/5)+32;

System.out.println("Temperature in Fahrenheit" \n+fahrenheit);

OUTPUT

EXNO.2
PROGRAM

import java.util.Scanner;

public class large

public static void main(String args[])

int num1,num2,largest;

Scanner scan= new Scanner(System.in);

System.out.println("Enter the first number");

num1= scan.nextInt();

System.out.println("Enter the second number");

num2= scan.nextInt();

if(num1 > num2)

largest= num1;

else

largest= num2;

System.out.print("\nlargest" + largest);

OUTPUT:

EXNO.3.
PROGRAM

import java.util.Scanner;

public class factorial

public static void main(String args[])

int i, fact=1,n;

Scanner scan= new Scanner(System.in);

System.out.println("Enter the number");

n= scan.nextInt();

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

fact=fact*i;

System.out.println("Factorial of n" +fact);

OUTPUT

EXNO.4

PROGRAM

import java.util.*;

class vect

{
public static void main(String args[])

Scanner sc= new Scanner(System.in);

int i,delete,n;

Vector<Integer>v=new Vector<Integer>();

System.out.println("How many elements do you want to insert");

n= sc.nextInt();

System.out.println("start inserting the element");

for(i=0;i<n;i++)

v.add(sc.nextInt());

System.out.println("enter the element do you want to delete");

delete=sc.nextInt();

boolean x=v.removeElement(delete);

if(x== true)

System.out.println("element after deleting("+delete+")are");

for(i=0;i<v.size();i++)

System.out.println(v.elementAt(i)+"");

else

System.out.println(" no such element in vector");

}
}

OUTPUT

EXNO.5

PROGRAM

import java.util.Scanner;

public class palindrome

public static void main(String args[])

String str,rev="";

Scanner sc= new Scanner(System.in);

System.out.println("Enter a String");

str=sc.nextLine();

int length=str.length();

for(int i=length-1;i>=0;i--)

rev=rev+str.charAt(i);

if(str.equals(rev))
System.out.println(str+" is palindrome");

else

System.out.println(str+" is not palindrome");

OUTPUT:

EXNO.6

PROGRAM

import java.util.Scanner;

import java.io.*;

class student

Scanner scan = new Scanner(System.in);

int regno,total=0,subjects;

String name;

int marks[];
student()

System.out.println("Enter regno");

regno=scan.nextInt();

System.out.println("Enter name");

name=scan.next();

getdismarks();

public void getdismarks()

marks= new int[3];

System.out.println("Enter the marks of Java");

marks[0]= scan.nextInt();

System.out.println("Enter the marks of OS");

marks[1]= scan.nextInt();

System.out.println("Enter the marks of DS");

marks[2]= scan.nextInt();

for(int i=0;i<3;i++)

total= marks[i];

System.out.println("Total marks " + total);

class classcreation
{

public static void main(String args[])

student s[]= new student[3];

for(int i=0;i<3;i++)

s[i]= new student();

OUTPUT
EXNO.7

PROGRAM

import java.util.Scanner;

import java.io.*;

class area

public static void main (String args[])

{
int r;

double pi=3.14,area;

Scanner s= new Scanner(System.in);

System.out.println("enter radius of circle");

r=s.nextInt();

area= pi*r*r;

System.out.println("Area of circle" + area);

OUTPUT

EXNO.8

PROGRAM

class car

public car()

System.out.println("class car");

}
public void vehicleType()

System.out.println("Vehicle Type:Car");

class maruti extends car

maruti()

System.out.println("class Maruti");

public void brand()

System.out.println("Brand: Maruti");

public void speed()

System.out.println("Max:90kmph");

class multilevel extends maruti

public multilevel()
{

System.out.println("Maruti Model: 800");

public void speed()

System.out.println("Max :80kmph");

public static void main(String args[])

multilevel m1= new multilevel();

m1.vehicleType();

m1.brand();

m1.speed();

OUTPUT

EXNO.9

PROGRAM
import java.io.*;

import java.util.*;

class myexception extends Exception

myexception(String s)

super(s);

class pgm9

public static void main(String args[])

try

System.out.println("enter no.");

Scanner sc = new Scanner(System.in);

int num= sc.nextInt();

if(num<1 || num>100)

throw new myexception("out of the range");

else

System.out.println("within range" + num);

catch(myexception e)
{

System.out.println("exception caught"+ e.getMessage());

OUTPUT:

EXNO.10

PROGRAM

import java.util.*;

import java.io.*;
class mythread1 extends Thread

public void run()

int i;

try

for(i=0;i<5;i++)

System.out.println("hello");

sleep(2000);

catch(Exception e)

System.out.println(e);

class mythread2 extends Thread

public void run()

int i;

try

{
for(i=0;i<5;i++)

System.out.println("Gud morning");

sleep(2000);

catch(Exception e)

System.out.println(e);

class mythread3 extends Thread

public void run()

int i;

try

for(i=0;i<5;i++)

System.out.println("welcome");

sleep(2000);

catch(Exception e)
{

System.out.println(e);

class threadprogram

public static void main(String args[])

mythread1 obj1= new mythread1();

mythread2 obj2= new mythread2();

mythread3 obj3= new mythread3();

obj1.start();

obj2.start();

obj3.start();

OUTPUT
EXNO.11

PROGRAM

import java.io.FileOutputStream;

import java.io.IOException;

class filecreation
{

public static void main(String args[])

try

FileOutputStream fout= new FileOutputStream("saran.txt");

String s="Hi this is saran";

byte b[]=s.getBytes();

fout.write(b);

fout.close();

catch(IOException e)

System.out.println(e);

OUTPUT
EXNO.12

PROGRAM

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class mouse extends JFrame implements MouseMotionListener

public JLabel label;

public static void main(String args[])

mouse mm= new mouse();


mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public mouse()

setSize(500,400);

setTitle("java swing mouse event");

label= new JLabel("no Mouse event", JLabel.CENTER);

add(label);

addMouseMotionListener(this);

setVisible(true);

public void mouseMoved(MouseEvent e)

label.setText("Mouse cusor cooridanates X:"+e.getX()+"y:"+e.getY());

public void mouseDragged(MouseEvent e)

OUTPUT
EXNO.13

PROGRAM

import java.applet.Applet;

import java.awt.*;

public class Appletdemo extends Applet

public void paint(Graphics g)

g.drawString("Line",100,100);

g.setColor(Color.red);

g.drawLine(30,100,10,10);

g.setColor(Color.magenta);

g.drawRect(30,50,20,30);

g.setColor(Color.yellow);

g.fillRect(30,50,20,30);

g.drawString("Rectangle",300,400);

/*<html>

<applet code="Appletdemo" width="600" height="600">

</applet>

</html>*/

OUTPUT
EXNO.14

PROGRAM

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class SimpleCalculator extends Applet implements ActionListener {

TextField t1, t2, t3;

Button b1, b2, b3, b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14;

public void init() {

t1 = new TextField(10);
t2 = new TextField(10);

t3 = new TextField(10);

b1 = new Button("+\n");

b2 = new Button("-");

b3 = new Button("*");

b4 = new Button("/");

b5 = new Button("1");

b6 = new Button("2");

b7 = new Button("3");

b8 = new Button("4");

b9 = new Button("5");

b10 = new Button("6");

b11 = new Button("7");

b12= new Button("8");

b13 = new Button("9");

b14 = new Button("C");

add(t1);

add(t2);

add(t3);

add(b1);

add(b2);

add(b3);

add(b4);

add(b5);

add(b6);
add(b7);

add(b8);

add(b9);

add(b10);

add(b11);

add(b12);

add(b13);

add(b14);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b10.addActionListener(this);

b11.addActionListener(this);

b12.addActionListener(this);

b13.addActionListener(this);

b14.addActionListener(this);

}
public void actionPerformed(ActionEvent ae) {

String s1 = t1.getText();

String s2 = t2.getText();

double a = Double.parseDouble(s1);

double b = Double.parseDouble(s2);

if (ae.getSource() == b1) {

t3.setText(String.valueOf(a + b));

} else if (ae.getSource() == b2) {

t3.setText(String.valueOf(a - b));

} else if (ae.getSource() == b3) {

t3.setText(String.valueOf(a * b));

} else if (ae.getSource() == b4) {

t3.setText(String.valueOf(a / b));

/*

<applet code="SimpleCalculator" width=400 height= 400>

</applet>

*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy