TY B.B.A. (C.A.) Sem V : Core Java Practical Slips Solutions

  

SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE

T.Y.B.B.A.(C.A.) Semester – V (CBCS 2019 Pattern) University Practical Examination

Lab Course: (CA-506) Computer Laboratory Based on 503, 504

Core Java, MongoDB / Python


Q.1. Core Java:

Slip No. 1

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

class Slip1_A  

{

    public static void main(String args[])

    {

        for(char ch = 'A' ; ch <= 'Z'; ch++)

        {

            System.out.print(ch+" ");

        }

    }  

}


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

import java.io.*;
class Slip1_B
{
public static void main(String args[]) throws IOException
{
File ifile = new File("in.dat");
File ofile = new File("out.dat");
try{
FileReader in = new FileReader(ifile);
FileWriter out = new FileWriter(ofile, true);
int ch;
while((ch = in.read()) != -1)
{
if(Character.isDigit(ch))
continue;
out.write(ch);
}
in.close();
out.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
}
}

Slip No. 2

/* A) Write a java program to display all the vowels from a given string */

class Slip2_A {
    public static void main(String args[])
    {
        String str = "Hello World";
        char ch;
        
        for(int i=0; i < str.length(); i++)
        {
            ch = str.charAt(i);
            
            if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
            {
                System.out.print(ch + " ");
            }
        }
    }
}


/** 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 */

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()); // Frame

p = new Panel();

p.setLayout(new GridLayout(2, 2, 5, 5));
l = new Label("Co-ordinates of clicking");
t = new TextField(20);

l1 = new Label("Co-ordinates of 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);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
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 Slip2_B
{
public static void main(String[] args) {
MyFrame f = new MyFrame("Slip Number 4");
}
}


Slip No. 3

/** A) Write a 'java' program to check whether given number is Armstrong or not. (Use static keyword).*/

import java.io.*;

class Slip3_A
{
public static int checkArmstrongNumber(int Number)
{
int rem, sum = 0;

while(Number > 0)
{
rem = Number%10;

sum = sum + rem * rem * rem;

Number = Number/10;
}

return sum;
}

public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number : ");
int n = Integer.parseInt(br.readLine());

if (n == checkArmstrongNumber(n))
{
System.out.println(n + " is armstrong number.");
}
else{
System.out.println(n + " is not 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). (Area of Cone = 3.14 * r ( r + l)) */

import java.io.*;

abstract class Shape
{
final double pi = 3.14;
abstract double area();
abstract double volume();
}

class Cone extends Shape{
int radius, height;

Cone(){
radius = 0;
height = 0;
}

Cone(int radius, int height){
this.radius = radius;
this.height = height;
}

double area(){
return pi * radius * ( Math.sqrt(radius*radius+height*height));
}

double volume(){
return (pi * (radius * radius) * height)/3;
}


}

class Cylinder extends Shape{
int radius, height;

Cylinder(){
radius = 0;
height = 0;
}

Cylinder(int radius, int height){
this.radius = radius;
this.height = height;
}

double area(){
return (2*pi*radius*height) + (2*pi*radius*radius);
}

double volume(){
return pi * radius * radius * height;
}


}


class Slip3_B
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius: ");
int radius = Integer.parseInt(br.readLine());
System.out.println("Enter the height: ");
int height = Integer.parseInt(br.readLine());

Cone c = new Cone(radius, height);
System.out.println("The area of a Cone = " + c.area());
System.out.println("The volume of a Cone = " + c.volume());

Cylinder cl = new Cylinder(radius, height);
System.out.println("The area of a Cylinder = " + cl.area());
System.out.println("The volume of a Cylinder = " + cl.volume());

}
}


Slip No. 5

/** A) Write a java program to display following pattern: 
5
4 5 
3 4 5 
2 3 4 5 
1 2 3 4 5
*/

class Slip5_A
{
public static void main(String args[])
{

for (int i = 5; i > 0; i--)
{
for (int j = i; j <= 5; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}


/** 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.*/

import java.io.*;

class Slip5_B
{
   public static void main(String args[]) throws IOException
   {
        for(int i = 0; i < args.length; i++)
        {
            File f = new File(args[i]);

            if(f.isFile())
            {
                String name = f.getName();
                if(name.endsWith(".txt"))
                {
                    f.delete();
                    System.out.println("File is deleted." + f);
                }
                else{
                    System.out.println("File Name : " + name);
                    System.out.println("File Location : " + f.getAbsolutePath());
                    System.out.println("File Size : " + f.length() + "bytes");
                }
            }
            else
            {
                System.out.println(args[i] + " is not a file.");
            }
        }
   } 
}


Slip No. 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.io.*;

class ZeroNumExc extends Exception{}

public class Slip6_A {
    static int n;
    public static void main(String args[]){
        try {
            int first, last = 0;
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Enter Number : ");
            n = Integer.parseInt(br.readLine());
            if(n == 0)
            {
                throw new ZeroNumExc();
            }
            else
            {
                if(n < 10)
                    first = n;
                else
                {
                    first = n%10;
                    while(n>0)
                    {
                        last = n%10;
                        n = n / 10;
                    }
                }                
                System.out.print("Sum of First and Last Number is : " + (first + last));
            }           
        } catch (ZeroNumExc nz) {
            System.out.println("Number is Zero");
        }
        catch(Exception e){}  
    }    
}


/** B) Write a java program to display transpose of a given matrix. */

class Slip6_B
{
    public static void main(String args[])
    {
        int matrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int transposeMatrix[][] = new int[3][3];

        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                transposeMatrix[i][j] = matrix[j][i]; 
            }
        }
        System.out.println("The Original Matrix is : ");
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println(); 
        }

        System.out.println("The Transpose Matrix is : ");
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                System.out.print(transposeMatrix[i][j] + " ");
            }
            System.out.println(); 
        }
    }
}


Slip No. 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) */

interface Shape
{
    public final double PI = 3.14;
    public double area();
}

class Circle implements Shape
{
    int radius;

    Circle(int radius)
    {
        this.radius = radius;
    }

    public double area()
    {
        return (PI * radius * radius);
    }
}

class Sphere implements Shape
{
    int radius;

    Sphere(int radius)
    {
        this.radius = radius;
    }

    public double area()
    {
        return (4 * PI * radius * radius);
    }
}

public class Slip8_A {
    public static void main(String args[])
    {
        Shape s = new Circle(2);
        System.out.println("The area of Circle : " + s.area());

        s = new Sphere(2);
        System.out.println("The area of Sphere : " + s.area());
    }
}


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

import java.io.*;

class Slip8_B
{
    public static void main(String args[])
    {
        File f = new File("./");

        String fileList[] = f.list();

        for(String file : fileList)
        {
            if(file.endsWith(".txt"))
            {
                System.out.println(file);
            }
        }
    }
}

Slip No. 9

/* A) Write a java program to display following pattern: 
1
0 1 
0 1 0 
1 0 1 0
*/

class Slip9_A
{
public static void main(String args[])
{

for (int i = 1; i <= 4; 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();
}
}
}


/** 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. */

import java.io.*;

class InvalidData extends Exception{
    InvalidData()
    {
        System.out.println("Invalid Data");
    }
}
class Slip9_B{
    public static void main( String args[]){
        Long mobileNumber;
        String panNumber;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try 
        {
            System.out.print("Enter PAN Number : ");
            panNumber = br.readLine();

            if(panNumber.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}")){
                System.out.println("Valid PAN CARD Number..!");
            }
            else
            {
                throw new InvalidData();
            }

            System.out.print("Enter Mobile Number : ");
            mobileNumber = Long.parseLong(br.readLine());
                    
            if(mobileNumber.toString().matches("(0/91)?[7-9][0-9]{9}")){
                System.out.println("Valid Mobile Number..!");
            }
            else
            {
                throw new InvalidData();
            }
        } catch (InvalidData id) {
            System.out.println("You Enter Invalid Details...!");
        }
        catch (NumberFormatException e){
            System.out.println("You Enter Invalid Details...!");
        }
        catch(Exception e){}
    }
}


Slip No. 12

/** A) Write a java program to display each String in reverse order from a String array.*/

class Slip12_A
{
    public static void main(String args[])
    {
        String strList[] = {"TYBCA", "SYBCS", "Arts", "Mathematics", "Computer Science"};

        for(String s : strList)
        {
             StringBuffer str = new StringBuffer(s);
             System.out.println(str.reverse());   
        }
    }    
}


/** B) Write a java program to display multiplication table of a given number into the List box by clicking on button.*/

import java.awt.*;
import java.awt.event.*;

class Slip12_B extends Frame implements ActionListener
{
    List list = new List();
    Label t;
    Button show;
    String str = "";
    Panel p;
    List result = new List();

    Slip12_B(String title)
    {
        super(title);

        setLayout(new FlowLayout());

        p = new Panel();
        p.setLayout(new GridLayout(20, 1, 5, 5));

        t = new Label("Select given number to display table");
        p.add(t);

        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");

        p.add(list);

        show  = new Button("Show");
        p.add(show);

        show.addActionListener(this);

        add(p);

        setSize(300, 800);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                dispose();;
            }
        });

    }

    public void actionPerformed(ActionEvent e){
        Button btn = (Button) e.getSource();
        
        if(btn.getLabel() == "Show")
        {
            int num = Integer.parseInt(list.getSelectedItem());
            
            result.removeAll();

            for(int i = 1; i <= 10; i++)
            {
                int a = num*i;
                result.add(num  +" * " + i +" = "+ a);              
            }

            p.add(result);
        }
    }

    public static void main(String args[])
    {
        new Slip12_B("Multiplication Table");
    }
}


Slip No. 18


/** A) Write a Java program to calculate area of Circle, Triangle & Rectangle. (Use Method Overloading) */

class Circle
{
    int radius;

    Circle(int r)
    {
        radius = r;
    }

    void area()
    {
        double a = 3.14 * radius * radius;
        System.out.println("The area of Circle = " + a);
    }
}

class Triangle
{
    int base, height;

    Triangle(int b, int h)
    {
        base = b;
        height = h;
    }

    void area()
    {
        double a = 0.5 * base * height;
        System.out.println("The area of Triangle = " + a);
    }
}

class Rectangle
{
    int length, breadth;

    Rectangle(int l, int b)
    {
        length = l;
        breadth = b;
    }

    void area()
    {
        double a = length * breadth;
        System.out.println("The area of Rectangle = " + a);
    }
}

class Slip18_A
{
    public static void main(String args[])
    {
        Circle c = new Circle(3);
        c.area();
        Triangle t = new Triangle(8, 6);
        t.area();
        Rectangle r = new Rectangle(5, 3);
        r.area();
    }
}


/** 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. */

import java.io.*;

class Slip18_B
{
    public static void main(String args[]) throws IOException
    {
        FileReader fin = new FileReader("in.dat");
        FileWriter fout = new FileWriter("out.dat");

        int b; 

        while((b=fin.read())!=-1) { 
            char ch = (char)b;

            if(Character.isLowerCase(ch))
                ch = Character.toUpperCase(ch);
            else if (Character.isUpperCase(ch))
                ch = Character.toLowerCase(ch);
            else if (Character.isDigit(ch))
                ch = '*';

            fout.write(ch);
        }

        fin.close();
        fout.close();
    }
}

Comments

Popular posts from this blog

TYBSc (CS) Sem V : Object Oriented Programming Using Java I Practical Slips Solutions

First Year Web Technology Practical Slips Solution