2 Replies - 387 Views - Last Post: 12 March 2013 - 11:54 AM Rate Topic: -----

#1 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-February 13

help with methods, arrays, and abstract class&methods

Posted 12 March 2013 - 10:45 AM

I am trying to complete this task and I am to a certain point I need some guidance.
my first task is as follows..........

task: Develop an abstract class called Recipe with the following elements:
• Instance Data: number of servings (an integer)
• Constructor method which accepts number of servings as a parameter, and assigns it to the instance variable.
• An abstract method named setIngredients, which accepts a String array as a parameter.

if anyone sees something I can improve or that would create an error. I am looking for help and criticism on how I can do this better or fix it.
and my code:
public abstract class Recipe {

	int numservings;
	String cookTime;
	int cookTemperature;
	
	public Recipe(int numservings, String cookTime, int cookTemperature){
		this.numservings = numservings;
		this.cookTime = cookTime;
		this.cookTemperature = cookTemperature;
		
		
	}
	
	public abstract void setingredients();

		
}


my next task is as follows.....
second task: Develop another class called CookieRecipe which extends Recipe. Include the following:
• Instance Data: cookTime (a String); cookTemperature(an integer); ingredientList [] (a String array)
• A Constructor which receives number of servings as a parameter, and then passes it to the constructor of the parent class using super.
• setIngredients method which receives a String array representing a list of ingredients.****NEED HELP WITH THIS
• Two other “setters” – for cookTime and cookTemperature
• A toString method to print a description of the object, including the ingredient list, the cookTime and the cookTemperature.*****ALSO NEED HELP WITH THIS

code....
public class CookieRecipe extends Recipe{
		
	private String ingredients;
	private String cookTime;
	private int cookTemperature;
	int numservings;

	public CookieRecipe(int numservings, int cookTemperature, String cookTime){
				
		super(numservings, cookTime, cookTemperature);
		cookTime = "20 Minutes";
		cookTemperature = 325;
	
		System.out.printf("Cookie Recipe ingredients for PeanutButter Cookies are %s\n" , this);
	}
	
	public String toString(){
		return String.format("%s %s %s %s", ingredients, cookTime, cookTemperature, numservings);
	}
	
	@Override
	public void setingredients() {
		
String[] ingredientList= new String[6];
	
		ingredientList[0] = "Flour";
		ingredientList[1] = "Sugar";
		ingredientList[2] = "Peanutbutter";
		ingredientList[3] = "Baking powder"; 
		ingredientList[4] = "Raisins";
		ingredientList[5] = "Vanilla";
	}
	
	public String getingredients(){
		
		return ingredients;
	}
	
	public void setcookTime(String cookTime){
		
		this.cookTime = cookTime;
	}
	
	public String getcookTime(){
		
		return cookTime;
	}
	
	public void setcookTemperature(int cookTemperature){
		
		this.cookTemperature = cookTemperature;
	}
	
	public int getcookTemperature(){
		
		return cookTemperature;
	}
}


Last task is as follows.... I need a lot of help on this one.......
TASK: Create the “Driver” class, named RecipeDriver, consisting of a main method which does the following:
• Create a String array of the ingredients, for example: "sugar", "milk", "raisins", etc. Each ingredient is an element of the array.
• Instantiate a CookieRecipe object
• Call the setIngredients method, passing the array of ingredients.
• Call the setCookTime and setTemperature methods, passing as arguments the time and temperature from your recipe, respectively.
• Print the instantiated object (using your toString method description)

And the code I have is ......
public class RecipeDriver {
	
	public static void main(String[] args) {
		Recipe recipe = new CookieRecipe(6, 325, "20 Minutes");
	}
	public static void ingredients(String x[]){
		x= IngredientList[0]
	}
}


My output right now is
OUTPUT:
Cookie Recipe ingredients for PeanutButter Cookies are null null 0 0

NEED HELP PLEASE!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: help with methods, arrays, and abstract class&methods

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help with methods, arrays, and abstract class&methods

Posted 12 March 2013 - 11:16 AM

You don't do what you are asking for
The goals of your teacher are quite clear, he wants you to realize that an Abstrart class father and its descendant can have a different cosntructor and you did not do that

The task

Quote

task: Develop an abstract class called Recipe with the following elements:
• Instance Data: number of servings (an integer)
• Constructor method which accepts number of servings as a parameter, and assigns it to the instance variable.


your code
public abstract class Recipe {  
    
    int numservings;  
    String cookTime;  
    int cookTemperature;  

    public Recipe(int numservings, String cookTime, int cookTemperature){  


The cookTime and the cookTemperature should not be in the instance variable of the abstract class neither should they appear in the constructor parameters.

Sill according to your assignment

Quote

Develop another class called CookieRecipe which extends Recipe. Include the following:
• Instance Data: cookTime (a String); cookTemperature(an integer); ingredientList [] (a String array)

Yes, it is the CookieReceipe that should have the 2 parameters that you added to your abstract class. That simplified your life... but defeat the purpose of the homework :)

This post has been edited by pbl: 12 March 2013 - 01:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12148
  • View blog
  • Posts: 45,161
  • Joined: 27-December 08

Re: help with methods, arrays, and abstract class&methods

Posted 12 March 2013 - 11:54 AM

As I told you when you PM'd me, you already have an open thread. Please do not duplicate post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1