Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 4th, 2008, 01:21 PM
ac1983fan ac1983fan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 1 ac1983fan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 44 sec
Reputation Power: 0
Java n00b needs help

Alright, I'm an AP Java student trying to make my first from-scratch program. here's what I have for code:
Code:
/**
 * AP Computer Science, October 2008
 * Prompts user for side of a square, radius of a circle,
 * and length and width of a rectangle.
 * Checks for data validity
 * calls Area method to calculate area of each object
 * displays area
 */


import javax.swing.JOptionPane;
import java.io.*;

public class Area
{
  public static void main(String[] args) throws IOException
  {

    //Variable declarations
    final double PI = 3.141592653589793;
    String input;
    double squareside, radius, length, width; //data to be input
    double square, circle, rectangle; //area of each figure.

    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));      //Input set up

    //Collect data
    System.out.println("Enter the length of a side of a square:");
    input = stdin.readLine();
    squareside = convertData(input);
    
    System.out.println("Enter the length of the radius of a circle:");
    input = stdin.readLine();
    radius = convertData(input);

    System.out.println("Enter the length of the length of a rectangle:");
    input = stdin.readLine();
    length = convertData(input);

    System.out.println("Enter the length of the width of a rectangle:");
    input = stdin.readLine();
    width = convertData(input);

    JOptionPane.showMessageDialog(null, "If invalid data was entered \n the result for that portion will be 0");

    //Calculate areas
    square = Area(squareside, squareside);
    circle = Area((radius * radius), PI);
    rectangle = Area(length, width);
    
    //Display data
    JOptionPane.showMessageDialog(null, "Square area:\n" + square);
    JOptionPane.showMessageDialog(null, "Circle area:\n" + circle);
    JOptionPane.showMessageDialog(null, "Rectangle area:\n" + rectangle);
    JOptionPane.showMessageDialog(null, "Thank you for using this program.");
    JOptionPane.showMessageDialog(null, "Have a nice day.");
  }

  private double convertData(String input)
  {
    double result;

    if ((Double.parseDouble(input)) <= 0)
      result = 0;
    else
      result = Double.parseDouble(input);
      
    return result;
  }

  private double Area(double factor1, double factor2)
  {
    double area;
    
    area = factor1 * factor2;
    return area;
  }
}

I'm getting 7 total errors, although they are all basically the same.
Code:
Area.java:30: non-static method convertData(java.lang.String) cannot be referenc
ed from a static context
    squareside = convertData(input);
                 ^
Area.java:47: non-static method Area(double,double) cannot be referenced from a
static context
    square = Area(squareside, squareside);
             ^

I have no clue how to fix this. any help?

Reply With Quote
  #2  
Old October 6th, 2008, 06:33 AM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
"Area" class not instantiated.

The problem you are experiencing comes from the fact that the "main" method is called from a static context (when you run your program). Thus, there is no "Area" object created. One way to correct this is to instantiate the "Area" class from inside the "main" method, and then have the "Area" constructor do the non-static things. The following code is not tested, but it should at least point you in the right direction:

Code:
/**
 * AP Computer Science, October 2008
 * Prompts user for side of a square, radius of a circle,
 * and length and width of a rectangle.
 * Checks for data validity
 * calls Area method to calculate area of each object
 * displays area
 */


import javax.swing.JOptionPane;
import java.io.*;

public class Area
{
  public static void main(String[] args) throws IOException
  {
      new Area( args );
  }

  public Area( String[] args )
  {
    //Variable declarations
    final double PI = 3.141592653589793;
    String input;
    double squareside, radius, length, width; //data to be input
    double square, circle, rectangle; //area of each figure.

    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));      //Input set up

    //Collect data
    System.out.println("Enter the length of a side of a square:");
    input = stdin.readLine();
    squareside = convertData(input);
    
    System.out.println("Enter the length of the radius of a circle:");
    input = stdin.readLine();
    radius = convertData(input);

    System.out.println("Enter the length of the length of a rectangle:");
    input = stdin.readLine();
    length = convertData(input);

    System.out.println("Enter the length of the width of a rectangle:");
    input = stdin.readLine();
    width = convertData(input);

    JOptionPane.showMessageDialog(null, "If invalid data was entered \n the result for that portion will be 0");

    //Calculate areas
    square = Area(squareside, squareside);
    circle = Area((radius * radius), PI);
    rectangle = Area(length, width);
    
    //Display data
    JOptionPane.showMessageDialog(null, "Square area:\n" + square);
    JOptionPane.showMessageDialog(null, "Circle area:\n" + circle);
    JOptionPane.showMessageDialog(null, "Rectangle area:\n" + rectangle);
    JOptionPane.showMessageDialog(null, "Thank you for using this program.");
    JOptionPane.showMessageDialog(null, "Have a nice day.");
  }

  private double convertData(String input)
  {
    double result;

    if ((Double.parseDouble(input)) <= 0)
      result = 0;
    else
      result = Double.parseDouble(input);
      
    return result;
  }

  private double Area(double factor1, double factor2)
  {
    double area;
    
    area = factor1 * factor2;
    return area;
  }
}

Last edited by paulscode : October 19th, 2008 at 02:05 PM.

Reply With Quote
  #3  
Old October 14th, 2008, 05:58 AM
updev updev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 3 updev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 41 sec
Reputation Power: 0
static methods (like your main method) can only access other static methods as well as class variable (which are also declared as static).
so to call any method from your main method, it has to be a static method.

Reply With Quote
  #4  
Old October 19th, 2008, 02:06 PM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
One more thing. If that last method "Area" causes a problem because of its name being the same as the class name, you may want to either change its name (to something like calcArea), or you could change the name of the class. I'm not sure if it would be a problem or not, just something I noticed.

Reply With Quote
  #5  
Old March 7th, 2009, 04:15 PM
coyote121403 coyote121403 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 coyote121403 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 28 sec
Reputation Power: 0
Error

Hi, I am working on this same project. I was having the same problem so I used your solution. I ran the corrected code as you have it and got these errors. Any ideas? I've been looking at it for a while and can't figure it out.

Area.java:40: unreported exception java.io.IOException; must be caught or declar
ed to be thrown
input = stdin.readLine();
^
Area.java:44: unreported exception java.io.IOException; must be caught or declar
ed to be thrown
input = stdin.readLine();
^
Area.java:48: unreported exception java.io.IOException; must be caught or declar
ed to be thrown
input = stdin.readLine();
^
Area.java:52: unreported exception java.io.IOException; must be caught or declar
ed to be thrown
input = stdin.readLine();
^
4 errors

C:\javach1>

Reply With Quote
  #6  
Old March 8th, 2009, 07:49 PM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
When you see a message like those, it means that a particular line of code might "throw an exception". An "exception" is just a message, and is most often used to indicate that some problem happened while trying to run the line of code.

These exceptions must be either "caught" or "thrown". Most of the time you will want to "catch" exceptions. "Throwing" them means you pass the exception back to a previous method, which doesn't really make sense in this case.

So to catch these exceptions, place a try/catch around those "input = stdin.readLine();" calls. Example:

Code:
try
{
    input = stdin.readLine();
}
catch( IOException ioe )
{
    // There was an error reading the input.
    // Do something here if you want.
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Java n00b needs help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap