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
Anestetic [448]
2 years ago
8

1. Write a static method named computeOddSum that is passed an array of int's named numbers. The method must compute and return

the sum of all the values in numbers that are odd. For example, if numbers is {3, 10, 11, 2, 6, 9, 5} then the value 28 should be returned since 3 11 9 5
Computers and Technology
1 answer:
S_A_V [24]2 years ago
5 0

Answer:

The method in Java is as follows:

public static int computeOddSum(int [] myarray){

       int oddsum = 0;

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

        if(myarray[i]%2==1) {

           oddsum+=myarray[i];

        }

     }

     return oddsum;

   }

Explanation:

This defines the static method

public static int computeOddSum(int [] myarray){

This initializes oddsum to 0

       int oddsum = 0;

This iterates through the array

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

This checks for odd number

        if(myarray[i]%2==1) {

The odd numbers are added, here

           oddsum+=myarray[i];

        }

     }

This returns the calculated sum of odd numbers

     return oddsum;

   }

To call the method from main, use:

<em>int [] myarray = {3, 10, 11, 2, 6, 9, 5};</em>

<em>      System.out.println("Odd sum = " + computeOddSum(myarray));</em>

You might be interested in
Your isp connects to the core routers of the internet via a _____. cable mode spine backbone satellite
melisa1 [442]

fairly certain its backbone

5 0
3 years ago
If you have different movie or audio file types saved to your computer and your computer can only play the OGG format instead of
galina1969 [7]

Answer:

convert the MP4 format file to the OGG format file                  

Explanation:        

An OGG file is a compressed audio file of Ogg Vorbis that is used for holding audio data. It is a free and an open container file format which is maintained by Xiph.Org Foundation. OGG format file can multiplex independent streams of video, text, audio and meta data.

The OGG file has better sound quality and characteristics than a MP3 or MP4 files. The MP4 file format can be converted into OGG format file if the files are saved in different formats in the computer which can play only OGG format.

5 0
2 years ago
An employee at a branch office is creating a quote for a customer. In order to do this, the employee needs to access confidentia
SOVA2 [1]

The employee would access an intranet to maintain security.

6 0
3 years ago
Write a loop that fills a list values with ten random numbers between 1 and 100. Write code for two nested loops that fill value
Alex787 [66]

Answer:

Please look for your answer on Stack Overflow, it's way better.

Explanation:

4 0
2 years ago
It is a well-researched fact that men in a restroom generally prefer to maximizetheir distance from already occupied stalls, by
Artyom0805 [142]

Answer:

The code below addresses the problem statement with appropriate comments for explanation.

Explanation:

public class Stall

{

public static void main(String[] args)

{

boolean[] arr = new boolean[10];

while(hasFreeSpots(arr))   // as long as there is a free slot

{

arr[position(arr)] = true;  

// position(arr) gets the position of slot a person would most likely enter at(view problem statement)

// setting that position true(position occupied) by arr[position(arr)]

System.out.println(stallView(arr));   // printing current state of the stall using 'X' and '_'

}

}

/**

* Find the empty position that is in the middle of the sub-array of largest group of empty spots

* @param arr an array of boolean values

* @return returns an an integer value. Optimal position to be occupied.

*/

public static int position(boolean[] arr)

{

int first = 0;   // first stores the starting position of "longest sequence of unoccupied places"

int last = 0;   // last stores the ending position of "longest sequence of unoccupied places"

int temp1 = 0;   // temp1 stores the starting position of "current sequence of unoccupied places"

int temp2 = 0; // temp2 stores the ending position of "current sequence of unoccupied places"

 

for(int i = 0; i < arr.length; i++)   // for each slot in the Stall

{    

if(!arr[i]) // if slot i is not occupied

{    

temp1 = i;   // store starting position of "current sequence of unoccupied places" in temp1

while(i < arr.length && !arr[i] )   // if i is a valid index and as long as this slot is not occupied increment i

{

i++;

}

// we break out of the loop either if i is invalid index(reached end of the array)

// or if the slot is occupied by a person already

temp2 = i;   // store ending position of "current sequence of unoccupied places" in temp2

if(Math.abs(first - last) < Math.abs(temp1 - temp2))

  // if current sequence is longer than longest sequence, update it

{

first = temp1;

last = temp2;    

}

}

}

return (first + last) / 2;   // return middle index of first and last

}

/**

* Checks if there are empty spots in an array, basically checks if there is a false boolean value

* thats representing an empty spot in an array

* @param arr the input array, of boolean values

* @return returns a boolean value, true if there are false elements inside the array

*/

public static boolean hasFreeSpots(boolean[] arr)

{

boolean rtn = true;   // initializing rtn to 'true'

for(int i = 0; i < arr.length; i++)   // for every slot in the Stall

{

if(!arr[i]){rtn = false;}   // sets rtn to false if a free spot found

}

return !rtn;   // returns true if a free spot found and returns false if no free spot found

}

/**

* X for true, _ for false. Representing occupied(X) and free stalls(_)

* @param arr a boolean array

* @return returns a string representation if the input

*/

public static String stallView(boolean[] arr)

{

String str = "";       // initializing str to be an empty String

for(int i = 0; i < arr.length; i++)   // for every slot in Stall

{

if(arr[i]){str += " X";}   // Add X to str if slot is occupied

else {str += " _";}           // prints _ to str if slot not occupied

}

return str;       // return str(pictorial representation of Stall with 'X', '_')

}

}

7 0
3 years ago
Other questions:
  • When you notice a safety hazard, what should your first step be?
    12·1 answer
  • A user is attempting to format a 4 TB HDD using NTFS but only has the option to format the disk with a size of approximately 2 T
    15·1 answer
  • All of the following are ways to limit what is tracked about you online, except:
    12·1 answer
  • Create a program in Python that prompts the user to enter an integer number within the range of 1 to 10 inclusive. The program s
    15·2 answers
  • What will be the value of “sumtotal” when this code is executed?
    6·1 answer
  • Is the following statement true or false?
    12·1 answer
  • 1) APPLICATION BASED QUESTION:-
    5·1 answer
  • When you purchase donuts, they come in boxes of 12, so 24 donuts take 2 boxes. All donuts need to be in a box, so if you buy 13
    10·1 answer
  • Identify five type of application software​
    12·2 answers
  • I need help now I really do what is the answer thank you
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!