NPTEL » Programming In Java Week 3 : Programming Assignment 5 Aug 2022

 

Week 3 : Programming Assignment 5

Due on 2022-08-18, 23:59 IST

Complete the code segment to swap two numbers using call by object reference.



import java.util.Scanner;

class Question {  //Define a class Question with two elements e1 and e2.

    Scanner sc = new Scanner(System.in);

    int e1 = sc.nextInt();  //Read e1

    int e2 = sc.nextInt();  //Read e2

}

public class Question3 {

// Define static method swap()to swap the values of e1 and e2 of class Question.


    public static void swap(Question t)

    {

    int temp = t.e1;

        t.e1 = t.e2;

        t.e2 = temp;

    }

    public static void main(String[] args) {

        //Create an object of class Question

    Question t = new Question ();

        //Call the method swap()

        swap(t);

        System.out.println(t.e1+" "+t.e2);

    }

}






1 comment

  1. Java Course in Noida
    https://aptronsolutions.home.blog/2022/08/01/8-java-frameworks-for-a-cloud-native-world/

    ReplyDelete

NPTEL » Programming in Java Week 07 : Programming Assignment 1 2 3 4 and 5

  Week 07 : Programming Assignment 1 Due on 2024-09-12, 23:59 IST Write a Java program to find longest word in given input. Select the Langu...