0% found this document useful (0 votes)
4 views

java 1-10

The document contains ten Java programs demonstrating various object-oriented programming concepts including classes, method overloading, static variables, the 'this' keyword, inheritance, polymorphism, aggregation, abstract classes, and interfaces. Each program is accompanied by source code that illustrates the specific concept being taught. The programs cover a range of topics from basic class structure to more advanced features like method overriding and interface implementation.

Uploaded by

Atish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java 1-10

The document contains ten Java programs demonstrating various object-oriented programming concepts including classes, method overloading, static variables, the 'this' keyword, inheritance, polymorphism, aggregation, abstract classes, and interfaces. Each program is accompanied by source code that illustrates the specific concept being taught. The programs cover a range of topics from basic class structure to more advanced features like method overriding and interface implementation.

Uploaded by

Atish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 1: Write a program declaring a class Rectangle with data member's

length and breadth and member functions Input, Output and CalcArea.

Source Code:

import java.util.Scanner;
class Rectangle {
double length;
double breadth;
void Input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length: ");
length = scanner.nextDouble();
System.out.print("Enter breadth: ");
breadth = scanner.nextDouble();
}
void Output() {
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
}
double CalcArea() {
return length * breadth;
}
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.Input();
rect.Output();
System.out.println("Area: " + rect.CalcArea());
}
}

1
Program 2: Write a program to demonstrate use of method overloading to
calculate area of square, rectangle and triangle.

Source Code:

class AreaCalculator {
double calculateArea(double side) {
return side * side;
}
double calculateArea(double length, double breadth) {
return length * breadth;
}
double calculateArea(int base, double height) {
return 0.5 * base * height;
}
public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();
System.out.println("Area of square: " + calculator.calculateArea(10));
System.out.println("Area of rectangle: " + calculator.calculateArea(10, 10));
System.out.println("Area of triangle: " + calculator.calculateArea(2, 10.0));
}
}

2
Program 3: Write a program to demonstrate the use of static variable, static
method and static block.

Source Code:

class StaticDemo {
static int count;
static {
count = 0;
System.out.println("Static block executed. Count initialized to " + count);
}
StaticDemo() {
count++;
}
static void displayCount() {
System.out.println("Current count: " + count);
}
public static void main(String[] args) {
StaticDemo obj1 = new StaticDemo();
StaticDemo obj2 = new StaticDemo();
StaticDemo obj3 = new StaticDemo();
StaticDemo.displayCount();
}
}

3
Program 4: Write a program to demonstrate concept of ``this``.

Source Code:

class ThisKeywordDemo {
int value;

ThisKeywordDemo(int value) {
this.value = value;
}

void display() {
System.out.println("Value: " + this.value);
}

void setValue(int value) {


this.value = value;
}

public static void main(String[] args) {


ThisKeywordDemo obj = new ThisKeywordDemo(5);
obj.display();
obj.setValue(10);
obj.display();
}
}

4
Program 5: Write a program to demonstrate multi-level and hierarchical
inheritance.

Source Code:

class Fruit {
String name;
String color;
Fruit(String name, String color) {
this.name = name;
this.color = color;
}
void displayInfo() {
System.out.println("Fruit: " + name + ", Color: " + color);
}
}
class SweetFruit extends Fruit {
SweetFruit(String name, String color) {
super(name, color);
}
void taste() {
System.out.println(name + " is sweet.");
}
}
class SourFruit extends Fruit {
SourFruit(String name, String color) {
super(name, color);
}
void taste() {
System.out.println(name + " is sour.");
}
}
class Mango extends SweetFruit {
Mango() {
super("Mango", "Yellow");
}
}
class Lemon extends SourFruit {
Lemon() {
super("Lemon", "Yellow");
}
}
public class q5 {
public static void main(String[] args) {
Mango mango = new Mango();
Lemon lemon = new Lemon();

mango.displayInfo();
mango.taste();

5
lemon.displayInfo();
lemon.taste();
}
}

6
Program 6: Write a program to use super() to invoke base class constructor.

Source Code:
class Animal {
String name;
Animal(String name) {
this.name = name;
System.out.println("Animal constructor called: " + name);
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
System.out.println("Dog constructor called: " + name);
}
}
public class q6 {
public static void main(String[] args) {
Dog dog = new Dog("Sheeeeeer");
}
}

7
Program 7: Write a program to demonstrate run-time polymorphism.

Source Code:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


void sound() {
System.out.println("Cat meows");
}
}

public class q7 {
public static void main(String[] args) {
Animal myAnimal;
myAnimal = new Dog();
myAnimal.sound();

myAnimal = new Cat();


myAnimal.sound();
}
}

8
Program 8: Write a program to demonstrate the concept of aggregation.

Source Code:

class Book {
String title;
String author;

Book(String title, String author) {


this.title = title;
this.author = author;
}
void displayInfo() {
System.out.println("Title: " + title + ", Author: " + author);
}
}
class Library {
String name;
Book book;

Library(String name, Book book) {


this.name = name;
this.book = book;
}
void displayLibraryInfo() {
System.out.println("Library: " + name);
book.displayInfo();
}
}
public class q8 {
public static void main(String[] args) {
Book book = new Book("1999", "BunnyLore_");
Library library = new Library("IDK what should i name it", book);
library.displayLibraryInfo();
}
}

9
Program 9: Write a program to demonstrate the concept of abstract class with
constructor and ``final`` method.
Source Code:

abstract class Shape {


String color;
Shape(String color) {
this.color = color;
}
abstract void draw();
final void displayColor() {
System.out.println("Color: " + color);
}
}
class Circle extends Shape {
Circle(String color) {
super(color);
}
void draw() {
System.out.println("Drawing a Circle");
}
}
class Rectangle1 extends Shape {
Rectangle1(String color) {
super(color);
}
void draw() {
System.out.println("Drawing a Rectangle");
}
}
public class q9 {
public static void main(String[] args) {
Shape circle = new Circle("AllRed");
Shape rectangle = new Rectangle1("AllWhite");
circle.draw();
circle.displayColor();
rectangle.draw();
rectangle.displayColor();
}
}

10
Program 10: Write a program to demonstrate the concept of interface when two
interfaces have unique methods and same data members.

Source Code:

interface FloweringPlant {
String type = "Flowering Plant";
void bloom();
}
interface NonFloweringPlant {
String type = "Non-Flowering Plant";
void grow();
}
class Rose implements FloweringPlant {
public void bloom() {
System.out.println("The rose is blooming.");
}
void displayType() {
System.out.println("Type: " + type);
}
}
class Fern implements NonFloweringPlant {
public void grow() {
System.out.println("The fern is growing.");
}
void displayType() {
System.out.println("Type: " + type);
}
}
public class q10 {
public static void main(String[] args) {
Rose rose = new Rose();
rose.bloom();
rose.displayType();
Fern fern = new Fern();
fern.grow();
fern.displayType();
}
}

11

You might also like