Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.LinkedList; import java.util.Scanner; import java.io . File; public class Lab 2 0 B { public static void main ( String [ ] args )
import java.util.LinkedList;
import java.util.Scanner;
import java.ioFile;
public class LabB
public static void mainString args throws Exception
LinkedList baskets new LinkedList;
File file new FileInputBtxt;
Scanner scanner new Scannerfile;
while scannerhasNextLine
String line scanner.nextLine;
String parts line.split;
try
int apples Integer.parseIntparts;
int oranges Integer.parseIntparts;
int pineapples Integer.parseIntparts;
baskets.addnew FruitBasketapples oranges, pineapples;
catch NumberFormatException e
System.out.printlnInvalid input format: line;
scanner.close;
System.out.printlnFruit Baskets:";
for FruitBasket basket : baskets
System.out.printlnbasket;
FruitBasket highestValueBasket findHighestValueBasketbaskets;
System.out.printlnHighest Value Basket: highestValueBasket;
public static FruitBasket findHighestValueBasketLinkedList baskets
MyComparator mycomp new MyComparator;
FruitBasket highestValue baskets.getFirst;
forFruitBasket basket : baskets
ifmycompcomparehighestValue basket
highestValue basket;
return highestValue;
import java.util.Comparator;
public class MyComparator implements Comparator
@Override
public int compareFruitBasket f FruitBasket f
int value fgetNumberOfApples fgetNumberOfOranges fgetNumberOfPineapples;
int value fgetNumberOfApples fgetNumberOfOranges fgetNumberOfPineapples;
if value value
return ;
else if value value
return ;
else
return ;
public class FruitBasket
private int apples;
private int oranges;
private int pineapples;
public FruitBasketint apples, int oranges, int pineapples
this.apples apples;
this.oranges oranges;
this.pineapples pineapples;
public int getNumberOfApples
return apples;
public int getNumberOfOranges
return oranges;
public int getNumberOfPineapples
return pineapples;
public String toString
return "Apples: apples Oranges: oranges Pineapples: pineapples;
output:
Invalid input format: apples
Invalid input format: oranges
Invalid input format: pineapples
Invalid input format: apples
Invalid input format: oranges
Invalid input format: pineapples
Invalid input format: apples
Invalid input format: oranges
Invalid input format: pineapple
Invalid input format: apples
Invalid input format: oranges
Invalid input format: pineapple
Fruit Baskets:
Exception in thread "main" java.util.NoSuchElementException
at java.basejavautil.LinkedList.getFirstLinkedListjava:
at LabBfindHighestValueBasketLabBjava:
at LabBmainLabBjava:
I cannot get it fixed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started