Results 1 to 8 of 8
Thread: Area of Triangle
- 03-30-2012, 04:40 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Area of Triangle
Hey guys,
I'm struggling to complete a class assignment to calculate the area of a triangle. We were given a skeleton class that compiles, but doesn't really do anything.
It is up to me to write the product code but I'm have trouble in particular with the constructor and validate method. I have included the skeleton class and the work I have completed so far. Any help would be very much appropriated.gif)
Java Code://skeleton class import java.util.StringTokenizer; public class Triangle { // Represent the 3 sides of a triangle private int side1; private int side2; private int side3; public Triangle(String parameters) throws TriangleException { // This method accepts as input the three integers that // represent a triangle. The numbers, separated by a // comma, are inputted as a string. // This method validates that the integers can form a // valid triangle and assigns the values to side1, // side2, and side3. } protected static void validate(String parameters) throws TriangleException { // This method validates that three integers form // a valid triangle. The numbers, separated by a // comma, are inputted as a string. // If there are no integers, insufficient integers, or // too many integers inputted , the method throws a // TriangleException with a meaningful message. // If non-integer data is inputted, the method throws a // TriangleException with a meaningful message. // If the integer values are invalid, the method throws a // TriangleException with a meaningful message. } public int getside1() { return side1; } public void setside1(int value) throws TriangleException { // This method validates and sets side1 value. // The method throws an exception if the side1 // value is invalid. } public int getside2() { return side2; } public void setside2(int value) throws TriangleException { // This method validates and sets side2 value. // The method throws an exception if the side2 // value is invalid. } public int getside3() { return side3; } public void setside3(int value) throws TriangleException { // This method validates and sets side3 value. // The method throws an exception if the side3 // value is invalid. } public double getArea() { // This method calculates the area of the triangle // using side1, side2, and side3. } }Java Code://My Code import java.util.StringTokenizer; public class Triangle { // Represent the 3 sides of a triangle private static int side1; private static int side2; private static int side3; //private double area; public Triangle(String parameters) throws TriangleException { side1=0; side2=0; side3=0; // This method accepts as input the three integers that // represent a triangle. The numbers, separated by a // comma, are inputted as a string. // This method validates that the integers can form a // valid triangle and assigns the values to side1, // side2, and side3. } protected static void validate(String paramenters) throws TriangleException { if (side1 > 0 && side2 > 0 && side3 > 0 && side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) { } else throw new TriangleException ("New Trangle failed: " + side1 + ", " + side2 + ", " + side3); // This method validates that three integers form // a valid triangle. The numbers, separated by a // comma, are inputted as a string. // If there are no integers, insufficient integers, or // too many integers inputted , the method throws a // TriangleException with a meaningful message. // If non-integer data is inputted, the method throws a // TriangleException with a meaningful message. // If the integer values are invalid, the method throws a // TriangleException with a meaningful message. } public int getside1() { return side1; } public void setside1(int value) throws TriangleException { if ( (value <= 0) && (value > 500) && (value > side2 +side3)) throw new TriangleException ("Input Data Invalid"); else side1=value; return; // This method validates and sets side1 value. // The method throws an exception if the side1 // value is invalid. } public int getside2() { return side2; } public void setside2(int value) throws TriangleException { if ( (value <= 0) && (value > 500) && (value > side1 +side2)) throw new TriangleException ("Input Data Invalid"); else side2=value; return; // This method validates and sets side2 value. // The method throws an exception if the side2 // value is invalid. } public int getside3() { return side3; } public void setside3(int value) throws TriangleException { if ( (value <= 0) && (value > 500) && (value > side1 +side2)) throw new TriangleException ("Input Data Invalid"); else side3=value; return; // This method validates and sets side3 value. // The method throws an exception if the side3 // value is invalid. } public double getArea() { double s = (side1 + side2 + side3) / 2.0; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); return area; // This method calculates the area of the triangle // using side1, side2, and side3. } }
- 03-30-2012, 05:09 AM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Area of Triangle
A good start in the validate method would be to get the comma separated string paramater into integer values. It looks as if you are expected to use StringTokenizer. The individual string will then need to be converted to an int.
StringTokenizer (Java Platform SE 7 )
Integer (Java Platform SE 7 ) - specifically the parseInt methodLast edited by yellowledbet; 03-30-2012 at 05:12 AM.
- 03-31-2012, 01:46 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Re: Area of Triangle
Hi thanks for your reply. But Im still having troubling understanding the StringTokenizer.
This I what I have come up with so far, it would be great if you could point me in the right direction thanks.
And here is a couple of my Junit test cases.Java Code:import java.util.StringTokenizer; public class Triangle { // Represent the 3 sides of a triangle private static int side1; private static int side2; private static int side3; //private double area; public Triangle(String parameters) throws TriangleException { side1=0; side2=0; side3=0; //side1 = Integer.parseInt(parameters); //side2 = Integer.parseInt(parameters); //side2 = Integer.parseInt(parameters); // This method accepts as input the three integers that // represent a triangle. The numbers, separated by a // comma, are inputted as a string. // This method validates that the integers can form a // valid triangle and assigns the values to side1, // side2, and side3. } protected static void validate(String paramenters) throws TriangleException { String strSide = null; side1 = Integer.parseInt( strSide ); side2 = Integer.parseInt( strSide ); side3 = Integer.parseInt( strSide ); if (side1 > 0 && side2 > 0 && side3 > 0 && side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) { } else throw new TriangleException ("New Trangle failed: "); // This method validates that three integers form // a valid triangle. The numbers, separated by a // comma, are inputted as a string. // If there are no integers, insufficient integers, or // too many integers inputted , the method throws a // TriangleException with a meaningful message. // If non-integer data is inputted, the method throws a // TriangleException with a meaningful message. // If the integer values are invalid, the method throws a // TriangleException with a meaningful message. }
ThanksJava Code:public void test001ValidTriangle() throws Exception { try { //To test if a valid triangle can be constructed Triangle t = new Triangle("500,2147483646,0"); fail("Exception Expected – Should not Get HERE!"); } catch (TriangleException te) { assertEquals("New Trangle failed: ", te.getMessage()); } } public void test002ValidTriangle() throws Exception { try { //To test if a valid triangle can be constructed Triangle t = new Triangle("-1,-2147483647,1"); fail("Exception Expected – Should not Get HERE!"); } catch (TriangleException te) { assertEquals("New Trangle failed: ", te.getMessage()); }
- 03-31-2012, 08:07 AM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Area of Triangle
According to the comments in your assignment, your validate() method accepts a string. The valid input for the string is going to be 3 numbers separated by commas that meet the criteria for a triangle. In order to validate that they are in fact 3 numbers that fall within the criteria of a triangle you are going to have to look at each value in the string. The StringTokenizer can be used to separate the string into tokens based on a delimiter(a comma in your case). The link to the Java documentation in the first post has an example of the StringTokenizer and a list of its methods and constructors. Take a look and try to fit it your situation.
- 03-31-2012, 11:35 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Re: Area of Triangle
I have almost given up on the string tokenizer method, as I just cant get my head around it. I have decided to switch my attentions to the scanner as its a bit more user friendly. This is what I have come up with so far but alas I'm getting an error when I run my Junit tests. I think it is a constructor problem.
Java Code:public Triangle(String parameters) throws TriangleException { Scanner scan = new Scanner(parameters); side1 = scan.nextInt(); side2 = scan.nextInt(); side3 = scan.nextInt(); scan.close(); } protected static void validate(String parameters) throws TriangleException { Scanner scan = new Scanner(parameters); int[] sides = new int[3]; for(int i = 0; i < 4; i++) if(scan.hasNextInt()) { if (i > 2) throw new TriangleException("too many parameters"); sides[i] = scan.nextInt(); } else { throw new TriangleException("Invalid input - non integers detected"); }Java Code:public void testTriangleTooManyParameters() throws Exception { try { Triangle t = new Triangle("10,20,30,40"); fail("Exception Expected – Should not Get HERE!"); } catch (TriangleException te) { assertEquals("too many parameters", te.getMessage()); } }Last edited by donfanzu; 03-31-2012 at 11:54 PM.
- 04-01-2012, 04:48 AM #6
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Area of Triangle
please paste the full text of the error
- 04-01-2012, 03:11 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
- 04-02-2012, 05:21 AM #8
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Area of Triangle
you are trying to test several things that you seem to not understand fully. I would personally work on creating and understanding each part of your program before trying to combine them all as it becomes difficult to know what exactly is going wrong.
Part of your problem is described in the quoted text from java docs: Scanner (Java Platform SE 7 )
To fix this you need to set the delimiter of the scanner class to the delimiter used in your program. Look at the useDelimiter() method.The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace
Here is a small example of using the scanner class.
Java Code:public static void main(String[] args) { String s = "45,bar,65.0, 55, foo"; //String Variable to be scanned Scanner scanner = new Scanner(s); //Scanner class scanner.useDelimiter(","); //sets delimiter to comma //loops through while(scanner.hasNext()){ String temp = scanner.next().trim();//Gets next string value from scanner, trim() removes whitespace try{ int x = Integer.parseInt(temp);//Tries to parse string to int System.out.println(x);//prints variable }catch(NumberFormatException nfe){ System.out.println("*** unable to parse [" + temp + "] to integer");//if not an integer prints } } }
Similar Threads
-
triangle
By dinn in forum New To JavaReplies: 6Last Post: 12-07-2011, 05:20 PM -
triangle
By Shyamz1 in forum New To JavaReplies: 4Last Post: 11-07-2010, 06:12 PM -
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Computing the area of a triangle using Heron's Formula
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:39 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks