-1

Hello I am new at java and haven't worked with lists. I need to find the maximum value of the 3 classes and print it out. Here is my code so far I don't know if I am way out of track but I did my best.

package com.emir;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Shape {

    public static Shape getShapeWithMaxvolume(List<Float> shapes) {
        Cone cone = new Cone(15,11);
        Cylinder cylinder = new Cylinder(12,5);
        Cuboid cuboid = new Cuboid(15,2,1);

        shapes.add(cone.getSum());
        shapes.add(cylinder.getSum());
        shapes.add(cuboid.getSum());

        for(int i = 0; i <shapes.size();i++){
            Collections.sort(shapes);
            System.out.println("The max value is " + shapes.get(i));
        }
        return null;
    }

}

Please help I am on a deadline with this one. or at least offer advice. for the geometric shapes, I extended from Shape (this class) I made a private variable sum to calculate the volume of the particular shape and made getter and setter for all of the shapes.

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
  • 1
    [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569/3788176) – Andy Turner Aug 06 '19 at 06:44
  • 1
    You don't want `Collections.sort(shapes);` inside the loop. You shouldn't need to sort it more than once, so that should be outside your loop. – Andy Turner Aug 06 '19 at 06:45
  • ```Collections.sort(shapes); System.out.println("The max value is " + shapes.get(shapes.size()-1));``` will give max value. Do this without loop. – Adarsh Anurag Aug 06 '19 at 06:48
  • You dont even need for loop, sort it in decreasing order and first element is max value. – voucher_wolves Aug 06 '19 at 06:49
  • @yashpandey you don't need to sort a list to get the maximal element. – Andy Turner Aug 06 '19 at 06:49
  • @AndyTurner yes, either you want for loop then no need of sorting and if you just sort then no need of for loop. Anyway in any of this case, there will be for loop involved somewhere :) – voucher_wolves Aug 06 '19 at 06:51
  • Ok what do i do? can anyone show me a solution how to do it ? – Emir Mustafoski Aug 06 '19 at 06:51
  • 1
    If you want to get he Max value from the list, use Collections.max(shapes). you dont have to sort. – JavaLearner1 Aug 06 '19 at 06:53
  • 3
    @EmirMustafoski finding the maximal element in a list is a really fundamental thing to know how to do. Simply telling you how to do it [skips the learning experience](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). Use your preferred search engine to search for something like "Java maximal list element". – Andy Turner Aug 06 '19 at 06:54

1 Answers1

-2

Why your code is not working as desired?

for(int i = 0; i <shapes.size();i++){
            Collections.sort(shapes); //You are sorting your collection for total size times as it is inside the loop. Only one sort outside the loop should work
            System.out.println("The max value is " + shapes.get(i));// You are displaying all the values of your sorted collection as it is inside loop
        }

With the following proposed changes, you will be able to get maximum value out of shapes. The changes have comment next to them to explain them.

package com.emir;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Shape {

    public static Shape getShapeWithMaxvolume(List<Float> shapes) {
        Cone cone = new Cone(15,11);
        Cylinder cylinder = new Cylinder(12,5);
        Cuboid cuboid = new Cuboid(15,2,1);

        shapes.add(cone.getSum());
        shapes.add(cylinder.getSum());
        shapes.add(cuboid.getSum());

        Collections.sort(shapes); //sort your collection
        System.out.println("The max value is " + shapes.get(shapes.size()-1)); // get last element from shapes for max value.

        return null;
    }

}

or

package com.emir;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Shape {

    public static Shape getShapeWithMaxvolume(List<Float> shapes) {
        Cone cone = new Cone(15,11);
        Cylinder cylinder = new Cylinder(12,5);
        Cuboid cuboid = new Cuboid(15,2,1);

        shapes.add(cone.getSum());
        shapes.add(cylinder.getSum());
        shapes.add(cuboid.getSum());


        float maxVal = -1;
        for(int i = 0; i <shapes.size();i++){//iterate over all values to get max value
            if(shapes.get(i)>maxVal)//checking if value at position I can be max value or not
                 maxVal = shapes.get(i);
        }
        System.out.println("The max value is " + maxVal);
        return null;
    }

}

Edit: I forgot about it. Thanks to @JavaLearner1 in the comments.

maxVal = Collections.max(shapes); 
//it returns the maximum element of the given collection according to the natural ordering of elements.
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Adarsh Anurag
  • 630
  • 5
  • 17