0% found this document useful (0 votes)
38 views24 pages

Slip1 10java

The document contains a series of Java programming exercises and their solutions, covering topics such as file handling, mouse events, object-oriented programming, applets, and exception handling. Each slip presents a specific task, such as displaying characters, copying non-numeric data from files, or calculating the area and volume of geometric shapes. The solutions provided include complete Java code implementations for each exercise.

Uploaded by

shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views24 pages

Slip1 10java

The document contains a series of Java programming exercises and their solutions, covering topics such as file handling, mouse events, object-oriented programming, applets, and exception handling. Each slip presents a specific task, such as displaying characters, copying non-numeric data from files, or calculating the area and volume of geometric shapes. The solutions provided include complete Java code implementations for each exercise.

Uploaded by

shivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

T.Y.

BBA(CA) Sem – V

Subject : Core java (Practical Slip Solution)

Slip No: 1 to 10
Slip 1 A) Write a ‘java’ program to display characters from ‘A’ to ‘Z’.

Answer :
class slip1A{
public static void main(String args[]){
for(int i=65; i<=90; i++){
System.out.print( " " + Character. toString((char) i));
}
}
}

Slip 1 B) Write a ‘java’ program to copy only non-numeric data from one file to
another file.

Answer :
import java.io.*;
class Slip1B{
public static void main(String args[]) throws IOException{
char ch;
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
int c;
while ((c=fr.read())!=-1){
ch=(char)c;
if(Character.isDigit(ch)==false){
fw.write(c);
}
}
fr.close();
fw.close();
}
}

Slip 2 A) Write a java program to display all the vowels from a given string.

Answer :
import java.io.DataInputStream;
class Slip2A {
public static void main(String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter String : ");
String str = dr.readLine().toLowerCase();
for(int i=0; i<str.length(); i++){
if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' ||
str.charAt(i)=='o' || str.charAt(i)=='u' ){
System.out.print(str.charAt(i));
}
}
} catch (Exception e) {}
}
}

Slip 2 B) Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED
and MOUSE_CLICK and display the position of the Mouse_Click in a TextField.

Answer :

import java.awt.*;

import java.awt.event.*;

class MyFrame extends Frame

TextField t,t1;

Label l,l1;

int x,y;

Panel p;

MyFrame(String title)

super(title);

setLayout(new FlowLayout());

p=new Panel();

p.setLayout(new GridLayout(2,2,5,5));

t=new TextField(20);
l= new Label("Mouse clicking");

l1= new Label("Mouse Movement");

t1=new TextField(20);

p.add(l);

p.add(t);

p.add(l1);

p.add(t1);

add(p);

addMouseListener(new MyClick());

addMouseMotionListener(new MyMove());

setSize(500,500);

setVisible(true);

class MyClick extends MouseAdapter

public void mouseClicked(MouseEvent me)

x=me.getX();

y=me.getY();

t.setText("X="+x+" Y="+y);

class MyMove extends MouseMotionAdapter

public void mouseMoved(MouseEvent me)


{

x=me.getX();

y=me.getY();

t1.setText("X="+ x +" Y="+y);

class Slip2B

public static void main(String args[])

MyFrame f = new MyFrame("Slip Number 4");

Slip 3 A) Write a ‘java’ program to check whether given number is Armstrong or not.
(Use static keyword)

Answer :
import java.io.DataInputStream;
class Slip3A {
static int temp;
public static void main(String args[]){
int n,r,sum=0;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Number");
n = Integer.parseInt(dr.readLine());
temp=n;
while(n>0){
r = n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum){
System.out.println(temp + " Is Armstrong Number : ");
}else{
System.out.println(temp + " Is Not Armstrong Number : ");
}
} catch (Exception e) {}
}
}

Slip 3 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.)

Answer :
import java.io.*;

abstract class Shape{


int a,b;
Shape(int x, int y){
a = x;
b = y;
}
abstract double area();
abstract double volume();
}
class Cone extends Shape{
Cone(int x, int y){
super(x,y);
}
double area(){
return (a*b*3.14);
}
double volume(){
return (3.14*a*a*b);
}
}
class Cylinder extends Shape{

Cylinder(int x, int y){


super(x,y);
}

double area(){
return (2*3.14*a*b*3.14*a*b);
}

double volume(){
return (3.14*a*a*b);
}

class Slip3B{
public static void main(String args[]) throws Exception{
int r,h,s;
DataInputStream dr = new DataInputStream(System.in);
System.out.println("Enter Radius, Height and Side Values : ");
r = Integer.parseInt(dr.readLine());
h = Integer.parseInt(dr.readLine());
s = Integer.parseInt(dr.readLine());
Shape s1;

Cone c1 = new Cone(r,s);


s1=c1;
System.out.println("Area of Cone is : " + s1.area());
System.out.println("Volume of Cone is : " +s1.volume());

Cylinder cy = new Cylinder(r,h);


s1 =cy;
System.out.println("Area of Cylinder is : " + s1.area());
System.out.println("Area of Cylinder is : " + s1.volume());
}
}

Slip 4 A) Write a java program to display alternate character from a given string.

Answer :
import java.io.DataInputStream;
class Slip4A {
public static void main(String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter String : ");
String str = dr.readLine();
for(int i=0;i<str.length();i+=2) {
System.out.print(" " + str.charAt(i));
}
} catch (Exception e) {}
}
}

Slip 4 B) Write a java program using Applet to implement a simple arithmetic


calculator.

Answer :

import java.awt.*;

import java.applet.*;

import java.awt.event.*;
public class Slip4B extends Applet implements ActionListener {

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

String s1 = "", s2;

Frame f;

Panel p2;

TextField t;

int n1, n2;

public void init(){

setLayout(new BorderLayout());

Frame title = (Frame) this.getParent().getParent();

title.setTitle("Simple Calc");

t = new TextField();

p2 = new Panel();

p2.setLayout(new GridLayout(6, 4, 2, 1));

p2.setFont( new Font("Times New Roman",Font.BOLD,15));

b1 = new Button("1");

b1.addActionListener(this);

b2 = new Button("2");

b2.addActionListener(this);
b3 = new Button("3");

b3.addActionListener(this);

b4 = new Button("+");

b4.addActionListener(this);

b5 = new Button("4");

b5.addActionListener(this);

b6 = new Button("5");

b6.addActionListener(this);

b7 = new Button("6");

b7.addActionListener(this);

b8 = new Button("-");

b8.addActionListener(this);

b9 = new Button("7");

b9.addActionListener(this);

b10 = new Button("8");

b10.addActionListener(this);
b11 = new Button("9");

b11.addActionListener(this);

b12 = new Button("*");

b12.addActionListener(this);

b13 = new Button("Clear");

b13.addActionListener(this);

b14 = new Button("0");

b14.addActionListener(this);

b15 = new Button("/");

b15.addActionListener(this);

b16 = new Button("=");

b16.addActionListener(this);

add(t, "North");

p2.add(b9);

p2.add(b10);

p2.add(b11);

p2.add(b4);
p2.add(b5);

p2.add(b6);

p2.add(b7);

p2.add(b8);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b12);

p2.add(new Label(""));

p2.add(b14);

p2.add(new Label(""));

p2.add(b15);

p2.add(new Label(""));

p2.add(new Label(""));

p2.add(new Label(""));

p2.add(b16);

add(p2);

p2.add(new Label(""));

p2.add(b13);

}
public void actionPerformed(ActionEvent e1){

String str = e1.getActionCommand();

if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")){

String str1 = t.getText();

s2 = str;

n1 = Integer.parseInt(str1);

s1 = "";

}else if (str.equals("=")){

String str2 = t.getText();

n2 = Integer.parseInt(str2);

int sum = 0;

if (s2 == "+")

sum = n1 + n2;

else if (s2 == "-")

sum = n1 - n2;

else if (s2 == "*")

sum = n1 * n2;

else if (s2 == "/")

sum = n1 / n2;

String str1 = String.valueOf(sum);

t.setText("" + str1);

s1 = "";

}else if (str.equals("Clear")){

t.setText("");
}else{

s1 += str;

t.setText("" + s1);

/*

* <applet code="Slip4B" height=250 width=250>

* </applet>

*/

Slip 5 A) Write a java program to display following pattern:

45

345

2345

12345

Answer :
class Slip5A {
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 5 B) Write a java program to accept list of file names through command line.
Delete the files having extension .txt. Display name, location and size of remaining files.

Answer :
import java.io.*;
class Slip5B{
public static void main(String args[]) throws Exception{
for(int i=0;i<args.length;i++){
File file=new File(args[i]);
if(file.isFile()){
String name = file.getName();
if(name.endsWith(".txt")){
file.delete();
System.out.println("file is deleted " + file);
}else{
System.out.println("File Name : " + name + "\nFile Location : "
+file.getAbsolutePath()+"\nFile Size : "+file.length()+" bytes");
}
}
else{
System.out.println(args[i]+ "is not a file");
}
}
}
}

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).

Answer :
import java.io.*;
class NumZero extends Exception{}
public class Slip6A {
static int n;
public static void main(String args[]){
int first,last=0;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Number : ");
n = Integer.parseInt(dr.readLine());
if(n!=0){
last = n % 10;
first = n;
while(n>=10){
n = n / 10;
}
first=n;
System.out.print("Sum of First and Last Number is : " + (first +
last));
}else{
throw new NumZero();
}

} catch (NumZero nz) {


System.out.println("Number is Zero");
}
catch(Exception e){}
}
}

Slip 6 B) Write a java program to display transpose of a given matrix.

Answer :
class Slip6B{
public static void main(String args[]){
int i, j;
int array[][] = {{1,3,4},{2,4,3},{3,4,5}};
System.out.println("Transpose of Matrix is :");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}

Slip 7 A) Write a java program to display Label with text “Dr. D Y Patil College”,
background color Red and font size 20 on the frame.

Answer :

import java.awt.*;

import java.awt.event.*;

public class Slip7A extends Frame{


public void paint(Graphics g){

Font f = new Font("Georgia",Font.PLAIN,20);

g.setFont(f);

g.drawString("Dr D Y Patil College", 50, 70);

setBackground(Color.RED);

public static void main(String args[]){

Slip7A sl = new Slip7A();

sl.setVisible(true);

sl.setSize(200,300);

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)

Answer :
import java.io.*;
class Cricket{
String Name;
int Total_runs;
int Notout;
int Inning;
float avg;

void accept(){
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try{
System.out.print("Enter Name of Player : ");
Name = br.readLine();
System.out.print("Enter Total Runs of Player : ");
Total_runs = Integer.parseInt(br.readLine());
System.out.print("Enter Name of Tixes Not out : ");
Notout = Integer.parseInt(br.readLine());
System.out.print("Enter Innings played by players : ");
Inning = Integer.parseInt(br.readLine());
}catch (Exception e) {}
}
void average(){
avg = Total_runs/Inning;
System.out.println("Name : "+Name+"\nTotal runs : "+Total_runs+"\
nAvergae : "+avg+"\nInning : "+ Inning);
}
}
public class Slip7B {
public static void main(String args[]){
float max =0;
int n;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
System.out.print("How many Players : ");
n = Integer.parseInt(br.readLine());
Cricket ob1[]= new Cricket[n];
for(int i=0; i<n; i++){
ob1[i] = new Cricket();
ob1[i].accept();
}
for(int i=0; i<n; i++){
ob1[i].average();
}
for(int i=0; i<n; i++){
if(max<ob1[i].avg){
max = ob1[i].avg;
}
}
System.out.println("-----------------------------\nMax avg : "+max);
} catch (Exception e) {
System.out.println("Error........."+e);
}
}
}

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)

Answer :
import java.util.*;

interface Shape{
final float pi= 3.14F;
double area();
}
class Circle implements Shape{
int rad;
Circle(int r){
rad=r;
}
public double area(){
return pi*rad*rad;
}
}
class Sphere implements Shape{
int rad;
Sphere(int r){
rad =r;
}
public double area(){
return 4*pi*rad*rad;
}
}

class Slip8A {
public static void main(String args[]) throws Exception{
int r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Radius : ");
r=sc.nextInt();

Shape sh;
Circle cl=new Circle(r);
sh=cl;
System.out.println("Area of Circle : " + sh.area());

Sphere sp=new Sphere(r);


sh=sp;
System.out.println("Area of Sphare : "+sh.area());
}
}

Slip 8 B) Write a java program to display the files having extension .txt from a given
directory.

Answer :
import java.io.File;
class Slip8B {
public static void main(String[] args) {
File file = new File("C:\\Users\\Saurabh_Sapkal\\Desktop\\ln\\java\\
Slips");
String[] fileList = file.list();
for(String str : fileList) {
if(str.endsWith(".txt")){
System.out.println(str);
}
}
}
}

Slip 9 A) Write a java Program to display following pattern:

01

010

1010

Answer :
class Slip9A {
public static void main(String args[]){
int i,j,k=1;;
for(i=1; i<=4; i++){
for(j=1; j<=i; j++){
if(k%2==1){
System.out.print(1 + " ");
}else{
System.out.print(0 + " ");
}
k++;
}
System.out.println();
}
}
}

Slip 9 B) Write a java program to validate PAN number and Mobile Number. If it is
invalid then throw user defined Exception “Invalid Data”, otherwise display it.

Answer :
import java.io.*;
class invaliddetails extends Exception{}
class Slip9B{
static int n;
public static void main( String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("********* Do you Want to Validate ********* \
n1. Mobile Number Press : 1 \n2. PAN Card Press : 2 \nEnter Number : ");
n = Integer.parseInt(dr.readLine());
switch(n){
case 1 :
System.out.print("Enter Mobile Number : ");
Long num = Long.parseLong(dr.readLine());
if(num.toString().matches("(0/91)?[7-9][0-9]{9}")){
System.out.print("Enter Valid Mobile Number..!");
}else{
throw new invaliddetails();
}
break;
case 2 :
System.out.print("Enter PAN Number : ");
String str= dr.readLine();
if(str.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}")){
System.out.print("Enter Valid PAN CARD Number..!");
}else{
throw new invaliddetails();
}
break;
default :
throw new invaliddetails();
}
} catch (invaliddetails nz) {
System.out.println("You Enter Invalid Details...!");
}
catch (NumberFormatException e){
System.out.println("You Enter Invalid Details...!");
}
catch(Exception e){}
}
}

Slip 10 A) Write a java program to count the frequency of each character in a given
string.

Answer :
import java.io.DataInputStream;
class Slip10A {
public static void main(String args[]){
int i, j;
String ch;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter String : ");
ch = dr.readLine();
int[] str = new int[ch.length()];
char string[] = ch.toCharArray();
for(i = 0; i <ch.length(); i++) {
str[i] = 1;
for(j = i+1; j <ch.length(); j++) {
if(string[i] == string[j]) {
str[i]++;
string[j] = '0';
}
}
}
for(i = 0; i <str.length; i++) {
if(string[i] != ' ' && string[i] != '0'){
System.out.println(string[i] + "-" + str[i]);
}
}

} catch (Exception e) {}
}
}

Slip 10 B) Write a java program for the following:

Answer :

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

class Slip10B extends JFrame implements ActionListener{

JLabel l1,l2,l3,l4,l5,l6;

JTextField t1,t2,t3,t4,t5;

JButton b1,b2,b3;

Panel p1,p2,p3,p4,p5;

GridLayout g1,g2,g3,g4,g5,g6;

JFrame jf;

public Slip10B(){

jf = new JFrame();

l1 = new JLabel("Simple Interest Calculator");

l2 = new JLabel("Principle Amount");


l3 = new JLabel("Interest Rate(%)");

l4 = new JLabel("Time(Yrs)");

l5 = new JLabel("Total Amount");

l6 = new JLabel("Interest Amount");

t1 = new JTextField(20);

t2 = new JTextField(20);

t3 = new JTextField(20);

t4 = new JTextField(20);

t5 = new JTextField(20);

b1 = new JButton("Calculate");

b2 = new JButton("Clear");

b3 = new JButton("Close");

p1 = new Panel();

g1= new GridLayout(1,1);

p1.setLayout(g1);

p1.add(l1);

p2 = new Panel();

g2 = new GridLayout(1,2);

p2.setLayout(g2);

p2.add(l2);

p2.add(t1);
p3 = new Panel();

g3 = new GridLayout(1,4);

p3.setLayout(g3);

p3.add(l3);

p3.add(t2);

p3.add(l4);

p3.add(t3);

p4 = new Panel();

g4 = new GridLayout(2,2);

p4.setLayout(g4);

p4.add(l5);

p4.add(t4);

p4.add(l6);

p4.add(t5);

p5 = new Panel();

g5 = new GridLayout(1,3);

p5.setLayout(g5);

p5.add(b1);

p5.add(b2);

p5.add(b3);

g6 = new GridLayout(5,1);
this.setLayout(g6);

this.add(p1);

this.add(p2);

this.add(p3);

this.add(p4);

this.add(p5);

this.setSize(500,250);

this.setVisible(true);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

public void actionPerformed(ActionEvent ae){

int p = Integer.parseInt(t1.getText());

float rt = Float.parseFloat(t2.getText());

float tm = Float.parseFloat(t3.getText());

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

double iamt = (p*tm*rt)/100;

t5.setText(Double.toString(iamt));

double tamt = iamt+p;

t4.setText(Double.toString(tamt));
}

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

t1.setText("");

t2.setText("");

t3.setText("");

t4.setText("");

t5.setText("");

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String args[]){

Slip10B s1 = new Slip10B();

You might also like