MISSING RETURN STATEMENT ERROR

import java.util.*;

public class areas2Calculation

{

//Method to calculate area of a square

public static double generic_area(double length, double height, double base_length, double radius, double PI, char shape)

{

PI = 3.1415927;

switch(shape)

{

case 's': return length * length;

case 'r': return length * height;

case 't': return base_length * height;

case 'c': return radius * radius * PI;

}

}

//Main method

public static void main(String[] args)

{

//Declare variables

Scanner dataInput = new Scanner(System.in);

double length, result, square, size, height, rectangle, base_length, triangle, radius, circle;

double PI = 3.1415927d;

int s, r, t, c;

String choice, quit;

{

//Display input message

System.out.printf("Select An Option Below (All Dimensions in Inches & Areas in Sq. Inches)%n");

System.out.printf("Enter 1 To Calculate The Area of Squares.%n");

System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");

System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");

System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");

System.out.printf("Enter 5 to Quit.%n");

System.out.print("Enter option: ");

choice = dataInput.next();

while ((choice.compareTo("1") != 0) && (choice.compareTo("2") != 0) && (choice.compareTo("3") != 0) && (choice.compareTo("4") != 0) && (choice.compareTo("5") != 0))

{

System.out.printf("Invalid option! Please re-enter.%n");

System.out.printf("Select An Option Below%n");

System.out.printf("Enter 1 To Calculate The Area of Squares.%n");

System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");

System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");

System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");

System.out.printf("Enter 5 to Quit.%n");

System.out.print("Enter option: ");

choice = dataInput.next();

}

//Check for option 1

if (choice.compareTo("1") == 0)

{

//get dimentions to calculate the area of the square(s)

System.out.print("Enter the length of the square: ");

length = dataInput.nextDouble();

System.out.print("Enter the number of further square areas to calculate: ");

square = dataInput.nextDouble();

System.out.print("Enter the length increment: ");

size = dataInput.nextDouble();

//calculate area of squares until no more remain

//int s;

for(s = 0; s < square; s++ )

{

result = generic_area(length,0,0,0,0, 's');

//display result and increase the length

System.out.println( "Square " + (s+1) + " has a Length of " + length + " and an Area equal to " + result );

length = length + size;

}

}

//check for option 2

if (choice.compareTo("2") == 0)

{

//get dimentions to calculate the area of the rectangle(s)

System.out.print("Enter the length of the rectangle: ");

length = dataInput.nextDouble();

System.out.print("Enter the height of the rectangle:");

height = dataInput.nextDouble();

System.out.print("Enter the number of further rectangle areas to calculate: ");

rectangle = dataInput.nextDouble();

System.out.print("Enter the length and height increment: ");

size = dataInput.nextDouble();

//calculate the area of rectangles until no more remain

//int r;

for (r = 0; r < rectangle; r++)

{

result = generic_area(length, height,0,0,0, 'r');

//display result, increase the length and the height

System.out.println("Rectangle " + (r+1) + " has a Length of " + length + ", a Height of " + height + " and an Area equal to " + result );

length = length + size;

height = height + size;

}

}

//check for option 3

if (choice.compareTo("3") == 0)

{

//get dimentions to calculate the area of the triangle(s)

System.out.print("Enter the base length of the triangle: ");

base_length = dataInput.nextDouble();

System.out.print("Enter the height of the triangle: ");

height = dataInput.nextDouble();

System.out.print("Enter the number of further triangle areas to calculate: ");

triangle = dataInput.nextDouble();

System.out.print("Enter the base length and height increment: ");

size = dataInput.nextDouble();

//calculate the area of rectangles until no more remain

//int t;

for (t = 0; t < triangle; t++)

{

result = generic_area(height,base_length,0,0,0, 't');

//display the result, increase the base_length and the height

System.out.println("Triangle " + (t+1) + " has a Base Length of " + base_length + ", a Height of " + height + " and an Area equal to " + result);

base_length = base_length + size;

height = height + size;

}

}

//check for option 4

if (choice.compareTo("4") == 0)

{

//get dimentions to calculate the area of the circle(s)

System.out.print("Enter the radius of the circle: ");

radius = dataInput.nextDouble();

System.out.print("Enter the number of further circle areas to calculate: ");

circle = dataInput.nextDouble();

System.out.print("Enter the radius increment: ");

size = dataInput.nextDouble();

//calculate the area of circles until no more remain

//int c;

for (c = 0; c < circle; c++)

{

result = generic_area(radius, PI,0,0,0, 'c');

//display the result and increase the radius

System.out.println("Circle " + (c+1) + " has a Radius of " + radius + " and an Area equal to " + result);

radius = radius + size;

}

}

//check for option 5

if (choice.compareTo("5") == 0)

{

//user wants to quit

System.out.print("Are you sure? (Y/N): ");

quit = dataInput.next();

if (quit.compareTo("Y") != 0)

{

System.out.print("Are you sure? (Y/N): ");

quit = dataInput.next();

}

System.out.printf("Goodbye!");

}

}

}

}

WHAT DO I NEED TO DO GET RID OF THIS ERROR?

[6332 byte] By [cgarnona] at [2007-10-2 13:47:34]
# 1

For future reference, use the code tags when posting code. There is a code button when you post. Also, always post the full, exact error message. Error messages are your friends.

I didn't go through all of your code. I found a problem with this part.static double generic_area(double length, double height, double base_length, double radius, double PI, char shape)

{

PI = 3.1415927;

switch(shape)

{

case 's': return length * length;

case 'r': return length * height;

case 't': return base_length * height;

case 'c': return radius * radius * PI;

}

}

In this method, if shape is not s,r,t,c then there is no return statement. You, as the programmer, might know that shape can never be anything else, but the compiler does not know that so it errors. You must either add a default: case that returns a double or add a return statement at the end of the method that returns a double.

atmguya at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 2
I have added the default: caseNow however, I am getting M:\LJMUWEB\School\Programming\Cwk3\areas2Calculation.java:13: '.class' expectedcase 's': return length * length;break;
cgarnona at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 3

> M:\LJMUWEB\School\Programming\Cwk3\areas2Calculatio

> n.java:13: '.class' expected

> case 's': return length * length;break;

One problem is the break after return length. Remove that.

I don't think this is the root cause of .class expected. You have changed your code from what was originally posted, so it's hard to say. The problem might be the line just above or just below the line where the error occurred.

atmguya at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 4
One might ask; are you supposed to develop "procedural Java" o_Oor an object oriented Java application?
tschodta at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 5

import java.util.*;

public class areas2Calculation

{

//Method to calculate area of a square

public static double generic_area(double length, double height, double base_length, double radius, double PI, char shape)

{

PI = 3.1415927;

switch(shape)

{

default: return double

case 's': return length * length;

case 'r': return length * height;

case 't': return base_length * height;

case 'c': return radius * radius * PI;

}

}

//Main method

public static void main(String[] args)

{

//Declare variables

Scanner dataInput = new Scanner(System.in);

double length, result, square, size, height, rectangle, base_length, triangle, radius, circle;

double PI = 3.1415927d;

int s, r, t, c;

String choice, quit;

{

//Display input message

System.out.printf("Select An Option Below (All Dimensions in Inches & Areas in Sq. Inches)%n");

System.out.printf("Enter 1 To Calculate The Area of Squares.%n");

System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");

System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");

System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");

System.out.printf("Enter 5 to Quit.%n");

System.out.print("Enter option: ");

choice = dataInput.next();

while ((choice.compareTo("1") != 0) && (choice.compareTo("2") != 0) && (choice.compareTo("3") != 0) && (choice.compareTo("4") != 0) && (choice.compareTo("5") != 0))

{

System.out.printf("Invalid option! Please re-enter.%n");

System.out.printf("Select An Option Below%n");

System.out.printf("Enter 1 To Calculate The Area of Squares.%n");

System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");

System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");

System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");

System.out.printf("Enter 5 to Quit.%n");

System.out.print("Enter option: ");

choice = dataInput.next();

}

//Check for option 1

if (choice.compareTo("1") == 0)

{

//get dimentions to calculate the area of the square(s)

System.out.print("Enter the length of the square: ");

length = dataInput.nextDouble();

System.out.print("Enter the number of further square areas to calculate: ");

square = dataInput.nextDouble();

System.out.print("Enter the length increment: ");

size = dataInput.nextDouble();

//calculate area of squares until no more remain

//int s;

for(s = 0; s < square; s++ )

{

result = generic_area(length,0,0,0,0, 's');

//display result and increase the length

System.out.println( "Square " + (s+1) + " has a Length of " + length + " and an Area equal to " + result );

length = length + size;

}

}

//check for option 2

if (choice.compareTo("2") == 0)

{

//get dimentions to calculate the area of the rectangle(s)

System.out.print("Enter the length of the rectangle: ");

length = dataInput.nextDouble();

System.out.print("Enter the height of the rectangle:");

height = dataInput.nextDouble();

System.out.print("Enter the number of further rectangle areas to calculate: ");

rectangle = dataInput.nextDouble();

System.out.print("Enter the length and height increment: ");

size = dataInput.nextDouble();

//calculate the area of rectangles until no more remain

//int r;

for (r = 0; r < rectangle; r++)

{

result = generic_area(length, height,0,0,0, 'r');

//display result, increase the length and the height

System.out.println("Rectangle " + (r+1) + " has a Length of " + length + ", a Height of " + height + " and an Area equal to " + result );

length = length + size;

height = height + size;

}

}

//check for option 3

if (choice.compareTo("3") == 0)

{

//get dimentions to calculate the area of the triangle(s)

System.out.print("Enter the base length of the triangle: ");

base_length = dataInput.nextDouble();

System.out.print("Enter the height of the triangle: ");

height = dataInput.nextDouble();

System.out.print("Enter the number of further triangle areas to calculate: ");

triangle = dataInput.nextDouble();

System.out.print("Enter the base length and height increment: ");

size = dataInput.nextDouble();

//calculate the area of rectangles until no more remain

//int t;

for (t = 0; t < triangle; t++)

{

result = generic_area(height,base_length,0,0,0, 't');

//display the result, increase the base_length and the height

System.out.println("Triangle " + (t+1) + " has a Base Length of " + base_length + ", a Height of " + height + " and an Area equal to " + result);

base_length = base_length + size;

height = height + size;

}

}

//check for option 4

if (choice.compareTo("4") == 0)

{

//get dimentions to calculate the area of the circle(s)

System.out.print("Enter the radius of the circle: ");

radius = dataInput.nextDouble();

System.out.print("Enter the number of further circle areas to calculate: ");

circle = dataInput.nextDouble();

System.out.print("Enter the radius increment: ");

size = dataInput.nextDouble();

//calculate the area of circles until no more remain

//int c;

for (c = 0; c < circle; c++)

{

result = generic_area(radius, PI,0,0,0, 'c');

//display the result and increase the radius

System.out.println("Circle " + (c+1) + " has a Radius of " + radius + " and an Area equal to " + result);

radius = radius + size;

}

}

//check for option 5

if (choice.compareTo("5") == 0)

{

//user wants to quit

System.out.print("Are you sure? (Y/N): ");

quit = dataInput.next();

if (quit.compareTo("Y") != 0)

{

System.out.print("Are you sure? (Y/N): ");

quit = dataInput.next();

}

System.out.printf("Goodbye!");

}

}

}

}

this is the latest code receiving the same

M:\LJMUWEB\School\Programming\Cwk3\areas2Calculation.java:12: '.class' expected

and to tschodt, I am a beginner programmer so please bear with me.

cgarnona at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 6
> tschodt, I am a beginner programmer Nothing wrong with that. I am merely suggesting that you check if you are doing what your coursework is intending. Calculating the area of a shape - is often used as an intro to OO.
tschodta at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 7

default: return double

What's this?

Your generic_area method is supposed to return a double. That means a numeric value. Just like in your other cases, where you return a numeric value like "length*length" For a default, may be you want to return zero. To make the compiler happy, you putdefault: return 0.0;

atmguya at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...
# 8
Thanks for the explanation. I understand the concept of the switch better. It works now. My compiler is happy
cgarnona at 2007-7-13 11:45:51 > top of Java-index,Developer Tools,Java Compiler...