java file 1-10
java file 1-10
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;
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 {
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++;
}
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");
}
}
one.speak();
}
}
OUTPUT:
6
//Hierarchical
class Animal {
void speak()
{
System.out.println("Animal makes a sound");
}
}
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");
}
}
dog.speak();
}
}
OUTPUT:
8
7. Write a program to demonstrate run-time polymorphism.
class Animal {
void speak()
{
System.out.println("Animal makes a sound");
}
}
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;
class Employee {
int id;
String name;
Address address;
class AggregationDemo {
public static void main(String[] args) {
Address address1 = new Address("New York", "NY", "USA");
Address address2 = new Address("Los Angeles", "CA", "USA");
emp1.display();
emp2.display();
}
}
10
OUTPUT:
11
9. Write a program to demonstrate the concept of abstract class with constructor and ``final``
method.
Shape(String name)
{
this.name = name;
}
@Override
void draw() {
System.out.println("Drawing a Circle");
}
}
OUTPUT:
12
10. Write a program to demonstrate the concept of interface when two interfaces have unique
methods and same data members
Shape(String name)
{
this.name = name;
}
@Override
void draw() {
System.out.println("Drawing a Circle");
}
}
OUTPUT:
13