Professional Documents
Culture Documents
II B Tech I Semester
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Course
coordinator
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
import java.util.*;
class FahrenheitToCelsius {
public static void main(String[] args) {
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit");
temperatue = in.nextInt();
temperatue = (temperature - 32)*5/9;
System.out.println("Temperature in Celsius = " + temperature);
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
else
System.out.println("too hot whether");
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
String name;
String rollno;
String res="PASS";
int sub1,sub2,sub3,sub4;
Scanner in = new Scanner(System.in);
System.out.println(" Enter name");
name=in.nextLine();
System.out.println(" Enter number");
rollno=in.nextLine();
System.out.println("Enter subject1 ");
sub1 = in.nextInt();
System.out.println("Enter subject2 ");
sub2 = in.nextInt();
System.out.println("Enter subject13 ");
sub3 = in.nextInt();
System.out.println("Enter subject4 ");
sub4 = in.nextInt();
tot=sub1+sub2+sub3+sub4;
if (sub1<40)
res="FAIL";
if (sub2<40)
res="FAIL" ;
if (sub3<40)
res="FAIL";
if (sub4<40)
res="FAIL";
System.out.println(rollno + "'s result is= "+ res);
System.out.println(rollno + "'s total is= "+ tot);
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
this.name=nm;
this.rollno=no;
}
void popu()
{
int i;
Scanner in = new Scanner(System.in);
for(i=0; i<4; i++)
{
System.out.println("Enter subject marks ");
sub[i] = in.nextInt();
}
}
int total()
{
int k,t=0;
for(k=0; k<4; k++)
{
t= t+sub[k];
}
return t;
}
void result()
{
String res="PASS";
if (sub[0]<40)
res="FAIL";
if (sub[1]<40)
res="FAIL" ;
if (sub[2]<40)
res="FAIL";
if (sub[3]<40)
res="FAIL";
System.out.println(rollno + "'s result is= "+ res);
}
}
class StudentDemo2
{
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
public static void main(String args[])
{
int tot;
Student2 st1= new Student2("Rao","12009999");
st1.popu();
tot= st1.total();
System.out.println(st1.name + "'s total marks = " +tot);
st1.result();
}
}
}
}
}
System.out.println("Sorted list of numbers");
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
}
}
class BoxDemoCon2
{
public static void main(String args[])
{
Box mybox1= new Box();
Box mybox2= new Box();
double vol;
vol=mybox1.volume();
System.out.print(" Volume is " + vol);
vol=mybox2.volume();
System.out.print(" Volume is " + vol);
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
vol=mybox2.volume();
System.out.println(" Volume is " + vol);
vol=mybox3.volume();
System.out.println(" Volume is " + vol);
}
}
10.
class Outer
{
int outer_x=100;
void test()
{
Inner inner = new Inner();
inner.display();
inner.showy();
}
class Inner
{
int y=10;
void display()
{
System.out.println("dispaly: outer_x =" + outer_x);
}
void showy()
{
System.out.println(y);
}
}
}
class InnerClassDemo1
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.test();
}
}
11.
class FunctionOverload2
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
{
int height;
FunctionOverload2()
{
System.out.println("bricks");
height = 0;
}
FunctionOverload2(int i)
{
System.out.println("Building new House that is "+ i + " feet tall");
height = i;
}
void info() {
System.out.println("House is " + height + " feet tall");
}
void info(String s) {
System.out.println(s + ": House is "+ height + " feet tall");
}
}
public class FODemo2 {
public static void main(String[] args) {
FunctionOverload2 t = new FunctionOverload2(0);
t.info();
t.info("overloaded method");
FunctionOverload2 t1 = new FunctionOverload2();
}
}
12.
Java program on inheritance and method
overriding
class Animal {
Animal() {
System.out.println("A new animal has been created!");
}
void sleep() {
System.out.println("An animal sleeps...");
}
void eat() {
System.out.println("An animal eats...");
}
void play() {
System.out.println("An animal play cricket...");
}
}
class Bird extends Animal {
Bird() {
super();
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
System.out.println("A new bird has been created!");
}
//Override
void sleep() {
System.out.println("A bird sleeps...");
}
//Override
void eat() {
System.out.println("A bird eats...");
} }
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
dog.play();
}
}
13.
Java program on inheritance and final class
method overeriding
class Animal
{
//String name;
int kk;
final int fkk=22;
Animal()
{
this.kk=2;
System.out.println("A new animal has been created!");
}
final void sleep()
{
System.out.println("An Animal sleeps for" + kk +" hours");
}
void eat()
{
System.out.println("An Animal eats" + fkk +"hours");
}
void incr()
{
kk=kk+1;
// fkk=fkk+1;
}
}
class Monkey extends Animal
{
Monkey()
{
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
System.out.println("A new monkey has been created!");
}
void sleep()
{
System.out.println("A monkey sleeps...");
}
void eat()
{
System.out.println("A monkey eats...");
}
void jump()
{
System.out.println("A monkey jumps...");
}
}
final class Human extends Monkey {
Human()
{
System.out.println("A new human has been created!");
}
/* void sleep()
{
System.out.println("A human sleeps...");
} */
void eat()
{
System.out.println("A human eats...");
}
void jump()
{
System.out.println("A human jumps...");
}
void smile()
{
System.out.println("A human smiles...");
}
}
class InhDemo4
{
public static void main(String[] args)
{
Animal animal = new Animal();
Monkey monkey = new Monkey();
Human human = new Human();
System.out.println();
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
animal.sleep();
animal.eat();
System.out.println();
animal.incr();
animal.sleep();
animal.eat();
monkey.sleep();
monkey.eat();
monkey.jump();
System.out.println();
human.sleep();
human.eat();
human.jump();
human.smile();
System.out.println();
}
}
14.
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i)
{ // set c's value
c = i;
}
int getc() {
// get c's value
return c;
}
}
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
ob.a = 10;
ob.b = 20;
//ob.c = 100;
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
15.
interface Callback
{
void cback(int param);
}
interface Hello
{
void yourName();
}
class Client implements Callback
{
public void cback(int p)
{
System.out.println("callback called with "+p);
}
void nonIfaceMeth()
{
System.out.println("classes that implements can also define other
methods");
}
}
class AnotherClient implements Callback,Hello
{
public void cback(int p)
{
System.out.println("Another version of callback ");
System.out.println("p squared is "+ p*p);
}
public void yourName()
{
System.out.println("your name is not xiomi");
}
}
class TestIface3
{
public static void main(String args[])
{
Callback c= new Client();
AnotherClient ob = new AnotherClient();
//c=ob;
c.cback(42);
ob.cback(42);
ob.yourName();
} } }
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
16.
import java.util.Random;
class ExcHandleError {
public static void main (String args[]) {
int a=0;
int b=0;
int c=0;
Random r= new Random();
for (int i=0; i<32; i++)
{
try {
b=r.nextInt();
c=r.nextInt();
a=12345/ (b/c);
} catch(ArithmeticException e) {
System.out.println("division by zero.");
a=0;
}
System.out.println("a= " +a);
}
}
}
17.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
int b = 42 / a;
System.out.println("a = " + a);
try {
if(a==1) a = a/(a-a); // division by zero
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
18.
Java program on threads using runnable
interface
ThreadChild() {
Thread t= new Thread(this,"example thread");
System.out.println("details of child thread: " +t);
t.start();
}
public void run() {
try {
for(int i=1;i<=5;i++)
System.out.println("from child thread 1: i=" +i);
Thread.sleep(600);
}catch(InterruptedException e) {
System.out.println("child thread 1 interrupted");
}
System.out.println("exit from child thread 1");
}
}
class M1threaddemo2 {
public static void main(String args[]) {
new ThreadChild();
try {
for( int m=1;m<=5;m++) {
System.out.println(" from main thread: m="+m);
Thread.sleep(1000);
}
} catch(InterruptedException e) {
System.out.println("main thread interrupted");
}
System.out.println("exit from main thread ");
}
}
19.
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}catch(InterruptedException e) {
System.out.println("child thread 1 interrupted");
}
System.out.println("exit from child thread 1");
}
}
class ThreadTwo extends Thread {
public void run() {
try {
for(int j=1;j<=5;j++)
System.out.println("from child thread 2: j=" +j);
Thread.sleep(100);
}catch(InterruptedException e) {
System.out.println("child thread 2 interrupted");
}
System.out.println("exit from child thread 2");
}
}
class ThreadThree extends Thread {
public void run() {
try {
for(int k=1;k<=5;k++)
System.out.println("from child thread 3: k=" +k);
Thread.sleep(2000);
}catch(InterruptedException e) {
System.out.println("child thread 3 interrupted");
}
System.out.println("exit from child thread 3");
}
}
class M1threademo {
public static void main(String args[]) {
ThreadOne a =new ThreadOne();
a.start();
ThreadTwo b =new ThreadTwo();
b.start();
ThreadThree c =new ThreadThree();
c.start();
try {
for( int m=1;m<=5;m++) {
System.out.println(" from main thread: m="+m);
Thread.sleep(1200);
}
} catch(InterruptedException e) {
System.out.println("main thread interrupted");
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
System.out.println("exit from main thread ");
}
}
20.
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
}
*****************
********
***
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
II B Tech I Semester
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
are all tested for quality purpose. The requirements are two
types as explained below.
1. Functional requirements: These includes how the input
is accepted by the project, intermediate results, final
computed outputs. All the results produced by the project.
Eg. They are project sensitive
2. Non-functional requirements: These includes how you
store data, process data, what techniques are to be used
to make the project. You may consider other factors like
quality, look and feel, security etc.
Eg. They are project insensitive, but basically technology
specific , are used across all the projects.
1. Use of data structures
2. Using constructor overloading
3. Use inheritance
4. Use of final key word
5. Use of abstract method
6. Use of method overloading
7. Use of method overriding
8. Use of exception handling
9. Use of encapsulation
10. Use atleast one static variable and static
method.
11. Use of interfaces
12. Use of call by reference
13. Abstract methods, classes
14. Use of menu (look and feel)
15. Use of password (security)
16. Use of data validations (quality)
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
final
outputs
combinations of inputs.
that
produced
for
specific
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
depends
on
the
department
discretion.
Model Project:
To help you in understanding the concept of lab
based project the course team will give you a model
project.
You
can
code
walk
of
this
project
and
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Project 6:
Project 7:
system
Project 8:
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Project 12:
Project 13:
Project 14:
Project 15:
Project 19:
Banking System
Project 21:
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Project 23:
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Functional requirements:
1. All students details must be avilable as personal data and
academic
data(constraint-1).
2. If anybody failed in atleast one subject result is fail,
result is pass only all subjects gets pass marks (constraint2).
3. Howmany are the total students ?
4. Howmany are passed?
5. Howmany of them are failed?
6. Who is the top scorer?
7. Howmany marks scored by the top scorer?
8. Failed student can not be top scorer (constraint-3).
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.InputMismatchException;
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
class PersonalDetails{
String name;
String phoneNo;
String city;
}
/*------------------------------AcademicDetails - interface
-------------------------------*/
interface AcademicDetails{
void registration();
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
BtechStudent(){
// default constructor
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
j=i+1;
while(true){
System.out.print("Enter course .." + j + " marks :");
sub[i] = in.nextInt();
if(sub[i]>=0 && sub[i]<=100)break;
else
System.out.println("Please enter b/w 0..100 range");
}
}
}catch(Exception e){
System.out.println(" error ="+e);
System.out.println(" problem in registration");
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
}
/*---------------------------------------------------------MtechStudent class - multi level inheritance is used
final class - this can not be furthur extended
Exception handling - try with multi catch is used
----------------------------------------------------------*/
final class MtechStudent extends BtechStudent {
MtechStudent() {
// default constructor
}
int[] msub=new int[4];
private int res;
int msum=0;
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
catch(ArrayIndexOutOfBoundsException aiobe)
{
System.out.println("array index exception");
}
}
int passFail(){
res=1;
if ((msub[0]<50)||(msub[1]<50)||(msub[2]<50)||
(msub[3]<50))
res=0;
return res;
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
}
");
System.out.println("
********************************");
System.out.println(" ");
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
/**------------------------------------------------------------------two object arrays for B.tech and M.tech students data to hold
-------------------------------------------------------------------*/
for (i=0;i<NO_OF_STUDENTS;i++)
{
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
st[i]=new BtechStudent();
count++;
j=i+1;
System.out.println(" ");
System.out.println("Enter "+ j +" B.Tech student DATA.");
st[i].registration();
System.out.println(" ");
}
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
System.out.println(" ");
System.out.println("Enter "+ j +" M.Tech student DATA.");
mst[i].registration();
System.out.println(" ");
}
}
if (porf==0)
cf=cf+1;
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ------------------- ");
System.out.println("
output ");
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
if (porf==1)
cp=cp+1;
if (porf==0)
cf=cf+1;
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ------------------- ");
System.out.println("
output ");
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
int i,j;
for (i=0;i<NO_OF_STUDENTS;i++)
{
st[i].sum=0;
for(j=0;j<3;j++)
{
if (st[i].sub[j]>=40)
st[i].sum=st[i].sum + st[i].sub[j];
else
{
st[i].sum=0;
break;
}
}
if (st[i].sum > big)
{
big=st[i].sum;
tr_no=i;
}
}
topMarks= st[tr_no].sum;
topName=st[tr_no].name;
}
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
}
topMarks= mst[tr_no].msum;
topName = mst[tr_no].name;
}
}
/*------------------------------------------------------------------StudentProject class - A demo class which has main()
method
-------------------------------------------------------------------*/
class StudentProject{
/*------------------------------------------------------------------------------go_run(ResultAnalysis ra) - a static method to pass object
reference variable
- it uses call by reference
-------------------------------------------------------------------------------*/
static void go_run(ResultAnalysis ra){
Scanner s1= new Scanner(System.in);
int choice;
/*--------------------------MENU - implementation
----------------------------*/
while(true){
System.out.println("
System.out.println(" ");
MENU");
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
System.out.println("
1. for B.tech");
System.out.println("
2. for M.tech");
System.out.println("
3. Quit");
System.out.print("
choice=s1.nextInt();
switch(choice){
case 1:
ra.allRegistrations(1);
int bt_cpp=ra.result_Analysis(1);
ra.topMarksName(1);
System.out.println(" no of students in B.tech =
"+ra.count);
System.out.println(" ------------------- ");
if (bt_cpp==0){
System.out.println(" No topper, all failures");
}
else {
System.out.print(" topper is "+ra.topName);
System.out.println(" with "+ra.topMarks+" marks");
}
System.out.println(" ------------------------------ ");
System.out.println(" ");
break;
case 2:
ra.allRegistrations(1.1);
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
int mt_cpp=ra.result_Analysis(1.1);
ra.topMarksName(1.1);
System.out.println(" No of students in M.tech =
"+ra.count);
System.out.println(" ------------------- ");
if (mt_cpp==0){
System.out.println(" No topper, all failures");
}
else {
System.out.print(" topper is "+ra.topName);
System.out.println(" with "+ra.topMarks+" marks");
}
System.out.println(" -------------------------------- ");
System.out.println(" ");
break;
case 3: System.exit(0);
//default: System.out.printf("Enter only 1 or 2\n");
default:JOptionPane.showMessageDialog(null,"Enter 1,2 or 3
only");
}
}
}
/*-----------------------------------------------main() method- Execution actually begins here
------------------------------------------------*/
Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17