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

Java_Assignment_1_Solutions

Uploaded by

maardanga
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)
35 views

Java_Assignment_1_Solutions

Uploaded by

maardanga
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
You are on page 1/ 13

Java Assignment Solutions - Assignment 1

1. Write a program to print "Hello JAVA".

public class HelloJava {

public static void main(String[] args) {

System.out.println("Hello JAVA");

2. Write a program to print the value given by the user using command-line arguments.

public class CommandLineValue {

public static void main(String[] args) {

if (args.length > 0) {

System.out.println("Value entered: " + args[0]);

} else {

System.out.println("No value entered!");

3. Write a program to print the sum of two numbers given by the user using command-line

arguments.

public class SumCommandLine {


public static void main(String[] args) {

if (args.length == 2) {

int num1 = Integer.parseInt(args[0]);

int num2 = Integer.parseInt(args[1]);

System.out.println("Sum: " + (num1 + num2));

} else {

System.out.println("Please provide two numbers as arguments.");

4. Write a program to print the sum of two numbers given by the user using the Scanner

class.

import java.util.Scanner;

public class SumScanner {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");

int num1 = scanner.nextInt();

System.out.print("Enter second number: ");

int num2 = scanner.nextInt();

System.out.println("Sum: " + (num1 + num2));

scanner.close();

}
}

5. Write a program to illustrate the concept of default constructor.

class DefaultConstructor {

DefaultConstructor() {

System.out.println("Default constructor called!");

public static void main(String[] args) {

new DefaultConstructor();

6. Write a program to illustrate the concept of parameterized and object parameterized

constructors.

class ParameterizedConstructor {

int value;

ParameterizedConstructor(int value) {

this.value = value;

System.out.println("Parameterized constructor called with value: " + value);

ParameterizedConstructor(ParameterizedConstructor obj) {
this.value = obj.value;

System.out.println("Object parameterized constructor called. Copied value: "

+ value);

public static void main(String[] args) {

ParameterizedConstructor obj1 = new ParameterizedConstructor(10);

ParameterizedConstructor obj2 = new ParameterizedConstructor(obj1);

7. Write a program to illustrate the concept of this keyword.

class ThisKeyword {

int value;

ThisKeyword(int value) {

this.value = value;

void display() {

System.out.println("Value is: " + this.value);

public static void main(String[] args) {

ThisKeyword obj = new ThisKeyword(20);


obj.display();

8. Write a program to demonstrate static and instance data members and methods.

class StaticInstanceDemo {

static int staticValue = 10;

int instanceValue;

StaticInstanceDemo(int instanceValue) {

this.instanceValue = instanceValue;

static void staticMethod() {

System.out.println("Static method called. Static value: " + staticValue);

void instanceMethod() {

System.out.println("Instance method called. Instance value: " +

instanceValue);

public static void main(String[] args) {

StaticInstanceDemo obj = new StaticInstanceDemo(20);

staticMethod();
obj.instanceMethod();

9. Write a program to implement the concept of importing classes from a user-defined

package and creating packages.

// Save this file as MyClass.java inside the mypackage folder

package mypackage;

public class MyClass {

public void displayMessage() {

System.out.println("Message from user-defined package!");

// Main.java

import mypackage.MyClass;

public class Main {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.displayMessage();

}
10. Write a program which illustrates the concept of inheritance.

class Parent {

void display() {

System.out.println("This is the parent class.");

class Child extends Parent {

void show() {

System.out.println("This is the child class.");

public class InheritanceExample {

public static void main(String[] args) {

Child obj = new Child();

obj.display();

obj.show();

11. Write a program to demonstrate the concept of method overloading and overriding.

class Overload {

void display(int num) {


System.out.println("Number: " + num);

void display(String text) {

System.out.println("Text: " + text);

class Override extends Overload {

@Override

void display(int num) {

System.out.println("Overridden number: " + num);

public class MethodDemo {

public static void main(String[] args) {

Override obj = new Override();

obj.display(10);

obj.display("Hello");

12. Write a program to demonstrate the use of a nested class.

class Outer {
private int value = 10;

class Inner {

void display() {

System.out.println("Value from Outer class: " + value);

public static void main(String[] args) {

Outer outer = new Outer();

Outer.Inner inner = outer.new Inner();

inner.display();

13. Write a program illustrating the concept of super keyword at variable and method level.

class Parent {

int value = 10;

void display() {

System.out.println("Parent class method.");

class Child extends Parent {


int value = 20;

void display() {

super.display();

System.out.println("Child class method. Parent value: " + super.value + ",

Child value: " + value);

public class SuperExample {

public static void main(String[] args) {

Child obj = new Child();

obj.display();

14. Write a program to calculate the sum of two integers and two floats using an abstract

method sum().

abstract class SumCalculator {

abstract void sum(int a, int b);

abstract void sum(float a, float b);

class Calculator extends SumCalculator {

@Override
void sum(int a, int b) {

System.out.println("Sum of integers: " + (a + b));

@Override

void sum(float a, float b) {

System.out.println("Sum of floats: " + (a + b));

public class AbstractExample {

public static void main(String[] args) {

Calculator calc = new Calculator();

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

public void methodA() {

System.out.println("Method A from interface A.");

@Override

public void methodB() {

System.out.println("Method B from interface B.");

public class InterfaceDemo {

public static void main(String[] args) {

C obj = new C();

obj.methodA();

obj.methodB();

16. Write a program to demonstrate the use of the final keyword.

class FinalDemo {

final int VALUE = 10;


void display() {

System.out.println("Final value: " + VALUE);

public class Main {

public static void main(String[] args) {

FinalDemo obj = new FinalDemo();

obj.display();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy