Answered step by step
Verified Expert Solution
Link Copied!

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.io.File;
public class Lab20B {
public static void main(String[] args) throws Exception {
LinkedList baskets = new LinkedList>();
File file = new File("Input20B.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] parts = line.split("");
try {
int apples = Integer.parseInt(parts[0]);
int oranges = Integer.parseInt(parts[1]);
int pineapples = Integer.parseInt(parts[2]);
baskets.add(new FruitBasket(apples, oranges, pineapples));
} catch (NumberFormatException e){
System.out.println("Invalid input format: "+ line);
}
}
scanner.close();
System.out.println("Fruit Baskets:");
for (FruitBasket basket : baskets){
System.out.println(basket);
}
FruitBasket highestValueBasket = findHighestValueBasket(baskets);
System.out.println("Highest Value Basket: "+ highestValueBasket);
}
public static FruitBasket findHighestValueBasket(LinkedList baskets){
MyComparator mycomp = new MyComparator();
FruitBasket highestValue = baskets.getFirst();
for(FruitBasket basket : baskets){
if(mycomp.compare(highestValue, basket)>0){
highestValue = basket;
}
}
return highestValue;
}
}
**************
import java.util.Comparator;
public class MyComparator implements Comparator {
@Override
public int compare(FruitBasket f1, FruitBasket f2){
int value1= f1.getNumberOfApples()*5+ f1.getNumberOfOranges()*2+ f1.getNumberOfPineapples()*10;
int value2= f2.getNumberOfApples()*5+ f2.getNumberOfOranges()*2+ f2.getNumberOfPineapples()*10;
if (value1> value2){
return 1;
} else if (value1 value2){
return -1;
} else {
return 0;
}
}
}
**********************
public class FruitBasket {
private int apples;
private int oranges;
private int pineapples;
public FruitBasket(int 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: 5 apples
Invalid input format: 2 oranges
Invalid input format: 1 pineapples
Invalid input format: 2 apples
Invalid input format: 5 oranges
Invalid input format: 0 pineapples
Invalid input format: 7 apples
Invalid input format: 7 oranges
Invalid input format: 0 pineapple
Invalid input format: 2 apples
Invalid input format: 2 oranges
Invalid input format: 10 pineapple
Fruit Baskets:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.LinkedList.getFirst(LinkedList.java:248)
at Lab20B.findHighestValueBasket(Lab20B.java:37)
at Lab20B.main(Lab20B.java:31)
I cannot get it fixed
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

( 4 . Also us as well as SNOSIN ( 4 . Also us as well as SNOSIN

Answered: 1 week ago

Question

What is the per-capita cost?

Answered: 1 week ago

Question

Timeline for progress report

Answered: 1 week ago