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

java file 1-10

The document contains multiple Java programs demonstrating various object-oriented programming concepts including class creation, method overloading, static variables and methods, the use of 'this', inheritance, polymorphism, aggregation, abstract classes, and interfaces. Each program includes a main method that executes the functionality and prints the output. The examples illustrate the implementation of these concepts in a clear and structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

java file 1-10

The document contains multiple Java programs demonstrating various object-oriented programming concepts including class creation, method overloading, static variables and methods, the use of 'this', inheritance, polymorphism, aggregation, abstract classes, and interfaces. Each program includes a main method that executes the functionality and prints the output. The examples illustrate the implementation of these concepts in a clear and structured manner.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Write a program declaring a class Rectangle with data member’s length and breadth and
member functions Input, Output and CalcArea.

import java.util.Scanner;

class Rectangle {
float length;
float breadth;

public void Input() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter length: ");
length = scanner.nextFloat();
System.out.print("Enter breadth: ");
breadth = scanner.nextFloat();
}

public void Output() {


System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
}

public float CalcArea() {


return length * breadth;
}

public static void main(String[] args) {


Rectangle rect = new Rectangle();
rect.Input();
rect.Output();
System.out.println("Area of Rectangle: " + rect.CalcArea());
}
}

OUTPUT:

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

import java.util.Scanner;
class AreaCalculator {

public float CalcArea(float side) {


return side * side;
}

public float CalcArea(float length, float breadth) {


return length * breadth;
}

public float CalcArea(float base, float height, boolean isTriangle) {


if (isTriangle) {
return 0.5f * base * height;
}
return 0;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
AreaCalculator calc = new AreaCalculator();

System.out.print("Enter side of square: ");


float side = scanner.nextFloat();
System.out.println("Area of Square: " + calc.CalcArea(side));

System.out.print("Enter length of rectangle: ");


float length = scanner.nextFloat();
System.out.print("Enter breadth of rectangle: ");
float breadth = scanner.nextFloat();
System.out.println("Area of Rectangle: " + calc.CalcArea(length, breadth));

System.out.print("Enter base of triangle: ");


float base = scanner.nextFloat();
System.out.print("Enter height of triangle: ");
float height = scanner.nextFloat();
System.out.println("Area of Triangle: " + calc.CalcArea(base, height, true));
}
}

2
OUTPUT:

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

import java.util.Scanner;

class StaticDemo {
static int count;

static
{
count = 0;
}

public StaticDemo() {
count++;
}

public static void displayCount() {


System.out.println("Number of objects : " + count);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
StaticDemo obj1 = new StaticDemo();
StaticDemo obj2 = new StaticDemo();
StaticDemo obj3 = new StaticDemo();

StaticDemo.displayCount();
}
}

OUTPUT:

4
4. Write a program to demonstrate concept of ``this``.

class Const
{
int a,b;

@Override
public String toString()
{
return this.a + "+" + this.b;
}

Const(int a,int b)
{
this.a=a;
this.b=b;
}

Const(int a)
{
this(a,a);
}

Const()
{
this(0);
}
};

class Main
{
public static void main(String[] args)
{
Const one=new Const(10,13);
Const two=new Const(20);
Const three=new Const();

System.out.println(one);
System.out.println(two);
System.out.println(three);

}
}

OUTPUT:

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

//Multi-level
class Animal {
void speak()
{
System.out.println("this is animal");
}
}

class Dog extends Animal {


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

class Husky extends Dog {


void speak()
{
super.speak();
System.out.println("Husky is trained");
}
}
public class Main {
public static void main(String[] args) {
Husky one = new Husky();

one.speak();
}
}

OUTPUT:

6
//Hierarchical
class Animal {
void speak()
{
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


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

class Cat extends Animal {


void speak()
{
System.out.println("cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();

dog.speak();
cat.speak();
}
}

OUTPUT:

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

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

class Dog extends Animal {


@Override
void speak() {
// Using super() to call the speak method of the parent class
super.speak(); // Calls Animal's speak method
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();

dog.speak();
}
}

OUTPUT:

8
7. Write a program to demonstrate run-time polymorphism.

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

class Dog extends Animal {


@Override
void speak()
{
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();

dog.speak();
}
}

OUTPUT:

9
8. Write a program to demonstrate the concept of aggregation.

import java.util.Scanner;
class Address {
String city;
String state;
String country;

Address(String city, String state, String country) {


this.city = city;
this.state = state;
this.country = country;
}
}

class Employee {
int id;
String name;
Address address;

Employee(int id, String name, Address address) {


this.id = id;
this.name = name;
this.address = address;
}
public void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Address: " + address.city + ", " + address.state + ", " + address.country);
}
}

class AggregationDemo {
public static void main(String[] args) {
Address address1 = new Address("New York", "NY", "USA");
Address address2 = new Address("Los Angeles", "CA", "USA");

Employee emp1 = new Employee(101, "John Doe", address1);


Employee emp2 = new Employee(102, "Jane Smith", address2);

emp1.display();
emp2.display();
}
}

10
OUTPUT:

11
9. Write a program to demonstrate the concept of abstract class with constructor and ``final``
method.

abstract class Shape


{
String name;

Shape(String name)
{
this.name = name;
}

abstract void draw();

final void display() {


System.out.println("This is a shape: " + name);
}
}

class Circle extends Shape {


Circle() {
super("Circle");
}

@Override
void draw() {
System.out.println("Drawing a Circle");
}
}

public class AbstractDemo {


public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
circle.display();
}
}

OUTPUT:

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

abstract class Shape


{
String name;

Shape(String name)
{
this.name = name;
}

abstract void draw();

final void display() {


System.out.println("Shape is : " + name);
}
}

class Circle extends Shape {


Circle() {
super("Circle");
}

@Override
void draw() {
System.out.println("Drawing a Circle");
}
}

public class AbstractDemo {


public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
circle.display();
}
}

OUTPUT:

13

You might also like