Page 1 of 2 12 LastLast
Results 1 to 20 of 37
Like Tree2Likes

Thread: Writing Two Classes help Please!!!!

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Writing Two Classes help Please!!!!

    I have 2 questions in which I have no idea on.

    1)Give the Java code for two classes: A class Point and a class Segment. The class Point should contain:
    • The x and y coordinates of a point on the plane, as two doubles
    • A constructor
    • Two methods getX and getY that return the coordinates
    • A method distance that computes the distance to a second point. (Remember: you can use Math.sqrt. method)

    The class Segment should contain:
    • The two end-points of a segment.
    • A constructor
    • A method trianglePerimeter which has one parameter C of type Point and determines the perimeter of the triangle formed by this segment and point C.
    Note that if a segment has end-points A and B then a point C forms a triangle, with perimeter AB+BC+CA, where PQ is the distance between points P and Q.


    2) Give the Java code for two classes: A class Account that contains information about Accounts and a class Bank that stores the accounts of a bank.
    The class Account should contain:
    • Necessary instance fields. • A constructor. • A method getOwner that returns the owner of the account. • A method getBalance that returns the balance of the account. • A method depositMoney that adds the specified amount to the account. • A method withdrawMoney that removes the specified amount from the account.
    The class Bank should contain:
    • An instance field for a list of accounts.
    • A constructor that creates an empty bank (initially there are no accounts).
    • A method addAccount that adds an account to the list.
    • A method findAccount with a string parameter owner that searches the list for an account with this owner and returns the account if found.
    • A method isSorted that checks if accounts appear in the list in alphabetical order of owners’ names.

    What does it mean by two classes. I know how to create one, but I haven't got a clue on how to link the two together. Could someone just explain the principle. Also for the second question how do i check if its in alphabetical order?

    Thanks

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Writing Two Classes help Please!!!!

    What does it mean by two classes. I know how to create one, but I haven't got a clue on how to link the two together.
    Suggested reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    Could someone just explain the principle. Also for the second question how do i check if its in alphabetical order?
    Suggested reading: Lesson: Algorithms (The Java™ Tutorials > Collections)

    If you have any questions beyond this, I recommend you tackle the assignment step by step and post an SSCCE that clearly defines your question (google SSCCE if you do not know what this means)
    Last edited by doWhile; 01-08-2012 at 04:55 AM.
    Fubarable likes this.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    I don't really understand what the question is asking though. What shall I do after creating the first class? Please help

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by stuckonjava View Post
    I don't really understand what the question is asking though. What shall I do after creating the first class? Please help
    A few more suggested readings:
    How to Program When you are Stuck.
    How to Get Help

    Again, tackle what you do understand, create some code, and post an SSCCE with a precise question (rather than the posting requirements verbatim. In other words, we don't understand what you don't understand unless you make an effort to help us understand

  5. #5
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Is knowledge of inheritance and interfaces required?

    thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by stuckonjava View Post
    Is knowledge of inheritance and interfaces required?

    thanks
    No, I don't believe that they are needed for your current assignments, but this knowledge will be needed in the future to be sure.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by stuckonjava View Post
    What shall I do after creating the first class?
    Create the second class? Here's a jump start:

    Java Code:
    public class Point {
       private double x, y;
    
       public Point(double x, double y) { this.x= x; this.y= y; }
    
       // getX() and getY() go here:
    }
    
    public class Segment {
       private Point a, b;
    
       public Segment(Point a, Point b) { this.a= a; this.b= b; }
    
       // more methods go here:
    }
    I think you can take it from here.

    kind regards,

    Jos
    Last edited by JosAH; 01-08-2012 at 08:49 PM.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Hi Jos,

    The thing that is confusing me is, do the two classes relate to one another? Or is the question just literally asking to create the two classes?

    Because if you look at Q2, correct me if I'm wrong , but it seems like there is a link and that's why I was wondering whether knowledge of inheritance and interfaces is required, as I was just skimming through my text book and 'superclass' and 'subclass' seems to fit.

    Thanks for your time.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by stuckonjava View Post
    The thing that is confusing me is, do the two classes relate to one another? Or is the question just literally asking to create the two classes?

    Because if you look at Q2, correct me if I'm wrong , but it seems like there is a link and that's why I was wondering whether knowledge of inheritance and interfaces is required, as I was just skimming through my text book and 'superclass' and 'subclass' seems to fit.
    They do in fact relate to one another, but (and this is very important) it is not an inheritance relationship but rather a composition relationship. A Segment is not a specialized form of a Point, so Segment does not inherit from Point, but instead a Segment contains two Point objects.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing Two Classes help Please!!!!

    In other words: pay real care to the has a relationship, as in: a Segment has a Point (two Points actually), compared to the is a relationship, as in: a Jos is a HighlyIntelligentPerson is a HumanBeing, which is a Primate etc. The 'is a' relationship is a specialization, e.g. a Fubarable is a Primate. A Segment isn't a specialization of a Point.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Two Classes help Please!!!!

    Hey don't I implement the HighlyIntelligentPerson interface too?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by Fubarable View Post
    Hey don't I implement the HighlyIntelligentPerson interface too?
    It must be another forum bug; I didn't write my^H^Hthat previous reply; I'm innocent as always; where are those admins when you need them ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Ok, thanks for this information. So when doing the Account and Bank class, do I approach it the same way?
    Thanks

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by stuckonjava View Post
    Ok, thanks for this information. So when doing the Account and Bank class, do I approach it the same way?
    Given what we've stated above, what do you think (and please understand that this is a legitimate question, not a sarcastic remark)?

  15. #15
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by Fubarable View Post
    Given what we've stated above, what do you think (and please understand that this is a legitimate question, not a sarcastic remark)?
    I think yes.

    I have another question regarding loops.

    1)
    int count = 0;
    do {
    System.out.println("Welcome to Java");
    }
    while (count++ < 5);

    I understand that the first "Welcome to Java" prints due it being a post test loop. But why does it print 6 of them?
    As far as I can see, there is no increment either so I'm pretty confused about this. I would've though it would be infinite as count++ is always going to be 1.

    2)
    int i = 0; for (i = 0; i < 10; i++);
    System.out.println(i + 4);

    In this loop it prints out 14. I thought i goes from 0-9 so it would've been 13. I don't understand where the 10 comes from to make i+4=14.

    In general I am fine with loops, just these two weird ones have confused me.

  16. #16
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Writing Two Classes help Please!!!!

    1) because count++ increments after evaluation. ++count would increment before evaluation!

    2) because i is 10 at the end of the loop. When i is incremented to 10, the loop terminates, not before. So, i hits 10, loop stops, then 10+4 = 14
    Fubarable likes this.

  17. #17
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Quote Originally Posted by quad64bit View Post
    1) because count++ increments after evaluation. ++count would increment before evaluation!

    2) because i is 10 at the end of the loop. When i is incremented to 10, the loop terminates, not before. So, i hits 10, loop stops, then 10+4 = 14
    Firstly thanks for your reply, secondly, I am still confused about them.

    For q1, what do you mean ++count would increment before the evaluation? I don't understand the condition while(count++ < 5), isn't 0++ equal to 1? I don't understand where the counter actually increments as it's just in the condition.

    For q2, doesn't i go through 0,1,2,3,4,5,6,7,8,9 and then finishes since the condition is <10 not <=10?

  18. #18
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    What is the difference between i++ and ++i.

    why does this happen:

    int i=10;

    System.out.print((++i) + (++i))

    print out 23?! I don't know where this comes from

    and System.out.print((i++) +(++i)) prints out 22?

    I am so confused by something I once though was simple!

    And finally why does i++ + i * 5 give 65?

    thanks for your time

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Two Classes help Please!!!!

    There are sites that explain increment and decrement well, so I will not repeat them, but if misused they can be quite confusing, and it is for this reason I feel that these operators should be called on a separate line from one where the variable is being used. White space is cheap, and if it will help you better understand your own code 3 months from now, use it.

  20. #20
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Writing Two Classes help Please!!!!

    Could you explain these ones please, I feel like I will understand them if you do.

    why does this happen:

    int i=10;

    System.out.print((++i) + (++i))

    print out 23?! I don't know where this comes from

    and System.out.print((i++) +(++i)) prints out 22?


    And finally why does i++ + i * 5 give 65?

    thanks for your time

Page 1 of 2 12 LastLast

Similar Threads

  1. difficulty understanding writing classes
    By elecleoalune in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 04:06 PM
  2. Writing classes???
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 09:06 AM
  3. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  4. Writing importable classes
    By Katsu in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 10:10 PM
  5. Writing classes in graphics
    By CyberFrog in forum Java Applets
    Replies: 2
    Last Post: 04-05-2008, 06:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •