Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-22-2007, 08:15 AM   #1
Solid State Member
 
Join Date: Mar 2006
Posts: 19
Default Help with Java Programming

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
__________________

Airwalker is offline   Reply With Quote
Old 02-22-2007, 09:45 AM   #2
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Help with Java Programming

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.
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 02-23-2007, 03:12 PM   #3
Solid State Member
 
Join Date: Mar 2006
Posts: 19
Default Re: Help with Java Programming

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!
Airwalker is offline   Reply With Quote
Old 02-23-2007, 03:34 PM   #4
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Help with Java Programming

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.
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 02-24-2007, 07:15 PM   #5
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: Help with Java Programming

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.
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Old 02-26-2007, 02:30 AM   #6
Beta Member
 
Join Date: Feb 2007
Posts: 1
Default Re: Help with Java Programming

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?ref...a83&channel=86

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

Cheers
Alpa
alpabarot is offline   Reply With Quote
Old 02-28-2007, 12:25 AM   #7
Fully Optimized
 
NeXuS's Avatar
 
Join Date: Dec 2005
Posts: 2,960
Default Re: Help with Java Programming

Wow I should ask for help then I got stuck making the game of nim on three classes, submited what I could do.
__________________

__________________
//Antec 902 V3 Case, Intel i7-2600K (@4.5GHz 1.280V watercooled w/ Antec H2O Kuhler 920), 8GB DDR3 Corsair Vengeance 1866MHz, ASUS P8P67 PRO B3, MSI 3GB N580 GTX Lightning Xtreme (@850MHz/2100MHz), 1TB Samsung Spinpoint F3, Corsair HX850W Pro Series//
//XPS 410 C2D E6400 @ 2.13GHz, 2GB DDR2 667MHz, 250GB HD, eVGA 320mb 8800GTS(605/1900MHz)//
//AMD Athlon XP +2000, Socket A MSI-6390, 768 mb of PC 2100 RAM, AGP ATI RADEON 9600SE//

I <3 TrancE
NeXuS is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:14 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0