Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Exercise for study course help?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Exercise for study course help?

    Hello SweetStacey,

    If you post the code you have so far, i'll help you complete it
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exercise for study course help?

    Ok proved me wrong Thanks, i'll have a go


    Stacey

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exercise for study course help?

    Right..i've hit another brick wall

    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!

    package.fruit
                  import java.util.Scanner;
     
    public abstract class fruit {
     
    Scanner myScanner = new Scanner(System.in);


    Any help?


    Stacey

  8. #8
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    package fruit;
     
                public class Apple {
     
                       public class Apple extends fruit; {

  9. #9
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default 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 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
    Last edited by Bryan; April 23rd, 2010 at 02:50 AM.

  10. #10
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  11. #11
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Exercise for study course help?

    Quote Originally Posted by SweetyStacey View Post
    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?

  12. #12
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exercise for study course help?

    Yep I think so, i'm usually better with theory than practical at first.


    Stacey

  13. #13
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Exercise for study course help?

    Quote Originally Posted by SweetyStacey View Post
    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.

Similar Threads

  1. JAVA exercise
    By gmilan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 01:30 PM
  2. Cash Register Exercise
    By Consus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 10:52 PM
  3. Exercise problems
    By Virgildonatifan in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 03:12 AM