1
votes

I'm working on code for a class that asked me to write an Area class that calculates the area for the following shapes: circles, rectangles, and cylinders.

Area of a circle = (π)(r^2) where π is Math.PI and r is the circle's radius

Area of a rectangle = (width) * (length)

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

I also have to create an Area Test class as well and I don't know where to start.

10
This is not rent-a-coder. What are you stuck on specifically? What have you tried? If you do not know how to create a class and add methods to it you should hit the books a little harder.Ed S.
is it the class definition or the math that is a problem?akf
What does the homework say about the test? Just a test or using a specific system?Kathy Van Stone
I'll give him the benefit of the doubt that their CS101 course does an "around the world in 30 days" kind of thing, where the first assignment does not have any programming and this is the first Java assignment.Uri
@Victor @Ed as someone who has taught introductory Java courses, it is often surprising how people can be doing very well, but then get hung up on a specific topic ("writing .equals() methods" - eg, it is inconsistent with the '==' operator). Or not be able to correlate a previously-taught concept (class relationships) to a new way of applying it (writing a tic-tac-toe game.) So, I'm not sure "hit the books harder" is necessarily a fair criticism - at least, not without some guidance on the types of concepts one should brush up on or new ways of thinking about them.poundifdef

10 Answers

8
votes

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.

3
votes

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

Sorry, this is simple wrong. (Just look at the units - they're length cubed, not length squared.) The surface area of a cylinder is not given by this formula. Where did you get it?

The surface area of a cylinder is the sum of the areas of the circles on the ends and the side, which is:

2πr^2 + 2πrh = 2πr(r+h)

Sounds like some kind of polymorphism assignment where you should have a Shape interface:

public interface Shape
{
    double area();
}

Then you'll have subclasses that implement this interface and return the area value. If you're really smart, you'll have an array of Shapes that you'll loop through, calling the different area() methods, and showing that you get a different result depending on the runtime type of each Shape. It practically writes itself.

1
votes
public static class Area 
{

public static double getArea(double radius) 
{
    return Math.PI * radius * radius;
}


public static double getArea(int length, int width) 
{
    return length * width;
}


public static double getArea(double radius, double height) 

{
    return Math.PI * radius * radius * height;
}

}
0
votes

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.

0
votes

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)

0
votes

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.

0
votes

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.

0
votes

Here is the class.

/**
 * 
 * @author  LAB 6
 * This program will calculate area of rectangle, circle, and cylinder.
 * Write a class that has three overloaded static methods for calculating the areas of the following 
 * geometric shapes: circles, rectangles, cylinders
 * Because the three methods are to be overloaded, they should each have the same name, 
 * but different parameter lists. Demonstrate the class in a complete program.
 */

public class AreaClass { //begins class


    /**
     * Method should calculate the area of a circle
     * 
     * @param radius
     * @return area of a circle
     */

    public static double getArea(double radius)  //method to get area of circle
    {
        return Math.PI * radius * radius; //calculates and returns circle area
    }//end method

    /**
     * Method should calculate the area of a rectangle
     * 
     * @param length
     * @param width
     * 
     * @return area of a rectangle
     */

    public static double getArea(int length, int width) //method to get area of rectangle
    {
        return length * width; //calculates and returns rectangle area
    }//end method

    /**
     * Method should calculate the area of a cylinder
     * 
     * @param radius
     * @param height
     * 
     * @return area of a cylinder
     */
    public static double getArea(double radius, double height) //method to get area of cylinder
    {
        return Math.PI * radius * radius * height; //calculates and returns area of cylinder
    }//end method
}//end class
0
votes

One approach: When you are stuck, (or, as many will argue, all the time), you should Start with the Test Code. Below I'm using skeletal JUnit3 for that, not sure what you are using in your class. Your imaginary thought process is in italics.

Well, I'm going to have to a test a method which computes the area of a circle.

public void testAreaOfCircle() {
   double areaCalculated = Well, I need a function here...
   assertEquals(TRUE_AREA, areaCalculated); 
}

o.k., next step, let's fill in the function and the test

public void testAreaOfCircle() {
   double r = 2.3;  // some arbitrary positive number
   double areaCalculated = MyClass.areaOfCircle(r);  // let's make the method static for now...
   assertEquals(r*r*Math.PI, areaCalculated); 
}

Hmm, what should happen if r is < 0? I'll ask the teacher what he wants and hope I get extra credit! Add that to your test code...

(minor note - assertEquals is not robust for floating point math, worry about that later...)

Finally, you should create a skeletal method to implement the algorithm, and work on it till the test succeeds.

public static double areaOfCircle(double r) {
   if (r < 0)
      throw new IllegalArgumentException();  // or whatever...
   double area = WHAT MATH DO YOU NEED HERE?
   return area;
}

Follow a similar process for your other methods.

0
votes
public static class Area 
{

    public static double getArea(double radius) 
    {
        return Math.PI * radius * radius;
    }


    public static double getArea(int length, int width) 
    {
        return length * width;
    }


    public static double getArea(double radius, double height) 

    {
        return Math.PI * radius * radius * height;
    }

}