• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Salutations. =]

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
mike bones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate your expedient and helpful response mr Foy.

=]
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic