You are on page 1of 51

Lab Manual

for II/IV B.Tech


Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Department of Computer Science Engineering

Object Oriented Programming

course code: 15CS 2002

Academic Year 2016-17

Project based Lab


Lab cycle

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

List of programs for Lab Cycle


1
2
3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Java program to read data from user data types


Java program to convert Fahrenheit To Celsius degrees
Java program to convert Fahrenheit To Celsius degrees
and
display messages depends on the temperature
Java program to generate a multiplication table using
for- loop
Java program to declare pass or fail
Java program on application of an Array
Java program on array bubble sort
Java program with constructor
Java program on constructor over loading
Java program on inner classes
Java program on method overloading
Java program on inheritance and method overriding
Java program on inheritance and final class method
overeriding
Java program on access specifiers
Java program with two interfaces
Java program on exception handling
Java program on nested try
Java program on threads using runnable interface
Java program on threads using Thread class
Java program on files copy a file

Course
coordinator

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

1. Write a Java program to read data from user data


types
import java.util.Scanner;
class GetInputAllTypes
{
public static void main(String args[])
{
byte by;
int a;
float b;
double d;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
System.out.println("Enter an integer number below 128");
by = in.nextByte();
System.out.println("You entered integer "+by);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float number");
b = in.nextFloat();
System.out.println("You entered float "+b);
System.out.println("Enter a double number");
d = in.nextDouble();
System.out.println("You entered float "+d);
}
}

2. Write a Java program to convert Fahrenheit To


Celsius degrees.

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

3. Write a Java program to convert Fahrenheit To


Celsius degrees and display messages depends on
the temperature .
import java.util.*;
class FahrenheitToCelsiusMsg {
public static void main(String[] args) {
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperatue in Fahrenheit");
temperature = in.nextInt();
temperature = (temperature - 32)*5/9;
System.out.println("Temperature in Celsius = " + temperature);

if (temperature <= 25)


System.out.println("very cool whether");
else if(temperature>25 && temperature<35)
System.out.println("pleasant whether");

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

4. Write a Java program to generate a multiplication


table using for- loop
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}

5. Write a Java program to declare pass or fail


import java.util.Scanner;
class Student_now
{
public static void main(String args[])
{
int tot;

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

6. Java program on application of an Array


import java.util.Scanner;
class Student2
{
String name;
String rollno;
int[] sub=new int[4];
Student2(String nm, String no)
{

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

7. Java program on array bubble sort


import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;

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

8. Java program with constructor


class Box
{
double width;
double height;
double depth;
Box()
{
System.out.println("constructing box");
width=10;
height=20;
depth=15;
}
double volume()
{
return width*height*depth;

}
}
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

9. Java program on constructor over loading


class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
System.out.println("constructing box with 3 params");
width=w;
height=h;
depth=d;
}
Box(double w, double h)
{
System.out.println("constructing box with 2 params");
width=w;
height=h;
depth=10;
}
Box(double w)
{
System.out.println("constructing box with 1 param");
width=w;
height=5;
depth=10;
}
double volume()
{
return width*height*depth;
}
}
class BoxDemoCon5
{
public static void main(String args[])
{
Box mybox1= new Box(10,20,15);
Box mybox2= new Box(3,6);
Box mybox3= new Box(3);
double vol;
vol=mybox1.volume();
System.out.println(" Volume is " + vol);

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.

Java program on inner classes

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.

Java program on method overloading

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

class Dog extends Animal {


Dog() {
super();
System.out.println("A new dog has been created!");
}
//Override
void sleep() {
System.out.println("A dog sleeps...");
}
//Override
void eat() {
System.out.println("A dog eats...");
}
}
class InhDemo {
public static void main(String[] args) {
Animal animal = new Animal();
Bird bird = new Bird();
Dog dog = new Dog();
System.out.println();
animal.sleep();
animal.eat();
bird.sleep();
bird.eat();
dog.sleep();
dog.eat();

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.

Java program on access specifiers

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.

Java program with two interfaces

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.

Java program on exception handling

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.

Java program on nested try

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.

Java program on threads using Thread class

class ThreadOne extends Thread{


public void run() {
try {
for(int i=1;i<=5;i++)
System.out.println("from child thread 1: i=" +i);
Thread.sleep(600);

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.

Java program on files copy a file

/* Copy a text file.


To use this program, specify the name
of the source file and the destination file.
For example, to copy a file called FIRST.TXT
to a file called SECOND.TXT, use the following
command line.
*/
import java.io.*;
class CopyFile {
public static void main(String args[])throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17
}
}

*****************
********
***

Department of Computer Science Engineering

Object Oriented Programming

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

course code: 15CS 2002

Academic Year 2016-17

Lab Manual for Lab based Projects

II B Tech I Semester

Notes on lab based projects


Software projects in java are basically interconnection of
various small classes inturn each class is collection of variables
and methods. Every method contribute some useful output and
every class purpose is separately defined. In this way by
planning 3 to 4 classes and combining them through
inheritance or any other means makes a lab based project. The
expected behaviour from the project is called requirement, so
that all the requirements are handled in the project and they

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)

Project development life cycle :


Every project has to undergo four phases before it
is completed, namely

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

1.Requirements gathering : Collection of the data,


procedures, conditions, rules, expected outputs are
gathered.
2.Design : Before starting the coding process
necessary planning is conducted like breaking the
total projects into smaller modules and the
relations among the modules are well planned.
3.Coding : The actual programming process begins
here, each individual modules (functions/programs)
are developed
4.separately and all the programs (classes) are
combined finally to make the project.
5.Testing : Each program is tested for its validity i.e
for its output and finally the total project is also
tested to confirm its outputs.
Project views:
A project can be viewed in three ways,
namely
1. Data view : What data to be used, how the data
to be used and the relationships among data items
are thoroughly investigated.

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

2.Code view : This is pure programming, programs


are developed to work on the input data to produce
the required output according to business logic.
3. Behavioural view : This is screens designing
(front end) step, from which the data is entered
into the project and the outputs are displayed in
the user friendly nature.

What a student supposed to do :


You have to choose one project from the list of
projects given in this lab manual. Each project is
different by its nature, data, behaviour. That is the data
requirement ( what data is used for that project), the
conditions or constraints used to make judgements,
comparisons, intermediate computations/results and
the

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

On successful selection of a project, one has to


study the domain knowledge of the project with the
help of your guide, seniors or by using internet. This
step helps you in recognising the data requirement and
functional requirements for your project.
There may be individual projects or batch wise
projects

depends

on

the

department

discretion.

Whatever may be the case every student suppose to


implement.
1. Any four functional requirements of your choice
2. Any eight non functional requirements of your
choice

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

understand what are the functional and non functional

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

requirements embedded in this project. This enables


you to get ready for your project that should be
submitted at the end of semester.

Note: The above model project source code is


attached at the end of the document.

--------------------------------------------------List of Projects for the a.y 2016-17


---------------------------------------------------Project 1: super market billing system

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Project 2: tourism information system

Project 3: course selection for a +2 student system

Project 4: college selection for a +2 student system

Project 5: campus recruitment companies information


system

Project 6:

Indian states information system

Project 7:
system

your city cinema theatre information

Project 8:

your city hospital information system

Project 9: facilities at KL University information system

Project 10: cities of AP information system

Project 11: real estate information system

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Project 12:

student Counselling System

Project 13:

House Tax Billing System

Project 14:

Alumni Information System

Project 15:

Electricity Billing System

Project 16: Voting Information System

Project 17: Library Information Management System

Project 18: Hospital Management System

Project 19:

Banking System

Project 20: Doctors information system

Project 21:

Marriage Bureau management

Project 22: Hotel Management system

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Project 23:

Water Tax Billing System

Project 24: Vijayawada railway station train


information system

Project 25: Student Information System

The source code of the model project is given below.

/* This is a PBL (Project based Lab) modelproject, developed by


course team of "Object Oriented Programming" @ KL
University
course coordinator: Dr.MSR PRASAD

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Description (Domain knowledge): There are B.Tech and


M.Tech
students, Students will have name, phone number
and city as common data.B.Tech students have 3 sujects with
pass mark as 40,
where as M.Tech students have 4 subjects
with pass mark as 50.
minimum and maximum marks are 0
and 100 respectively.

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

Non functional requirements:


1. Used one data structure(Array)
2. Inheritance is implemented
3. Method overloading is used

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

4. Method overriding is used


5. static and final key words are used
6. interface is implemnted at appropriate place.
7. call by value is used
8. call by referece is used
9. Exception handling is implemented
10.Encapsulation( use of privatekey etc. word) is used
11.data validation is done
12.Menu kind of front end is used
13.proper comments are added
*/

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

/*--------------------------------PersonalDetails - base class


---------------------------------*/

class PersonalDetails{
String name;
String phoneNo;
String city;
}

/*------------------------------AcademicDetails - interface
-------------------------------*/
interface AcademicDetails{
void registration();
}

/*---------------------------------------------------------BtechStudent class - extends base class and interface


----------------------------------------------------------*/

class BtechStudent extends PersonalDetails implements


AcademicDetails{

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

BtechStudent(){
// default constructor
}

int[] sub=new int[3];


private int res;
int sum=0;

/*---------------------------------------------------------registration() - method of interface implemented


simple exception handling is implemented
data entry and validation is done
----------------------------------------------------------*/
public void registration(){
int i,j;
Scanner in = new Scanner(System.in);
try{
System.out.print("Enter name: ");
name=in.next();
System.out.print("Enter phone number: ");
phoneNo=in.next();
System.out.print("Enter city :");
city=in.next();
for(i=0; i<3; i++){

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

/*------------------------------------------------------------------passFail()- to decide pass/fail of the result for b.tech


students
-------------------------------------------------------------------*/
int passFail(){
res=1;
if ((sub[0]<40)||(sub[1]<40)||(sub[2]<40))
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

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

/*---------------------------------------------------------registration()- used for data entry for m.tech students


- overrided method from its base class
- validation for numeric data is done
----------------------------------------------------------*/
public void registration()
{
int i,j;

Lab Manual
for II/IV B.Tech
Course: Object Oriented programming (15 CS 2002) for the a.c year 2016-17

Scanner in = new Scanner(System.in);


try{
System.out.print("Enter name :");
name=in.next();
System.out.print("Enter phone number :");
phoneNo=in.next();
System.out.print("Enter city :");
city=in.next();
for(i=0; i<4; i++)
{
j=i+1;
while(true)
{
System.out.print("Enter course .." + j + " marks :");
msub[i] = in.nextInt();
if(msub[i]>=0 && msub[i]<=100)break;
else
System.out.println("Please enter b/w 0..100 range");
}
}
}catch(InputMismatchException ime)
{
System.out.println(" error ="+ime);
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

}
catch(ArrayIndexOutOfBoundsException aiobe)
{
System.out.println("array index exception");
}
}

/*------------------------------------------------------------------passFail()- to decide pass/fail of the result for M.tech


students
- an overrided method from its base class
-------------------------------------------------------------------*/

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

}
}

/**---------------------------------------------------------ResultAnalysis class - Independent class for outputs


- object arrays are used to hold students data
----------------------------------------------------------*/
final class ResultAnalysis {
/**------------------------------------------------------------------constant declaration to fix number of students...
-------------------------------------------------------------------*/
final static int NO_OF_STUDENTS=2;

/**------------------------------------------------------------------a static block executes before invoking main() method


-------------------------------------------------------------------*/
static{
System.out.println("
********************************");
System.out.println("

-- welcome to student project --

");
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
-------------------------------------------------------------------*/

BtechStudent st[] = new BtechStudent[NO_OF_STUDENTS];


MtechStudent mst[] = new MtechStudent[NO_OF_STUDENTS];
int count=0;
String topName;
int topMarks;

/**------------------------------------------------------------------void allRegistrations(int) - method for B.tech


-------------------------------------------------------------------*/

void allRegistrations(int bt){


count=0;
int i,j;

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

/**------------------------------------------------------------------void allRegistrations(double) - overloaded method for


M.tech
-------------------------------------------------------------------*/
void allRegistrations(double mt){
count=0;
int i,j;
for (i=0;i<NO_OF_STUDENTS;i++)
{
mst[i]=new MtechStudent();
count++;
j=i+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("Enter "+ j +" M.Tech student DATA.");
mst[i].registration();
System.out.println(" ");
}
}

/**------------------------------------------------------------------void result_Analysis(int) - for B.tech - number passes/fails


-------------------------------------------------------------------*/
int result_Analysis(int bt){
int porf=0;
int cp=0;
int cf=0;
int bt_cp;
int i,j;
for (i=0;i<NO_OF_STUDENTS;i++)
{
porf=st[i].passFail();
if (porf==1)
cp=cp+1;

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 ");

System.out.println(" ------------------- ");


System.out.println(" no of passes = "+cp);
System.out.println(" no of failures = "+cf);
bt_cp=cp;
return bt_cp;
}
/**------------------------------------------------------------------void result_Analysis(double) - for M.tech - an overloaded
method
-------------------------------------------------------------------*/
int result_Analysis(double mt){
int porf=0;
int cp=0;
int cf=0;
int mt_cp;
int i,j;
for (i=0;i<NO_OF_STUDENTS;i++)
{
porf=mst[i].passFail();

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 ");

System.out.println(" ------------------- ");


System.out.println(" no of passes = "+cp);
System.out.println(" no of failures = "+cf);
mt_cp=cp;
return mt_cp;
}

/**------------------------------------------------------------------void topMarksName(int) - for B.tech - gives top marks and


topper name?
-------------------------------------------------------------------*/
void topMarksName(int bt){
int big=-100;
int tr_no=0;

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

/*------------------------------------------------------------------void topMarksName(double) - for M.tech - an overloaded


method
-------------------------------------------------------------------*/
void topMarksName(double mt){
int big=-100;
int tr_no=0;
int i,j;
for (i=0;i<NO_OF_STUDENTS;i++)
{
mst[i].msum=0;
for(j=0;j<4;j++)
{
if (mst[i].msub[j]>=50)
mst[i].msum=mst[i].msum + mst[i].msub[j];
else
{
mst[i].sum=0;
break;
}
}
if (mst[i].msum > big)
{
big=mst[i].msum;
tr_no=i;

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("

Enter your choice:");

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

public static void main(String args[]){


ResultAnalysis ra=new ResultAnalysis();
go_run(ra);
}

// End of the project code

You might also like