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

Complete Java Project With Programs and Outputs

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 views

Complete Java Project With Programs and Outputs

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

Java Project with Programs and Outputs

1. Java Program to Print "Hello" Command

Code:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello");

Output:

Hello

2. Java Program to Represent Abstract Class

Code:

abstract class Animal {

abstract void sound();

class Dog extends Animal {

@Override

void sound() {

System.out.println("Woof");

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

myDog.sound();

Output:
Woof

3. Java Program to Show Different Levels of Inheritance

Code:

class Animal {

void eat() {

System.out.println("Eating...");

class Mammal extends Animal {

void walk() {

System.out.println("Walking...");

class Dog extends Mammal {

void bark() {

System.out.println("Barking...");

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.eat();

myDog.walk();

myDog.bark();

Output:

Eating...

Walking...
Barking...

4. Java Program to Find Fibonacci Series Using Recursion

Code:

public class Fibonacci {

static int fibonacci(int n) {

if (n <= 1)

return n;

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

public static void main(String[] args) {

int n = 10; // Example for 10 terms

for (int i = 0; i < n; i++) {

System.out.print(fibonacci(i) + " ");

Output:

0 1 1 2 3 5 8 13 21 34

5. Java Program to Show Method Overloading

Code:

public class OverloadExample {

void display(int num) {

System.out.println("Number: " + num);

void display(String text) {

System.out.println("Text: " + text);

public static void main(String[] args) {


OverloadExample obj = new OverloadExample();

obj.display(5);

obj.display("Hello");

Output:

Number: 5

Text: Hello

6. Java Program to Create Inner Classes

Code:

class OuterClass {

private String msg = "Hello from the outer class!";

class InnerClass {

void showMessage() {

System.out.println(msg);

public static void main(String[] args) {

OuterClass outer = new OuterClass();

OuterClass.InnerClass inner = outer.new InnerClass();

inner.showMessage();

Output:

Hello from the outer class!

7. Java Program to Show Constructor Overloading

Code:

public class ConstructorOverload {


int value;

ConstructorOverload() {

value = 0;

ConstructorOverload(int x) {

value = x;

public void display() {

System.out.println("Value: " + value);

public static void main(String[] args) {

ConstructorOverload obj1 = new ConstructorOverload();

ConstructorOverload obj2 = new ConstructorOverload(10);

obj1.display();

obj2.display();

Output:

Value: 0

Value: 10

8. Java Program to Exhibit Polymorphism

Code:

class Animal {

void sound() {

System.out.println("Animal makes a sound");

}
class Dog extends Animal {

void sound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Dog();

myAnimal.sound();

Output:

Dog barks

9. Java Program to Show Encapsulation and Overriding

Code:

class Person {

private String name;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

void display() {

System.out.println("Person name: " + name);

}
class Student extends Person {

@Override

void display() {

System.out.println("Student name: " + getName());

public static void main(String[] args) {

Student s = new Student();

s.setName("Alice");

s.display();

Output:

Student name: Alice

10. Java Program to Represent ArrayList Class

Code:

import java.util.ArrayList;

public class ArrayListExample {

public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();

list.add("Apple");

list.add("Banana");

list.add("Orange");

for (String fruit : list) {

System.out.println(fruit);

Output:

Apple
Banana

Orange

11. Applet Program to Display a Simple Message

Code:

import java.applet.Applet;

import java.awt.Graphics;

public class SimpleApplet extends Applet {

public void paint(Graphics g) {

g.drawString("Hello, Applet!", 20, 20);

Output:

Hello, Applet! (Displayed in Applet Window)

12. Applet Program for Passing Parameters

Code:

import java.applet.Applet;

import java.awt.Graphics;

public class ParameterApplet extends Applet {

String message;

public void init() {

message = getParameter("message");

if (message == null) {

message = "No message provided";

public void paint(Graphics g) {


g.drawString(message, 20, 20);

Output:

Displays the message passed as parameter in the applet HTML tag.

You might also like