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

  Savitribai Phule Pune University

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

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


Q.2 Python:
A) Write a Python program to accept n numbers in list and remove duplicates from a list.

>>> l = [5, 3, 6, 5, 9, 3, 4]
>>> s = set(l)
>>> l = list(s)
>>> l
[3, 4, 5, 6, 9]


B) Write Python GUI program to take accept your birthdate and output your age when a button is pressed.

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

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

Comments

Popular posts from this blog

First Year Web Technology Practical Slips Solution

SYBSc (CS) Sem III : Data Structure Slip 14 Que - 2

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2