Question

Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int...

Please Help! Need in Java C++ Object oriented programming. Thank you!

Method 2:
public static int sumCapped(int[] nums, int x)

This method will return the largest sum that is less than or equal x found in one pass of the array. This means that you must check each number in succession to determine whether or not you should add it in. For example, for the array {4, 2, 3, 5} with the value of the paramter x set to 7, the value 6 will be returned. If the array is empty, then the built-in constant Integer.MAX_VALUE may be returned.

Method 4:
public static int largestAscent(int[] values)

Given an array of integers, we can conceptually split it into separate non-decreasing sequences. Each sequence has a first and last item, and thus spans some range of values. Find the sequence that has the largest gap from its beginning low-point to its ending high-point, and return this distance. You may assume that there will always be one element in the array.

Examples:

sequence largest ascent reasoning
1,2,3,4,2,10 8 (10-2) is greater than (4-1)
1,5,5,10,2,9 9 1,5,5,10 counts as one ascent.
5 0 degenerate case; (5-5)==0.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Main
{
//method 4
public static int LargestAscent(int values[])
{
int max=0;
int p1=0,p2=0;
for(int i=0;i<values.length-1;i++)
{
if(i==0)
p1=values[i];
  
if(values[i+1]<values[i])
{
  
p2=values[i];
if(max< (p2-p1))//updating max value
max=p2-p1;
  
p1=values[i+1];
}
p2=values[i+1];
}
if(max< (p2-p1))//updating max value
max=p2-p1;
return max;
}
  
//method 2
public static int sumCapped(int nums[],int x)
{
int s=0;
if(nums.length==0)//if array is empty
return Integer.MAX_VALUE;
for(int i=0;i<nums.length;i++)//one pass
{

if(x<s+nums[i])
return s;
s=s+nums[i];//finding sum
}
return s;
}
   public static void main(String[] args) {
   //testing above methods
   int a[] = {4,2,3,5};
   int b[] = {1,2,3,4,2,10};
   int c[] = {1,5,5,10,2,9};
   int d[]= {5};
       System.out.println(sumCapped(a,6));
       System.out.println(LargestAscent(b));
       System.out.println(LargestAscent(c));
       System.out.println(LargestAscent(d));
   }
  
  
}

Add a comment
Know the answer?
Add Answer to:
Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int...
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
  • hi, can someone explain this method please: public static int sumCapped(int nums[] , int x) {...

    hi, can someone explain this method please: public static int sumCapped(int nums[] , int x) { int sum=0; if(nums.length==0) { return Integer.MAX_VALUE; } for(int i=0;i<nums.length;i++) { if(sum+nums[i]<=x) // check if adding the succssive element return gives a values less than or equal to x { sum=sum+nums[i]; // if true then add } } return sum; // return the output }

  • Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...

    Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[] two) Given two arrays, it returns a new array with all values that are in both arrays. The returned array has a size fitting its elements exactly and without duplicates. The order of values does not matter. For example, given {1,5,5,1} and {5,3}, the method returns {5}.

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

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

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Programming 3_4: Write the method public static int rangeProduct(int a, int b). Assume that a <...

    Programming 3_4: Write the method public static int rangeProduct(int a, int b). Assume that a < b. Your method must compute and return the product of the integers in the range from a to b. For example, if a = 3 and b = 6, your method would compute and return the product 3 × 4 × 5 × 6 = 360. public static int rangeProduct(int a, int b) {...}

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • please check first question before you do this one Java please it's data sturcture classs using...

    please check first question before you do this one Java please it's data sturcture classs using java editor netbeans 2.   3. public static int indexOfLargest(int[] arr) – if arr is null then return -1; otherwise return the index of the largest value. If the largest value appears more than one time, then return the left-most index. Example: If arr = {9, -2, 3, 50, 4, 99, 11, 4, 5, 99, 3, 4, -6}; then the method returns 5 4. public...

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