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

  • 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...

  • *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 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

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a 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) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Create a C# Using a GUI, Define a Method named CalcPay that takes as argument the...

    Create a C# Using a GUI, Define a Method named CalcPay that takes as argument the hours and rate and return the gross pay, the method signature is as follow: public static double CalcPay(double hours, double rate) Take into consideration that an employee has to be paid 1.5 the rate for the hours more than 40.

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

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
ADVERTISEMENT