Likes

BSC-IT 4TH SEMESTER PRACTICAL SOLUTIONS


KU PRACTICAL 16 SETS WITH SOLUTIONS

SET 1
#1. Write a program to print “Welcome to Java Programming” on the screen.
class welcome
{
public static void main(String a[])
{
System.out.println(“Welcome to Java Progamming”);
}
}
#2. Using a case statement, write a shell program to read a command (eg., who, cal, ls, ps) from the user and execute it.
echo “Enter any command:”
read com
case $com in
who ) who;;
cal) cal;;
ls) ls;;
ps) ps;;
*) echo “Invalid command”
Esac



SET 2
#1. Using awk, sum the file sizes in your working directory and print the results.
awk ’ ,total += length ($0)-
END ‘,Print total-’ * (sameline)


#2. Write a program to check whether the given two numbers are equal. If not then find the greater of the given two numbers.
import java.lang.*;
class Comparision
{
public static void main(String args[])
{
int x,y;
x=67;
y=97;
if(x==y)
{
System.out.println(“Given two number are equal”);
}
else if(x>y)
{
System.out.println(x+ ” is greater than ” +y);
}
else
{
System.out.println(y+ ” is greater than ” +x);
}
}
}

SET 3
#1. Using switch and case statements write a program to add, subtract , multiply and divide the two numbers.
import java.io.*;
public class BasicCalc
{
public static void main(String a[])throws IOException
{
DataInputStream in = new DataInputStream(System.in);
System.out.println(“What do you want to do? \n 1. Add, \n 2. Substract, \n 3. Multiply, \n 4. Divide”);
int i = Integer.parseInt(in.readLine());
//int i=4;
int m, n;
m = 10;
n = 5;
switch (i)
{
case 1:
System.out.println(“Result after Adding” + ” ” + (m + n));
break;
case 2:
System.out.println(“Result after Substracting” + ” ” + (m – n));
break;
case 3:
System.out.println(“Result after Multiplying” + ” ” + (m * n));
break;
case 4:
System.out.println(“Result after Dividing” + ” ” + (m / n));
break;
default:
System.out.println(“This is not a valid input parameter”);
break;
}
}
}#2. Write a shell program that adds the integers 1 to 100 and displays the result with a suitable message.
s=0
c=1
while [$c-le 100]
do
s=’expr $s + $c’
c=’expr $c + 1’
done
echo “Sum of 1 to 100 is $s”SET 4
#1. Write a shell program to test for arguments arg1, arg2 and arg3. If they are not present, prompt the user for them.
if [$ # -lt 3]
then
arg=’expr $ # +1’
while [$ arg –le 3]
do
echo “Enter argument number $arg:”
read val
set $* $val
arg=’expr $arg +1’
done
fi
echo “Argument on command line entered by you are:”
echo $*
#2. Using for loop write a program to print the table of a given number on the screen.
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print(“Enter a number : “);
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}SET 5
#1. Write a program to find the even sum and the odd sum in the given array.
class EvenOddSum
{
static int sum1, sum2;
static int n[] ={ 2, 3, 4, 5, 6, 7, 92, 65, 45, 87 };
public static void main(String a[])
{
for (int i = 0; i <=n; i++)
{
if (n[i] % 2 == 0)
{
System.out.println(n[i] + ” ” + “*” + ” ” + “Even”);
sum1 = sum1 + n[i];
}
else
{
System.out.println(n[i] + ” ” + “*” + ” ” + “Odd”);
sum2 = sum2 + n[i];
}
}
System.out.println(“sum of even no. is ” + sum1);
System.out.println(“sum of odd no. is ” + sum2);
}
}

#2. Write a background command to edit all the files in the directory replacing the word while with until.
tr ‘while’ ‘until’ < myfile &SET 6
#1. Write a program to print all the prime numbers between n and m.
import java.io.*;
class PrimeNumber
{
public static void main(String[] args) throws Exception
{
int i;
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println(“Enter 1st number: “);
int num = Integer.parseInt(bf.readLine());
System.out.println(“Enter 2nd number: “);
int num2 = Integer.parseInt(bf.readLine());
System.out.println(“Prime numbers are: “);
for (i = num; i <num2; i++)
{
int j;
for (j=2; j<i; j++)
{
int n = i % j;
if (n == 0)
{
break;
}
}
if (i == j)
{
System.out.print(” ” + i);
}
}
}
}#2. Write a shell program using if-the-else to test whether a variable name is a directory or a file.
echo “Enter a name”
read name
if [-f $name]
then
echo “$name is a file”
then
echo “$name is a directory”
else
echo “$name is not a file nor a directory.”
fiSET 7
#1. Write a program to add the two matrices.
class MatrixSum
{
public static void main(String[] args)
{
int array[][]= {{4,5,6},{6,8,9}};
int array1[][]= {{5,4,6},{5,6,7}};
System.out.println(“Number of Row = ” + array.length);
System.out.println(“Number of Column = ” + array[1].length);
int l= array.length;
System.out.println(“Matrix 1 : “);
for(int i = 0; i < l; i++)
{
for(int j = 0; j <= l; j++)
{
System.out.print(” “+ array[i][j]);
}
System.out.println();
}
int m= array1.length;
System.out.println(“Matrix 2 : “);
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(” “+array1[i][j]);
}
System.out.println();
}
System.out.println(“Addition of both matrix : “);
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= m; j++)
{
System.out.print(” “+(array[i][j]+array1[i][j]));
}
System.out.println();
}
}
}



#2. Write a shell program to translate the /etc/passwd file into uppercase and translate the “:” delimiter into tab characters.
tr ”*a-z:+” “*A-Z:+” </etc/passwd

SET 8
#1. Write a class circle which consists of functions getdata() and area(), and also write a main program to create a circle object and to find the area by calling the function area () in circle class and display the result on the screen.
import java.lang.*;
import java.io.*;
class Circle
{
double r,a;
static final double p=3.14;
public void getData()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the radius of the circle :”);
r=Double.parseDouble(br.readLine());
}
catch(Exception e)
{
}
}
public void area()
{
a=p*(r*r);
System.out.print(“The area of the circle is :”+a);
}
public static void main(String args[])
{
Circle c=new Circle();
c.getData();
c.area();
}
}

#2. Write a shell program to extract and sort all the users in the file system. Use the /etc/passwd file as input.
first (with pipe and filter)
Cut –d ”:” –f1/etc/passwd | sort
Second (without pipe and filter)
Cut –d “:” –f1/etc/passwd>tmp sort<temp

SET 9
#1. Write a class Box with the variable width, depth and height and constructor to assigning the values for these variables and a method to find the volume of the box and also write the main program, which creates the box object, and find the volume of the box. Print the result on the screen.
class classBox
{
static int width, depth, height;
dimention()
{
width = 10;
depth = 5;
height = 20;
}
void volume()
{
int box = width * depth * height;
System.out.println(“volume of box is :” + ” ” + box);
}
public static void main(String args[])
{
dimention box = new dimention();
box.volume();
}
}

#2. Write an interactive shell using I/O redirection to accept input from file1, put stdout in file2 and stderr in file3.
echo “Enter input file name:”
read f1
echo “Enter output file name:”
read f2
echo “Enter error file name:”
read f3
cat <$f1 , cat >$f2 , cat >$f3

SET 10
#1. Write a program to demonstrate the method overloading for sum ( ) function.
class Demo
{
void add(int a)
{
System.out.println(a);
}
void add(int x, int y)
{
System.out.println(x + y);
}
public static void main(String args[])
{
demo q = new demo();
q.add(10);
q.add(20, 30);
}
}

#2. The ls command with the –R option lists the files in the specified directory and also subdirectories, if any. Use this with a suitable pip-and-filter arrangement to determine whether a file exists in the account logged in.
echo “Enter the name to search :”
read file
dat =’ls-R $ Home 1 grep $ file’
if [-s “$ dat”+
then
echo “$file File exist”
else
echo “$file File not exist”
fi

SET 11
#1. Write a program to demonstrate the overriding of constructor methods for the program no. 9.
class A
{
String s = “Omprakash”;
void display()
{
System.out.println(s);
}
}
class B extends A
{
void display()
{
System.out.println(s + ” ” + “Prasad”);
}
public static void main(String args[])
{
B over = new B();
over.display();
}
}

#2. Put a long-running process in the background and check the accuracy of the sleep command.
date;sleep10;date$

SET 12
#1. Write a program to concatenate two strings.
class Conct
{
public static void main(String a[])
{
String a1 = “Omprakash”;
String a2 = “Prasad”;
System.out.println(a1.concat(” ” + a2));
}
}

#2. For a directory myfolder in your working directory, do the following:
a) allow everyone to list files in myfolder. No other privileges are to be changed.
b) allow the owner to and group members to list, remove or add files. All privileges
are to be removed from everyone else.
c) give write privileges to everyone except the owner
d) allow the owner and group members to execute myfolder. Only the owner gets
read or write permission.
a)Ans:- chmod a+rx myfolder
b)Ans:-chmod 770 myfolder
c)Ans:- chmod go+w myfolder
d)Ans:- chmod 710 my folder

SET 13
#1. Write a program to find whether a given string is a palindrome or not.
class Palin
{
public static void main(String []s)
{
int i=0,j;
j=s[0].length()-1;
while(i<j)
{
if(s[0].charAt(i) != s[0].charAt(j))
break;
i++;
j–;
}
if(i<j)
System.out.println(“Not palin”);
else
System.out.println(“Palin”);
}
}

#2. For a file named myfile in the working directory, do the following:
a) give everyone permission to read myfile. No other privilege is to be changed
b) allow the owner and group members to read and write the file. All privileges are
to be removed for everyone else.
c) remove write privileges from everyone except the owner
d) allow the owner and group members to execute myfile and give only the owner
permission to read or write to it.
a)Ans :- chmod a+r myfile
b)Ans:- chmod 660 myfile
c)Ans:- chmod go-w myfile
d)Ans:- chmod 710 myfile , chmod ug=x,u+rw myfile

SET 14
#1. Write a program to change the case of the given string.
import java.io.*;
class case
{
public static void main(String ar[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println(“Enter any String”);
System.out.println(“and to quit write “ Exit“ “);
System.out.println(“\n”);
do
{
str = br.readLine();
int textLength = str.length();
for (int i = 0; i < str.length; i++)
{
char ch = Character.toUpperCase(str.charAt(i));
System.out.print(ch);
}
} while (!str.equals(“exit”));
}
}

#2. Assuming you have a directory containing the following files:
sprite, cola, choco, orange, book, lemon, lotus, apple
Use the ls command to list only those files that
a) consist of four letters
b) begin with the letter c
c) end with the letter e
d) have the second letter o
a) consist of four letters
ls ????
b) begin with the letter c
c*.*
c) end with the letter e
*e.*
d) have the second letter o
?o.*

SET 15
#1. Write a program to create a thread by extending the thread class.
import java.lang.*;
class Child extends Thread
{
Child()
{
super(“Thread Demo”);
System.out.println(“Child Thread :” +this);
start();
}
public void run()
{
try
{
for(int i = 5; i > 0; i–)
{
System.out.println(“Child Thread: ” + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(“Child interrupted.”);
}
System.out.println(“Exiting child thread.”);
}
}
class ExtendThread
{
public static void main(String args[])
{
new Child();
try
{
for(int i = 5; i > 0; i–)
{
System.out.println(“Main Thread: ” + i);
Thread.sleep(1500);
}
}
catch (InterruptedException e)
{
System.out.println(“Main thread interrupted.”);
}
System.out.println(“Main thread exiting.”);
}
}

#2. For a file named myfile in the working directory, do the following:
a) give everyone permission to read myfile. No other privilege is to be changed
b) allow the owner and group members to read and write the file. All privileges are
to be removed for everyone else.
c) remove write privileges from everyone except the owner
d) allow the owner and group members to execute myfile and give only the owner
permission to read or write to it.
a)Ans :- chmod a+r myfile
b)Ans:- chmod 660 myfile
c)Ans:- chmod go-w myfile
d)Ans:- chmod 710 myfile , chmod ug=x,u+rw myfile

SET 16
#1. Use the echo command to display the line
UNIX is fun to learn
Redirect the displayed line to a file. Append the outputs of the commands to show the users logged into your system, your login account and the current date in the dd-mm-yyyy format to the file.
echo ”UNIX is fun to learn”
script –a myfile who –a whoami date +%d-%m-%Y

#2. Using for loop write a program to print the table of a given number on the screen.
import java.util.Scanner;
public class Table
{
public static void main(String args[])
{
int num,i,res;
Scanner s = new Scanner(System.in);
System.out.print(“Enter a number : “);
num =s.nextInt();
for(i=1; i<=10; i++)
{
res=i*num;
System.out.println(res);
}
}
}


No comments: