0% found this document useful (0 votes)
2 views17 pages

Java Practicals

The document contains a series of Java programs demonstrating various programming concepts including input/output statements, control structures, constructors, method overloading, and inheritance types. Each program is accompanied by its aim, code, and expected output. The examples range from simple 'Hello World' to more complex implementations of inheritance and interfaces.

Uploaded by

dongareanil149
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)
2 views17 pages

Java Practicals

The document contains a series of Java programs demonstrating various programming concepts including input/output statements, control structures, constructors, method overloading, and inheritance types. Each program is accompanied by its aim, code, and expected output. The examples range from simple 'Hello World' to more complex implementations of inheritance and interfaces.

Uploaded by

dongareanil149
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

Program 1 :

AIM : WRITE A PROGRAM TO PRINT HELLO WORLD


Code :-
class HelloWorld{
public static void main(String[] args){
[Link]("Hello, World");}
}

Output :-
Program 2:
AIM :- WRITE A PROGRAM FOR INPUT OUTPUT STATEMENT

Code :-

import [Link];
class input{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
[Link]("Enter an Integer :\n");
int number = [Link]();
[Link]("You Entered : " + number);
[Link]();
}}

Output :-

Program 3 :
AIM :- WRITE A PROGRAM FOR NUMBER IS ODD AND EVEN

Code :-

import [Link];

public class OddEven {

public static void main(String[] args) {

int num;

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

num = [Link]();

if(num % 2 == 0) {

[Link]("The number is Even");

} else {

[Link]("The number is Odd");

Output :-
Program 4:
AIM :- WAP IN JAVA TO IMPLEMENT FOR LOOP
Code:

class ForLoopExample{
public static void main(String[]args)
{
for (int i=1;i<=5;i++)
{
[Link]("Number:"+i);
}
}
}

OUTPUT:
Program 5 :
AIM :- DESIGN AND IMPLEMENTATION OF CLASS
Code :

class Car {

String color;

String model;

int year;

void displayDetails(){

[Link]("Model:"+ model);

[Link]("Color:"+ color);

[Link]("Year:"+ year);

public class CarDemo {

public static void main(String[]args){

Car myCar = new Car();

[Link]= "Toyota Camry";

[Link] = 2024;

[Link]= "red";

[Link]("Details of myCar:");

[Link]();

[Link]();

}}

OUTPUT:
Program 6:
AIM :- WAP FOR CONSTRUCTOR ( DEFAULT CONSTRUCTOR )
Code:-
class A

int a; String b;

A()

a=1010;b="Ram";

void Disp()

[Link]("Roll Number:"+a);

[Link]("Student:"+b);

class E

public static void main(String[]args)

A r=new A();

[Link]();
}}

OUTPUT :-

Program 7 :
AIM :-WAP For DEFAULT CONSTRUCTOR.
Code :-

class Demo {
int x;
Demo(){
x=10;
}
public static void main (String[]args){
Demo d=new Demo();
[Link](d.x);
}
}

OUTPUT :-
Program 8:
AIM :-WAP For PARAMETERIZED CONSTRUCTOR.

Code :-

class Car{
String model;
Car(String m){
model = m;

}
public static void main(String[]args){
Car c = new Car("Toyota");
[Link]([Link]);
}
}

OUTPUT :-
Program 9:
AIM :-WAP For METHOD OVERLOADING.

Code :-

class Addition {
int add(int a, int b){
return a + b;
}
int add(int a, int b, int c){
return a + b + c;
}
public static void main(String[]args){
Addition obj = new Addition();
[Link]("Sum of 2 number:"+ [Link](10,20));
[Link]("Sum of 3 number:"+ [Link](10,20,30));
}
}

OUTPUT:
Program 10:
AIM :- WAP FOR METHOD OVERLOADING.
Code :-

class Animal {
void sound(){
[Link]("Animal makes a sound");
}
}
class Tiger extends Animal {
void sound(){
[Link]("Tiger make sound");
}
public static void main(String[]args){
Animal obj = new Tiger();
[Link]();
}
}
OUTPUT:

Program 11:
Aim : WAP For Single inheritance.
Code:
class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}
class Car extends Vehicle {
Car() {
[Link]("This Vehicle is Car");
}
}
public class Test {
public static void main(String[] args) {
Car obj = new Car();
}
}
Output:

Program 12:
Aim : WAP For Multilevel inheritance.
Code:
class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}
class FourWheeler extends Vehicle {
FourWheeler() {
[Link]("4 Wheeler Vehicles");
}
}
class Car extends FourWheeler {
Car() {
[Link]("This 4 Wheeler Vehicle is a Car");
}
}
public class Geeks {
public static void main(String[] args) {
Car obj = new Car();
}
}
OUTPUT:

Program 13:
Aim : WAP for Hierarchical inheritance.
Code:
class Vehicle {
Vehicle() {
[Link]("This is a Vehicle");
}
}
class Car extends Vehicle {
Car() {
[Link]("This Vehicle is Car");
}
}
class Bus extends Vehicle {
Bus() {
[Link]("This Vehicle is Bus");
}
}
public class Test {
public static void main(String[] args) {
Car obj1 = new Car();
Bus obj2 = new Bus();
}
}
Output:

Program 14:
Aim : WAP For Multiple inheritance.
Code:
interface LandVehicle {
default void landInfo() {
[Link]("This is a LandVehicle");
}
}
interface WaterVehicle {
default void waterInfo() {
[Link]("This is a WaterVehicle");
}
}
// Subclass implementing both interfaces
class AmphibiousVehicle implements LandVehicle, WaterVehicle {
AmphibiousVehicle() {
[Link]("This is an AmphibiousVehicle");
}
}
public class Test {
public static void main(String[] args) {
AmphibiousVehicle obj = new AmphibiousVehicle();
[Link]();
[Link]();
}
}
Output:
Program 15:
Aim : WAP For Crafting package in java.
Code:
package mypack;
class MyPackageClass {
public static void main(String[] args) {
[Link]("This is my package!");
}
}
Output:

You might also like