JAVA PROGRAMS
JAVA PROGRAMS
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";
}
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
class Calculator {
// 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");
}
}
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
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;
// String length
int length1 = charArray1.length;
int length2 = charArray2.length;
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
@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.");
}
}
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
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
Output:
Save thes two files in same folder in vehicle
D:\Ranjith>javac Car.java
D:\Ranjith>javac Mainran.java