0% found this document useful (0 votes)
11 views

javapractical

Uploaded by

ps0474985
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

javapractical

Uploaded by

ps0474985
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

A) Write a ‘java’ program to display characters from ‘A’ to ‘Z’.

public class Slip1q1 {


public static void main(String[] args) {
for(char c='A';c<='Z';c++){
System.out.print(c+" ");
}
System.out.println();
for(char c='a';c<='z';c++){
System.out.print(c+" ");
}
}
}
A) Write a ‘java’ program to check whether given number is Armstrong or not.
(Use static keyword) [15 M]
import java.util.Scanner;
public class Armstrong
{
static int number ,check, rem, sum = 0;
public static void main(String args[])
{
System.out.println("Enter the number to be verified:");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
check = number;
while(check != 0)
{
rem = check % 10;
sum = sum + (rem * rem * rem);
check = check / 10;
}
if(sum == number)
System.out.println("Given number is an armstrong number.");
else
System.out.println("Given number is not an armstrong number.");
}
}

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();
}
}

A) Write a java program to display alternate character from a given string.[15 M]

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

A) Write a java program to display following pattern:


5
45
345
2345
12345

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(){}

Cricketplayer(int pid1,String pname,int run1,int iplayed1,int notout1)


{
pid=pid1;
name=pname;
run=run1;
iplayed=iplayed1;
notout=notout1;
}

public double average(String name1)


{
double avg=0;
if(name.equals(name1))
{
avg=(double)run/(double)iplayed;
return avg;
}
else
return 0;
}
public double average()
{
double avg=0;
avg +=(double)run/(double)iplayed;
return avg;
}
}
class Slip7B
{
public static void main(String args[])
{
final List<Double> allAverages=new ArrayList<Double>(); //all average list
int pcode,iplayed,noplayer,notout,runs,i;

double avg=0, avgall=0;


String name;

Scanner src=new Scanner(System.in);


System.out.println("Enter how many players you want");
noplayer=src.nextInt();

Cricketplayer s[]=new Cricketplayer[noplayer];


for(i=0;i<noplayer;i++)
{
System.out.println("Enter pid:");
pcode=src.nextInt();

System.out.println("Enter pname:");
name=src.next();

System.out.println("Enter Runs:");
runs=src.nextInt();

System.out.println("Enter Inning Played:");


iplayed=src.nextInt();

System.out.println("Enter Notout Players:");


notout=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();
}

class Circle implements Shape


{
final float PI=3.14f;
float area_of_circle,r;
Scanner src=new Scanner(System.in);

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;
}

public void show()


{
System.out.println("Area of Sphere is :"+area_of_sphere);
}
}

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

A) Write a java Program to display following pattern:


1
01
010
1 0 1 0 [15 M]

import java.util.Scanner;

public class Pattern {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
}
}

SLIP-10

A) Write a java program to count the frequency of each character in a given string.
[15 M]

public class slip10A


{
public static void main(String[] args) {
String str = "Java Programming";
int[] freq = new int[str.length()];
int i, j;

//Converts given string into character array


char string[] = str.toCharArray();

for(i = 0; i <str.length(); i++) {


freq[i] = 1;
for(j = i+1; j <str.length(); j++) {
if(string[i] == string[j]) {
freq[i]++;
string[j] = '0';
}
}
}
System.out.println("Characters and their corresponding frequencies");
for(i = 0; i <freq.length; i++) {
if(string[i] != ' ' && string[i] != '0')
System.out.println(string[i] + "-" + freq[i]);
}
}
}

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]

public class Reverse


{
public static void main(String[] args) {
String string = "Dream big";
String revStr = "";
for(int i = string.length()-1; i >= 0; i--){
revStr = revStr + string.charAt(i);
}
System.out.println("Original string: " + string);
System.out.println("Reverse of given string: " + revStr);
}
}

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 calculate power of a number using recursion.


[15 M]
class slip14A {
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
public static void main(String[] args)
{
int N = 5;
int P = 3;
System.out.println(power(N, P));
}
}
SLIP-15

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");
}
}
}

B) Write an applet application to display smiley face. [25 M]

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));
}

public static void main(String args[])


{
int num = 232;
int result = sum_of_digit(num);
System.out.println("Sum of digits in " + num + " is " + result);
}
}
B) Write a java program to accept n employee names from user. Sort them in
ascending order and Display them.(Use array of object and Static keyword)
[25 M]

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

A) Write a Java program to display Fibonacci series using function. [15 M]

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 {

public static void main(String[] args)


{
List li = new LinkedList();
li.add("CPP");
li.add("Java");
li.add("Python");
li.add("PHP");
System.out.println("Dislay content in List in using Iterator:");
ListIterator listIterator= li.listIterator();
while (listIterator.hasNext())
{
System.out.println(listIterator.next());
}
System.out.println("Dislay content in List in reverse order using ListIterator:");
while (listIterator.hasPrevious())
{
System.out.println(listIterator.previous());
}
}
}

SLIP-22

A) Write a Java program to calculate factorial of a number using recursion.


[15 M]

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

A) Write a java program to check whether given string is palindrome or not.


[15 M]
import java.util.Scanner;
public class slip25A {
public static void main(String[] args) {
String s = "leve";
String rev = "";
for (int i = s.length()-1; i >=0 ; i--)
rev=rev+s.charAt(i);
if(s.equals(rev))
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}
}

SLIP-26

B) Write a java program using applet to draw Temple. [25 M]


import java.applet.Applet;
import java.awt.*;
public class Slip26B extends Applet
{
public void paint(Graphics g)
{
g.drawRect(100,150,90,120);
g.drawRect(130,230,20,40);
g.drawLine(150,100,100,150);
g.drawLine(150,100,190,150);
g.drawLine(150,50,150,100);
g.drawRect(150,50,20,20);
}
}

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();
}
}

You might also like