这个作业是英文版的,英文好的给看看吧!!
Develop a program that implements(实现) a cookbook. We are providing(提供) a general description(种类) of the classes associated(关联) with this design. Feel free to add any methods/data members (成员)you understand are necessary. Try to practice the following concepts as you develop the implementation:
One-dimensional array of objects(一维数组对象)
get/set methods for classes(给类设置方法)
private vs. public access specifiers(private和public接口说明)
Passing primitives and objects as parameters(原始和对象参数传递)
I. Recipe(食谱) class - represents a cooking recipe.(代表烹饪食谱)
---------------
data members:
1. name - name associated with the recipe (e.g. "YuXiangRouSi")
2. Instructions(操作指南) - an array of String objects where each entry represents one
instruction associated with the recipe.
3. numberOfInstructions - integer representing the number of instructions(操作指南的数量)
4. Ingredients(配料) - an array of String objects where each entry represents an ingredient(字符串数组表示参加的配料)
5. numberOfIngredients - integer representing the number of ingredients methods:
1. Appropriate(适当的) constructor
2. get/set methods you understand are necessary
3. toString() method
4. public boolean equals(Object obj) --- to compare two Recipes and return true if the two Recipes have same contents.(比较两个食谱如果内容相等返回真)
II. Cookbook - represents the collection of recipes(代表收集的食谱)
-------------
data members:
1. listOfRecipes - an ArrayList of Recipe objects
2. numberOfRecipes - an integer representing the number of recipes(代表int型recipes数量)
methods:
1. Appropriate(适当的) constructor
2. get/set methods you understand are necessary
3. public void addRecipe (Recipe r) - adds the recipe to the list.
You must keep the recipes ordered(安排好) by recipe name.
4. public Recipe findRecipe( String recipeName) - locates(找出) a recipe based on a recipe name.
5. public void removeRecipe(String name) - deletes a recipe from the cookbook.
6. public string toString( ) - implement the appropriate toString method(实施适当的toString()方法)
7. main - write a main method that tests your class and show how your methods work.
//**************************************************************************************
//我的一点思路,可能完全不对~~哪个高手有想法的请尽量顺着我下面的这个写,或自己写个新的
class Recipe{
String name;
int numberOfInstructions,numberOfIngredients;
String[] instructions=new String[5];
String[] ingredients=new String[5];
Recipe(){
name={"yuxiangrousi"};
}
public String getInstructions(){
for(int i=0;i <5;i++)
{instructions[i]=""}
}
public String getIngredients(){}
public String toString(){
return
}
public boolean equals(Object obj){} }
public class Cookbook{
Develop a program that implements(实现) a cookbook. We are providing(提供) a general description(种类) of the classes associated(关联) with this design. Feel free to add any methods/data members (成员)you understand are necessary. Try to practice the following concepts as you develop the implementation:
One-dimensional array of objects(一维数组对象)
get/set methods for classes(给类设置方法)
private vs. public access specifiers(private和public接口说明)
Passing primitives and objects as parameters(原始和对象参数传递)
I. Recipe(食谱) class - represents a cooking recipe.(代表烹饪食谱)
---------------
data members:
1. name - name associated with the recipe (e.g. "YuXiangRouSi")
2. Instructions(操作指南) - an array of String objects where each entry represents one
instruction associated with the recipe.
3. numberOfInstructions - integer representing the number of instructions(操作指南的数量)
4. Ingredients(配料) - an array of String objects where each entry represents an ingredient(字符串数组表示参加的配料)
5. numberOfIngredients - integer representing the number of ingredients methods:
1. Appropriate(适当的) constructor
2. get/set methods you understand are necessary
3. toString() method
4. public boolean equals(Object obj) --- to compare two Recipes and return true if the two Recipes have same contents.(比较两个食谱如果内容相等返回真)
II. Cookbook - represents the collection of recipes(代表收集的食谱)
-------------
data members:
1. listOfRecipes - an ArrayList of Recipe objects
2. numberOfRecipes - an integer representing the number of recipes(代表int型recipes数量)
methods:
1. Appropriate(适当的) constructor
2. get/set methods you understand are necessary
3. public void addRecipe (Recipe r) - adds the recipe to the list.
You must keep the recipes ordered(安排好) by recipe name.
4. public Recipe findRecipe( String recipeName) - locates(找出) a recipe based on a recipe name.
5. public void removeRecipe(String name) - deletes a recipe from the cookbook.
6. public string toString( ) - implement the appropriate toString method(实施适当的toString()方法)
7. main - write a main method that tests your class and show how your methods work.
//**************************************************************************************
//我的一点思路,可能完全不对~~哪个高手有想法的请尽量顺着我下面的这个写,或自己写个新的
class Recipe{
String name;
int numberOfInstructions,numberOfIngredients;
String[] instructions=new String[5];
String[] ingredients=new String[5];
Recipe(){
name={"yuxiangrousi"};
}
public String getInstructions(){
for(int i=0;i <5;i++)
{instructions[i]=""}
}
public String getIngredients(){}
public String toString(){
return
}
public boolean equals(Object obj){} }
public class Cookbook{
private String name;
private String[] instructions;
private String[] ingredients;
public Recipe(String name,String[] instructions,String[] ingredients){
if(name==null||instructions==null||ingredients==null){
throw new RuntimeException(" the parameter can not be null");
}
this.name=name;
this.ingredients=instructions;
this.ingredients=ingredients;
}
public String toString(){
return name;
}
public String[] getIngredients(){
return ingredients;
}
public int getNumberOfInstructions(){
return instructions.length;
} public int getNumberOfIngredients (){
return ingredients.length;
}
public String getName(){
return name;
}
public String[] getInstructions(){
return instructions;
}
public boolean equal(Object obj){
Recipe other=(Recipe)obj;
if(!name.equals(other.getName())){
return false;
}
// String[] otherInstructions=other.getInstructions();
// String[] otherIngredients=other.getIngredients();
// if(instructions.length!=otherInstructions.length){
// return false;
// }
//
// if(ingredients.length!=otherIngredients.length){
// return false;
// }
//
// for(int i=0;i<instructions.length;i++){
// if(!ingredients[i].equals(otherInstructions[i])){
// return false;
// }
// }
//
// for(int i=0;i<otherIngredients.length;i++){
// if(!otherIngredients[i].equals(otherIngredients[i])){
// return false;
// }
// }
return true;
}
}import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class Cookbook {
private List listOfRecipes = new ArrayList();
private String bookName; public Cookbook(String bookName) {
if (bookName == null) {
throw new RuntimeException(" the bookName can not be nul");
}
this.bookName=bookName;
} public void addRecipe(Recipe recipe) {
listOfRecipes.add(recipe); } public Recipe findRecipe(String recipeName) {
for (Iterator it = listOfRecipes.iterator(); it.hasNext();) {
Recipe recipe = (Recipe) it.next();
if (recipe.getName().equals(recipeName)) {
return recipe;
}
}
return null;
} public void removeRecipe(String recipeName) {
for (Iterator it = listOfRecipes.iterator(); it.hasNext();) {
Recipe recipe = (Recipe) it.next();
if (recipe.getName().equals(recipeName)) {
listOfRecipes.remove(recipe);
return;
}
}
} public String toString() {
return bookName + ":" + listOfRecipes;
} public static void main(String[] args) {
Cookbook cookbook = new Cookbook("cookbook");
// create
for (int i = 0; i < 10; i++) {
Recipe recipe = new Recipe("recipe" + i,
new String[] { "instructions1", "instructions2", "instructions3" },
new String[] { "ingredients1", "ingredient2", "ingredient3" });
cookbook.addRecipe(recipe);
}
// find
Recipe recipe = cookbook.findRecipe("recipe5");
System.out.println(recipe);
//remove
System.out.println("-------------------before remove recipe3---------------");
System.out.println(cookbook);
cookbook.removeRecipe("recipe3");
System.out.println("-------------------after remove---------------");
System.out.println(cookbook);
}