File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Salutations. =] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Salutations. =]" Watch "Salutations. =]" New topic

Salutations. =]

mike bones

Joined: Mar 24, 2007
Posts: 4
Salutations. This is my first post. Glad to be here.

I have a question for a java developer whom may be willing to assist a newbie in need of aid?

Like many of you i'm attempting to teach myself Java. I have been reading tutorials and reading on the messageboard and thank you for your interesting advice as of yet.

My question: I have just developed a small program to calculate the perimeter and area of a rectangle. What i'm trying to do is create seperate methods for each of several functions I want to implement.

my code:

import javax.swing.*; //imports java swing to implement msgbox
public class rectangle //creates class rectangle

public static void main (String[] args) //beginning of code
String a1, a2, output;
double area, perimeter, length, width;

a1 = JOptionPane.showInputDialog("Enter length");
a2 = JOptionPane.showInputDialog("Enter width");

length = Double.parseDouble(a1);
width = Double.parseDouble(a2);
area = length * width;
perimeter = (length * 2) + (width * 2);

output = "The length of the rectangle is " + length + " and the width is " + width + " Therefore; The area is " + area + " and the perimeter is " + perimeter;
JOptionPane.showMessageDialog(null, output, "rectangle logic",


As you can see my code works fine. However; I was wondering if anyone could help me break down my sections into seperate methods? IE: have a method for perimeter() (would define required calculation for perimeter), area() (would define required calculation for area), getData() (which would display calculation)

Basically I think I have someone of a solid understanding in terms of basic coding; however I have no idea how to implement methods. If someone could help me implement methods into this small program i'd be very greatful.

I look forward to anyone willing to assist me in my everexpansive quest for knowledge.

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143

Stephen Foy - Microsoft Application Development Consultant
mike bones

Joined: Mar 24, 2007
Posts: 4
I appreciate your expedient and helpful response mr Foy.

I agree. Here's the link:
subject: Salutations. =]
It's not a secret anymore!