0% found this document useful (0 votes)
12 views9 pages

Untitled Document

Java notes

Uploaded by

tannuswami150
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)
12 views9 pages

Untitled Document

Java notes

Uploaded by

tannuswami150
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
You are on page 1/ 9

**1.

Java Program to Print "Hello" Command**

```java
//HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello");
}
}
```

**Output:**

```
Hello
```

**2. Java Program to Represent Abstract Class with Example**

```java
// AbstractClass.java
abstract class Shape {
abstract double getArea();
}

class Rectangle extends Shape {


double length, width;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
double getArea() {
return length * width;
}
}

class Circle extends Shape {


double radius;

Circle(double radius) {
this.radius = radius;
}

@Override
double getArea() {
return Math.PI * radius * radius;
}
}

public class AbstractClassExample {


public static void main(String[] args) {
Shape[] shapes = {new Rectangle(5, 10), new Circle(3)};

for (Shape shape : shapes) {


System.out.println("Area: " + shape.getArea());
}
}
}
```

**Output:**

```
Area: 50.0
Area: 28.274333882308138
```

**3. Java Program to Show Different Levels of Inheritance**

```java
// InheritanceExample.java
class Animal {
String name;

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

public void speak() {


System.out.println("Animal says...");
}
}

class Dog extends Animal {


Dog(String name) {
super(name);
}

@Override
public void speak() {
System.out.println("Dog barks...");
}
}
class Cat extends Animal {
Cat(String name) {
super(name);
}

@Override
public void speak() {
System.out.println("Cat meows...");
}
}

public class InheritanceExample {


public static void main(String[] args) {
Animal animal = new Animal("Animal");
Dog dog = new Dog("Dog");
Cat cat = new Cat("Cat");

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

**Output:**

```
Animal says...
Dog barks...
Cat meows...
```

**4. Java Program to Find the Fibonacci Series Using Recursive Functions**

```java
// Fibonacci.java
public class Fibonacci {
public static int fib(int n) {
if (n <= 1) {
return n;
}

return fib(n - 1) + fib(n - 2);


}

public static void main(String[] args) {


int n = 10;
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + " ");
}
}
}
```

**Output:**

```
0 1 1 2 3 5 8 13 21 34
```

**5. Java Program to Show Method Overloading**

```java
// MethodOverloading.java
class Calculator {
int sum(int a, int b) {
return a + b;
}

double sum(double a, double b) {


return a + b;
}

public static void main(String[] args) {


Calculator calculator = new Calculator();

System.out.println(calculator.sum(10, 20)); // Integer arguments


System.out.println(calculator.sum(12.5, 15.3)); // Double arguments
}
}
```

**Output:**

```
30
27.8
```

**6. Java Program to Create Inner Classes**

```java
// InnerClassExample.java
class OuterClass {
int x = 10;

class InnerClass {
int y = 5;

public void display() {


System.out.println("x = " + x);
System.out.println("y = " + y);
}
}

public static void main(String[] args) {


OuterClass outerClass = new OuterClass();
OuterClass.InnerClass innerClass = outerClass.new InnerClass();

innerClass.display();
}
}
```

**Output:**

```
x = 10
y=5
```

**7. Java Program to Show Constructor Overloading**

```java
// ConstructorOverloading.java
class Student {
int rollNumber;
String name;

Student() {
// Default constructor
}

Student(int rollNumber) {
this.rollNumber = rollNumber;
}

Student(int rollNumber, String name) {


this.rollNumber = rollNumber;
this.name = name;
}
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student(10);
Student student3 = new Student(20, "John");

System.out.println(student1.rollNumber + " " + student1.name);


System.out.println(student2.rollNumber + " " + student2.name);
System.out.println(student3.rollNumber + " " + student3.name);
}
}
```

**Output:**

```
0 null
10 null
20 John
```

**8. Java Program to Exhibit Polymorphism**

```java
// PolymorphismExample.java
class Animal {
public void speak() {
System.out.println("Animal says...");
}
}

class Dog extends Animal {


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

class Cat extends Animal {


@Override
public void speak() {
System.out.println("Cat meows...");
}
}

public class PolymorphismExample {


public static void main(String[] args) {
Animal[] animals = { new Animal(), new Dog(), new Cat() };
for (Animal animal : animals) {
animal.speak();
}
}
}
```

**Output:**

```
Animal says...
Dog barks...
Cat meows...
```

**9. Java Program to Show Concepts of Encapsulation and Overriding**

```java
// EncapsulationAndOverridingExample.java
class Animal {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public void speak() {


System.out.println("Animal says...");
}
}

class Dog extends Animal {


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

public class EncapsulationAndOverridingExample {


public static void main(String[] args) {
Animal animal = new Animal();
animal.setName("Animal");
System.out.println("Animal's name: " + animal.getName());
animal.speak();
Dog dog = new Dog();
dog.setName("Dog");
System.out.println("Dog's name: " + dog.getName());
dog.speak();
}
}
```

**Output:**

```
Animal's name: Animal
Animal says...
Dog's name: Dog
Dog barks...
```

**10. Java Program to Represent ArrayList Class**

```java
// ArrayListExample.java
import java.util.ArrayList;

public class ArrayListExample {


public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();

numbers.add(10);
numbers.add(20);
numbers.add(30);

System.out.println(numbers);

for (int number : numbers) {


System.out.println(number);
}
}
}

**Output:**

[10, 20, 30]


10
20
30

You might also like