Java Pracs
Java Pracs
import java.util.*;
class Student
{
int rollNo;
String name;
Student(int rollNo, String name)
{
this.rollNo = rollNo;
this.name = name;
}
}
class Marks extends Student
{
int m1, m2;
Marks(int rollNo, String name, int m1, int m2)
{
super(rollNo, name);
this.m1 = m1;
this.m2 = m2;
}
}
class Sports extends Marks
{
int score;
Sports(int rollNo, String name, int m1, int m2, int score)
{
super(rollNo, name, m1, m2);
this.score = score;
}
}
class Total extends Sports
{
int total_score;
Total(int rollNo, String name, int m1, int m2, int score)
{
super(rollNo, name, m1, m2, score);
total_score = 0;
}
void calc()
{
total_score = m1 + m2 + score;
}
void display()
{
System.out.println("Total = "+total_score);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter roll no.");
int rollNo = sc.nextInt();
sc.nextLine();
System.out.println("Enter name ");
String name = sc.nextLine();
System.out.println("Enter m1 and m2 ");
int m1 = sc.nextInt();
int m2 = sc.nextInt();
System.out.println("Enter sports score ");
int score = sc.nextInt();
Total t = new Total(rollNo, name, m1, m2, score);
t.calc();
t.display();
}
}
9) Write a java program to implement the following.
import java.util.*;
interface I1
{
void exam();
void per_cal();
}
class Student
{
String name;
int rollNo;
int m1, m2;
}
class Result extends Student implements I1
{
double per;
public void exam()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
name = sc.nextLine();
System.out.println("Enter roll no: ");
rollNo = sc.nextInt();
System.out.println("Enter m1 and m2: ");
m1 = sc.nextInt();
m2 = sc.nextInt();
}
public void per_cal()
{
per = (m1+m2)/2.0;
}
void display()
{
System.out.println("Name: "+name);
System.out.println("Roll no: "+rollNo);
System.out.println("m1: "+m1);
System.out.println("m2: "+m2);
System.out.println("Percentage: "+per);
}
}
class Test
{
public static void main(String args[])
{
Result r = new Result();
r.exam();
r.per_cal();
r.display();
}
}
10) Write a program to count number of occurrence of specified
character in user entered string.
import java.util.*;
class Test
{
public static void main(String args[])
{
int count = 0;
char c = ' ';
String s = new String();
Scanner sc = new Scanner(System.in);
System.out.println("Enter String:- ");
s = sc.nextLine();
System.out.println("Enter the character:- ");
c = sc.next().charAt(0);
for(int i=0; i<s.length(); i++)
if(s.charAt(i) == c)
count++;
System.out.println("No. of Occurences: "+count);
}
}
11) Write a program to demonstrate this keyword.
import java.util.*;
class Test
{
int a, b;
Test(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
System.out.println("a = "+a);
System.out.println("b = "+b);
}
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
a = sc.nextInt();
b = sc.nextInt();
Test t = new Test(a, b);
t.display();
}}
12) Write a program to read string and
a) Find its length
b) Reverse it
c) Display whether it is palindrome or not
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = new String();
StringBuffer rev;
int length;
System.out.println("Enter String: ");
str = sc.nextLine();
//Converting String to StringBuffer
StringBuffer str1 = new StringBuffer(str);
length = str1.length();
System.out.println("Length: "+length);
rev = str1.reverse();
System.out.println("Reverse: "+rev);
//Convert StringBuffer to String
String reverseString = rev.toString();
if(str.equals(reverseString))
System.out.println("String is Palindrome");
else
System.out.println("Not a Palindrome");
}
}
OR
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = "", rev = "";
int len;
System.out.println("Enter String: ");
str = sc.nextLine();
System.out.println("Length: "+str.length());
for(int i = str.length()-1; i>=0 ; i--)
rev += str.charAt(i);
System.out.println("Reverse: "+rev);
if(str.equals(rev))
System.out.println("It is a Palindrome");
else
System.out.println("Not a Palindrome");
}
}
13) Write a program which will read userid and password check
whether both are correct and display hello and welcome otherwise
display login failure . Also display the password in uppercase and
reverse.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String userID = "ABC";
String pass = "abcdefgh";
String rev = "";
System.out.println("Enter User ID: ");
String enteredUserID = sc.nextLine();
System.out.println("Enter Password: ");
String enteredPass = sc.nextLine();
if(enteredUserID.equals(userID) && enteredPass.equals(pass))
{
System.out.println("Hello and Welcome");
System.out.println("Password in uppercase:
"+pass.toUpperCase());
for(int i = pass.length()-1; i>=0; i--)
rev = rev + pass.charAt(i);
System.out.println("Reversed Password: "+rev);
}
else
System.out.println("Login Failure");
}
}
14) Implement program to perform following task by using string
and stringbuffer class
a) To search a word inside a string
b) To search last position of substring
c) To compare 2 strings
d) To print reverse of the string.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String1: ");
String s1 = sc.nextLine();
System.out.println("Enter String2: ");
String s2 = sc.nextLine();
StringBuffer sbr = new StringBuffer(s2);
import java.util.*;
class Test
{
public static void main(String args[])
{
String defaultPass = "abcd1234";
Scanner sc = new Scanner(System.in);
System.out.println("Enter Password: ");
String pass = sc.nextLine();
StringBuffer sbr = new StringBuffer(pass);
if(pass.equals(defaultPass))
System.out.println("Good");
else
System.out.println("Wrong");
System.out.println("Reverse Password: "+sbr.reverse());
pass += "welcome";
System.out.println("Appended Password: "+pass);
}
}
import java.util.*;
class NoMatchException extends Exception
{
NoMatchException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
System.out.println("Enter String: ");
String s = sc.nextLine();
if(!(s.equals("MSBTE")))
throw new NoMatchException("Strings don't
match.");
}
catch (NoMatchException e)
{
System.out.println(e);
}
}
}
20) Write a program to input name and age of person and throws
user defined exception, if entered age is negative.
import java.util.*;
class NegativeException extends Exception
{
NegativeException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
String name = "";
int age = 0;
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Age: ");
age = sc.nextInt();
if(age < 0)
throw new NegativeException("Age is negative.");
}
catch (NegativeException e)
{
System.out.println(e);
}
}
}
21) Write a program to input name and balance of customer and
thread an user defined exception if balance less than 1500.
import java.util.*;
class LessBalanceException extends Exception
{
LessBalanceException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
String name = "";
int bal = 0;
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Balance: ");
bal = sc.nextInt();
if(bal < 1500)
throw new LessBalanceException("Balance is less
than 1500.");
}
catch (LessBalanceException e)
{
System.out.println(e);
}
}
}
22) Write a program to create two thread one to print odd number
only and other to print even numbers.
class EvenThread extends Thread
{
public void run()
{
for(int i=0; i<11; i+=2)
System.out.println("Even Thread: "+i);
}
}
class OddThread extends Thread
{
public void run()
{
for(int i=1; i<10; i+=2)
System.out.println("Odd Thread: "+i);
}
}
class Test
{
public static void main(String args[])
{
EvenThread e = new EvenThread();
OddThread o = new OddThread();
e.start();
o.start();
}
}
23) Write a program to define two thread one to print from 1 to 100
and other to print from 100 to 1. First thread transfer control to
second thread after delay of 500 ms.
class Thread1 extends Thread
{
public void run()
{
try
{
for(int i=0; i<101; i++)
{
System.out.println("Thread1: "+i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class Thread2 extends Thread
{
public void run()
{
try
{
for(int i=100; i>=0; i--)
{
System.out.println("Thread2: "+i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class Test
{
public static void main(String args[])
{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start(); } }
24) Write a program to set the priority of thread to display table of 2
and 5 (max priority for table of 5 and priority value 6 to print table of
2)
class TableOfFive extends Thread
{
public void run()
{
for(int i=1; i<11; i++)
System.out.println("5 * "+i+" = "+(5*i));
}
}
class TableOfTwo extends Thread
{
public void run()
{
for(int i=1; i<11; i++)
System.out.println("2 * "+i+" = "+(2*i));
}
}
class Test
{
public static void main(String args[])
{
TableOfFive t1 = new TableOfFive();
TableOfTwo t2 = new TableOfTwo();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(6);
t1.start();
t2.start();
}
}
25) Develop a program which consists of package named
let_me_calculate with a class named calculator and a method named
add to add two integer numbers.Import let_me_calculate package in
another program with class named as Demo to add 2 numbers.
package let_me_calculate;
public class Calculator
{
public int add(int a, int b)
{
return (a+b);
}
}
import let_me_calculate.*;
import java.util.*;
class Demo
{
public static void main(String[] args)
{
Calculator c = new Calculator();
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 no.s ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Addition of two numbers: "+c.add(a, b));
}
}
26) Define a package named myinstitute include class name as
department with one method to display the staff of that department.
Develop a program to import this package in a java application and
call the method defined in the package.
package myinstitute;
public class Department
{
public void display()
{
System.out.println("Dep: CO");
System.out.println("Staff: ABC");
System.out.println("Dep: CM");
System.out.println("Staff: PQR");
}
}
import myinstitute.*;
class Demo1
{
public static void main(String[] args)
{
Department d = new Department();
d.display();
}
}
}
}
28) Design an applet to draw and fill 5 point polygon with blue
color.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
int x[] = {150,100,100,200,200};
int y[] = {100,150,200,200,150};
int points = 5;
g.setColor(Color.BLUE);
g.fillPolygon(x,y,points);
}
}
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 500 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.RED);
setForeground(Color.BLUE);
g.drawString("Background color is RED and Foreground
color is BLUE",100,100);
}
}
34) Write an applet to accept user name in the form of parameter
and print ‘Hello<username>’.
import java.applet.*;
import java.awt.*;
//<applet code=Test width=500 height =500 >
//<param name ="username" value = "abcd"></applet>
public class Test extends Applet
{
String s;
public void init()
{
s = getParameter("username");
}
public void paint(Graphics g)
{
g.drawString("Hello"+s,50,50);
}
}
35) Design an applet which display equals size three rectangle one
below the other and fill them with orange, white and green color
respectively.
import java.util.*;
import java.awt.*;
import java.applet.*;
//<applet code=Test width=500 height =500></applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
//setBackground(Color.GRAY);
g.setColor(Color.orange);
g.fillRect(50,10,180,30);
g.setColor(Color.white);
g.fillRect(50,40,180,30);
g.setColor(Color.green);
g.fillRect(50,70,180,30);
}
}
36) WAP to copy contents of one file to another using character
stream.
import java.util.*;
import java.io.*;
class Test
{
public static void main(String args[])
{
File in = new File("copy.txt");
File out = new File("example.txt");
try
{
FileReader r = new FileReader(in);
FileWriter w = new FileWriter(out);
int ch;
while((ch = r.read()) != -1)
w.write(ch);
System.out.println("Copied");
r.close();
w.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
class Test
{
public static void main(String args[])
{
File in = new File("copy.txt");
FileReader r = null;
int ch,count=0;
try
{
r = new FileReader(in);
while( (ch = r.read())!= -1)
count++;
System.out.println("Number of characters "+count);
r.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}