Results 1 to 20 of 37
- 01-08-2012, 03:36 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
- 01-08-2012, 03:51 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Writing Two Classes help Please!!!!
Suggested reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)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: Algorithms (The Java™ Tutorials > Collections)Could someone just explain the principle. Also for the second question how do i check if its in alphabetical order?
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 03:55 AM.
- 01-08-2012, 04:24 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
- 01-08-2012, 04:39 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: Writing Two Classes help Please!!!!
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
- 01-08-2012, 07:05 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: Writing Two Classes help Please!!!!
Is knowledge of inheritance and interfaces required?
thanks
-
Re: Writing Two Classes help Please!!!!
- 01-08-2012, 07:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
Re: Writing Two Classes help Please!!!!
Create the second class? Here's a jump start:
I think you can take it from here.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: }
kind regards,
JosLast edited by JosAH; 01-08-2012 at 07:49 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2012, 08:06 PM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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.
-
Re: Writing Two Classes help Please!!!!
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.
- 01-08-2012, 08:47 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
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 ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Re: Writing Two Classes help Please!!!!
Hey don't I implement the HighlyIntelligentPerson interface too?
- 01-08-2012, 08:52 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
Re: Writing Two Classes help Please!!!!
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-08-2012, 09:49 PM #13
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
-
- 01-08-2012, 10:51 PM #15
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: Writing Two Classes help Please!!!!
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.
- 01-08-2012, 11:29 PM #16
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
- 01-08-2012, 11:43 PM #17
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: Writing Two Classes help Please!!!!
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?
- 01-09-2012, 12:27 AM #18
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
-
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.
- 01-09-2012, 12:41 AM #20
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
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
Similar Threads
-
difficulty understanding writing classes
By elecleoalune in forum New To JavaReplies: 11Last Post: 04-18-2011, 03:06 PM -
Writing classes???
By Bgreen7887 in forum New To JavaReplies: 1Last Post: 11-04-2010, 08:06 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Writing importable classes
By Katsu in forum New To JavaReplies: 6Last Post: 03-08-2009, 09:10 PM -
Writing classes in graphics
By CyberFrog in forum Java AppletsReplies: 2Last Post: 04-05-2008, 05:37 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks