javalab
javalab
1. Write a JAVA program to calculate: Add, Subtract , Multiply Divide using Switch
Case............................................................................................................................................1
2. Write a JAVA program to calculate Add two digit using parameterized
constructor................................................................................................................................3
3. Write a Java program to achieve encapsulation using private access modifier.........4
4. Write a program to write encapsulation using getter and setter.................................5
5. Write a Java Program to demonstrate “this” keyword................................................6
6. Write a Java Program to achieve abstraction using abstract class..............................7
7. Write a Java program to achieve 100% Abstraction Using Interface........................8
8. Write a java program to calculate area & perimeter of a rectangle using
inheritance................................................................................................................................9
9. Write a java program to demonstrate dynamic method dipatch in overriding....................10
1. Write a JAVA program to calculate Add, Subtract , Multiply
Divide using Switch Case.
Project Directory :
INPUT:
import java.util.Scanner;
public class App {
public static void main(String[]
args) throws Exception {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter
first digit:");
int a = sc.nextInt();
System.out.println("Enter second digit:");
int b = sc.nextInt();
System.out.println("Enter 1 for Add:");
System.out.println("Enter 2 for Sub:");
System.out.println("Enter 3 for Mul:");
System.out.println("Enter 4 for Div:");
int option = sc.nextInt();
Calculation obj = new Calculation();
switch (option) {
case 1:
int c = obj.Add(a, b);
System.out.println(c);
break;
case 2:
int d = obj.Sub(a, b);
System.out.println(d);
break;
case 3:
int e = obj.Mul(a, b);
System.out.println(e);
break;
case 4:
int f = obj.Div(a, b);
System.out.println(f);
break;
}
sc.close(); }
}
class Calculation {
public int Add(int a, int b)
{
return a + b;
1
}
public int Sub(int a, int b)
{
return a - b;
}
public int Mul(int a, int b)
{
return a * b;
}
public int Div(int a, int b)
{
return a / b;
}
}
OUTPUT:
2
2. Write a JAVA program to calculate Add two digit using
parameterized constructor.
Project Directory :
INPUT:
import java.util.Scanner;
public class App {
public static void main(String[]
args) throws Exception {
Scanner sc=new
Scanner(System.in);
System.out.println("Enter first
digit :");
int a=sc.nextInt();
System.out.println("Enter second digit :");
int b=sc.nextInt();
Rectangle rect=new Rectangle(a,b);
rect.Display();
sc.close();
}
}
class Rectangle
{
int first=0;
int second=0;
public Rectangle(int a,int b)
{
first=a;
second=b;
}
public void Display()
{
System.out.println(first+second);
}
}
OUTPUT:
3
3. Write a Java program to achieve encapsulation using private
access modifier.
Project Directory:
INPUT:
public class App {
private int length;
private int breadth;
public App(int l, int b)
{
this.length = l;
this.breadth = b;
}
public void Area() {
System.out.println(length * breadth);
}
public static void main(String[] args) {
App ap = new App(3, 4);
ap.Area();
}
}
OUTPUT :
4
4. Write a program to write encapsulation using getter and setter.
Project Directory :
INPUT:
public class App {
public static void main(String[]
args) throws Exception {
Rectangle rect=new
Rectangle();
rect.setLength(6);
rect.setBreadth(6);
System.out.println(rect.getLength()*rect.getBreadth());
}
}
class Rectangle
{
private int length;
private int breadth;
public int getLength(){
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
}
OUTPUT:
5
5. Write a Java Program to demonstrate “this” keyword.
Project Directory :
INPUT:
public class App {
int num = 10;
public App() {
System.out.println("Inside
constructor");
}
public App(int num) {
this();
this.num = num;
}
void display() {
this.show();
System.out.println(" num: "+ this.num);
}
void show() {
System.out.println("Inside show method");
}
public static void main(String[] args){
App obj = new App(100);
obj.display();
}
}
OUTPUT:
6
6. Write a Java Program to achieve abstraction using abstract
class.
Project Directory :
INPUT:
public class AbstractionExample {
public static void
main(String[] args) throws
Exception {
Shape shape = new
Circle();
shape.draw(); // Output:
Drawing Circle
}
}
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing Rectangle");
}
}
OUTPUT:
7
7. Write a Java program to achieve 100% Abstraction Using
Interface.
Project Directory :
INPUT:
public class App {
public static void main(String[] args)
throws Exception {
Circle circle=new Circle();
circle.draw();
Drawable rectangle=new
Rectangle();
rectangle.draw();
}
}
interface Drawable {
void draw();
}
class Circle implements Drawable{
public void draw(){
System.out.println("Drawing Circle");
}
}
class Rectangle implements Drawable{
public void draw(){
System.out.println("Drawing Rectangle");
}
}
OUTPUT:
8
8. Write a java program to calculate area & perimeter of a
rectangle using inheritance.
Project Directory :
INPUT:
public class App {
public static void
main(String[] args) {
Calculation cal=new
Calculation(4, 6);
System.out.println(cal.calculateArea());
System.out.println(cal.calculatePerimeter());
}
}
interface Area{
double calculateArea();
}
interface Perimeter{
double calculatePerimeter();
}
9
9. Write a java program to demonstrate dynamic method dipatch
in overriding.
Project Directory :
INPUT:
public class App {
public static void main(String[]
args) throws Exception {
}
}
class Animal{
public void eat(){
System.out.println("Eat all eatables");
}
}
class Dog extends Animal{
public void eat(){
super.eat();
System.out.println("Dog likes eating bones");
}
public void move()
{
System.out.println("Dog move");
}
public static void main(String[] args) {
Animal d=new Dog();
Dog d1=new Dog();
d.eat();
d1.move();
}
}
OUTPUT:
10
INPUT:
11
12