13 Replies - 339 Views - Last Post: 10 March 2013 - 05:32 PM Rate Topic: -----

#1 dstevens  Icon User is offline

  • New D.I.C Head

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

abstract class and methods help with task,please.

Posted 10 March 2013 - 01:30 PM

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.

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.
• 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.


with the instructions above , would you say I am close to or way off the instructions?????

The code below is what I have so far, if anyone could be kind enough to guide me and to tell me what I am doing wrong please do.

public abstract class Recipe {

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



now with that I have another class ..

public class CookieRecipe extends Recipe{
	
	
	
	public CookieRecipe( String cookTime, int cookTemperature){
				
		super(cookTime, cookTemperature);
		cookTime = "cook for 20 minutes";
		cookTemperature = 325;
	
	
	
	}
	@Override
	public void setingredients() {
		
		
	}
}


and this class...............

public class ingredientList {
	
	private ingredientList[] ingredient = new ingredientList[6];

	private int i = 0;
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: abstract class and methods help with task,please.

#2 dstevens  Icon User is offline

  • New D.I.C Head

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

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 01:43 PM

If anyone would be kind enough to help me with some tutoring in Java. I am struggling with the arrays and do not have a lot of time in between work. I work 60-65 hours a week and on my day off I try to get some school in. I have a family of 3 kids and my wife. so between school and work I don't get to spend a lot of time with them.
I am carrying a 74.5 average in java class but need a 77 or better to get the class transferred and get credit for it at my original college.

Could some one please help me understand Java!!!!!

Thank you in advance....
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 01:48 PM

What exact questions/problems do you have?
Was This Post Helpful? 0
  • +
  • -

#4 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 01:53 PM

Any specific questions?

An array basically holds variables, and lets you access them by index (which starts at zero).

String[] names = new String[10];

names[0] = "Bob";
names[1] = "Jane";
names[8] = "Anonymous";
names[10] = "test"; // this isn't part of the array; it only has 10 spaces. 0, 1, 2, 3, 4...9

System.out.println("What's up, " + names[0] + "?"); // -> What's up, Bob?
System.out.println(names[10]); // -> error: array index out of bounds. (doesn't exist)



Was This Post Helpful? 1
  • +
  • -

#5 dstevens  Icon User is offline

  • New D.I.C Head

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

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:11 PM

Well it is more than just arrays...
I have a project due in a couple of weeks march 23. and he asked us to do this 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

public class RecipeDriver {
	
	
	
	ingredientList[] ingredient = new ingredientList[6];
	
	private int i = 0;
	
	public static void main(String[] args) {
		
	ingredientList[0] = "Flour";
	ingredientList[1] = "Sugar";
	ingredientList[2] = "Peanutbutter";
	ingredientList[3] = "Baking powder"; 
	ingredientList[4] = "Raisins";
	ingredientList[5] = "Vanilla";
	
	
}
}



I am getting errors on the ingredientList. Why?
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:20 PM

Currently you are telling Java to create an array of ingredientList objects. But what are ingredientList's? You haven't told Java what they are so it complains through an error.

If we go back to the task you need to create a String array, not an array of ingredientList.

The syntax goes
<type>[] variableName = new <type>[size]


In your case <type> will be String.

This post has been edited by Ryano121: 10 March 2013 - 02:20 PM

Was This Post Helpful? 2
  • +
  • -

#7 dstevens  Icon User is offline

  • New D.I.C Head

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

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:30 PM

ok. I changed the type in the array to string and it looks like the following.

public class RecipeDriver {
	
	
	
	String[] ingredientList= new String[6];
	
	private int i = 0;
	
	public void main(String[] args) {
		
	ingredientList[0] = "Flour";
	ingredientList[1] = "Sugar";
	ingredientList[2] = "Peanutbutter";
	ingredientList[3] = "Baking powder"; 
	ingredientList[4] = "Raisins";
	ingredientList[5] = "Vanilla";
	
	
}
}


so I think this is right and I can proceed to the next.

now it told me the method was not static so I removed the static in the main method, was this correct in removing it?
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:30 PM

Just an FYI kind of thing, you should put all of your variable declarations within the main method itself. There is no reason to make them class variables in this case.

Edit - just saw what you said. No don't remove static. The main method has to exactly match public static void main(String[] args). Anything else and you will get an error.

To solve look at the above comment about putting your variables in main.

This post has been edited by Ryano121: 10 March 2013 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#9 dstevens  Icon User is offline

  • New D.I.C Head

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

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:40 PM

Thank you ....
now when they say • Instantiate a CookieRecipe object......
my other class is as follows....

public class CookieRecipe extends Recipe{
	
	
	
	public CookieRecipe( String cookTime, int cookTemperature){
				
		super(cookTime, cookTemperature);
		cookTime = "cook for 20 minutes";
		cookTemperature = 325;
	
	
	
	}
	@Override
	public void setingredients() {
		
		
	}


is the cookTime and cookTemperature the objects?
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 02:44 PM

No.. they are parameters.

Something tells me you are biting off way more than you can chew right now. You don't know what an object is, yet you are dealing with inheritance - that's not the way to do it.

I understand that you don't have much time, but racing through everything without understanding the basics is just going to waste your time even more. You'll just end up having to repeat yourself again and again when you move onto to something new without understanding the core concepts.

Take a step back, learn about the core concepts of Java - objects and classes, methods, parameters. At the moment you are just setting yourself up for a very bad time.
Was This Post Helpful? 2
  • +
  • -

#11 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-February 13

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 03:17 PM

Your abstract class looks right with the exception of the extra parameter of cooktime in the constructor.

Your CookieRecipe class needs some work. I don't see any instance variables for the class a d you're not accepting enough variables in the constructor. You don't have all the getters and setters either but I'm guessing you know that already.

Why do you have an IngredientList class? I'm not seeing that int the instructions.
Was This Post Helpful? 0
  • +
  • -

#12 dstevens  Icon User is offline

  • New D.I.C Head

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

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 03:21 PM

Ok........I get where you are saying in to deep, but I have to push on and understand as I go.
I wrote the code as follows and an error comes up..........


public class RecipeDriver {
	
	public static void main(String[] args) {
	
	String[] ingredientList= new String[6];
	
	final int i = 0;
	
	ingredientList[0] = "Flour";
	ingredientList[1] = "Sugar";
	ingredientList[2] = "Peanutbutter";
	ingredientList[3] = "Baking powder"; 
	ingredientList[4] = "Raisins";
	ingredientList[5] = "Vanilla";
	
	Recipe CookieRecipe = new Recipe(); //OBJECT CookieRecipe Instantiated
	
	}
}



And I think my professor is going way to fast. I am a visual learner and not so much a reader so I have to rely on forums and video tutorials to learn.
Was This Post Helpful? 0
  • +
  • -

#13 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 03:27 PM

You can't call the variable name CookieRecipe - that's a name of a class you have created - it's reserved.

I think you are wanting to do

Recipe recipe = new CookieRecipe();


This is now going on to polymorphism though. Perhaps your professor needs to read the comment above.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: abstract class and methods help with task,please.

Posted 10 March 2013 - 05:32 PM

Related threads merged.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1