在这种情况下,我必须制定一种方法来返回所有重量大于或等于输入参数的直方糖。我尝试做自己的方法,但似乎不起作用。任何建议或帮助将不胜感激。我将在底部发布这两种方法。.我尝试使用forloop,但是我不知道如何将数字加在一起。
主要:
import java.util.ArrayList;
public class Lab16
{
public static void main(String[] args)
{
ArrayList<CandyBar> bars = new ArrayList<CandyBar>();
addCandyBarsToList(bars);
/**
* Use the methods you wrote below to answer all of
* the following questions.
*/
/**
* Part A:
*/
/**
* Is Twix in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
int location = findCandyBar(bars, "Twix");
if(location == -1){
System.out.println("The Candy bar is not in the list");
} else{
bars.get(location).printInfo();
}
/**
* Is Mounds in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
System.out.println();
int index = findCandyBar(bars, "Mounds");
if(index == 1){
System.out.println("The Candy bar is not in the list");
} else{
bars.get(index).printInfo();
}
/**
* Part B:
*/
/**
* Print all the names of candy bars with yellow wrappers.
*/
findByColor(bars, CandyBar.Color.YELLOW);
/**
* Part C:
*/
/**
* Count how many candy bars are 1.75 oz or more.
*/
double weight = countByWeight(bars, 1.75);
if(weight >= 1.75){
System.out.println(weight);
}else{
System.out.println("They are no candy bars with that much weight");
}
/**
* Part D:
*/
/**
* Is there a candy bar that is 1.86 oz?
* If so, print the information.
* If not, print a message to the user.
* Write a binary search method to get the answer.
*/
}
/**
* This method searches a list to find a candy bar by name.
*
* @param list the list to search
* @param n a name to find
* @return the index of the candy bar
*/
public static int findCandyBar(ArrayList<CandyBar> list, String n)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getName() == n){
return i;
}
}
return 1;
}
/**
* This method prints the names of the candy bars that have a certain
* wrapper color.
*
* @param list the list to search
* @param c the color wrapper to find
*/
public static void findByColor(ArrayList<CandyBar> list, CandyBar.Color c)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getWrapperColor() == CandyBar.Color.YELLOW){
System.out.println(list.get(i));
} else{
System.out.println("There is no such candy bar with that color");
}
}
}
/**
* This method counts the number of candy bars that weigh greater than
* or equal to the weight input parameter.
*
* @param list the list to search
* @param w the weight to compare to
* @return the count of candy bars
*/
public static int countByWeight(ArrayList<CandyBar> list, double weight)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getWeight() >= 1.70){
return
}
}
return 1;
}
/**
* This method searches a list using binary search.
*
* @param list the list to search
* @param first the first index
* @param last the last index
* @param w the value to search for
* @return the index of the candy bar
*/
public static int binaryFind(ArrayList<CandyBar> list, int first, int last, double w)
{
return -1;
}
/**
* This method adds candy bars to a list.
*
* @param list the list to add to
*/
public static void addCandyBarsToList(ArrayList<CandyBar> list)
{
CandyBar kitkat = new CandyBar("KitKat", CandyBar.Color.RED, 1.5);
list.add(kitkat);
CandyBar grand = new CandyBar("One-hundred Grand", CandyBar.Color.RED, 1.5);
list.add(grand);
CandyBar crunch = new CandyBar("Crunch", CandyBar.Color.BLUE, 1.55);
list.add(crunch);
CandyBar hershey = new CandyBar("Hershey", CandyBar.Color.BROWN, 1.55);
list.add(hershey);
CandyBar krackel = new CandyBar("Krackel", CandyBar.Color.RED, 1.55);
list.add(krackel);
CandyBar caramello = new CandyBar("Caramello", CandyBar.Color.PURPLE, 1.6);
list.add(caramello);
CandyBar what = new CandyBar("Whatchamacallit", CandyBar.Color.YELLOW, 1.6);
list.add(what);
CandyBar almond = new CandyBar("Almond Joy", CandyBar.Color.BLUE, 1.61);
list.add(almond);
CandyBar goodbar = new CandyBar("Mr. Goodbar", CandyBar.Color.YELLOW, 1.75);
list.add(goodbar);
CandyBar twix = new CandyBar("Twix", CandyBar.Color.GOLD, 1.79);
list.add(twix);
CandyBar henry = new CandyBar("Oh Henry", CandyBar.Color.YELLOW, 1.8);
list.add(henry);
CandyBar milkyWay = new CandyBar("Milky Way", CandyBar.Color.GREEN, 1.84);
list.add(milkyWay);
CandyBar payDay = new CandyBar("PayDay", CandyBar.Color.ORANGE, 1.85);
list.add(payDay);
CandyBar snickers = new CandyBar("Snickers", CandyBar.Color.BLUE, 1.86);
list.add(snickers);
CandyBar butterfinger = new CandyBar("Butterfinger", CandyBar.Color.YELLOW, 1.9);
list.add(butterfinger);
CandyBar musketeers = new CandyBar("Three Musketeers", CandyBar.Color.SILVER, 1.92);
list.add(musketeers);
CandyBar reeses = new CandyBar("Reese's FastBreak", CandyBar.Color.ORANGE, 2);
list.add(reeses);
CandyBar babyRuth = new CandyBar("Baby Ruth", CandyBar.Color.SILVER, 2.1);
list.add(babyRuth);
}
}
方法:
public class CandyBar
{
public enum Color { BLUE, BROWN, GOLD, GREEN, ORANGE, PURPLE, RED, SILVER, WHITE, YELLOW }
// instance variables
private String name;
private double weight;
private Color wrapper;
/**
* Constructor for objects of class CandyBar
*/
public CandyBar(String n, Color c, double w)
{
this.name = n;
this.weight = w;
this.wrapper = c;
}
public String getName()
{
return this.name;
}
public double getWeight()
{
return this.weight;
}
public Color getWrapperColor()
{
return this.wrapper;
}
public void printInfo()
{
System.out.println(this.name);
System.out.printf("%.1f oz with a ", this.weight);
System.out.println(this.wrapper + " wrapper");
}
}
答案 0 :(得分:0)
以下是计算等于或大于指定重量的钢筋的方法:
public static int countByWeight(ArrayList<CandyBar> list, double weight)
{
int countOfBarsHeavierOrEqual = 0;
for(int i = 0; i < list.size(); i++){
if(list.get(i).getWeight() >= weight){
countOfBarsHeavierOrEqual++;
}
}
return countOfBarsHeavierOrEqual;
}
此外,您的findCandyBar()
实现应为:
public static int findCandyBar(ArrayList<CandyBar> list, String n)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getName().equals(n)){
return i;
}
}
return -1;
}
然后解决此问题:
int index = findCandyBar(bars, "Mounds");
if(index == -1){ // <-- check for -1 to see if not found
编辑:一种使“未找到”情况显而易见的方法是在班级顶部设置一个常量:
private static final int NOT_FOUND = -1;
然后找不到您想要返回的内容,或对其进行测试:
return NOT_FOUND;
...或...
if(index == NOT_FOUND){
答案 1 :(得分:0)
尝试一下:
public static int countByWeight(ArrayList<CandyBar> list, double weight) {
return (int)list.stream().map(CandyBar::getWeight).filter(w -> w >= weight).count();
}
请注意,您的代码忽略了weight
参数,而是将1.70
硬编码为阈值。