0% found this document useful (0 votes)
11 views13 pages

Java Programs - 9-16

The document contains Java programming examples demonstrating key Object-Oriented Programming concepts such as Inheritance, Method Overriding, Polymorphism, Abstraction, and Encapsulation. It also includes examples of control flow statements and method usage, culminating in a simple console calculator program. Each example is structured with class definitions, methods, and a main function to illustrate the concepts effectively.

Uploaded by

24110cn336
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Java Programs - 9-16

The document contains Java programming examples demonstrating key Object-Oriented Programming concepts such as Inheritance, Method Overriding, Polymorphism, Abstraction, and Encapsulation. It also includes examples of control flow statements and method usage, culminating in a simple console calculator program. Each example is structured with class definitions, methods, and a main function to illustrate the concepts effectively.

Uploaded by

24110cn336
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS with JAVA Programming 9-16 programs

9. write a java program to demonstrate Inheritance (Single Inheritance). Student details

// Parent class

class Person {

String name;

int age;

void displayPerson() {

[Link]("Name: " + name);

[Link]("Age: " + age);

// Child class

class Student extends Person {

int rollNo;

void displayStudent() {

[Link]("Roll No: " + rollNo);

// Main class

public class InheritanceDemo {

public static void main(String[] args) {

Student s = new Student();

[Link] = "XYZ";
[Link] = 20;

[Link] = 101;

[Link]();

[Link]();

10: write a program to demonstrate Method Overriding (Runtime Polymorphism) for


Shape → Circle

// Parent class
class Shape {
void draw() {
[Link]("Drawing a shape");
}
}

// Child class
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing a circle");
}
}

// Main class
public class PolymorphismDemo {
public static void main(String[] args) {
Shape s; // reference of parent
s = new Circle(); // object of child

[Link](); // calls Circle's draw()


}
}

11. write a program to perfor Polymorphism with Multiple Child Classes

class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


void sound() {
[Link]("Dog barks");
}
}

class Cat extends Animal {


void sound() {
[Link]("Cat meows");
}
}

public class AnimalDemo {


public static void main(String[] args) {
Animal a;

a = new Dog();
[Link]();

a = new Cat();
[Link]();
}
}

12. write a program for Abstraction (Using Abstract Class) for Shape → Circle &
Rectangle

// Abstract class
abstract class Shape {
abstract void draw(); // abstract method

void message() { // concrete method


[Link]("This is a shape");
}
}

// Child class 1
class Circle extends Shape {
void draw() {
[Link]("Drawing a Circle");
}
}
// Child class 2
class Rectangle extends Shape {
void draw() {
[Link]("Drawing a Rectangle");
}
}

// Main class
public class AbstractionDemo {
public static void main(String[] args) {
Shape s;

s = new Circle();
[Link]();
[Link]();

s = new Rectangle();
[Link]();
}
13. Write a Java Program for Encapsulation student class
class Student {
// private data members
private String name;
private int age;

// public setter methods


public void setName(String name) {
[Link] = name;
}

public void setAge(int age) {


[Link] = age;
}

// public getter methods


public String getName() {
return name;
}

public int getAge() {


return age;
}
}

public class EncapsulationDemo {


public static void main(String[] args) {
Student s = new Student();

// setting values using setters


[Link]("Kavi");
[Link](20);

// accessing values using getters


[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}

14. Write a program to demonstrate switch case, if, if-else and for loop

// Java program to illustrate If statement

import [Link].*;

class IfDemo {

public static void main(String args[])

int i = 10;

if (i < 15)

[Link]("Inside If block");

[Link]("10 is less than 15");

// This statement will be executed

// as if considers one statement by default

[Link]("I am Not in if");

Output
Inside If block
10 is less than 15
I am Not in if

15. Write a program to demonstrate the working of methods.

class A {

private int i;

protected int j;

public int m;

int n;

void set(int i){

this.i=i;

void showDataMember() {

[Link]("i,j, m and n: " + i + " " + j +" "+m+" " +n);

class B extends A {

int k;

void showk() {

[Link]("k: " + k);

void sum() {
[Link]("j+k: " + (j+k));

class SimpleInheritance {

public static void main(String args[]) {

A superOb = new A();

B subOb = new B();

[Link](10);

//superOb.i = 10;

superOb.j = 20;

[Link]("Contents of superOb: ");

[Link]();

[Link]();

[Link](7);

//subOb.i = 7;

subOb.j = 8;class Main {

// create a method

public int addNumbers(int a, int b) {

int sum = a + b;

// return value

return sum;

}
public static void main(String[] args) {

int num1 = 25;

int num2 = 15;

// create an object of Main

Main obj = new Main();

// calling method

int result = [Link](num1, num2);

[Link]("Sum is: " + result);

Run Code

Output

Sum is: 40

subOb.k = 9;

[Link]("Contents of subOb: ");

[Link]();

[Link]();

[Link]();

[Link]("Sum of i, j and k in subOb:");


[Link]();

16. Write a program which has four methods – add(), subtract(), multiply() and divide() and
demonstrate a simple console calculator.

import [Link];

class Main {

public static void main(String[] args) {

char operator;

Double number1, number2, result;

// create an object of Scanner class

Scanner input = new Scanner([Link]);

// ask users to enter operator

[Link]("Choose an operator: +, -, *, or /");

operator = [Link]().charAt(0);

// ask users to enter numbers

[Link]("Enter first number");

number1 = [Link]();

[Link]("Enter second number");

number2 = [Link]();
switch (operator) {

// performs addition between numbers

case '+':

result = number1 + number2;

[Link](number1 + " + " + number2 + " = " + result);

break;

// performs subtraction between numbers

case '-':

result = number1 - number2;

[Link](number1 + " - " + number2 + " = " + result);

break;

// performs multiplication between numbers

case '*':

result = number1 * number2;

[Link](number1 + " * " + number2 + " = " + result);

break;

// performs division between numbers

case '/':
result = number1 / number2;

[Link](number1 + " / " + number2 + " = " + result);

break;

default:

[Link]("Invalid operator!");

break;

[Link]();

Output

Choose an operator: +, -, *, or /

Enter first number

Enter second number

3.0 * 9.0 = 27

You might also like