Exercise for study course help?
I've posted previous thread about me just starting to learn Java programming and I've come to an exercise in my course textbook that I really can't understand what's to be done.
The exercise is: Create an abstract superclass named "Fruit" and a concrete subclass named "Apple" The superclass should belong to a package called "food" and the subclass can belong to the default package. Make the superclass public and give the subclass default access.
Now..I feel I understand bits of it but don't know how to put them together.
Can someone give me an example of what I have to do for that exercise and I will adapt it to fit it?
Thanks, Stacey
Re: Exercise for study course help?
Please see this post for an example of an abstract shape class and the sub-classes square and circle :)
Re: Exercise for study course help?
Hello SweetStacey,
If you post the code you have so far, i'll help you complete it :)
Re: Exercise for study course help?
Ok I think I understand it..but..the idea of a fruit superclass is odd. Might be just me but theres not a lot that can be done with fruit interfaces? I only got ripen for a method all fruit classes can do but they all ripen the same way don't they?
Stacey
Re: Exercise for study course help?
Fruit has a color.
Some have seeds.
Fruit have a shape ( difficult one ;) )
Some fruit are not eatable.(poison)
Here is a start ;)
Re: Exercise for study course help?
Ok proved me wrong :P Thanks, i'll have a go :)
Stacey
Re: Exercise for study course help?
Right..i've hit another brick wall #-o
Ok I know what I want to do in this program now...I want to have an abstract superclass named "Fruit" and have a few subclasses which are "Apple, Banana and Tangerine". I want to have the computer ask me what colour I am, I type in, say, Orange..it then takes my input and goes through a for? loop and checks through the subclasses and finds the subclass that is orange in colour and then gives an output that the fruit that is orange is a Tangerine...and the same thing happens for apple and banana.
Yes I know this probably sounds like a stupid program but i'm just trying to make it an easy as possible.
So I've tried various things and keep getting errors so here is what i've got so far, which probably doesnt make sense, i'm just trying out various things to see what works, so basically i'm learning through trial and error!
Code :
package.fruit
import java.util.Scanner;
public abstract class fruit {
Scanner myScanner = new Scanner(System.in);
Any help?
Stacey
Re: Exercise for study course help?
I should add I know that the subclasses have to be in different files. For which I have got so far
Code :
package fruit;
public class Apple {
public class Apple extends fruit; {
Re: Exercise for study course help?
Hey Stacey,
Let me help you a bit with that.
You'll have the next objects:
Scanner, Fruit, Apple, Banana, Tangerine.
And now about the objects:
Scanner
This object has a few methods like, findByColor(String color), now with this example I think you'll know the other ones ;)
Also this Scanner has a ArrayList as a attribute, lets call it fruitbasket :D In this fruitbasket you put all your fruits.
Fruit
Fruit is a abstract class that as a few attributes: color(Color), seeds(boolean), eatable(boolean), shape(noIdea).
The rest of the fruit I think you'll manage ;)
Re: Exercise for study course help?
Thanks for your help, I stayed up till 2am this morning and managed to get it done :) I did have to change the superclass to Animals instead of fruit..the fruit was just throwing me off lol.
Stacey
Re: Exercise for study course help?
Quote:
Originally Posted by
SweetyStacey
Thanks for your help, I stayed up till 2am this morning and managed to get it done :) I did have to change the superclass to Animals instead of fruit..the fruit was just throwing me off lol.
Stacey
Haha yeah, Animals and Cars are most used as example because they are easier to explain.:(|)
Anyway, you get how it works? the exercise is just a way to get the understanding, but just completing the exercise is not enough, do you understand how it works?
Re: Exercise for study course help?
Yep I think so, i'm usually better with theory than practical at first.
Stacey
Re: Exercise for study course help?
Quote:
Originally Posted by
SweetyStacey
Yep I think so, i'm usually better with theory than practical at first.
Stacey
Well, in case of OOP thats better ;)
If you code but dont understand you'll keep debugging.
But if you understand but dont know the code you can still learn it, the syntax is easier to learn than understanding it.