java 1-10
java 1-10
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);
}
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");
}
}
public class q7 {
public static void main(String[] args) {
Animal myAnimal;
myAnimal = new Dog();
myAnimal.sound();
8
Program 8: Write a program to demonstrate the concept of aggregation.
Source Code:
class Book {
String title;
String author;
9
Program 9: Write a program to demonstrate the concept of abstract class with
constructor and ``final`` method.
Source Code:
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