Ans:
Code:
public class Program {
static void simpleAry()
{
int a[] = new int[10];//declare array
for(int i=0;i<10;i++)
a[i] = -113;//for each element fill
//with -113
for(int i=0;i<10;i++)//print each element
System.out.print(a[i]+" ");
System.out.println() ;
}
static double sumAry(double[] a)
{
double sum=0.0;
//number of elements = a.length
//for each element add to sum
for(int i =0;i<a.length;i++)
sum = sum + a[i];
//return sum
return sum;
}
static boolean isElement(int[] a,double x)
{//for each element compare with x
for(int i =0;i<a.length;i++)
if(a[i]==x)//if found return true
return true;
return false; //else return false
}
static boolean isEqual(char[] a,char[] b)
{//if length differ return false
if(a.length!=b.length)
return false;
//compare element by element
for(int i = 0;i<a.length;i++)
if(a[i]!=b[i]) //if not equal return false
return false;
return true;
}
//for running the above methods
public static void main(String[] args) {
simpleAry();
double []a = {1.0,2.3,3.1,4.1};
System.out.println( sumAry(a));
double x = 3.0;
int []b = {1,2,4,3} ;
System.out.println(isElement(b,x));
char []c = {'a','b','c'};
char []d = {'a','b','c'};
System.out.println(isEqual(c,d));
}
}
Output:-
Output for the above functions
1st and 2nd line of o/p are of simpleAry()
3rd represent o/p for sumAry()
4th represent o/p for isElement()
5th represent o/p for isEqual()
Upload rest of the questions for other experts.
As per Chegg policy, An expert is allowed to answer first four question that i have done.
Please appreciate the work by giving a thumbs up.
If any doubt ask in the comments.
Stay safe and keep Chegging!
3. Write Java methods to accomplish each of the following Write a method named simpleAry which...
JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...
4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...
Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...
Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This method should accept a two-dimensional array as its argument and return the total of all the values in the array. Write overloaded versions of this method that work with int , double , and long arrays. (A) getAverage . This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Write overloaded versions of...
Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized to 0. A method named deposit that accepts a parameter of type double. The value of the balance instance variable is increased by the value of the parameter. A method named withdraw that accepts a parameter of type double. The value of the balance instance variable is decreased by the value of the parameter. A method named getBalance that accepts no parameters, returns the...
Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...
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...
write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...
Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...