Question

Create a public static method named valueG that takes an ArrayList of type Double and returns...

Create a public static method named valueG that takes an ArrayList of type Double and returns a double. This method returns the maximum result ( do not return the original value) of taking the sine of each value from the input
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

import java.util.*;

//Declare main class TestStaticMethod
class TestStaticMethod
{
// Create and implement static method valueG() with ArrayList Arugment "arr"
public static double valueG(ArrayList<Double> arr)
{
Double max=Double.MIN_VALUE; // Declare double variable max with constant MIN_VALUE
System.out.print(arr); // display sine of array list
for(double i:arr) // declare foreach loop for ArrayList Object "arr"
{
if(i>max) max=i; // check condition is true then store maximum value into max

}

return max; // return max value
}

public static void main(String args[])
{
ArrayList<Double> a=new ArrayList<Double>(); // Declare ArrayList Object a of Double elements
Scanner scr=new Scanner(System.in); // Declare Scanner Object scr for reading elements
double num; // declare double variable num
int size; // declare integer variable size
System.out.print("Enter size of an Array: ");
size=scr.nextInt(); // read size of an array
System.out.println("Enter "+size+" Array Elements:");
for(int i=0;i<size;i++){
num=scr.nextDouble(); // read array elements
a.add(Math.sin(num)); // assign ArrayList Object a with sine values
}


double m=valueG(a); // calling valueG() method and stores the maximum result into m
System.out.println("\n\nMaximum: "+m); // display maximum element
}
}

OUTPUT-1


F:\>javac TestStaticMethod.java

F:\>java TestStaticMethod
Enter size of an Array: 5
Enter 5 Array Elements:
12.36
45.12
1.35
2.34
96.34
[-0.20490888331957058, 0.9076718222547393, 0.9757233578266591, 0.7184647930691263, 0.86711071009699]

Maximum: 0.9757233578266591

OUTPUT-2


F:\>java TestStaticMethod
Enter size of an Array: 10
Enter 10 Array Elements:
63.12
3.26
0.36
45.36
7.12
6
45.36
1.25
3.64
12.9
[0.28417604045984296, -0.11813085589181738, 0.35227423327508994, 0.9814153067011522, 0.7425132717958018, -0.27941549819892586, 0.9814153067011522, 0.9489846193555862, -0.47802724613534286, 0.32747443913769303]

Maximum: 0.9814153067011522

Add a comment
Know the answer?
Add Answer to:
Create a public static method named valueG that takes an ArrayList of type Double and returns...
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* Create a public static method named maxRes that takes an ArrayList of type Double and...

    *Java* Create a public static method named maxRes that takes an ArrayList of type Double and returns a double. This method returns the maximum result (do not return the original value) of taking the tangent of each value from the input

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • (Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList...

    (Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0. public static Integer max(ArrayList<Integer> list) Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach.

  • 1) Write a public static method named printArray, that takes two arguments. The first argument is...

    1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument. For example, given the following Array declaration and instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9 printArray(myArray,...

  • *q3: Write a public static method named q3 that takes no parameters and has return type...

    *q3: Write a public static method named q3 that takes no parameters and has return type void. In this method, you may assume there is a file named "properties.csv" with lines in the format "name, opposed, pure,glad" where "name" is a String and all other values are well-formed integers. There is no header line in this file. This method will create a new * file named "output.csv" in the format "name, pure" containing only these two columns from "properties.csv" and...

  • Write a static method named findMin that accepts an ArrayList of Person objects named list. Each...

    Write a static method named findMin that accepts an ArrayList of Person objects named list. Each Person object has private myAge and myName properties and corresponding public getAge and getName accessors. The method must return the name of the youngest person. public static String findMin(ArrayList<Person>list) {

  • java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...

    java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...

  • /** * Write a method named outOfOrder that takes as a parameter an array of double...

    /** * Write a method named outOfOrder that takes as a parameter an array of double and returns a value of type int. * This method will test the array for being out of order, meaning that the array violates the conditions: * array[0] <= arrayValues[0] <= arrayValues[2] <=... * * The method returns -1 if the elemnts are not out of order; otherwise , it returns the index of the first element of the array that is out of...

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

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