Help with Java Programming

Airwalker

Solid State Member
Messages
19
Hi guys,

I've been trying to help a friend with Java programming but have to admit I really don't know much about it, and he's struggling a lot. He's trying to teach himself some Java before beginning what sounds like a rather advanced college course in Computing Science. Here's the task we've been trying to figure out:

"Design and implement a Java Class to represent a rectangle. You should provide two Constructors for the Class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Member functions should be provided that allow a user of the Class to find out the area and perimeter of the shape plus a showDetails()method to print the values of the basic dimensions."

Does anyone have any idea where to start? I'm afraid I'm completely lost.

Thank-you in advance for any suggestions.

Tom
 
ok, it sounds like this is a basic type assignment(not to be condescending) because there are a couple ways you could do this.

Lets try this:
public class rectangle {
private int length, width = 0;
private int area, perimeter = 0;
public rectangle() {

} // end no argument default constructor

public rectangle(int theLength, int theWidth) {
setLength(theLength); // call method setLength with arguments of length
setWidth(theWidth); // call method setWidth with arguments of width
calcArea(); // call to compute area
calcPerimeter(); // call to compute perimeter

} // end overloaded 2 argument constructor
public void setLength(int myLength) { // getLength is used to validate input and access class wide variables
if (myLength > 0) {
length= myLength;
} else {
System.out.println("You cannot have a negative value for length!/n");
}
} // end method setLength
public void setWidth(int myWidth) { // getWidth is used to validate input and access class wide variables
if (myWidth > 0) {
width = myWidth;
} else
{
System.out.println("You cannot have a negative value for Width!/n");
}
}// end method setWidth
public int getWidth() {
return width; // return the private class-wide instance variable of width as an integer
} // end method getWidth
public int getLength() { // return the private class-wide instance variable length as an integer
return length;
} // end method getLength
public void calcArea() { // compute area using get methods
area = (getWidth() * getLength());
} // end method calcArea
public void calcPerimeter() {
perimeter = ((getWidth() * 2) + (getLength()*2));
} // end method calcPerimeter
public String showDetails() {
return "The Length of a side is: " + length + ".\n\nThe Width of a side is: " + width + ".\n\n";
}
public String toString() { // default toString method is overrided to output the values contained in this class
return "Area is: " + area + ".\n\n" + "Perimeter is: " + perimeter + ".\n\n";
}

public static void main(String args[]) { // main method could go in another class file, but this saves time
rectangle R1 = new rectangle(); // this line calls the no argument constructor
/* Now R1 is what we use to access the methods in our rectangle class */
R1.setLength(50); // access method setLength with a value of 50
R1.setWidth(12); // access method setWidth with a value of 12
/*Note, values can be adjusted to play around with
A negative or 0 value will return an error */
R1.calcArea();
R1.calcPerimeter();
System.out.print("First Rectangle\n---------------\n");
System.out.print(R1.showDetails());
System.out.print(R1.toString());
// because the constructor takes no values, we have to call the methods
// of rectangle individually to give them value before we can print the results
// this is one of the many purposes of a constructor
rectangle R2 = new rectangle(100, 150); // this line calls the 2 argument constructor
/* Now R2 is what we use to access the methods in our rectangle class */
System.out.print("Second Rectangle\n---------------\n");
System.out.print(R2.showDetails());
System.out.print(R2.toString());
// Here, all we do is print the values
}
} // end class rectangle

just copy paste, it copiles and all. If it's going to be turned in, I would delete most of those comments.

I can answer questions if you have them too about the logic. Construcors were hard for me at first as well.
 
Wow!

Thank-you very much indeed, I wasn't expecting that. It certainly seems to do what the specification says, although this really is beyond me now so I'll have to check with my friend to make sure he understands it all. If he has any trouble I'll post back again.

Thanks again!
 
No problem. That is probably pretty close to what is being asked, as far as getting things done. There isn't a whole lot there really, just methods, and overloaded methods.
 
very cool, Daeva, I have yet to look at Java programming but now that I see a bit it's actually quite easy to understand (with me knowing C/C++ that is). Looks like that code gets the job done.
 
Hi,
Came across your Forum while browsing around…cool stuff u have going on here. Also I thought I'd tell u about something I came across, thought u might find it useful, bcoz ur in IT…it's this site called Myndnet…u should check it out..the link is here http://www.myndnet.com/login.jsp?referral=alpa83&channel=86

It's this cool place where u get paid for responding to queries…very cool stuff!! http://www.myndnet.com/login.jsp?referral=alpa83&channel=86
Sign up n lemme know what u think…my mail id is barot.alpa@gmail.com

Cheers :)
Alpa
 
Wow I should ask for help then I got stuck making the game of nim on three classes, submited what I could do.
 
Back
Top Bottom