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

JAVA PROGRAMS

The document contains multiple Java programs demonstrating various concepts such as multiple constructors, method overloading, inheritance, arrays, string manipulation, interfaces, and package usage. Each program is accompanied by its output, showcasing the functionality implemented. The examples illustrate fundamental programming techniques in Java, including object-oriented principles and data handling.

Uploaded by

packiaabilan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA PROGRAMS

The document contains multiple Java programs demonstrating various concepts such as multiple constructors, method overloading, inheritance, arrays, string manipulation, interfaces, and package usage. Each program is accompanied by its output, showcasing the functionality implemented. The examples illustrate fundamental programming techniques in Java, including object-oriented principles and data handling.

Uploaded by

packiaabilan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Write a java Program using Multiple Constructors

class Person {
private String name;
private int age;
private String city;

// Constructor 1: No arguments
public Person() {
this.name = "Unknown";
this.age = 0;
this.city = "Not specified";
}

// Constructor 2: Name only


public Person(String name) {
this.name = name;
this.age = 0;
this.city = "Not specified";
}

// Constructor 3: Name and age


public Person(String name, int age) {
this.name = name;
this.age = age;
this.city = "Not specified";
}

// Constructor 4: Name, age, and city


public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}

// Method to display information


public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
public static void main(String[] args) {
// Using Constructor 1
Person person1 = new Person();
person1.displayInfo();

System.out.println();

// Using Constructor 2
Person person2 = new Person("M.RANJITH");
person2.displayInfo();

System.out.println();

// Using Constructor 3
Person person3 = new Person("R.NIRANJANA", 03);
person3.displayInfo();

System.out.println();

// Using Constructor 4
Person person4 = new Person("R.VAIJU", 30, "TENKASI");
person4.displayInfo();
}
}
OUTPUT:
D:\Ranjith>javac Person.java
D:\Ranjith>java Person.

Name: Unknown
Age: 0
City: Not specified

Name: M.RANJITH
Age: 0
City: Not specified

Name: R.NIRANJANA
Age: 3
City: Not specified

Name: R.VAIJU
Age: 30
City: TENKASI

2.Write a java Program

class Calculator {

// Method to add two integers


int add(int a, int b) {
return a + b;
}

// Method to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Method to add two double values


double add(double a, double b) {
return a + b;
}

// Method to concatenate two strings


String add(String a, String b) {
return a + b;
}
}

public class Main {


public static void main(String[] args) {
Calculator calculator = new Calculator();

// Calling overloaded methods


System.out.println("Addition of two integers: " + calculator.add(5, 10));
System.out.println("Addition of three integers: " + calculator.add(5, 10, 15));
System.out.println("Addition of two doubles: " + calculator.add(5.5, 4.5));
System.out.println("Concatenation of two strings: " + calculator.add("Hello", " World"));
}
}
Output:
D:\Ranjith>javac Main.java
D:\Ranjith>java Main
Addition of two integers: 15
Addition of three integers: 30
Addition of two doubles: 10.0
Concatenation of two strings: Hello World
3.overriding

// Superclass
class Animal {
// Method to be overridden
void sound() {
System.out.println("Animals make different sounds");
}
}

// Subclass 1
class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("Dog barks: Woof Woof");
}
}

// Subclass 2
class Cat extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("Cat meows: Meow Meow");
}
}

public class Main1 {


public static void main(String[] args) {
// Create objects of Animal, Dog, and Cat
Animal genericAnimal = new Animal();
Animal dog = new Dog();
Animal cat = new Cat();
// Call the overridden methods
genericAnimal.sound(); // Calls Animal's sound method
dog.sound(); // Calls Dog's overridden sound method
cat.sound(); // Calls Cat's overridden sound method
}
}
Output:
D:\Ranjith>javac Main1.java
D:\Ranjith>java Main1
Animals make different sounds
Dog barks: Woof Woof
Cat meows: Meow Meow

4. Write a java program using one-dimensional arrays

public class ArrayExample {


public static void main(String[] args) {
// Declare and initialize a one-dimensional array of integers
int[] numbers = {10, 20, 30, 40, 50};

// Print the elements of the array using a loop


System.out.println("Elements of the array:");
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}

// Find the sum of the elements in the array


int sum = 0;
for (int num : numbers) {
sum += num;
}

// Print the sum


System.out.println("Sum of the array elements: " + sum);
}
}
Output:
D:\Ranjith>javac ArrayExample.java

D:\Ranjith>java ArrayExample
Elements of the array:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Sum of the array elements: 150

5. Write a java program using Two-dimensional arrays

public class TwoDimensionalArrayExample {


public static void main(String[] args) {
// Declare a 2D array with 3 rows and 4 columns
int[][] array = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

// Print the values of the 2D array


System.out.println("The values of the 2D array are:");
for (int i = 0; i < array.length; i++) { // Iterates through rows
for (int j = 0; j < array[i].length; j++) { // Iterates through columns
System.out.print(array[i][j] + " ");
}
System.out.println(); // New line after each row
}
}
}
Output:

D:\Ranjith>javac TwoDimensionalArrayExample.java

D:\Ranjith>java TwoDimensionalArrayExample.java
The values of the 2D array are:
1234
5678
9 10 11 12

D:\Ranjith>
6. Write a program to do String Manipulation using Character Array
and perform the following string operations: String length, Finding a
character at a particular position, Concatenating two strings

import java.util.Scanner;

public class StringManipulationUsingCharArray {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Input two strings


System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();

// Convert strings to character arrays


char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();

// String length
int length1 = charArray1.length;
int length2 = charArray2.length;

System.out.println("Length of first string: " + length1);


System.out.println("Length of second string: " + length2);

// Finding a character at a particular position (example position 3)


int position = 3; // You can change the position as needed
if (position < length1) {
System.out.println("Character at position " + position + " in first string: " + charArray1[position]);
} else {
System.out.println("Position " + position + " is out of bounds for the first string.");
}

// Concatenating two strings


char[] concatenatedArray = new char[length1 + length2];
for (int i = 0; i < length1; i++) {
concatenatedArray[i] = charArray1[i];
}
for (int i = 0; i < length2; i++) {
concatenatedArray[length1 + i] = charArray2[i];
}

// Convert concatenated char array back to string


String concatenatedString = new String(concatenatedArray);
System.out.println("Concatenated string: " + concatenatedString);

scanner.close();
}
}
OUTPUT:
D:\Ranjith>javac StringManipulationUsingCharArray.java

D:\Ranjith>java StringManipulationUsingCharArray
Enter the first string: NIRANJANA
Enter the second string: RANJITH
Length of first string: 9
Length of second string: 7
Character at position 3 in first string: A
Concatenated string: NIRANJANARANJITH

7.Write a JAVA program implementing interface(s)

// Interface for basic vehicle operations


interface Vehicle {
void start(); // Method to start the vehicle
void stop(); // Method to stop the vehicle
}

// Interface for electric vehicle-specific operations


interface ElectricVehicle {
void charge(); // Method to charge the vehicle
}

// Car class implements both Vehicle and ElectricVehicle interfaces


class Car implements Vehicle, ElectricVehicle {

@Override
public void start() {
System.out.println("The car is starting.");
}
@Override
public void stop() {
System.out.println("The car is stopping.");
}

@Override
public void charge() {
System.out.println("The car is charging.");
}
}

public class InterfaceExample {


public static void main(String[] args) {
// Create an instance of Car
Car myCar = new Car();

// Use methods from Vehicle interface


myCar.start();
myCar.stop();

// Use methods from ElectricVehicle interface


myCar.charge();
}
}

Output:
D:\Ranjith>javac InterfaceExample.java

D:\Ranjith>java InterfaceExample
The car is starting.
The car is stopping.
The car is charging.

D:\Ranjith>

8.
package vehicles; // Define the package

public class Car {


private String model;
private int year;

// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}

// Method to display car information


public void displayCarInfo() {
System.out.println("Car Model: " + model);
System.out.println("Car Year: " + year);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
import vehicles.Car; // Importing the Car class from the vehicles package

public class Mainran {


public static void main(String[] args) {
// Creating an instance of the Car class
Car myCar = new Car("Tesla Model S", 2023);

// Calling the method to display car information


myCar.displayCarInfo();
}
}
----------------------------------------------------------------------------------------------------------

Output:
Save thes two files in same folder in vehicle
D:\Ranjith>javac Car.java

D:\Ranjith>javac Mainran.java

You might also like