having problems with my homework. 1. Write code to accomplish the following: cre
ID: 3666626 • Letter: H
Question
having problems with my homework.
1. Write code to accomplish the following:
create an array to hold 20 integers
fill the array with random numbers between 1 and 10 (inclusive)
iterate through the array and print each number to the console
iterate through the array and double the value of each even number stored in the array; odd numbers should not be changed
iterate through the array again and print each number to the console
2.
Write code to accomplish the following:
create an array to these five words: apple, pear, banana, peach, tomato
iterate through the array and print each word to the console
iterate through the array and replace each word with an upper-case version of that word (use a method instead of re-writing the words)
create a larger array that holds ten words; use a loop to copy the five words over then add five more of your own; make your original array reference point to this new array
3. Write a method to compute the average of the elements of an int array and return the value as a double. The method header is below. The int array and the number of elements in the array are both passed as parameters to the method.
You can assume that:
numElements is the correct number of elements in the array
numElements is > 0
numElements could be less than the size of the array (meaning some array positions might not hold values to be included in the average)
array elements are filled sequentially starting at position 0
public double calculateAverage(int[] nums, int numElements)
4. Write a method that takes an int array originalArray as a parameter. The method returns an int array that contains the contents of originalArray in reverse order. The elements of originalArray should remain the same when the method completes. The method header is below.
public int[] createReverseArray(int[] originalArray)
5.
Write a method called switchThem with the following method header. The method accepts two integer arrays as parameters. If the arrays are the same size, the method switches the contents of the arrays. If the arrays are different sizes, an error message is printed.
public void switchThem(int[] first, int[] second)
Hint: to test this method, invoke it from main. Print the contents of the two arrays from main before and after invoking the method. (You will need to add the "static" keyword to the method header to invoke it from main: public static void switchThem).
Thank you for people to answer this!
Explanation / Answer
1)
/**The java program Program1 that promtps user
* to generate 20 random numbers and print the numbers
* to console. The double the random vlaues of even numbers
* and print the numbers to console*/
//Program1.java
import java.util.Random;
public class Program1
{
public static void main(String[] args)
{
//create an array of type integer of size,20
int[] arr=new int[20];
//Create an Random class object
Random random=new Random();
//Generate an array of random numbers
//in a range of 1 to 10
for (int i = 0; i < arr.length; i++)
arr[i]=random.nextInt(10)+1;
//print numbers
System.out.println("Print numbers");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
//double the even numbers
for (int i = 0; i < arr.length; i++)
{
if(arr[i]%2==0)
arr[i]=2*arr[i];
}
System.out.println("Print numbers after double even numbers");
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
}
Sample output:
Print numbers
7
5
1
3
7
8
5
3
3
9
6
2
9
10
2
4
8
3
8
3
Print numbers after double even numbers
7
5
1
3
7
16
5
3
3
9
12
4
9
20
4
8
16
3
16
3
-------------------------------------------------------------------------------------------------------------------
2)
/**The java program Program2 that create an array of type stirng
* and conver the words to upper case. Then copy the words
* to newly created longer array. Add 5 more words to newly
* created array and reference new array to old array.*/
//Program2.java
public class Program2
{
public static void main(String[] args)
{
//create an array, words of type stirng
String words[]={ "apple","pear","banana","peach","tomato"};
System.out.println("Print words");
for (int i = 0; i < words.length; i++)
System.out.println(words[i]);
//convert words to upper case
for (int i = 0; i < words.length; i++)
words[i]=words[i].toUpperCase();
//create an array of longerarray of size,10
String[] longerarray=new String[10];
//copy the words to longerarray
for (int i = 0; i < words.length; i++)
{
longerarray[i]=words[i];
}
//create an array of type to my own words
String mywords[]={"sunday","monday","tuesday","wednesday","thurday"};
for (int i = 0; i < longerarray.length/2; i++)
{
longerarray[i+mywords.length]=mywords[i];
}
//Reference words to the longerarray
words=longerarray;
System.out.println("Printing new array ");
for (int i = 0; i < words.length; i++)
{
System.out.println(words[i]);
}
}
}
sample output:
Print words
apple
pear
banana
peach
tomato
Printing new array
APPLE
PEAR
BANANA
PEACH
TOMATO
sunday
monday
tuesday
wednesday
thurday
------------------------------------------------------------------------------------------------------------------------------------------------------------------
3)
/**The java program that calculates average of integer array. */
//Program3.java
public class Program3
{
public static void main(String[] args)
{
//Create an array
int arr[]={1,2,3,4,5,6,7,8,9,10};
//Call calculateAverage
double average=calculateAverage(arr, 10);
//print average
System.out.println("Average of array values "+average);
}
/**The method calculateAverage that takes an array of type integer
* and number of elements in the array and finds the averate of the
* numbers and returns the calling method*/
public static double calculateAverage(int[] nums, int numElements)
{
//set total to zero
double total=0;
for (int i = 0; i < numElements; i++)
//Add to total
total+=nums[i];
//calculate average
double average= total/nums.length;
//return average
return average;
}
}
sample output:
Average of array values 5.5
----------------------------------------------------------------------------------------------------------------------------------
4.
/**The java program that reverse the array and
* print the array elements.*/
//Program4.java
public class Program4
{
public static void main(String[] args)
{
//create an array
int arr[]={2,5,6,7,8,9};
System.out.println("Reverse array");
//call method createReverseArray
int reverse[]=createReverseArray(arr);
//print elements
for (int i = 0; i < reverse.length; i++)
System.out.println(reverse[i]+" ");
}
/**The method createReverseArray that takes an integer
* array and reverse the array and returns the new reverse
* array.*/
public static int[] createReverseArray(int[] originalArray)
{
//Create an array
int[] reverse=new int[originalArray.length-1];
int k=0;
//reverse array
for (int i = reverse.length-1; i >=0 ; i--,k++)
reverse[k]=originalArray[i];
//return reverse
return reverse;
}
}
Sample output:
Reverse array
8
7
6
5
2
------------------------------------------------------------------------------------------------------------------------
5.)
//Program5.java
public class Program5
{
public static void main(String[] args)
{
int arr1[]={1,2,3,4,5};
int arr2[]={6,7,8,9,10};
System.out.println("Before swapping");
System.out.println("arr1");
for (int i = 0; i < arr1.length; i++)
System.out.println(arr1[i]);
System.out.println("arr2");
for (int i = 0; i < arr2.length; i++)
System.out.println(arr2[i]);
switchThem(arr1, arr2);
System.out.println("After swapping");
System.out.println("arr1");
for (int i = 0; i < arr1.length; i++)
System.out.println(arr1[i]);
System.out.println("arr2");
for (int i = 0; i < arr2.length; i++)
System.out.println(arr2[i]);
}
/**The method switchThem that takes first and second array
* as input arguments and swaps the first array
* to second array.*/
public static void switchThem(int[] first, int[] second)
{
if(first.length==second.length)
{
for (int i = 0; i < second.length; i++)
{
int temp=first[i];
first[i]=second[i];
second[i]=temp;
}
}
}
}
Sample output:
Before swapping
arr1
1
2
3
4
5
arr2
6
7
8
9
10
After swapping
arr1
6
7
8
9
10
arr2
1
2
3
4
5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.