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

Assignment No1 (JAVA)

advance java

Uploaded by

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

Assignment No1 (JAVA)

advance java

Uploaded by

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

Assignment No.

1 Submission Date:

ADVANCE JAVA PROGRAMING 29, August 2024

1. Write a Java program to illustrate the concept of class with method overloading.
Ans: -
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}

// Method to add three integers


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

// Method to add two double values


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

public static void main(String[] args) {


Calculator calc = new Calculator();

// Calling the add method with two integers


System.out.println("Sum of 10 and 20: " + calc.add(10, 20));

// Calling the add method with three integers


System.out.println("Sum of 10,20,and 30: " + calc.add(10, 20, 30));

// Calling the add method with two double values


System.out.println("Sum of 10.5 and 20.5: " + calc.add(10.5, 20.5));
}
}
2. Write a program in java to demonstrate the various uses of static keyword

Ans: -
public class StaticDemo {
// Static variable
static int staticVar = 10;

// Instance variable
int instanceVar;

// Static method
static void staticMethod() {
System.out.println("Inside static method.");
// Can access static variables
System.out.println("Static variable: " + staticVar);
// Cannot access instance variables directly
// System.out.println("Instance variable: " + instanceVar); // This
would cause an error
}

// Instance method
void instanceMethod() {
System.out.println("Inside instance method.");
// Can access both static and instance variables
System.out.println("Static variable: " + staticVar);
System.out.println("Instance variable: " + instanceVar);
}

// Static block
static {
System.out.println("Inside static block.");
staticVar = 20; // Can initialize static variables
}

public static void main(String[] args) {


// Accessing static method without creating an instance
StaticDemo.staticMethod();

// Creating an instance of StaticDemo


StaticDemo demo = new StaticDemo();
demo.instanceVar = 30;

// Accessing instance method


demo.instanceMethod();
}
}
3. Write a program in java to demonstrate how command line arguments are handled
in java.

Ans: -

public class CommandLineDemo {


public static void main(String[] args) {
// Check if any arguments are passed
if (args.length > 0) {
System.out.println("Command-line arguments are:");
// Loop through all arguments and print them
for (String arg : args) {
System.out.println(arg);
}
} else {
System.out.println("No command-line arguments found.");
}
}
}
4. Write a program in java to demonstrate the concept of dynamic method dispatch.

Ans: -
// Base class
class Animal {
void makeSound() {
System.out.println("Some generic animal sound");
}
}

// Derived class 1
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}

// Derived class 2
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow");
}
}

public class DynamicDispatchDemo {


public static void main(String[] args) {
// Creating objects of type Animal, Dog, and Cat
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();

// Calling the makeSound method on each object


myAnimal.makeSound(); // Outputs: Some generic animal sound
myDog.makeSound(); // Outputs: Bark
myCat.makeSound(); // Outputs: Meow
}
}
5. Write a program in java to demonstrate how different types of constructors are
used in java including copy constructor.
Ans: -
class Student {
String name;
int age;

// No-argument constructor
public Student() {
this.name = "Unknown";
this.age = 0;
}

// Parameterized constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}

// Copy constructor
public Student(Student student) {
this.name = student.name;
this.age = student.age;
}

// Method to display student details


public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}

public static void main(String[] args) {


// Using no-argument constructor
Student student1 = new Student();
student1.display();

// Using parameterized constructor


Student student2 = new Student("Alice", 20);
student2.display();

// Using copy constructor


Student student3 = new Student(student2);
student3.display();
}
}

You might also like