1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nimfa-mama [501]
2 years ago
14

Write two public static methods in the U6_L4_Activity_Two class. The first should have the signature swap(int[] arr, int i, int

j) and return type void. This method should swap the values at the indices i and j of the array arr (precondition: i and j are both valid indices for arr). The second method should have the signature allSwap(int[] arr) and return type void. The precondition for this method is that arr has an even length. The method should swap the values of all adjacent pairs of elements in the array, so that for example the array {3, 5, 2, 1, 8, 10} becomes {5, 3, 1, 2, 10, 8} after this method is called.
Computers and Technology
1 answer:
Tems11 [23]2 years ago
4 0

Answer:

public static void swap(int[] arr, int i, int j) {

   if(i >=0 && i <=arr.length-1 && j >=0 && j<=arr.length-1){

       int hold = arr[i];

       arr[i] = arr[j];

       arr[j] = hold;

   }

}

public static void allSwap(int[] arr) {

   if (arr.length % 2 == 0){

       for(int i=0; i<arr.length; i+=2){

           int hold = arr[i+1];

           arr[i+1] = arr[i];

           arr[i] = hold;

       }

   } else{

       System.out.println("array length is not even.");

   }

}

Explanation:

The two functions, swap and allSwap are defined methods in a class. The latter swaps the item values at the specified index in an array while the second accepts an array of even length and swap the adjacent values.

You might be interested in
Create two classes. The first, named Sale, holds data for a sales transaction. Its private data members include the day of the m
olya-2409 [2.1K]

Answer:

A C++ program was used in creating two classes. the code is stated below.

Explanation:

Solution

The C++ program is executed below:

#include<iostream>

using namespace std;

//declare class (will be define later)

class Salesperson;

//class Sale

class Sale

{

  //private members of class

  private:

      string day;

      double amtOfSale;

      int salesPersonId;

 //public methods

  public:

      //constructor that takes day,amount of sale and salesPersonId as parameters

      Sale(string date,double sale,int id)

      {

          //set the private members to the initial values passed

          day=date;

          amtOfSale=sale;

          salesPersonId=id;

      }    

      //declare a friend function that takes objects of the two classes as parameters

      friend void display(Sale,Salesperson);

};  

//define class Salesperson

class Salesperson

{

  //private members of the class

  private:

      int salesPersonId;

      string lastName;    

  //public methods

  public:

      //constructor that takes name and id as parameters

      Salesperson(int id,string name)

      {

          //set the members of the class with the parameters passed

          salesPersonId=id;

          lastName=name;

      }  

      //declare a friend function that takes objects of the two classes as parameters

      friend void display(Sale,Salesperson);

};

//define the friend funtion

void display(Sale saleObj,Salesperson personObj)

{

  //display the sales info using the objects of the two classes

  cout<<"\nSale #"<<saleObj.salesPersonId<<" on "<<saleObj.day<<" for $"<<saleObj.amtOfSale<<" sold by #"<<personObj.salesPersonId<<" "<<personObj.lastName;

}  

int main()

{

  //create object for Sale class and pass default values

  Sale sObj1("12/25/2016",559.95,103);

  //create object for Salesperson class and pass default values

  Salesperson pObj1(103,"Woods");

 

  //create another object for Sale class and pass default values

  Sale sObj2("11/15/2016",359.95,106);

  //create another object for Salesperson class and pass default values

  Salesperson pObj2(106,"Hansen");

  //using the friend function dislay the sales info

  display(sObj1,pObj1);

  display(sObj2,pObj2);

  return 0;

}

4 0
3 years ago
To prevent rust from forming, a light coating of_____should be applied to all machined surfaces
Fantom [35]
What is that I never heard of that before
4 0
3 years ago
Simplify the Boolean expression (AB(C + BD) + AB]CD.
finlep [7]

Explanation:

Simplify the Boolean expression (AB(C + BD) + AB]CD.

6 0
2 years ago
Read 2 more answers
Make a hierarchical directory structure under /root that consists of one directory containing three subdirectories.
Alika [10]
You can make a hierarchical directory structure under /root that consists of one directory containing subdirectories  by using cd and mkdir
the mkdir command created the directories while the cd command changes which directory you're currently in
5 0
2 years ago
Explain how to number text in a document​
uranmaximum [27]

Answer:

Lol

Explanation:

Yes iri

8 0
3 years ago
Read 2 more answers
Other questions:
  • What is the primary criticism against a national identification system based on biometric data?
    13·1 answer
  • How would you say an hard drive works
    9·2 answers
  • Two technicians are discussing the voltage measurements taken on the circuit below: V1 = 12Volts; V2 = 12Volts; V3 = 0Volts; V4
    11·1 answer
  • 1. In Access, a template is which of the following? a. A database to manage contacts b. Where a database is stored c. Two tables
    5·1 answer
  • What is an instruction set architecture​
    7·2 answers
  • Which C99 function can be used to convert a string to a double?
    15·1 answer
  • For some reason, Danica's classmate George could not find the registered symbol in the symbol gallery. He is selling
    14·1 answer
  • Courses that enable students to begin jobs that require course completion certificates are know as
    8·1 answer
  • For C++ ONLY please,
    14·1 answer
  • SOMEONE PLEASE HELP ME WITH THIS PLEASE HELP ME PLEASE!!!!!!
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!