javapractical
javapractical
B) Define an abstract class Shape with abstract methods area () and volume (). Derive
abstract class Shape into two classes Cone and Cylinder. Write a java Program
to calculate area and volume of Cone and Cylinder.(Use Super Keyword.)
[25 M]
import java.util.*;
abstract class Shape
{
float a,r,h,v;
final float PI=3.14f;
Scanner src=new Scanner(System.in);
abstract void area();
abstract void volume();
}
class Cone extends Shape
{
float a,r,h,v;
void accept()
{
System.out.println("Enter radius and height of cone");
r=src.nextFloat();
h=r=src.nextFloat();
}
public void area()
{
a=super.PI*r*(r+(float)Math.sqrt(h*h+r*r));
System.out.print("Area of Cone:"+a);
}
public void volume()
{
v=super.PI*r*r*(h/3);
System.out.print("Volume of Cone:"+v);
}
}
class Cylinder extends Shape
{
float a,r,h,v;
void accept()
{
System.out.println("Enter radius and height of Cylinder");
r=src.nextFloat();
h=src.nextFloat();
}
public void area()
{
a=(2*super.PI*r*h)+(2*super.PI*r*r);
System.out.println("Area of Cylinder:"+a);
}
public void volume()
{
v=super.PI*r*r*h;
System.out.print("Volume of Cylinder:"+v);
}
class Slip3B
{
public static void main(String args[])
{
Shape s;
Cylinder c1=new Cylinder();
s=c1;
c1.area();
c1.volume();
}
}
import java.util.Scanner;
8public class Alternatecharfromstr {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print
("Enter a string :");
String str = sc.nextLine();
sc.close();
for (int i=0;i<str.length();i+=2){ //run only 2 mulriple index
System.out.print(str.charAt(i)+" ");
}
}
}
SLIP-5
class pattern
{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j<=5;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
SLIP-6
A) Write a java program to accept a number from user, if it zero then throw user
defined Exception “Number Is Zero”, otherwise calculate the sum of first and last digit
of that number. (Use static keyword)
import java.util.*;
class ZeroException extends Exception
{
ZeroException()
{
super("Number is 0");
}
}
class addfirstlastdigit
{
static int n;
public static void main(String args[])
{
int i, rem,sum=0;
try
{
Scanner r = new Scanner(System.in);
n = r.nextInt();
if(n==0)
{
throw new ZeroException();
}
else
{
rem=n%10;
sum=sum+rem;
if(n>9)
{
while(n>0)
{
rem=n%10;
n = n/10;
}
sum=sum+rem;
}
System.out.println("Sum is :" +sum);
}
}
catch(ZeroException e)
{
System.out.println(e);
}
}
}
B) Write a java program to display transpose of a given matrix. [25 M]
import java.util.Scanner;
class transposematrix
{
public static void main(String args[])
{
int arr[][]=new int[2][2];
Scanner r=new Scanner(System.in);
System.out.print("Enter Array Elements:");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
arr[i][j]=r.nextInt();
}
}
System.out.print("Array Matrix:");
System.out.print("\n");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");
}
System.out.print("Array Transpose Matrix:");
System.out.print("\n");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
System.out.print(arr[j][i]+" ");
}
System.out.print("\n");
}
}
}
SLIP-7
B) Write a java program to accept details of ‘n’ cricket player (pid, pname, totalRuns,
InningsPlayed, NotOuttimes). Calculate the average of all the players. Display the
details of player having maximum average. (Use Array of Object) [25 M]
import java.util.*;
class Cricketplayer
{
int pid,run,notout;
String name;
int iplayed;
Cricketplayer(){}
System.out.println("Enter pname:");
name=src.next();
System.out.println("Enter Runs:");
runs=src.nextInt();
s[i]=new Cricketplayer(pcode,name,runs,iplayed,notout);
}
for(i=0;i<noplayer;i++)
{
avgall=s[i].average();
System.out.println();
System.out.println("Average of"+s[i].name+"is: "+avgall);
}
allAverages.add(avgall);
System.out.println("Maximum average is:"+ Collections.max(allAverages));
}
}
SLIP-8
A) Define an Interface Shape with abstract method area(). Write a java program to
calculate an area of Circle and Sphere.(use final keyword) [15 M]
import java.util.*;
interface Shape
{
void area();
}
void accept()
{
System.out.println("Enter radius of circle");
r=src.nextFloat();
}
public void area()
{
area_of_circle=PI*r*r;
}
public void show()
{
System.out.println("Area of Circle:"+area_of_circle);
}
}
class Sphere implements Shape
{
final float PI=3.14f;
float area_of_sphere,r;
Scanner src=new Scanner(System.in);
void accept()
{
System.out.println("Enter radius of sphere");
r=src.nextFloat();
}
public void area()
{
area_of_sphere=4*PI*r*r;
}
class Test
{
public static void main(String args[])
{
Circle c1=new Circle();
Sphere s1=new Sphere();
c1.accept();
s1.accept();
c1.area();
s1.area();
c1.show();
s1.show();
}
}
SLIP-9
import java.util.Scanner;
SLIP-10
A) Write a java program to count the frequency of each character in a given string.
[15 M]
SLIP-11
B) Write an applet application to display Table lamp. The color of lamp should get
change randomly. [25 M]
import java.awt.*;
import java.applet.*;
public class Slip11B extends Applet{
public float R,G,B;
Graphics gl;
public void init(){
repaint();
}
public void paint(Graphics g)
{
R = (float)Math.random();
G = (float)Math.random();
B = (float)Math.random();
Color cl = new Color(R,G,B);
g.drawRect(0,250,290,290);
g.drawLine(125,250,125,160);
g.drawLine(175,250,175,160);
g.drawArc(85,157,130,50,-65,312);
g.drawArc(85,87,130,50,62,58);
g.drawLine(85,177,119,89);
g.drawLine(215,177,181,89);
g.setColor(cl);
g.fillArc(78,120,40,40,63,-174);
g.fillOval(120,96,40,40);
g.fillArc(173,100,40,40,110,180);
}
}
SLIP11B.html
<applet code="Slip11B.class" width="300" height="300">
</applet>
SLIP-12
A) Write a java program to display each String in reverse order from a String array.
[15 M]
SLIP-13
A) Write a java program to accept ‘n’ integers from the user & store them in an
ArrayList collection. Display the elements of ArrayList collection in reverse order.
[15 M]
import java.util.*;
class array
{
public static void main(String a[])
{
Scanner src=new Scanner(System.in);
System.out.println("Enter Limit of ArrayList :");
int n=src.nextInt();
ArrayList a1=new ArrayList();
System.out.println("Enter Elements of ArrayList :");
for(int i=0;i<n;i++)
{
String element=src.next();
a1.add(element);
}
System.out.println("Original ArrayList is :"+a1);
Collections.reverse(a1);
System.out.println("Reverse of a ArrayList is :"+a1);
}
}
SLIP-14
A) Write a java program to search given name into the array, if it is found then display
its index otherwise display appropriate message. [15 M]
import java.util.Scanner;
public class Search_Element
{
public static void main(String[] args)
{
int n, x, flag = 0, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the element you want to find:");
x = s.nextInt();
for(i = 0; i < n; i++)
{
if(a[i] == x)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
System.out.println("Element found at position:"+(i + 1));
}
else
{
System.out.println("Element not found");
}
}
}
import java.applet.*;
import java.awt.*;
public class slip15B extends Applet {
public void paint(Graphics g)
{
g.drawOval(80, 70, 150, 150);
g.fillColor(Color.YELLOW);
g.setColor(Color.BLACK);
g.fillOval(120, 120, 15, 15);
g.fillOval(170, 120, 15, 15);
g.drawArc(130, 180, 50, 20, 180, 180);
}
}
SLIP-16
A) Write a java program to calculate sum of digits of a given number using recursion.
[15 M]
import java.io.*;
class slip16A
{
static int sum_of_digit(int n)
{
if (n == 0)
return 0;
return (n % 10 + sum_of_digit(n / 10));
}
import java.util.*;
class slip16B
{
static String[] str=new String[5]; static Scanner sr= new Scanner(System.in); static
ArrayList<String>list = new ArrayList<String>();
public static void main(String args[])
{
for(int i=0;i<str.length;i++)
{
System.out.print("Please Enter Employee Name:");
str[i]=sr.next();
list.add(str[i]);
}
Collections.sort(list);
System.out.println(list);
}
}
SLIP-17
A) Write a java Program to accept ‘n’ no’s through command line and store only
armstrong no’s into the array and display that array. [15 M]
class Slip17A1
{
public static void main(String args[])
{
int num,i,r,sum=0,temp,count=0;
num=args.length;
int a[]=new int[num];
int b[]=new int[10];
for(i=0;i<num;i++)
{
a[i]=Integer.parseInt(args[i]);
sum=0;
temp=a[i];
while(a[i] !=0)
{
r=a[i]%10;
sum=sum+r*r*r;
a[i]=a[i]/10;
}
if(temp==sum)
{
b[count]=temp;
count++;
}
}
for(i=0;i<count;i++)
{
System.out.println(b[i]);
}
}
}
B) Define a class Product (pid, pname, price, qty). Write a function to accept the product
details, display it and calculate total amount. (use array of Objects) [25 M]
import java.util.*;
class Product
{
int pid;
String pname;
float price,total;
int qty;
void accept()
{
Scanner src=new Scanner(System.in);
try
{
System.out.println("Enter Product id:");
pid=src.nextInt();
System.out.println("Enter Product name:");
pname=src.next();
System.out.println("Enter Product Price:");
price=src.nextFloat();
System.out.println("Enter Product qty:");
qty=src.nextInt();
}
catch(Exception e){}
}
void display()
{
total= qty*price;
System.out.println("pid is"+pid);
System.out.println("pname is"+pname);
System.out.println("Quantity is"+qty);
System.out.println("Price is"+price);
System.out.println("Total Amount is"+total);
}
}
class Slip17B
{
public static void main(String args[])
{
int n;
float t=0;
Scanner src=new Scanner(System.in);
System.out.println("Enter how many products you want");
n=src.nextInt();
Product p1[]=new Product[n];
for(int i=0;i<n;i++)
{
p1[i]=new Product();
p1[i].accept();
}
for(int i=0;i<n;i++)
{
p1[i].display();
}
for(int i=0;i<n;i++)
{
t=t+p1[i].total;
System.out.println("Total Cost is:"+t);
}
}
}
SLIP-18
A) Write a Java program to calculate area of Circle, Triangle & Rectangle.(Use Method
Overloading) [15 M]
import java.util.*;
class slip18A
{
void Area(int r)
{
System.out.println("Area of Circle :"+(3.14*r*r));
}
float Area(int b,float h)
{
return b*h/2;
}
double Area(int l, int rb)
{
return l*rb;
}
public static void main(String args[])
{
int r,b,l,rb;
float h;
Scanner sr=new Scanner(System.in);
System.out.print("Enter radius:");
r=sr.nextInt();
System.out.print("Enter base:");
b=sr.nextInt();
System.out.print("Enter height:");
h=sr.nextFloat();
System.out.print("Enter length:");
l=sr.nextInt();
System.out.print("Enter breadth");
rb=sr.nextInt();
slip18A s1=new slip18A();
s1.Area(r);
System.out.println("Area of Triangle is:" +s1.Area(b,h));
System.out.println("Area of Reactangle is:" +s1.Area(l,rb));
}
}
B) Write a java program to copy the data from one file into another file, while copying
change the case of characters in target file and replaces all digits by ‘*’ symbol.
[25 M]
SLIP-19
class slip19A
{
static int a=0,b=1,c;
void printF(int i)
{
if(i>=1)
{
c=a+b;
System.out.println("Value of c is"+c);
a=b;
b=c;
printF(i-1);
}
}
public static void main(String args[])
{
slip19A slip=new slip19A();
System.out.print(a+""+b);
slip.printF(10);
}
}
B) Create an Applet that displays the x and y position of the cursor movement
using Mouse and Keyboard. (Use appropriate listener) [25 M]
SLIP-20
A) Write a java program using AWT to create a Frame with title “TYBBACA”,
background color RED. If user clicks on close button then frame should close.
[15 M]
import javax.swing.*;
import java.awt.*;
class slip20a
{
public static void main(String args[])
{
JFrame frame =new JFrame("TYBBACA");
frame.setVisible(true);
}
}
B) Construct a Linked List containing name: CPP, Java, Python and PHP. Then
extend your java program to do the following:
i. Display the contents of the List using an Iterator
ii. Display the contents of the List in reverse order using a ListIterator.
[25 M]
import java.util.*;
public class slip20 {
SLIP-22
class slip22A
{
static int fact=1;
void calFact(int no)
{
if(no>=1)
{
fact=fact*no;
calFact(no-1);
}
}
public static void main(String args[])
{
int no=5;
slip22A slip= new slip22A();
slip.calFact(no);
System.out.print("Factorial number is : " +no+"is"+fact);
}
}
SLIP-25
SLIP-26
SLIP-28
A) Write a java program to count the number of integers from a given list. (Use
Command line arguments). [15 M]
import java.util.*;
public class Slip28A
{
public static void main(String args[])
{
int count=0;
ArrayList<String> a1=new ArrayList();
for(int i=0; i<args.length;i++)
{
a1.add(args[i]);
}
for(int i=0;i<a1.size();i++)
{
String ele=a1.get(i);
try
{
int j=Integer.parseInt(ele);
count++;
}catch(NumberFormatException e){}
}
System.out.println("Integers are present in list are" +count);
}
}
B) Write a java Program to accept the details of 5 employees (Eno, Ename, Salary) and
display it onto the JTable. [25 M]
import java.awt.*;
import java.applet.*;
public class slip29B extends Applet implements Runnable
{
int x,y;
int dy;
public void init()
{
x=140;
y=40;
dy=30;
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
if(y<40||y>260)
{
dy=-dy;
}
y=y+dy;
try
{
Thread.sleep(800);
}
catch(Exception e){}
repaint();
}
}
public void paint(Graphics g)
{
g.fillRect(10,10,330,300);
if(y>260)
g.setColor(new Color((int)(Math.random() * 1000)));
else
g.setColor(Color.red);
g.fillOval(x,y,30,30);
}
}
.HTML FILE
<applet code="slip29B.class" width="300" height="300">
</applet>
SLIP-30
B) Write a java program using Applet for bouncing ball. Ball should change its color
for each bounce.
import java.awt.*;
import javax.swing.*;
class Slip30B extends JFrame
{
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3;
JTextArea t;
JPanel p,p1,p2,p3;
ButtonGroup bg;
JRadioButton m,f;
JCheckBox c1,c2,c3;
JButton b1,b2;
Slip30B()
{
p =new JPanel();
p1=new JPanel();
l1=new JLabel("First Name ");
l2=new JLabel("last Name ");
l3=new JLabel("Address ");
l4=new JLabel("mobile No ");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
t=new JTextArea(2,10);
p.add(l1); p.add(t1);
p.add(l2); p.add(t2);
p.add(l3); p.add(t);
p.add(l4); p.add(t3);
p.setLayout(new GridLayout(4,2));
add(p);
l5=new JLabel("Gender ");
m = new JRadioButton("male");
f = new JRadioButton("female");
bg = new ButtonGroup();
bg.add(m);
bg.add(f);
p1.add(l5);
p1.add(m);
p1.add(f);
p1.setLayout(new GridLayout(1,3));
p2=new JPanel();
l6=new JLabel("Your Interests ");
c1=new JCheckBox("Computer");
c2=new JCheckBox("Sports");
c3=new JCheckBox("Music");
p2.add(l6);
p2.add(c1);
p2.add(c2);
p2.add(c3);
p2.setLayout(new GridLayout(1,4));
p3=new JPanel();
b1=new JButton("submit");
b2=new JButton("clear");
p3.add(b1);
p3.add(b2);
add(p);
add(p1);
add(p2);
add(p3);
setSize(300,400);
setLayout(new FlowLayout(FlowLayout.LEFT));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String a[])
{
new Slip30B();
}
}