234k views
5 votes
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum values in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10.

I think I have the max and min part down, but I need help figuring how to return the new array with all the same values as the orginal plus 10....

Here is what I have so far for the max / min. please use this in helping to create a matching method that accepts the array as a parameter. :

/*

*/
package arrayvalues;

/**
*
* @author dreadsguy
*/
public class ArrayValues {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int [] numbers = {99,56,103,9,31,63,81,92,13};

int m = maximum (numbers);
int n = minimum (numbers);


System.out.println("max ="+ m);
System.out.println("min ="+ n);


}


public static int maximum (int [] numbers){
//index
int i=0;
//current maximum
int max;
//initial maximum
max = numbers[i];
//calculate maximum

for (i = 1; i < numbers.length; i++){
if (numbers[i]> max)
{
max = numbers[i];
}

}
return(max);
}

public static int minimum (int [] numbers){
//index
int i=0;
//current minimum
int min;
//initial minimum
min = numbers[i];
//calculate minimum

for (i = 1; i < numbers.length; i++){
if (numbers[i]< min)
{
min = numbers[i];
}


}
return(min);
}
}

1 Answer

2 votes

Answer:

Here is the additional method you need to write:

public static int [] newArrayNumbers (int [] numbers){

int [] newArray = new int[10];

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

newArray[i]=numbers[i];

}

newArray[9]=10;

return newArray;

}

The complete code is given in the explanation section

Step-by-step explanation:

import java.util.Arrays;

public class NewQues{

public static void main(String[] args) {

int [] numbers = {99,56,103,9,31,63,81,92,13};

int m = maximum (numbers);

int n = minimum (numbers);

System.out.println("max ="+ m);

System.out.println("min ="+ n);

System.out.println(Arrays.toString(newArrayNumbers(numbers)));

}

public static int maximum (int [] numbers){

int i=0;

int max;

max = numbers[i];

for (i = 1; i < numbers.length; i++){

if (numbers[i]> max)

{

max = numbers[i];

}

}

return(max);

}

public static int minimum (int [] numbers){

int i=0;

int min;

min = numbers[i];

for (i = 1; i < numbers.length; i++){

if (numbers[i]< min)

{

min = numbers[i];

}

}

return(min);

}

public static int [] newArrayNumbers (int [] numbers){

int [] newArray = new int[10];

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

newArray[i]=numbers[i];

}

newArray[9]=10;

return newArray;

}

}

This for statement here does the work for you. assigning the values in the first array to the second array.

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

newArray[i]=numbers[i];

}

The second logic is, since you knew the first array had 9 elements, you create the second to have 10 elements so you can assign the element at the last index (9) the value of ten as required by the question

User Graham Chiu
by
5.0k points