Java_Assignment_1_Solutions
Java_Assignment_1_Solutions
Java_Assignment_1_Solutions
System.out.println("Hello JAVA");
2. Write a program to print the value given by the user using command-line arguments.
if (args.length > 0) {
} else {
3. Write a program to print the sum of two numbers given by the user using command-line
arguments.
if (args.length == 2) {
} else {
4. Write a program to print the sum of two numbers given by the user using the Scanner
class.
import java.util.Scanner;
scanner.close();
}
}
class DefaultConstructor {
DefaultConstructor() {
new DefaultConstructor();
constructors.
class ParameterizedConstructor {
int value;
ParameterizedConstructor(int value) {
this.value = value;
ParameterizedConstructor(ParameterizedConstructor obj) {
this.value = obj.value;
+ value);
class ThisKeyword {
int value;
ThisKeyword(int value) {
this.value = value;
void display() {
8. Write a program to demonstrate static and instance data members and methods.
class StaticInstanceDemo {
int instanceValue;
StaticInstanceDemo(int instanceValue) {
this.instanceValue = instanceValue;
void instanceMethod() {
instanceValue);
staticMethod();
obj.instanceMethod();
package mypackage;
// Main.java
import mypackage.MyClass;
obj.displayMessage();
}
10. Write a program which illustrates the concept of inheritance.
class Parent {
void display() {
void show() {
obj.display();
obj.show();
11. Write a program to demonstrate the concept of method overloading and overriding.
class Overload {
@Override
obj.display(10);
obj.display("Hello");
class Outer {
private int value = 10;
class Inner {
void display() {
inner.display();
13. Write a program illustrating the concept of super keyword at variable and method level.
class Parent {
void display() {
void display() {
super.display();
obj.display();
14. Write a program to calculate the sum of two integers and two floats using an abstract
method sum().
@Override
void sum(int a, int b) {
@Override
calc.sum(10, 20);
calc.sum(10.5f, 20.5f);
15. Write a program to demonstrate the use of implementing and extending interfaces.
interface A {
void methodA();
interface B extends A {
void methodB();
}
class C implements B {
@Override
@Override
obj.methodA();
obj.methodB();
class FinalDemo {
obj.display();