5 Replies - 3960 Views - Last Post: 11 November 2008 - 07:31 PM Rate Topic: -----

#1 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

calculate the areas of different shapes

Posted 11 November 2008 - 05:40 PM

My program is supposed to calculate the different areas of different shapes
but

My problem is that I am trying to get it to tell the user to enter in their input again if they do not enter a number like they are supposed to for case 1 or the square currently it will work if they enter only one letter such as a b or c but not if they enter more than one combined
such as asdf and also it only prints out please try again once after just a single letter is entered than it will break unless they enter a number like they are supposed to




/**
 * @(#)shape.java
 *
 * shape application
 *
 * @author 
 * @version 1.00 2008/10/24
 */
 import java.util.Scanner;
 
public class shape {
	
	public static void main(String[] args) {
			Scanner kboard = new Scanner (System.in);
			String start, shape, square, triangle, triangle2, rectangle, rectangle2, circle, trapezoid, trapezoid2, trapezoid3, sphere, sphere2, x, x2, y, y2;
			String[] num = {"1", "2", "3", "4", "5", "6", "7", "8"};
			String[] alpha = {"", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
							  "y", "z"};
			  
			
			boolean go = true;
		
	   String[] shapes = {"1. SQUARE", "2. TRIANGLE" , "3. RECTANGLE" , "4. CIRCLE" , "5. TRAPEZOID" , "6. SPHERE" , "7. LINE", "8. QUIT"};
			
		System.out.println("WELCOME TO THE SHAPES GAME");
		System.out.println("------------------------------");
		System.out.println("Created by:\n\n\n");
  
  while (go)
	{
			boolean repeat = false;
	for (int list = 0; list < shapes.length; list++)
	 {
	 	System.out.println (shapes[list] + " ");
	 }
		
		System.out.println("Please insert the number of the shape you would \nlike to calculate the formulas for: \nENTER 8 TO EXIT!!");
	 shape = kboard.nextLine();
	 shape = shape.replaceAll(" ","").toLowerCase();
	 
	 	  for (int check = 0; check < num.length; check++)
	   {
		 	if (shape.equals(num[check]))
		 		repeat = true;
	   }
	   
	   while (!repeat)
	   {
	 System.out.println("Please try again: ");
	 shape = kboard.nextLine();
	 shape = shape.replaceAll(" ","").toLowerCase();
	  for (int check = 0; check < num.length; check++)
				   if (shape.equals(num[check]))
					   repeat = true;
	   }
	
	int bacon = Integer.parseInt(shape);
		 		
	 
	 switch (bacon)
	 {
	 	case 1:
	 		System.out.println("\n\nYou have entered the square");
	 		System.out.print("Please enter the length of the sides: ");
	 			 square = kboard.nextLine();
	 			 square = square.replaceAll(" ","").toLowerCase();
	 				
	 				boolean repeatSQ = false;
	for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
	   {
		 	if (square.equals(alpha[checkSQ]))
		 		repeatSQ = true;
	   }
	   
	   while (repeatSQ)
	   {
	 System.out.println("Please try again: ");
	 square = kboard.nextLine();
	 square = square.replaceAll(" ","").toLowerCase();
	  for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
				   if (!square.equals(alpha[checkSQ]))
					   repeatSQ = false;			   
	   }			 
			
			 sqRet(square);
			break;
		case 2:
			System.out.println("\n\nYou have entered the triangle");
			System.out.print("Please enter base, height: ");
				 triangle = kboard.nextLine();
	 			 triangle = triangle.replaceAll(" ","");
				 triangle2 = kboard.nextLine();
	 			 triangle2 = triangle2.replaceAll(" ","");
	 		triRet (triangle, triangle2);
	 		break;
	 	case 3:
			System.out.println("\n\nYou have entered the rectangle");
			System.out.print("Please enter length, width: ");
				 rectangle = kboard.nextLine();
	 			 rectangle = rectangle.replaceAll(" ","");
				 rectangle2 = kboard.nextLine();
	 			 rectangle2 = rectangle2.replaceAll(" ","");
	 		recRet (rectangle, rectangle2);
	 		break;
	 	case 4:
			System.out.println("\n\nYou have entered the circle");
			System.out.print("Please enter the radius of the circle: ");
				 circle = kboard.nextLine();
	 			 circle = circle.replaceAll(" ","");
	 		cirRet (circle);
	 		break;
	 	case 5:
			System.out.println("\n\nYou have entered the trapezoid");
			System.out.print("Please enter top base, bottom base, height: ");
				trapezoid = kboard.nextLine();
	 			trapezoid = trapezoid.replaceAll(" ","");
				trapezoid2 = kboard.nextLine();
	 			trapezoid2 = trapezoid2.replaceAll(" ","");
				trapezoid3 = kboard.nextLine();
	 			trapezoid3 = trapezoid3.replaceAll(" ","");
			traRet (trapezoid, trapezoid2, trapezoid3);
			break;
		case 6:
			System.out.println("\n\nYou have entered the sphere");
			System.out.print("Please enter the radius: ");
				 sphere = kboard.nextLine();
	 			 sphere = sphere.replaceAll(" ","");
	 		spheRet (sphere);
	 		break;
	 	case 7:
			System.out.println("\n\nYou have entered to line");
			System.out.print("Please enter first set of points: ");
				x = kboard.nextLine();
	 			x = x.replaceAll(" ","");
				 y = kboard.nextLine();
	 			 y = y.replaceAll(" ","");
	 		System.out.print("Please enter second set of points: ");
				 x2 = kboard.nextLine();
	 			 x2 = x2.replaceAll(" ","");
				 y2 = kboard.nextLine();
	 			 y2 = y2.replaceAll(" ","");
	 	   lineRet (x,y,x2,y2);
	 		break;
	 	case 8:
			System.out.println("You have chosen to quit");
	 		System.exit(0);
	 		go = false;
	 		break;
	 	default:
			System.out.println("Please Try Again!!!");
	 		
	 }
	 
   
	
	}
	
	
}


private static void sqRet (String square)
{
	double sq = Double.parseDouble(square);
					   System.out.println("\n\nYou have inserted " + square + " for the sides\n" + "The area of this square is: " 
					   	+ square + "^2" + "=" + (double)Math.pow(sq,2) + "\n\n");  
}

private static void triRet (String triangle, String triangle2)
{
	double tri = Double.parseDouble(triangle);
	double tri2 = Double.parseDouble(triangle2);
	 					System.out.println("You have inserted a base of " + triangle + " and a height of " 
	 							+ triangle2 + "\nThe area of this triangle is: " + triangle + "^2"
	 							+ "/2= " + (tri*tri2)/2); 
}

private static void recRet (String rectangle, String rectangle2)
{
	double rec = Double.parseDouble(rectangle);
	double rec2 = Double.parseDouble(rectangle2);
	 					System.out.println("You have inserted a length of " + rectangle + " and a width of " 
	 							+ rectangle2 + "\nThe area of this rectangle is: " + rectangle + "*"
	 							+ rectangle2 + "= " + (rec*rec2));
}

private static void cirRet (String circle)
{
   double circ = Double.parseDouble(circle);
			System.out.print("You have inserted " + circle + " for the radius\n" + "The area of this circle is: " 
							 + circle + "^2" + "*" + "3.14=" + (circ*circ)*3.14 + "\n\n");
}

private static void traRet (String trapezoid, String trapezoid2, String trapezoid3)
{
	double trap = Double.parseDouble(trapezoid);
	double trap2 = Double.parseDouble(trapezoid2);
	double trap3 = Double.parseDouble(trapezoid3);
	 	   System.out.println("You have inserted a top base of " + trapezoid + " a bottom base of "
	 				   		  + trapezoid2 + " and a height of " + trapezoid3 + "\nThe area of this " 
	 				   		  + "trapezoid is: 1/2*" + trapezoid3 +"*("+ trapezoid +"+"+ trapezoid2+")" + "="
	 				   		  + (.5*trap3)*(trap+trap2)+"\n\n"); 
}

private static void spheRet (String sphere)
{
	double sph = Double.parseDouble(sphere);
	 	   System.out.println("\n\nYou have inserted a radius of " + sphere
	 						   + "\nThe area of this sphere is: " + "4*3.14*" + sphere + "^2"
	 						  + "=" + (4*3.14*sph*sph) + "\n\n");
}

private static void lineRet (String x, String y, String x2, String y2)
{
   double x11 = Double.parseDouble(x);
   double y11 = Double.parseDouble(y);
   double x111 = Double.parseDouble(x2);
   double y111 = Double.parseDouble(y2);
   double solve = ((x111-x11)*(x111-x11)+(y111-y11)*(y111-y11));	  
	 			  System.out.println("\nYou have inserted the points (" + x + "," + y + ") & (" + x2 + "," + y2 + ")\n"
	 			  + "The Distance between these points is: " + "The square root of (" + x2 + "-" + x + ")^2+(" + y2 + "-" + y + ")^2=" 
	 			  + Math.sqrt(solve));
}


}





really the only code you may need to look at is
String[] alpha = {"", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
							  "y", "z"};



and
 switch (bacon)
	 {
	 	case 1:
	 		System.out.println("\n\nYou have entered the square");
	 		System.out.print("Please enter the length of the sides: ");
	 			 square = kboard.nextLine();
	 			 square = square.replaceAll(" ","").toLowerCase();
	 				
	 				boolean repeatSQ = false;
	for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
	   {
		 	if (square.equals(alpha[checkSQ]))
		 		repeatSQ = true;
	   }
	   
	   while (repeatSQ)
	   {
	 System.out.println("Please try again: ");
	 square = kboard.nextLine();
	 square = square.replaceAll(" ","").toLowerCase();
	  for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
				   if (!square.equals(alpha[checkSQ]))
					   repeatSQ = false;			   
	   }			 
			
			 sqRet(square);
			break;


This post has been edited by Jayman: 11 November 2008 - 07:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: calculate the areas of different shapes

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: calculate the areas of different shapes

Posted 11 November 2008 - 06:49 PM

Before I say anything, I have a few things I think I should point out, both for my sanity and the sanity of others, and for your own good.

1) Indent Properly. If you indent one thing in a loop, indent all things in a loop, if you don't, I have to put all your code into Eclipse.

2) Variable names. You program has a lot of variables, and it would be nice if I could determine what they represent, the name "bacon" kinda threw me off.

3) Comments!!! If you leave comments, people will be able to help you much faster because we can understand what you think your code is doing, and then we can see what it is really doing.

Now that we have that out of the way, let's look at your code here.

The problem I found happens in this block of code:
			case 1:
				System.out.println("\n\nYou have entered the square");
				System.out.print("Please enter the length of the sides: ");
				square = kboard.nextLine();
				square = square.replaceAll(" ","").toLowerCase();

				boolean repeatSQ = false;
				for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
				{
					if (square.equals(alpha[checkSQ]))
						repeatSQ = true;
				}

				while (repeatSQ)
				{
					System.out.println("Please try again: ");
					square = kboard.nextLine();
					square = square.replaceAll(" ","").toLowerCase();
					for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
						if (!square.equals(alpha[checkSQ]))
							repeatSQ = false;               
				}             

				sqRet(square);
				break;


Now, I looked at this for a minute, and I narrowed it down to what you do right here:

for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
						if (!square.equals(alpha[checkSQ]))
							repeatSQ = false;        

Now, from what I've gathered, your whole method for comparing strings is flawed when you consider you are comparing a String of any length to a single character. My approach to this would be just trying to parse, and then catching the exception, like so:
case 1:
				System.out.println("\n\nYou have entered the square");
				System.out.print("Please enter the length of the sides: ");
				square = kboard.nextLine();
				square = square.replaceAll(" ","").toLowerCase();

				boolean repeatSQ = false;
				
				try
				{
					Integer.parseInt(square);
				}
				catch(NumberFormatException e)
				{
					repeatSQ = true;
				}
				while (repeatSQ)
				{
					System.out.println("Please try again: ");
					square = kboard.nextLine();
					square = square.replaceAll(" ","").toLowerCase();
					try
					{
						Integer.parseInt(square);
						repeatSQ = false;
					}
					catch(NumberFormatException e)
					{
						repeatSQ = true;
					}
					
				}             

				sqRet(square);
				break;

Now I know this could be done a lot differently, but this is how I got it. I just make sure that everything in the string is an integer by trying to parse the string to an integer. If it doesn't like that, then it throws an exception, the NumberFormatException, so I have a piece of code waiting there for when it throws that exception to catch it, and redirect it to continue looping.

Any questions?

This post has been edited by TriggaMike: 11 November 2008 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

Re: calculate the areas of different shapes

Posted 11 November 2008 - 07:08 PM

Thanks your way makes alot more sense to me I really appreciate it

also if I wanted to put that into a private would I do it like this
and would I return a String or a boolean?
private static String SQUAREret (String square)
{
	 boolean repeatSQ = false;  
				Scanner kboard = new Scanner (System.in);
				try  
				{  
					Integer.parseInt(square);  
				}  
				catch(NumberFormatException e)  
				{  
					repeatSQ = true;  
				}  
				while (repeatSQ)  
				{  
					System.out.println("Please try again: ");  
					square = kboard.nextLine();  
					square = square.replaceAll(" ","").toLowerCase();  
					try  
					{  
						Integer.parseInt(square);  
						repeatSQ = false;  
					}  
					catch(NumberFormatException e)  
					{  
						repeatSQ = true;  
					}  
					  
				}
			return square;
}



and thanks again for the help
I will also comment it and indent correctly and use variables that make more sense thanks for the advice






View PostTriggaMike, on 11 Nov, 2008 - 05:49 PM, said:

Before I say anything, I have a few things I think I should point out, both for my sanity and the sanity of others, and for your own good.

1) Indent Properly. If you indent one thing in a loop, indent all things in a loop, if you don't, I have to put all your code into Eclipse.

2) Variable names. You program has a lot of variables, and it would be nice if I could determine what they represent, the name "bacon" kinda threw me off.

3) Comments!!! If you leave comments, people will be able to help you much faster because we can understand what you think your code is doing, and then we can see what it is really doing.

Now that we have that out of the way, let's look at your code here.

The problem I found happens in this block of code:
			case 1:
				System.out.println("\n\nYou have entered the square");
				System.out.print("Please enter the length of the sides: ");
				square = kboard.nextLine();
				square = square.replaceAll(" ","").toLowerCase();

				boolean repeatSQ = false;
				for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
				{
					if (square.equals(alpha[checkSQ]))
						repeatSQ = true;
				}

				while (repeatSQ)
				{
					System.out.println("Please try again: ");
					square = kboard.nextLine();
					square = square.replaceAll(" ","").toLowerCase();
					for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
						if (!square.equals(alpha[checkSQ]))
							repeatSQ = false;               
				}             

				sqRet(square);
				break;


Now, I looked at this for a minute, and I narrowed it down to what you do right here:

for (int checkSQ = 0; checkSQ < alpha.length; checkSQ++)
						if (!square.equals(alpha[checkSQ]))
							repeatSQ = false;        

Now, from what I've gathered, your whole method for comparing strings is flawed when you consider you are comparing a String of any length to a single character. My approach to this would be just trying to parse, and then catching the exception, like so:
case 1:
				System.out.println("\n\nYou have entered the square");
				System.out.print("Please enter the length of the sides: ");
				square = kboard.nextLine();
				square = square.replaceAll(" ","").toLowerCase();

				boolean repeatSQ = false;
				
				try
				{
					Integer.parseInt(square);
				}
				catch(NumberFormatException e)
				{
					repeatSQ = true;
				}
				while (repeatSQ)
				{
					System.out.println("Please try again: ");
					square = kboard.nextLine();
					square = square.replaceAll(" ","").toLowerCase();
					try
					{
						Integer.parseInt(square);
						repeatSQ = false;
					}
					catch(NumberFormatException e)
					{
						repeatSQ = true;
					}
					
				}             

				sqRet(square);
				break;

Now I know this could be done a lot differently, but this is how I got it. I just make sure that everything in the string is an integer by trying to parse the string to an integer. If it doesn't like that, then it throws an exception, the NumberFormatException, so I have a piece of code waiting there for when it throws that exception to catch it, and redirect it to continue looping.

Any questions?

Was This Post Helpful? 0
  • +
  • -

#4 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: calculate the areas of different shapes

Posted 11 November 2008 - 07:14 PM

If you just wanted to add this error handling as part of your method, all you would need to do is just have a loop that surrounds the parsing of the string that catches the exception, and just then runs the piece of code to get a new string and has it's own boolean variable to repeat in that sense. Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#5 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

Re: calculate the areas of different shapes

Posted 11 November 2008 - 07:26 PM

View PostTriggaMike, on 11 Nov, 2008 - 06:14 PM, said:

If you just wanted to add this error handling as part of your method, all you would need to do is just have a loop that surrounds the parsing of the string that catches the exception, and just then runs the piece of code to get a new string and has it's own boolean variable to repeat in that sense. Hope that helps!




Yes Thank You :^:
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: calculate the areas of different shapes

Posted 11 November 2008 - 07:31 PM

Topic renamed to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1