posted 11 years ago

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",

JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

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.

=]

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",

JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

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.

=]

It is sorta covered in the JavaRanch Style Guide. |