tags:

views:

373

answers:

7
+1  Q: 

Java area class

Please help - I'm new to java.

I'm working on a code for class that ask me to write an Area class that calcualtes the following shapes-circles-rectangles-cylinders

Area of a circle: Area= (TT)(r^2) where TT is Math.PI and r is the circle radius

Area of a Rectangle: Area= (width) * (length)

Area of a cylinder: Area= (TT)(r^2)(h) where TT is Math.PI and r is the cylinder's base, and h is the cylinder's height

I was doing awesome in the class until now and I cant drop it, not like that's anyone elses problem but I really could use some help with this. I also have to create an Area Test class as well. I DO NOT KNOW WHERE TO START, this assignment was doing a month ago and I'm still stuck. If anyone could at least help just a bit it would be so appreciative. I would pay if I could but Im broke ; (

+8  A: 

Since this is a homework assignment, we can't give you the answer, but we can direct you in the correct way.

Since you say you've been doing well in the class, I'll assume you know some Java already - this is never a first assignment in an intro to programming.

Think about the Area class. It provides services but never gets instantiated. In other words, there is no Area "object". There are just mathematical functions. Hence, all the functions need to be "static", so you could write something like Area.circleArea(...)

Now to writing: obviously, you understand what these functions are mathematically. Think about how you would write them in Java. Obviously, you are dealing with three different functions. How would you declare each of them?

In other words, what would be the ????s in functions like the following?

static ???? areaOfCircle(????)
{
   BODY
}

static ???? areaOfRectangle(????, ????)
{
   BODY
}

[What about the third one? That one is up to you...]

If you figure out the question mark parts, you will have the "empty shells" of the functions. Once you have that, you will find that it is very straightforward to write the actual BODY. Or, show us what you've got and we'll try to help then.

Uri
+1 for being a super guy :)
Ed Swangren
+1  A: 

You could start with something like that:

public class AreaCalculator{

 public static double AreaOfCylinder(double r, double h){
   // compute area

   return answer;
 }

 public static double AreaOfCircle(double r){
   // compute area

   return answer;
 }

 public static double AreaOfRectangle(double w, double l){
   // compute area

   return answer;
 }

}

Then you can call the functions this way:

double myArea = AreaCalculator.AreaOfRectangle(10,10);
Wadih M.
A little too much help IMO.
ChrisW
I think the right way to teach someone is to give him skeleton code and a way to test it. That's all what I gave.
Wadih M.
I think it's too much help because what he needs to learn is to translate English-language requirements/specifications into code: so say that he needs one class, with three static methods, each with names and parameters, but don't supply the code. Just my opinion. Your answer was certainly helpful, just, maybe, just a little too helpful.
ChrisW
Hey. Upper case method names? This is not C# :-P
Mark Renouf
A: 

Depending on the exact question there may be two ways to do this.

  1. Create a single class named area. Give this class no constructor and no member data. Just give it three methods, best if they're static methods, with names like calculateAreaOfCircle, each with suitable parameters.

  2. Create an abstract class named AbstractArea, with an abstract method named calculateArea ... or, instead of an abstract class, an interface named IArea. Create three concrete subclasses, named Circle etc. Give the subclasses suitable member data (e.g. the Circle subclass should contain its radius as a data member). Implement the parameterless calculateArea method in each subclass.

The first of these is the more exact answer to the exact way in which you phrased the question.

ChrisW
+3  A: 
duffymo
Good catch, if the surface area of a cylinder is actually what is wanted; his formula is for the volume (π*r^2*h). And +1 for a polymorphic solution instead of static procedural code.
Software Monkey
A: 

When it comes to testing, a simple way to do a test would be like this. Lets say I had a "calculate the square of a number" function:

public class MathClass
{
   public static double square(double n)
   {
      return (2*n);
   }
}

In order to test, you need to run your program - in this case, my square() function - and compare that to known output.

So I might say, "Well, I know the square of 4. 4 squared is 16. Let me make sure my square() function returns 16."

To write a bare-bones unit test for this, I might write:

public static void main(String args[])
{
   System.out.println("Expected output: 4 squared = 16");
   System.out.println("Actual output: 4 squared = " + MathClass.square(4));
}

If I ran this program, I would seen an error immediately - wait a sec, my output looks like this:

Expected output: 4 squared = 16
Actual output: 4 squared = 8

In this case, one could go back and correct the code, looking for a problem with square().

You can do a similar thing with your area-calculating code. Write a basic test which compares your code's output with a known value - that is, the value which you calculated by hand.

It is good to test values like 0 and 1 - but it is also good to test "non-obvious" numbers like "23.49" because they don't have the special properties and identities that 0 or 1 (or 2, or integers have)

rascher
A: 

Ask your professor, you're already paying quite a bit for the class I'm sure so get help from him.

Also, your goal is the complete the assignment the way the professor wants, plain and simple. The BEST person in the entire world suited to help you with that is the professor.

Allen
A: 

I would do something like this -- you are coding in Java after all ;):

an interface basically defines what methods will be mandatory

public interface IShape{
  double getArea();
}

And to calculate the area or surface area, simply implement the algorithm in the getArea() method. Here's an example to get your started, the constructors for different shapes will differ, and they are used to define the shapes. For example, for circles, you would want the constructor to take in the radius as input; cylinder, base radius/diameter and height.

public class Square implements IShape{
  private double side;

  public Square(double side){
    this.side = side;
  }

  public double getArea(){
    return side * side;
  }
}

To calculate the area, simple do

new Square(5).getArea();

For testing, I suggest using JUnit. It should be fairly straightforward for this project.

Cambium