Question: Please help with Java program output: Input: 5 Cheese; 1 1 Foil, 7 Socks, - 1 CallProduct.java - import java.util.ArrayList; import java.util.Scanner; public class CallProduct

Please help with Java program output:
Input:
5 Cheese; 11 Foil, 7 Socks, -1
CallProduct.java
-
import java.util.ArrayList;
import java.util.Scanner;
public class CallProduct {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
ArrayList productList = new ArrayList();
int currPrice;
String currName;
int i;
Product resultProduct;
currPrice = scnr.nextInt();
while (currPrice >=0){
resultProduct = new Product();
currName = scnr.next();
resultProduct.setPriceAndName(currPrice, currName);
productList.add(resultProduct);
currPrice = scnr.nextInt();
}
resultProduct = productList.get(0);
for (i =0; i < productList.size(); ++i){
if (productList.get(i).getPrice()< resultProduct.getPrice()){
resultProduct = productList.get(i);
}
}
System.out.println(resultProduct.getName()+": "+ resultProduct.getPrice());
}
}
Product.java
-
public class Product extends CallProduct {
private int price;
private String name;
public void setPriceAndName(int productPrice, String productName){
price = productPrice;
name = productName;
}
public int getPrice(){
return price;
}
public String getName(){
return name;
}
}
-
Also, if you would be able to help understand how to run multiple classes in one java file, I would appreciate it. I am using my own compiler for java code but I am having a hard time understanding how to run both CallProduct.java and Product.java at the same time, if it is even possible. Thank you.

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!