Question: Below is the code that I got. I want to understand the codes below, so I need step by step explanation of how the codes

Below is the code that I got. I want to understand the codes below, so I need step by step explanation of how the codes are working.
Also, I want descriptions of the object-oriented features of this program.

 

import java.util.*;

//to program prompt the user to enter the brand name of the equipment, the gain, and the cost
class Equipment{
    String name;
    double gain;
    double cost;

    public Equipment(String name, double gain, double cost) {
        super();
        this.name = name; //to assign value to the 'name' variable
        this.gain = gain;
        this.cost = cost;
    }
    public String getName() {
       
        return name;
    }
    public double getGain() {
       
        return gain;
    }
    public double getCost() {
       
        return cost;
    }
}

public class Main{
    //Main program to run and test the program
    public static void main (String[] args) {

        List list=new LinkedList();
        String name="default";
        double gain=0,cost=0;
        for(int loop=0;loop<3;loop++){
            System.out.println("Item "+(loop+1)+" : ");
            System.out.print("\tName of the item:");
            Scanner sc=new Scanner(System.in);
            name=sc.next();

            System.out.print("\tGain:$");
            gain=sc.nextDouble();
            System.out.print("\tCost:$");
            cost=sc.nextDouble();

            list.add(new Equipment(name, gain, cost));
        }
        list.sort(Comparator.comparingDouble(p->p.getGain()/p.getCost())); //Sorting list in ascending order of ROI.
        Collections.reverse(list); //Reverse the list.Now list in increasing descending order.
        System.out.println("The items sorted according to ROI:");

        int loop=1;
        list.forEach(p->{
            System.out.println("Details of item "+loop);
            System.out.println("\t Name:"+p.getName());
            System.out.println("\t Gain:$"+p.getGain());
            System.out.println("\t Cost:$"+p.getCost());
        });
    }
}

 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!