Question

Please use Java to solve this problem

Problem Cases Output 05m 465 The current selected programming language is Java. We emphasize the submission of a fully workinProblem Test Cases Output Java 05m 265 Note There is at least one pair whose combined weight is less than or equals to the ma

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TwoHeaviest.java

import java.util.Arrays;

public class TwoHeaviest {
    public static class Pair {
        double first;
        double second;
        Pair(double first, double second) {
            this.first = first;
            this.second = second;
        }
    }

    public static Pair solution(int numContainers, double maxCapacity, double[] containersWt) {
        Arrays.sort(containersWt);
        int r = numContainers - 1;
        double max = 0;
        Pair ans = null;
        for (int l = 0; l < r; l ++) {
            while (r > l && containersWt[l] + containersWt[r] > maxCapacity) r --;
            if (r > l && containersWt[l] + containersWt[r] > max) {
                max = containersWt[l] + containersWt[r];
                ans = new Pair(containersWt[l], containersWt[r]);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int numContainers = 6;
        double maxCapacity = 33.3;
        double[] containersWt = {7.33, 8.13, 103.7313, 11.24, 23.79, 18.3501};
        Pair ans = solution(numContainers, maxCapacity, containersWt);
        System.out.println(ans.first + " " + ans.second);
    }
}

Add a comment
Know the answer?
Add Answer to:
Please use Java to solve this problem Problem Cases Output 05m 465 The current selected programming...
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
  • ZYBOOKS LAB: 10.23.1 Hello, I'm trying to solve this problem with creating a program code in...

    ZYBOOKS LAB: 10.23.1 Hello, I'm trying to solve this problem with creating a program code in Python about People's Weights. Here is the question: 10.23 LAB: Warm up: People's weights (Lists) (1) Prompt the user to enter four numbers, each corresponding to a person's weight in pounds. Store all weights in a list. Output the list. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3]...

  • Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...

    Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali owns a coconut plantation and a number of monkeys for bringing down coconuts. Every day, Ali would record the number of coconuts brought down by his monkeys. At the end of a certain time period, he would determine from the data recorded, the lowest and the highest number of coconuts as well as their number of occurrences. Write a program...

  • Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif...

    Write a C++/Java program to solve the problem below using Memoization: Problem Definition: Theres a theif that enters a store during the night,and upon his entrance,the stores security systems detects the theif and the alarm goes off.The theif knows that theres a hidden door at the corner of this store and in order to escape,he has to get to it.Since the theif has limited time for his burglary,he can only Rob the stuff he comes by on his path towards...

  • Write the code in C with all the right output please. Problem: In this assignment you...

    Write the code in C with all the right output please. Problem: In this assignment you will analyze a data set of 10,000 randomly generated integers. From this data set you will display a count of the prime numbers that are greater than the average (double) of the entire data set. The user will input only a single integer value to serve as the seed for the random number generator. The use of a common seed will result in output...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you......

    Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you... PROGRAM 1 –Linear Data Structure Implementation (Due date: March 5th, 2019, 5% Grade Points) Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically...

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

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