Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Salutations. =]

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

=]
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
mike bones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your expedient and helpful response mr Foy.

=]
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic