• Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntax problems.  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm in a beginner java programming class and have this assignment (see attachment) I'm not asking for the work to be done for me, but I'm not understanding how to do the syntax of both parts. Help please?


Program 8 – Classes
30 points – 15 per class
1. Create a RectangleFL (replacing FL with your first and last initial) class with the following UML:

RectangleFL
-length: double
-width: double
+Rectangle()
+Rectangle(double, double)
+setLength(double): void
+setWidth(double): void
+getLength(): double
+getWidth(): double
+calcArea(): double
+calcPerimeter(): double
+makeCopy(RectangleFL): void
+getCopy(): RectangleFL
---------------------------------------------




The set methods should verify that the length and width are each double numbers larger than 0.0 and less than 20.0. The default length and width are 5.0 if none are provided in the instantiation. Save and cleanly build this .class file.
2. Create a Lastname08 class that will test the RectangleFL class.
a. This program should instantiate at least two objects of type RectangleFL.
i. One rectangle can be instantiated with the defaults.
ii. For the second rectangle, you must prompt the user to enter the length and width. Your program should print the length, width, area, and perimeter of each rectangle.
b. This program should change the length and/or width of one of the rectangles and reprint the new length, width, area, and perimeter.
c. This program should make a copy of a rectangle and be able to access that copy. Then compare the copied rectangle to one of the other rectangles to see if they are equal. Appropriate messages should be printed.
d. Build AND run this class/program.
e. Compare your results to mine posted. They should be SIMILAR – don’t have to be exact.
3. Both classes should be heavily commented.
4. Hand in BOTH source code programs (.java) in Bb.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? This looks very straightforward to me.

I would STRONGLY suggest you take this in very small bits. If it were me, i would write exactly this much before I compiled:


Then, I'd add a main method that prints "i'm in RectangleFR.main()", compile and make sure that works.

Once that worked, i'd add the member variables, and test those. I would then add a method at a time, compiling after adding each one.

In fact, I would add the method that did nothing but printed "i'm in this method", recompile and test. I'd call it from the main. I'd test each and every method as I wrote them.

Once all the methods have been written for the RectangleFR class, and thoroughly tested, I'd start with the Rosenberger08 class, and again, write it in VERY small chunks.
 
Jim Keen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where i'm at currently, from the first part.

note: yes I realize i'm sucking at java, what I should have stated was I am beyond beginner.



public class rectangleJK

{
private double length;
private double width;

public rectangleJK()

{
length=0;
width=0;
}

public rectangleJK(double length, double width)

{

}

}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is your code re-posted with code tags. It's a little easier to read. See this. I took out a few unnecessary blank lines, and replaced your tabs with spaces. IMHO, spaces are better, but there is a debate over that. Just be consistent in whichever you use.


Next, you haven't followed directions. The assignment says "Create a RectangleFL ". Your class is named "rectangleJK".
Class names should begin with a capital letter. These are the sort of details that seem like a PITA, but you should get used to paying attention to them now.

Aside from that...what problem are you having? Where are you stuck?
 
Jim Keen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i was unaware of the code tags as i'm new to the site, i'll keep that in mind from now on. I'm (believe it or not) stuck on the setLength setWidth and the getlength/Width.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries about being new - we can tell from your post count (look under someones name to the left to see how long they've been around).

You should not be stuck on "setLength setWidth and the getlength/Width". You should be stuck on ONE method ONLY. Don't worry about ANYTHING else until you get one working, and then only worry about the next one. If you panic over too many things at once (and too many == 2 or more), you'll never get things working.

So, the idea behind a setter method (such as setLength) is this:

Your class has member variables called "length" and "width". You don't want anyone to be able to stick anything in there. You may not want them to set it to 0. You many not want them to set it to a negative number. Your variable is private, so nobody can directly say "length should be -9383737443". They have to go through the logic of your setter. So, they pass in a value to your setter that they want length to be. You can do whatever logic you want to validate it, and if you approve, you set it to that value, otherwise, you either set it to some default or (in a more advanced class) could throw an exception.

So, when they pass the value in, do whatever validation you need, and if it is OK, simply set the value of length to what they pass in. something like


Note: I have not tested or tried to compile the above, so typos are probable

a "getter" simply return the value of the variable. There's not much more to it than that.

Give it a try, and let us know what you come up with.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!