Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label FIRST WEEK LAB PROGRAM. Show all posts
Showing posts with label FIRST WEEK LAB PROGRAM. Show all posts

Saturday, 2 February 2019

FIRST WEEK TERM WORK Program 1

1) Write a Java Program that implements a Queue using OOP concepts.

package Program1;

import java.util.*;

class Queue {
public int Q[];
public int top;
public int rear = 0;

Queue(int size) {
Q = new int[size];
top = -1;
rear = 0;
}

public void EnQueue(int item) {
if (top == Q.length - 1) {
System.out.println("Queue overflow");
} else {
Q[++top] = item;
System.out.println("EnQueue");
}
}

public int DeQueue() {
if (top < 0) {
System.out.println("Queue underflow");
return 0;
} else {
int a = Q[rear];
for (int i = 0; i < Q.length - 1; i++) {
Q[i] = Q[i + 1];
}
top--;
return a;
}
}

public void display() {
if (top < 0) {
System.out.println("Queue underflow");
}

for (int i = 0; i <= top; i++) {
System.out.println(Q[i]);
}
}

}

public class QueueOperation {

public static void main(String args[]) {
int m = 1;
Queue obj = new Queue(5);
Scanner input = new Scanner(System.in);
do {
System.out.println("QUEUE MENU");
System.out.println("Press 1 For EnQueue");
System.out.println("Press 2 For DeQueue");
System.out.println("Press 3 For Display");
System.out.println("Press 4 For Exit");
System.out.println("Enter the operation");
int Op = input.nextInt();
switch (Op) {
case 1:
System.out.print("Enter the Element u want insert");
int n = input.nextInt();
obj.EnQueue(n);
break;
case 2:
n = obj.DeQueue();
System.out.println("Element DeQueue:" + n);
break;
case 3:
obj.display();
break;
case 4:
System.out.println("Exit");
m = 0;
break;
default:
System.out.println("Enter Proper Input");
}
} while (m == 1);
}
}

FIRST WEEK TERM WORK Program 3

3) Using the concept of interface, write a Java Program that calculates and prints the areas of triangle, square and rectangle.

package Program3;

interface shapes {
abstract void print();

abstract void calc();
}

class triangle implements shapes {
public int a, b, area;

triangle(int p, int q) {
a = p;
b = q;
}

public void calc() {

area = a * b;

}

public void print() {
System.out.println("Triangle area" + area);
}

}

class rectangle implements shapes {
public int a, b, area;

rectangle(int p, int q) {
a = p;
b = q;
}

public void calc() {

area = a * b;

}

public void print() {
System.out.println("Rectangle area" + area);
}

}

class square implements shapes {
public int a, area;

square(int p) {
a = p;

}

public void calc() {

area = a * a;

}

public void print() {
System.out.println("Square area" + area);
}

}

public class ShapesInterface {
public static void main(String args[]) {
shapes obj = new triangle(5, 10);
obj.calc();
obj.print();
obj = new rectangle(5, 10);
obj.calc();
obj.print();
obj = new square(5);
obj.calc();
obj.print();
}

}

FIRST WEEK TERM WORK Program 2

2) Using the concept of abstract class and dynamic method dispatch, write a Java Program that calculates and prints the areas of triangle, square and rectangle. 

package Program2;

abstract class shapes {
abstract void print();

abstract void calc();
}

class triangle extends shapes {
public int a, b, area;

triangle(int p, int q) {
a = p;
b = q;
}

void calc() {

area = a * b;

}

void print() {
System.out.println("Triangle area" + area);
}

}

class rectangle extends shapes {
public int a, b, area;

rectangle(int p, int q) {
a = p;
b = q;
}

void calc() {

area = a * b;

}

void print() {
System.out.println("Rectangle area" + area);
}

}

class square extends shapes {
public int a, area;

square(int p) {
a = p;

}

void calc() {

area = a * a;

}

void print() {
System.out.println("Square area" + area);
}

}

public class ShapesAbstract {
public static void main(String args[]) {
shapes obj = new triangle(5, 10);
obj.calc();
obj.print();
obj = new rectangle(5, 10);
obj.calc();
obj.print();
obj = new square(5);
obj.calc();
obj.print();
}

}

FIRST WEEK TERM WORK Program 4

4) “A Person has First Name, Last Name, Mobile Number & age. An Employee has Employee ID & Salary. Also, Employee IS-A Person.” Using the concept of inheritance, write a Java Program that displays all the attributes of Employee and Person for the given problem scenario.

package Program4;

class person {
String firstname, lastname;
double mobileno, age;

person(String fname, String lname, double n, double m) {
firstname = fname;
lastname = lname;
mobileno = n;
age = m;
}

void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
}
}

class employee extends person {
double empid, Sal;

employee(String fname, String lname, double n, double m, double a, double b) {
super(fname, lname, n, m);
empid = a;
Sal = b;
}

void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
System.out.println("empid:" + empid);
System.out.println("Sal:" + Sal);

}

}

public class Test {
public static void main(String args[]) {
person obj = new person("amit", "sharma", 8660, 20);
obj.show();
employee obj1 = new employee("amit", "sharma", 8660, 20, 1, 12000);
obj1.show();
}
}

FIRST WEEK TERM WORK Program 5

5) Write a Java Program for the following problem scenario: Create a package named “BasicMath” which has a class named Basic and has methods to perform following computations: 

a. Addition of two numbers 

b. Subtraction of two numbers 

c. Multiplication of two numbers

d. Division of two numbers

 Create another package named “AdvancedMath” which has a class named Advanced and has methods to perform following computations: 

a. Find sine of an angle 

b. Find ab 

c. Find log10 of a number

 Create a class named TestDemo defined inside a default package. This class must invoke all the methods of Basic and Advanced classes.


Default Package(Main class):

import AdvanceMath.Advance;
import BasicMath.Basic;

public class TestDemo {
public static void main(String[] args) {
Basic obj = new Basic();
Advance obj1 = new Advance();
int a;
double d;
a = obj.add(4, 5);
System.out.println("Sum= " + a);
a = obj.sub(4, 5);
System.out.println("Sub= " + a);
a = obj.mult(4, 5);
System.out.println("mult= " + a);
a = obj.div(4, 5);
System.out.println("div= " + a);
d = obj1.sin(30);
System.out.println("Sin= " + d);
d = obj1.exppow(2, 3);
System.out.println("Pow= " + d);
d = obj1.log(10);
System.out.println("Log= " + d);
}

}