Professional Documents
Culture Documents
PRACTICAL ASSIGNMENTS ON
MICROPROCESSOR AND ASSEMBLY LANGUAGE PROGRAMMING,
OBJECT ORIENTED PROGRAMMING (C++ & JAVA) AND DATABASE MANAGEMENT SYSTEM
(Paper-VII)
Submitted by
Rajib Nath
TDC Part-II
Session: 2007-08
Co-processor: Installed
RAM: 256 MB
Hard disk: 40 GB
Language: Java
Compiler: Javac
78
Object Oriented Programming (Java)
Practical Assignment - 01
Write a Java program to check whether two strings are equal or not.
79
CODING:
if(args.length<2){
System.exit(1);
if(args.length>2){
System.exit(1);
else
OUTPUT:
80
Object Oriented Programming (Java)
Practical Assignment - 02
81
CODING:
import java.lang.*;
class StringRev
{ if (args.length>1)
System.exit(1);
try{ str.append(args[0]); }
catch(ArrayIndexOutOfBoundsException out)
System.exit(1);
OUTPUT:
82
Object Oriented Programming (Java)
Practical Assignment - 03
83
CODING:
//Java assignment module 03
import java.lang.*;
class SumOfDigits
{ public static void main(String args[ ])
{ if (args.length>1)
{ System.out.println("Too many parameters. Only 1 parameter expected.");
System.exit(1);
}
int num=0; int dig=0; int sum=0;
try
{ num=Integer.parseInt(args[0]); int number=num;
for(;number>0;dig=number%10,sum=sum+dig, number=number/10);
System.out.println("The sum of digits of "+num+" is "+sum);
}
catch(ArrayIndexOutOfBoundsException out)
{ System.out.println("Too few parameters. 1 parameter expected.");
System.exit(1);
}
catch(NumberFormatException numx)
{ System.out.println("Integer expected as argument.");
System.exit(1);
}
finally
{ System.out.println("Thank you.");
System.exit(1);
}
}
}
OUTPUT:
C:\Java files>java SumOfDigits
Too few parameters. 1 parameter expected.
84
Object Oriented Programming (Java)
Practical Assignment - 04
85
CODING:
OUTPUT:
86
Object Oriented Programming (Java)
Practical Assignment - 05
Write a Java program to display all prime numbers between two given numbers.
87
CODING:
//Java assignment module 05
import java.lang.*;
class ShowPrimes
{ public static void main(String args[ ])
{ try
{
if (args.length>2)
{ System.out.println("Too many parameters. Only 2 parameters expected.");
System.exit(1);
}
int num=Integer.parseInt(args[0]);
int max=Integer.parseInt(args[1]);
int factor=2;
for(;num<=max;num++)
{ for(factor=2;factor<num;factor++)
{ if(num%factor==0) break; }
if(factor==num) System.out.print(num+", ");
}
}
catch(ArrayIndexOutOfBoundsException outx)
{ System.out.println("Too few parameters. 2 parameters expected.");
System.exit(1);
}
catch(NumberFormatException numx)
{ System.out.println("Integer expected as argument.");
System.exit(1);
}
finally
{ System.out.println("\nThank you.");
System.exit(1);
}
}
}
OUTPUT:
C:\Java files>java ShowPrimes 47.32
Integer expected as argument.
89
CODING:
import java.io.*;
class sortName
int n=0;
try
n=Integer.parseInt(streamIn.readLine());}
catch(Exception e)
{ }
String temp;
try{
for (i=0;i<n;i++)
array[i]=streamIn.readLine();
catch(Exception e)
{ }
for(i=0;i<array.length;i++)
for (j=0;j<array.length-i-1;j++)
90
if(array[j].compareTo(array[j+1])>0)
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
for(i=0;i<array.length;i++)
System.out.println(array[i]);
OUTPUT:
Debojit
Tanushree
Amit
Rajeev
Amit
Debojit
Rajeev
Tanushree
91
Object Oriented Programming (Java)
Practical Assignment - 07
92
CODING:
93
System.out.print("Enter any two integer numbers: ");
int c=Integer.parseInt(buf.readLine());
int d=Integer.parseInt(buf.readLine());
System.out.printf("The max of %d and %d is %d%n",c, d, Math.max(c, d));
System.out.printf("The min of of %d and %d is %d%n",c, d, Math.min(c, d));
System.out.println("A random number: "+(int)(Math.random() * 10));
}
}
OUTPUT:
94
Object Oriented Programming (Java)
Practical Assignment - 08
95
CODING:
OUTPUT:
Contents of “MyFile.txt”
This program is for counting words and lines of text in a file.
Some text is taken from the user and entered into the file.
Then the program performs counting and displays result.
96
Object Oriented Programming (Java)
Practical Assignment - 09
Assume that a bank maintains two kinds of accounts for customers, one called as
savings account and the other as current account. The savings account provides
compound interest and withdrawal facilities but no cheque book facility. The
current account provides cheque book facility but no interest. Current account
holders should also maintain a minimum balance and if the balance falls below
this level, a service charge is imposed.
Create a class account that stores customer name, account number and type of
account. From this derive the classes’ cur_acct and sav_acct to make them more
specific to their requirements. Include necessary member functions in order to
achieve the following tasks:
¾ Accept deposit from a customer and update the balance.
¾ Display the balance.
¾ Compute and deposit interest.
¾ Permit withdrawal and update the balance.
¾ Check for the minimum balance, impose penalty, necessary and update the balance.
Include constructors for all the three classes.
97
CODING:
99
System.out.print("Or, press any key to abort withdrawal. ");
char ch;
switch(ch=inStream.readLine().charAt(0)){
case 'c':
case 'C':System.out.print("Enter cheque number: ");
inStream.readLine();
break;
case 'w':
case 'W':System.out.print("Enter withdrawal slip number: ");
inStream.readLine();
break;
default: System.out.println("Withdrawal process aborted.");
}
System.out.print("Enter the amount to be withdrawn: Rs.");
float amnt=Float.valueOf(inStream.readLine()).floatValue();
if(amnt>balance)
System.out.println("Withdrawal request denied. Account balance insufficient.");
else{
if(balance-amnt<MIN_C){
System.out.println("Remaining balance will be less than Rs."+MIN_C+", the minimum
balance. ");
System.out.println("Service charge of Rs."+SERVIC+" will be deducted from account
balance. ");
System.out.print("Continue withdrawing? (Y/N) ");
switch(ch=inStream.readLine().charAt(0)){
case 'y':
case 'Y':balance=balance-(amnt+SERVIC);
System.out.println("Rs."+amnt+" withdrawn successfully.");
break;
default: System.out.println("Withdrawal process aborted.");
}
}
else{
balance=balance-amnt;
System.out.println("Rs."+amnt+" withdrawn successfully.");
}
showBal();
}
}
public void deposit(double amnt){
balance=balance+amnt;
System.out.printf("Account balance updated to Rs. %.2f\n",balance);
}
}
public class BankService{
public static void main(String[] argArray) throws IOException{
BufferedReader inStream=new BufferedReader(new InputStreamReader(System.in));
Account acc[ ]=new Account[25];
100
int i=0; int j=0; char x=' ';
String name, acct; double amount;
System.out.println("WELCOME TO THE XYZ BANK");
do{
System.out.println("Enter your choice:");
System.out.println("O>pen a new account.");
System.out.println("L>og into an existing account.");
System.out.print("E>xit without doing anything. ");
switch(x=inStream.readLine().charAt(0)){
case 'o':
case 'O':System.out.println("Select account type: S>avings or C>urrent.”);
System.out.print(“Or, enter anything to return to main menu. ");
switch(x=inStream.readLine().charAt(0)){
case 's':
case 'S':System.out.println("At least Rs.200 should be deposited initially to
open a new savings account.");
System.out.print("Proceed? (Y/N) ");
x=inStream.readLine().charAt(0);
if(x=='n'||x=='N') break;
System.out.println("Please enter the following vital information.");
System.out.print("Enter the Customer's name: ");
name=inStream.readLine();
System.out.print("Enter the Account number: ");
acct=inStream.readLine();
System.out.print("Enter the initialising amount: Rs.");
acc[i++]=new SavAccount(name, acct, Account.MIN_S);
System.out.println("Savings account created successfully.");
break;
case 'c':
case 'C':System.out.println("At least Rs.450 should be deposited initially to
open a new current account.");
System.out.print("Proceed? (Y/N) ");
x=inStream.readLine().charAt(0);
if(x=='n'||x=='N') break;
System.out.println("Please enter the following vital information.");
System.out.print("Enter the Customer's name: ");
name=inStream.readLine();
System.out.print("Enter the Account number: ");
acct=inStream.readLine();
System.out.print("Enter the initialising amount: Rs.");
initBal=Float.parseFloat(inStream.readLine());
acc[i++]=new CurAccount(name, acct, Account.MIN_C);
System.out.println("Current account created successfully.");
break;
default: System.out.println("Main menu:");
}
break;
101
case 'l':
case 'L': if(i==0){
System.out.println("No accounts created till now.");
break;
}
System.out.print("Enter the account number to log into: ");
acct=inStream.readLine();
for(j=0;j<i && acc[j].matchAcctNo(acct)==false;j++);
if(j<=i)
do{
System.out.println("Enter your choice: ");
System.out.println("V>iew the account status.");
System.out.println("W>ithdraw from the account.");
System.out.println("D>eposit into the account.");
System.out.print("Or, enter anything to return to main menu. ");
x=inStream.readLine().charAt(0);
switch(x){
case 'v':
case 'V':acc[j].showStatus();
break;
case 'w':
case 'W':acc[j].withdraw();
break;
case 'd':
case 'D':System.out.print("Enter the amount to be deposited: Rs.");
amount=Float.parseFloat(inStream.readLine());
acc[j].deposit(amount);
break;
default: x='X';
System.out.println("Main menu:");
}
}while(x!='X');
else
System.out.println("The searched account does not exists.");
break;
default: System.out.println("Thanks for running the program.");
System.exit(0);
}
} while (true); //an infinite do-while loop
}
}
102
OUTPUT:
105
Object Oriented Programming (Java)
Practical Assignment - 10
Write a Java program to implement the use of overloading and overriding methods.
106
CODING:
//Java assignment module 10
import java.io.*;
import java.lang.Math;
class BasicPolygon{
double dim1; double dim2;
BasicPolygon(double a, double b){
dim1=a; dim2=b;
}
void areaOfShape(){
System.out.println("Area of BasicShape is undefined.");
}
}
class Triangle extends BasicPolygon{
Triangle(double a, double b){
super(a,b);
}
void areaOfShape(){
double area=dim1*dim2/2;
System.out.println("Area of triangle is "+area+" sq. units");
}
}
class Quadrilateral extends BasicPolygon{
Quadrilateral(double a, double b){
super(a,b);
}
void areaOfShape(){
double area=dim1*dim2;
System.out.println("Area of quadrilateral (rectangle) is "+area+" sq. units");
}
void areaOfShape(double dim){
double area=dim*dim;
System.out.println("Area of quadrilateral (square) is "+area+" sq. units");
}
}
public class OverDemo{
public static void main(String[ ] argArray){
BasicPolygon bp=new BasicPolygon(10,10);
Triangle tri=new Triangle(10,8); Quadrilateral quad=new Quadrilateral(9,5);
bp.areaOfShape(); tri.areaOfShape();
quad.areaOfShape(); quad.areaOfShape(15);
}
}
OUTPUT:
Area of BasicShape is undefined.
Area of triangle is 40.0 sq. units
Area of quadrilateral (rectangle) is 45.0 sq. units
Area of quadrilateral (square) is 225.0 sq. units
107
Object Oriented Programming (Java)
Practical Assignment - 11
108
CODING:
109
System.out.println("Demonstration of multiple inheritance in Java.");
System.out.println("This program outputs the sum, difference and product of two complex
numbers.");
System.out.println("Enter two complex numbers for the program to continue...");
System.out.print("Real part of first complex number: ");
try{real=Float.parseFloat(inStream.readLine());}
catch(NumberFormatException numE)
{System.out.println("Inavlid number input.");}
System.out.print("Imaginary part of first complex number: ");
try{imag=Float.parseFloat(inStream.readLine());}
catch(NumberFormatException numE)
{System.out.println("Inavlid number input.");}
Complex com1=new Complex(real,imag);
System.out.print("Real part of second complex number: ");
try{real=Float.parseFloat(inStream.readLine());}
catch(NumberFormatException numE)
{System.out.println("Inavlid number input.");}
System.out.print("Imaginary part of second complex number: ");
try{imag=Float.parseFloat(inStream.readLine());}
catch(NumberFormatException numE)
{System.out.println("Inavlid number input.");}
Complex com2=new Complex(real,imag);
System.out.println("Outputting result.");
Complex com3=new Complex();
System.out.println("Sum: "+com3.add(com1, com2));
System.out.println("Difference (first minus second): "+com3.sub(com1, com2));
System.out.println("Product: "+com3.mul(com1, com2));
System.out.println("Thanks for running the program.");
}
}
OUTPUT:
110
Object Oriented Programming (Java)
Practical Assignment - 12
Design a package to contain the class student and another package to contain the
class sports. Write a Java program to import these two packages into class test
and class result.
111
CODING:
112
System.out.print("Enter marks in second paper: ");
paper2=Integer.parseInt(inStream.readLine());
}
public void showMarks(){
showProfile();
System.out.println("Marks in paper1: "+paper1);
System.out.println("Marks in paper2: "+paper2);
}
}
class Result extends pax.Sports{
Test report;
float total;
public Result(String roll, String name, int age, float score){
super(score);
report= new Test(roll, name, age);
total=0;
}
public void getReport(){
try{
report.getMarks();
}
catch(IOException iox){
System.out.println("Error reading input from console: "+iox);
}
total=report.paper1+report.paper2+score;
}
public void showResult(){
report.showMarks();
showScore();
System.out.println("Total marks for grading: "+total);
}
}
class ReportCard{
public static void main(String[] argArray) throws IOException{
BufferedReader inStream=new BufferedReader(new InputStreamReader(System.in));
String str1, str2; int i=0;
System.out.println("This java program is for creating a student's report card.");
System.out.print("Enter the student's roll number: ");
str1=new String(inStream.readLine());
System.out.print("Enter student's name: ");
str2=new String(inStream.readLine());
System.out.print("Enter student's age: ");
i=Integer.parseInt(inStream.readLine());
System.out.print("Enter student's score in sports: ");
Result res=new Result(str1, str2, i, Float.parseFloat(inStream.readLine()));
res.getReport();
res.showResult();
System.out.println("Thanks for running the program.");
}
113
OUTPUT:
Roll number: 37
Age: 21
Marks in paper1: 53
Marks in paper2: 67
114
Object Oriented Programming (Java)
Practical Assignment - 13
Write a Java program to implement various thread methods i.e. yield( ), stop( ),
run( ), start( ) and sleep( ) methods.
115
CODING:
117
OUTPUT:
This program is to demonstrate multiple threading in Java.
Enter an integer value, preferably more than 10 : 13
Child thread: Thread[Square thread,5,main]
0 squared is 0
1 squared is 1
From main thread: 13
Child thread: Thread[Factorial thread,5,main]
0 ! =1
1 ! =1
2 ! =2
Yielding from Factorial thread
3 ! =6
4 ! =24
5 ! =120
6 ! =720
From main thread: 12
From main thread: 11
From main thread: 10
From main thread: 9
From main thread: 8
From main thread: 7
From main thread: 6
From main thread: 5
From main thread: 4
From main thread: 3
From main thread: 2
From main thread: 1
Main thread exited.
Square thread put to sleep.
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Yielding from Square thread
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
11 squared is 121
12 squared is 144
13 squared is 169
Factorial thread put to sleep.
7 ! =5040
8 ! =40320
Stopped Factorial thread
118
Object Oriented Programming (Java)
Practical Assignment - 14
119
CODING:
//Java assignment module 14
import java.lang.Runnable;
import java.lang.Thread;
import java.io.*;
class ThreadOne implements Runnable{
int times;
public ThreadOne(int val){
Thread aThread=new Thread(this, "ThreadOne");
times=val;
System.out.println("Child thread: "+aThread);
aThread.start();
}
public void run(){
while(times>=0)
System.out.println("From ThreadOne: "+times--);
}
}
public class ThreadDemo{
public static void main(String argArray[ ]) throws IOException{
BufferedReader inStream=new BufferedReader(new InputStreamReader(System.in));
System.out.println("This program is to demonstrate the implementation of Runnble interface.");
System.out.print("Enter an integer value, preferably less than 10 : ");
int value=Integer.parseInt(inStream.readLine());
int i=0;
new ThreadOne(value);
while(i<=value)
System.out.println("From main thread: "+i++);
}
}
OUTPUT:
This program is to demonstrate the implementation of Runnble interface.
Enter an integer value, preferably less than 10 : 7
Child thread: Thread[ThreadOne,5,main]
From main thread: 0
From ThreadOne: 7
From main thread: 1
From ThreadOne: 6
From main thread: 2
From ThreadOne: 5
From main thread: 3
From ThreadOne: 4
From main thread: 4
From ThreadOne: 3
From ThreadOne: 2
From ThreadOne: 1
From ThreadOne: 0
From main thread: 5
From main thread: 6
From main thread: 7
120
Object Oriented Programming (Java)
Practical Assignment - 15
121
CODING:
//Java assignment module 15
import java.io.*;
class NoMatchException extends Exception{
private String paramString;
NoMatchException(String str){
paramString=new String(str);
}
public String toString(){
return("NoMatchException: Parameter String=\""+paramString+"\"");
}
}
public class CustomException{
static String aString;
static void matchString(String param) throws NoMatchException{
System.out.println("Matching string...");
if(param.equals("Assam")==false)
throw new NoMatchException(param);
System.out.println("String matched.");
}
public static void main(String argArray[ ]){
BufferedReader inStream=new BufferedReader(new InputStreamReader(System.in));
System.out.println("This program matches your input string to the string \"Assam\".");
System.out.print("Enter a string: ");
try{
aString=new String(inStream.readLine());
}
catch(IOException ioe){
System.out.println("Error reading string from console.");
System.exit(1);
}
try{
matchString(aString);
}
catch(NoMatchException nm){
System.out.println("Exception caught: "+nm);
}
System.out.println("Thanks for running the program.");
}
}
OUTPUT:
C:\Java files>java CustomException
This program matches your input string to the string "Assam".
Enter a string: assam
Matching string...
Exception caught: NoMatchException: Parameter String="assam"
Thanks for running the program.
122
Object Oriented Programming (Java)
Practical Assignment - 16
Write a Java program to append names to a file already containing a few names.
123
CODING:
import java.io.*;
class AppendToFile{
String str1;
do{
str1=inStream.readLine();
outStream.write("\n"+str1);
}while(Character.toUpperCase(inStream.readLine().charAt(0))!='N');
outStream.close();
while((str=inStream.readLine())!=null)
System.out.println(str);
inStream.close();
124
OUTPUT:
Ajay
Rahul
Gautam
Shakti
Mridul
Rajan
Joy
Kumar
125
Object Oriented Programming (Java)
Practical Assignment - 17
126
CODING:
OUTPUT:
127
Object Oriented Programming (Java)
Practical Assignment - 18
Write a Java program to create a sequential file that could store details about
five products. Details include product code, cost and number of items available
and are provided through the keyboard.
128
CODING:
129
OUTPUT:
A18 345.98 15
V765 90.60 43
130
Object Oriented Programming (Java)
Practical Assignment - 19
Rewrite the previous program (i.e., Practical Assignment 18) using random access
file so that we can add more products to the file, if necessary.
131
CODING:
This Java program creates a random access file to store details of products.
p98 14.70 70
i65 25.80 80
y32 36.90 90
nm7 45.60 60
vb6 23.00 45
as7 56.90 14
ds8 45.36 85
133
Object Oriented Programming (Java)
Practical Assignment - 20
Write a Java program to create a TextField that allows only numeric values and
in specified length.
134
CODING:
OUTPUT:
135
Object Oriented Programming (Java)
Practical Assignment - 21
Develop an Applet that receives three numeric values as input from the user and
then displays the largest of the three on the screen. Write a HTML page and test
the applet.
136
CODING:
OUTPUT:
138
Object Oriented Programming (Java)
Practical Assignment - 22
Write a Java program to create a Frame and a CheckBox group with five
CheckBoxes, with label as Red, Green, Blue, Yellow and White. At runtime change
the background colour of the form with appropriate selection of the checkbox.
139
CODING:
140
OUTPUT:
141
Object Oriented Programming (Java)
Practical Assignment - 23
Write a Java program to create a Frame with three Scrolls, change the background
colour of the Frame using RGB function with values of scrolls.
142
CODING:
OUTPUT:
143
Object Oriented Programming (Java)
Practical Assignment - 24
Write a Java program to create a Frame, Choice and Label. Add five items in the
choice. At runtime display the selected items of choice in the label.
144
CODING:
import java.awt.*;
import java.awt.event.*;
Label lb;
Choice Chooser;
public ChoiceFrame(){
super("Option selector");
Chooser.add("One");
Chooser.add("Two");
Chooser.add("Three");
Chooser.add("Four");
Chooser.add("Five");
add(Chooser);
add(lb);
Chooser.addItemListener(this);
addWindowListener(new WindowAdapter(){
System.exit(0);
});
lb.setText(Chooser.getSelectedItem());
app.setSize(250,200); app.setVisible(true);
145
OUTPUT:
146
PRACTICAL ASSIGNMENTS
Co-processor: Installed
RAM: 256 MB
Hard disk: 40 GB
147
Assignment # 1
Query:
SQL> create table category_header(cat_code number(5),cat_desc varchar2(20));
Result:
SQL> desc category_header;
Name Null? Type
----------------------------------------- -------- ----------------------------
CAT_CODE NUMBER(5)
CAT_DESC VARCHAR2(20)
Assignment # 2
Route_id number (5), route_no number (5), cat_code number (5), origin varchar2 (20), Destination
varchar2 (20), destination varchar2 (20), fare number (7, 2), distance number (3), capacity number (3).
Query:
SQL> create table route_header (route_id number(5), route_no number(5), cat_code
number(5), origin varchar2(20), destination varchar2(20), frame number(7,2),
distance number(3), capacity number(3));
Table created.
Result:
ROUTE_ID NUMBER(5)
ROUTE_NO NUMBER(5)
CAT_CODE NUMBER(5)
ORIGIN VARCHAR2(20)
DESTINATION VARCHAR2(20)
FRAME NUMBER(7,2)
DISTANCE NUMBER(3)
CAPACITY NUMBER(3)
148
Assignment # 3
Create table place_header whose column definitions are provided in the table below. Place_id number
(5), place_name varchar2 (20), place_address varchar2 (20), bus_station varchar2 (10).
Query:
SQL> create table place_header(place_id number(5), place_name varchar2(20),
place_address varchar2(30), bus_station varchar2(10));
Table created.
Result:
SQL> desc place_header;
Name Null? Type
----------------------------------------- -------- ----------------------------
PLACE_ID NUMBER(5)
PLACE_NAME VARCHAR2(20)
PLACE_ADDRESS VARCHAR2(30)
BUS_STATION VARCHAR2(10)
Assignment # 4
Fleet_id number (5), day date, route_id number (5), cat_code number (5)
Query:
SQL> create table fleet_header(fleet_id number(5),day date,route_id
number(5),cat_code number(5));
Table created.
Result:
SQL> desc fleet_header;
Name Null? Type
----------------------------------------- -------- ----------------------------
FLEET_ID NUMBER(5)
DAY DATE
ROUTE_ID NUMBER(5)
CAT_CODE NUMBER(5)
149
Assignment # 5
Create table ticket_header with the given columns:
Fleet_id number (5), ticket_no number (5), doi date, dot date, time_travel char (8), board_place
varchar2 (20), origin varchar2 (20), destination varchar2 (20), adults number (3), children number (3),
total_fare number (7,2), route_id number (3)
Query:
SQL> create table ticket_header(fleet_id number(5), ticket_no number(5), doi date,
dot date, time_travel char(8), board_place varchar2(20), origin varchar2(20),
destination varchar2(20), adults number(3), children number(3), total_fare
number(7,2), route_id number(5));
Table created.
Result:
Assignment # 6
Create table ticket_detail, the structure of which is given in the table below.
Ticket_no number (5), name varchar2 (20), sex char (1), age number (3), fare number (5, 2)
Query:
SQL> create table ticket_detail(ticket_no number(5),name varchar2(20),sex
char(1),age number(3),fare number(5,2));
Table created.
Result:
SQL> desc ticket_detail;
Name Null? Type
----------------------------------------- -------- ----------------------------
TICKET_NO NUMBER(5)
NAME VARCHAR2(20)
SEX CHAR(1)
AGE NUMBER(3)
FARE NUMBER(5,2)
150
Assignment # 7
Insert into the table category_header the following values:
Cat_code Cat_desc
01 Super deluxe
02 Deluxe
03 Super fast
04 Normal
Query:
1 row created.
SQL> /
Enter value for cat_code: 02
Enter value for cat_desc: Deluxe
old 1: insert into category_header values (&CAT_CODE, '&CAT_DESC')
new 1: insert into category_header values (02, 'Deluxe')
1 row created.
SQL> /
Enter value for cat_code: 03
Enter value for cat_desc: Super fast
old 1: insert into category_header values (&CAT_CODE, '&CAT_DESC')
new 1: insert into category_header values (03, 'Super fast')
1 row created.
SQL> /
Enter value for cat_code: 04
Enter value for cat_desc: Normal
old 1: insert into category_header values (&CAT_CODE, '&CAT_DESC')
new 1: insert into category_header values (04, 'Normal')
1 row created.
Result:
SQL> select * from category_header;
CAT_CODE CAT_DESC
---------- --------------------
1 Super deluxe
2 Deluxe
3 Super fast
4 Normal
151
Assignment # 8
Query:
1 row created.
SQL> /
Enter value for route_id: 102
Enter value for route_no: 25
Enter value for cat_code: 02
Enter value for origin: Trichy
Enter value for destination: Madurai
Enter value for fare: 40
Enter value for distance: 159
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (102, 25, 02, 'Trichy', 'Madurai', 40,
159, 50)
1 row created.
SQL> /
Enter value for route_id: 103
Enter value for route_no: 15
Enter value for cat_code: 03
Enter value for origin: Thanjavur
Enter value for destination: Madurai
Enter value for fare: 59
152
Enter value for distance: 140
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (103, 15, 03, 'Thanjavur', 'Madurai', 59,
140, 50)
1 row created.
SQL> /
Enter value for route_id: 104
Enter value for route_no: 36
Enter value for cat_code: 04
Enter value for origin: Madras
Enter value for destination: Bangalore
Enter value for fare: 79
Enter value for distance: 375
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (104, 36, 04, 'Madras', 'Bangalore', 79,
375, 50)
1 row created.
SQL> /
Enter value for route_id: 105
Enter value for route_no: 40
Enter value for cat_code: 01
Enter value for origin: Bangalore
Enter value for destination: Madras
Enter value for fare: 80
Enter value for distance: 375
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (105, 40, 01, 'Bangalore', 'Madras', 80,
375, 50)
1 row created.
SQL> /
Enter value for route_id: 106
Enter value for route_no: 38
Enter value for cat_code: 02
Enter value for origin: Madras
Enter value for destination: Madurai
Enter value for fare: 39
Enter value for distance: 250
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (106, 38, 02, 'Madras', 'Madurai', 39,
250, 50)
1 row created.
153
SQL> /
Enter value for route_id: 107
Enter value for route_no: 39
Enter value for cat_code: 03
Enter value for origin: Hyderabad
Enter value for destination: Madras
Enter value for fare: 50
Enter value for distance: 430
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (107, 39, 03, 'Hyderabad', 'Madras', 50,
430, 50)
1 row created.
SQL> /
Enter value for route_id: 108
Enter value for route_no: 41
Enter value for cat_code: 04
Enter value for origin: Madras
Enter value for destination: Cochin
Enter value for fare: 47
Enter value for distance: 576
Enter value for capacity: 50
old 1: insert into route_header values (&ROUTE_ID, &ROUTE_NO, &CAT_CODE,
'&ORIGIN', '&DESTINATION', &FARE,
new 1: insert into route_header values (108, 41, 04, 'Madras', 'Cochin', 47,
576, 50)
1 row created.
Result:
SQL> select * from route_header;
8 rows selected.
154
Assignment # 9
Insert into the place_header the following values:
Query:
1 row created.
SQL> /
Enter value for place_id: 02
Enter value for place_name: Madurai
Enter value for place_address: 21, canalbank road
Enter value for bus_station: Kknagar
old 1: insert into place_header values (&PLACE_ID, '&PLACE_NAME',
'&PLACE_ADDRESS', '&BUS_STATION')
new 1: insert into place_header values (02, 'Madurai', '21, canalbank road',
'Kknagar')
1 row created.
SQL> /
Enter value for place_id: 03
Enter value for place_name: Trichy
Enter value for place_address: 11, first cross road
Enter value for bus_station: Bheltown
old 1: insert into place_header values (&PLACE_ID, '&PLACE_NAME',
'&PLACE_ADDRESS', '&BUS_STATION')
new 1: insert into place_header values (03, 'Trichy', '11, first cross road',
'Bheltown')
1 row created.
155
SQL> /
Enter value for place_id: 04
Enter value for place_name: Bangalore
Enter value for place_address: 15, firstmainroad
Enter value for bus_station: Cubbonpark
old 1: insert into place_header values (&PLACE_ID, '&PLACE_NAME',
'&PLACE_ADDRESS', '&BUS_STATION')
new 1: insert into place_header values (04, 'Bangalore', '15, firstmainroad',
'Cubbonpark')
1 row created.
SQL> /
Enter value for place_id: 05
Enter value for place_name: Hyderabad
Enter value for place_address: 115, lakeview road
Enter value for bus_station: Charminar
old 1: insert into place_header values (&PLACE_ID, '&PLACE_NAME',
'&PLACE_ADDRESS', '&BUS_STATION')
new 1: insert into place_header values (05, 'Hyderabad', '115, lakeview road',
'Charminar')
1 row created.
SQL> /
Enter value for place_id: 06
Enter value for place_name: Thanjavur
Enter value for place_address: 12, temple road
Enter value for bus_station: Railway jn
old 1: insert into place_header values (&PLACE_ID, '&PLACE_NAME',
'&PLACE_ADDRESS', '&BUS_STATION')
new 1: insert into place_header values (06, 'Thanjavur', '12, temple road',
'Railway jn')
1 row created.
Result:
6 rows selected.
156
Assignment # 10
01 10-apr-96 101 01
02 10-apr-96 101 01
03 10-apr-96 101 01
04 10-apr-96 102 02
05 10-apr-96 102 03
06 10-apr-96 103 04
Query:
SQL> insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE);
Enter value for fleet_id: 01
Enter value for day: 10-apr-96
Enter value for route_id: 101
Enter value for cat_code: 01
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(01, '10-apr-96', 101, 01)
1 row created.
SQL> /
Enter value for fleet_id: 02
Enter value for day: 10-apr-96
Enter value for route_id: 101
Enter value for cat_code: 01
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(02, '10-apr-96', 101, 01)
1 row created.
SQL> /
Enter value for fleet_id: 03
Enter value for day: 10-apr-96
Enter value for route_id: 101
Enter value for cat_code: 01
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(03, '10-apr-96', 101, 01)
1 row created.
157
SQL> /
Enter value for fleet_id: 04
Enter value for day: 10-apr-96
Enter value for route_id: 102
Enter value for cat_code: 02
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(04, '10-apr-96', 102, 02)
1 row created.
SQL> /
Enter value for fleet_id: 05
Enter value for day: 10-apr-96
Enter value for route_id: 102
Enter value for cat_code: 03
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(05, '10-apr-96', 102, 03)
1 row created.
SQL> /
Enter value for fleet_id: 06
Enter value for day: 10-apr-96
Enter value for route_id: 103
Enter value for cat_code: 04
old 1: insert into fleet_header values(&FLEET_ID, '&DAY', &ROUTE_ID, &CAT_CODE)
new 1: insert into fleet_header values(06, '10-apr-96', 103, 04)
1 row created.
Result:
SQL> select * from fleet_header;
6 rows selected.
158
Assignment # 11
Query:
1 row created.
SQL> /
Enter value for fleet_id: 02
Enter value for ticket_no: 02
Enter value for doi: 12-apr-96
Enter value for dot: 10-may-96
Enter value for time_travel: 09:00:00
Enter value for board_place: Kknagar
Enter value for origin: Madurai
Enter value for destination: Madras
Enter value for adults: 2
Enter value for children: 1
Enter value for total_fare: 60
Enter value for route_id: 102
159
old 1: insert into ticket_header values (&FLEET_ID, &TICKET_NO, '&DOI', '&DOT',
'&TIME_TRAVEL', '&BOARD_PLAC
new 1: insert into ticket_header values (02, 02, '12-apr-96', '10-may-96',
'09:00:00', 'Kknagar', 'Madurai',
1 row created.
SQL> /
Enter value for fleet_id: 03
Enter value for ticket_no: 03
Enter value for doi: 21-apr-96
Enter value for dot: 10-may-96
Enter value for time_travel: 20:00:00
Enter value for board_place: Cubbonpark
Enter value for origin: Bangalore
Enter value for destination: Madras
Enter value for adults: 4
Enter value for children: 2
Enter value for total_fare: 400
Enter value for route_id: 101
old 1: insert into ticket_header values (&FLEET_ID, &TICKET_NO, '&DOI', '&DOT',
'&TIME_TRAVEL', '&BOARD_PLAC
new 1: insert into ticket_header values (03, 03, '21-apr-96', '10-may-96',
'20:00:00', 'Cubbonpark', 'Bangal
1 row created.
SQL> /
Enter value for fleet_id: 04
Enter value for ticket_no: 04
Enter value for doi: 12-apr-96
Enter value for dot: 10-may-96
Enter value for time_travel: 10:00:00
Enter value for board_place: Charminar
Enter value for origin: Hyderabad
Enter value for destination: Madras
Enter value for adults: 10
Enter value for children: 0
Enter value for total_fare: 500
Enter value for route_id: 103
old 1: insert into ticket_header values (&FLEET_ID, &TICKET_NO, '&DOI', '&DOT',
'&TIME_TRAVEL', '&BOARD_PLAC
new 1: insert into ticket_header values (04, 04, '12-apr-96', '10-may-96',
'10:00:00', 'Charminar', 'Hyderab
1 row created.
SQL> /
Enter value for fleet_id: 05
Enter value for ticket_no: 250
Enter value for doi: 10-apr-96
Enter value for dot: 10-may-96
Enter value for time_travel: 15:00:00
Enter value for board_place: Parrys
Enter value for origin: Madras
Enter value for destination: Cochin
Enter value for adults: 2
Enter value for children: 2
Enter value for total_fare: 141
Enter value for route_id: 103
old 1: insert into ticket_header values (&FLEET_ID, &TICKET_NO, '&DOI', '&DOT',
'&TIME_TRAVEL', '&BOARD_PLAC
new 1: insert into ticket_header values (05, 250, '10-apr-96', '10-may-96',
'15:00:00', 'Parrys', 'Madras',
1 row created.
160
Result:
SQL> select * from ticket_header;
FLEET_ID TICKET_NO DOI DOT TIME_TRA BOARD_PLACE ORIGIN DESTINATION ADULTS CHILDREN TOTAL_FARE ROUTE_ID
---------- ---------- --------- --------- -------- -------------- -------------- -------------- ------- -------- ---------- ----------
1 1 10-APR-96 10-MAY-96 15:00:00 Parrys Madras Madurai 1 1 60 101
2 2 12-APR-96 10-MAY-96 09:00:00 Kknagar Madurai Madras 2 1 60 102
3 3 21-APR-96 10-MAY-96 20:00:00 Cubbonpark Bangalore Madras 4 2 400 101
4 4 12-APR-96 10-MAY-96 10:00:00 Charminar Hyderabad Madras 10 0 500 103
5 250 10-APR-96 10-MAY-96 15:00:00 Parrys Madras Cochin 2 2 141 103
161
Assignment # 12
01 Charu F 24 14.00
01 Latha F 10 15.55
02 Anand M 28 17.80
02 Gautham M 28 16.00
02 Bala M 9 17.65
05 sandeep M 30 18.00
Query:
SQL> insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE, &FARE);
Enter value for ticket_no: 01
Enter value for name: Charu
Enter value for sex: F
Enter value for age: 24
Enter value for fare: 14.00
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (01, 'Charu', 'F', 24, 14.00)
1 row created.
SQL> /
Enter value for ticket_no: 01
Enter value for name: Latha
Enter value for sex: F
Enter value for age: 10
Enter value for fare: 15.55
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (01, 'Latha', 'F', 10, 15.55)
1 row created.
SQL> /
Enter value for ticket_no: 02
Enter value for name: Anand
Enter value for sex: M
Enter value for age: 28
Enter value for fare: 17.80
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (02, 'Anand', 'M', 28, 17.80)
1 row created.
162
SQL> /
Enter value for ticket_no: 02
Enter value for name: Gautham
Enter value for sex: M
Enter value for age: 28
Enter value for fare: 16.00
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (02, 'Gautham', 'M', 28, 16.00)
1 row created.
SQL> /
Enter value for ticket_no: 02
Enter value for name: Bala
Enter value for sex: M
Enter value for age: 9
Enter value for fare: 17.65
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (02, 'Bala', 'M', 9, 17.65)
1 row created.
SQL> /
Enter value for ticket_no: 05
Enter value for name: Sandeep
Enter value for sex: M
Enter value for age: 30
Enter value for fare: 18.00
old 1: insert into ticket_detail values (&TICKET_NO, '&NAME', '&SEX', &AGE,
&FARE)
new 1: insert into ticket_detail values (05, 'Sandeep', 'M', 30, 18.00)
1 row created.
Result:
6 rows selected.
163
Assignment # 13
Alter the table route_header to add a column comments with datatype as long.
Query:
Table altered.
Result:
ROUTE_ID NUMBER(5)
ROUTE_NO NUMBER(5)
CAT_CODE NUMBER(5)
ORIGIN VARCHAR2(20)
DESTINATION VARCHAR2(20)
FARE NUMBER(7,2)
DISTANCE NUMBER(3)
CAPACITY NUMBER(3)
COMMENTS LONG
Assignment # 14
Describe route_header
Query:
SQL> desc route_header;
Result:
Name Null? Type
----------------------------------------- -------- ----------------------------
ROUTE_ID NUMBER(5)
ROUTE_NO NUMBER(5)
CAT_CODE NUMBER(5)
ORIGIN VARCHAR2(20)
DESTINATION VARCHAR2(20)
FARE NUMBER(7,2)
DISTANCE NUMBER(3)
CAPACITY NUMBER(3)
COMMENTS LONG
164
Assignment # 15
Display only distinct category code from the table route_header order in the descending manner.
Query:
SQL> select distinct cat_code from route_header order by cat_code desc;
Result:
CAT_CODE
----------
4
3
2
1
Assignment # 16
Alter the table to modify the length of the column distance in the table route_header to 4.
Query:
SQL> alter table route_header modify distance number(4);
Table altered.
Result:
SQL> desc route_header;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROUTE_ID NUMBER(5)
ROUTE_NO NUMBER(5)
CAT_CODE NUMBER(5)
ORIGIN VARCHAR2(20)
DESTINATION VARCHAR2(20)
FRAME NUMBER(7,2)
DISTANCE NUMBER(4)
CAPACITY NUMBER(3)
165
Assignment # 17
Create table route_detail – route_id number (5), place_id number (5), nonstop char (1).
Query:
SQL> create table route_detail (route_id number(5),place_id number(5),nonstop
char(1));
Table created.
Result:
SQL> desc route_detail;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROUTE_ID NUMBER(5)
PLACE_ID NUMBER(5)
NONSTOP CHAR(1)
Assignment # 18
105 01 n
102 02 s
106 01 s
108 05 n
Query:
SQL> insert into route_detail values (&NUMBER, &PLACE_ID, '&NONSTOP');
Enter value for number: 105
Enter value for place_id: 01
Enter value for nonstop: n
old 1: insert into route_detail values (&NUMBER, &PLACE_ID, '&NONSTOP')
new 1: insert into route_detail values (105, 01, 'n')
1 row created.
166
SQL> /
Enter value for number: 102
Enter value for place_id: 02
Enter value for nonstop: s
old 1: insert into route_detail values (&NUMBER, &PLACE_ID, '&NONSTOP')
new 1: insert into route_detail values (102, 02, 's')
1 row created.
SQL> /
Enter value for number: 106
Enter value for place_id: 01
Enter value for nonstop: s
old 1: insert into route_detail values (&NUMBER, &PLACE_ID, '&NONSTOP')
new 1: insert into route_detail values (106, 01, 's')
1 row created.
SQL> /
Enter value for number: 108
Enter value for place_id: 05
Enter value for nonstop: n
old 1: insert into route_detail values (&NUMBER, &PLACE_ID, '&NONSTOP')
new 1: insert into route_detail values (108, 05, 'n')
1 row created.
Result:
SQL> select * from route_detail;
ROUTE_ID PLACE_ID N
---------- ---------- -
105 1 n
102 2 s
106 1 s
108 5 n
167
Assignment # 19
Insert into the above table the following values: 106, 02, n.
Query:
1 row created.
Result:
ROUTE_ID PLACE_ID N
---------- ---------- -
105 1 n
102 2 s
106 1 s
108 5 n
106 2 n
Assignment # 20
Change the nonstop to “N”, of the route_detail where the route_id is 102
Query:
SQL> update route_detail set nonstop='n' where route_id=102;
1 row updated.
Result:
SQL> select * from route_detail;
ROUTE_ID PLACE_ID N
---------- ---------- -
105 1 n
102 2 n
106 1 s
108 5 n
106 2 n
168
Assignment # 21
Query:
SQL> delete from route_detail where route_id=102;
1 row deleted.
Result:
SQL> select * from route_detail;
ROUTE_ID PLACE_ID N
---------- ---------- -
105 1 n
106 1 s
108 5 n
106 2 n
Assignment # 22
Display only those routes that originates in madras and terminate at cochin
Query:
SQL> select * from route_header where origin='Madras' and destination='Cochin';
Result:
169
Assignment # 23
Update the table route_header to set the distance between madras and madurai to be 300.
Query:
SQL> update route_header set distance=300 where origin in ('Madras', 'Madurai') and
destination in (
'Madras', 'Madurai');
2 rows updated.
Result
SQL> select * from route_header;
8 rows selected.
Assignment # 24
Delete only those rows that have the destination as Bangalore and the origin as madras.
Query:
SQL> delete from route_header where origin='Madras' and destination='Bangalore';
1 row deleted.
Result:
SQL> select * from route_header;
7 rows selected.
170
Assignment # 25
Display only those rows from route_header whose origin begins with ‘M’.
Query:
SQL> select * from route_header where origin like 'M%';
Result:
SQL> select * from route_header;
Assignment # 26
Display only those rows whose fare ranges between 30 and 50.
Query:
SQL> select * from route_header where frame between 30 and 50;
Result:
SQL> select * from route_header;
5 rows selected.
171
Assignment # 27
Display the fare and the origin for those route_no which are greater than 15.
Query:
SQL> select frame, origin from route_header where route_no>15;
Result:
FRAME ORIGIN
---------- --------------------
35 Madurai
40 Trichy
80 Bangalore
39 Madras
50 Hyderabad
47 Madras
6 rows selected.
Assignment # 28
Query:
SQL> select * from place_header where place_name like 'M%';
Result:
PLACE_ID PLACE_NAME PLACE_ADDRESS BUS_STATIO
---------- -------------------- ------------------------------ ----------
1 Madras 10, ptc road Parrys
2 Madurai 21, canalbank road Kknagar
Assignment # 29
Query:
SQL> select * from place_header where place_name like 'A%';
Result:
no rows selected
172
Assignment # 30
Create pseudo table with the same structure as the table category_header and insert rows into the
Query:
SQL> create table pseudo as(select * from category_header);
Table created.
Result:
SQL> select * from category_header;
CAT_CODE CAT_DESC
---------- --------------------
1 Super deluxe
2 Deluxe
3 Super fast
4 Normal
Assignment # 31
Display the date two months after the date of issue of the ticket
Query:
SQL> select add_months (doi, 2) from ticket_header;
Result:
ADD_MONTH
---------
10-JUN-96
12-JUN-96
21-JUN-96
12-JUN-96
10-JUN-96
173
Assignment # 32
Display only those place names that begin with ‘M’ and replace them with ‘V’.
Query:
SQL> select translate (place_name, 'M', 'V') from place_header where place_name
like 'M%';
Result:
TRANSLATE(PLACE_NAME
--------------------
Vadras
Vadurai
Assignment # 33
Display only 3 characters from the third character in the names of the traveler.
Query:
SQL> select substr (name, 3, 3) from ticket_detail;
Result:
SUB
---
aru
tha
and
uth
la
nde
6 rows selected.
174
Assignment # 34
Query:
SQL> (select ticket_no from ticket_header) union all(select ticket_no from
ticket_detail);
Result:
TICKET_NO
----------
1
2
3
4
250
1
1
2
2
2
5
11 rows selected.
Assignment # 35
Select only the common fleet_ids that are present in the fleet_header and ticket_header.
Query:
SQL> (select fleet_id from fleet_header) intersect (select fleet_id from
ticket_header);
Result:
FLEET_ID
----------
1
2
3
4
5
175
Assignment # 36
Select row from ticket_detail such that the ticket numbers exceed the average of the total fare and the
origin is ‘Madras’.
Query:
SQL> select * from ticket_detail where ticket_no>all (select avg (total_fare) from
ticket_header where origin= 'Madras');
Result:
no rows selected
176