Question

3. Java: Write a Java method to swap two integers. Attach your code as well as a test run screenshot.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Getting the values from the user and then do the swapping of values (Dynamically):

======================= Swap.java ====================================

CODE:

 import java.util.Scanner;  // Import the Scanner class

public class Swap
{
    static int First_Number;    //for store the first integer value from the user
    static int Second_Number;    //for store the  second integer value from the user
 
    public static void main(String args[])
    {

          Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        
        System.out.print("Enter First Integer value:-");
        First_Number = myObj.nextInt();  // Read user input
        
        System.out.print("Enter Second Integer value:-");
        Second_Number = myObj.nextInt();  // Read user input

        System.out.println("\n Before swap");
        System.out.println("First Integer Value is: " + First_Number);
        System.out.println("Second Integer Value is: " + Second_Number);

        //Function call
        swap(First_Number,Second_Number);
    }  


    //Function Declaration
    public static void swap(int first,int second)
    {
        //Changing the variable Values
        First_Number = second;
        Second_Number = first;

        //Printing the value
        System.out.println("\n After swap");
        System.out.println("First Integer Value is: " + First_Number);
        System.out.println("Second Integer Value is: " + Second_Number);
    }
}

OUTPUT:

Activities Terminal Mar 12 1:09 PM jignesh@Jignesh: -/Desktop jignesh@Jignesh:-/Desktop$ java Swap.java Enter First Integer v

  • Swapping static values:

============================Main.java ===============================

CODE:

 public class Main
{
    static int First_Number;    
    static int Second_Number;    
    
    public static void main(String args[])
    {
        First_Number = 10;
        Second_Number = 20;

        System.out.println("\n Before swap");
        System.out.println("First Integer Value is: " + First_Number);
        System.out.println("Second Integer Value is: " + Second_Number);

        swap(First_Number,Second_Number);


        First_Number = 15;
        Second_Number = 25;

        System.out.println("\n Before swap");
        System.out.println("First Integer Value is: " + First_Number);
        System.out.println("Second Integer Value is: " + Second_Number);

        swap(First_Number,Second_Number);
    }  

    public static void swap(int first,int second)
    {
        First_Number = second;
        Second_Number = first;

        System.out.println(" \n After swap");
        System.out.println("First Integer Value is: " + First_Number);
        System.out.println("Second Integer Value is: " + Second_Number);
    }
}

OUTPUT:

Activities Terminal Mar 12 1:15 PM jignesh@Jignesh: -/Desktop jignesh@Jignesh:-/Desktop$ java Main.java Before swap First Int

In case of any Query do comment , Rate the Answer as well .

Thank You :)

Add a comment
Know the answer?
Add Answer to:
3. Java: Write a Java method to swap two integers. Attach your code as well as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java code Write a method called reverse that takes an array of integers as an input...

    java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.

  • Java 3) Write the-G-code to convert a linked list of integers, to a stack of integers....

    Java 3) Write the-G-code to convert a linked list of integers, to a stack of integers. Thus, your code will traverse the linked list and populate the stack. [ 15 points ] Linked List 1→ 2→ 3 Stack 3 becomes First Last

  • java code Write a method that given a list of integers as a reference to its...

    java code Write a method that given a list of integers as a reference to its first node, determines if the list is sorted in non-decreasing order. Please select file(s) Select file(s)

  • Intro to Java Programming Write a method named isDivisible that takes two integers, n and m,...

    Intro to Java Programming Write a method named isDivisible that takes two integers, n and m, and that returns true if n is divisible by m, and false otherwise. Include your isDivisible() function as a method in Functions. In the main static method, demonstrate isDivisible() works by testing that it returns both possibilities correctly for a few different pairs of numbers. Include comments before your test code describing what's going on. The definition of divisibility is "One whole number is...

  • write the java code to convert a linked list of integers, to a stack of integers....

    write the java code to convert a linked list of integers, to a stack of integers. thus your code will traverse the linked listand populate the stsck. linked list 1->2->3. becomes stack 3 First Last 2 1

  • Write a Java program that allows the following: 1.Create an array list of 20 integers. The...

    Write a Java program that allows the following: 1.Create an array list of 20 integers. The 20 integers are generated randomly with possible values from 0 to 10. 2.Print the content of the array list. 3.Find how many of these values are multiple of 3. Show a screenshot of the output with your name and Id in the first line (if there is no screenshot of the output, the student shall get a zero mark for this question). Program typical...

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add...

    *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4, -3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • Java generics Write a program that calls a method to multiply two integers and return the...

    Java generics Write a program that calls a method to multiply two integers and return the integer result. Modify the program to make the method generic, calling it with two generic data types and then returning the result in the same type which also has to be generic. Call the method two times, once with an integer and once with a double in your main program and display the results

  • JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers....

    JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers. Write two loops that iterate from 0 through 35. Before each loop, set an integer variable (IV) to 1. Within each loop, print the loop count and the value of IV formatted in two columns. Within the first loop, multiply IV by 2. Within the second loop, multiply IV by the appropriate StrictMath method. The second loop will not complete since there will eventually...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT