Java or C++ gurus help!

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Hi, I am new to Java and I am working on it. I have a problem that needs to be solved. I need to find if the triangle is "isosceles triangle" based on 3 inputs: angleA, angleB, angleC. Can someone complete it? It can be done in Java or C++. Format should be: public class Triangle { public static String reportTriangleType(int angleA, int angleB, int angleC) { … } } Thank you very much.

ktm_4 · Sep 30, 2011 5:01 PM · 503 views

9 Replies

 Here is a simple function (C++): std::string reportTriangleType( int angleA, int angleB, int angleC) { if (angleA == AngleB || angleB == angleC || angleA  ==  angleC)      return "isosceles"; else      return "nonisosceles"; } Last edited: 30-Sep-11 05:32 PM

livetorock · Sep 30, 2011 5:31 PM

@LiveToRock Thanks for the response. I wrote below code: but got error. #include <iostream> #include <cmath> std::string reportTriangleType( int angleA, int angleB, int angleC) { if (angleA == angleB || angleB == angleC || angleA  ==  angleC)      return "isosceles"; else      return "nonisosceles";      system("pause"); } Also, the format should be exactly: public class Triangle { public static String reportTriangleType(int angleA, int angleB, int angleC) { … } } Can someone do it in Java as well? Thanks.

ktm_4 · Sep 30, 2011 5:45 PM

Buddy you gotta try man. This is a simple shit. Here's one in C++ fully working. If you can't solve this, you might need to retake the class man. No hard feelings #include <iostream> using namespace std; class triangle {     double angle1,angle2,angle3;     public:     triangle(double,double,double);     bool iso( ); }; triangle::triangle(double x,double y, double z){    angle1=x;    angle2=y;    angle3=z;    cout << "Triangle : Created\n"; } bool triangle::iso( ) {      if ( (angle1 == angle2) || (angle2 == angle3) || (angle3 == angle1 ) )         return 1;      else         return 0; } int main()     {     triangle t1(23,23,30);     triangle t2(1,2,3);     if ( t1.iso( ) ) cout << "Triangle(1) is Isosceles\n";     if ( t2.iso( ) ) cout << "Triangle(2) is Isosceles\n";     return 0;     }                                                                                                                                                                                                                                                                                                                                                                                                                                                   

dalle_momo · Sep 30, 2011 5:57 PM

@ dalle_momo, Thanks for the response. It is my first semester. So i needed some help. On: triangle t1(23,23,30); triangle t2(1,2,3); How can user insert input and get an output? I want to check my numbers. Stil, I am looking for a Java code as well. And something exactly as described on question. Thanks.

ktm_4 · Sep 30, 2011 7:31 PM

 @ktm_4 Is this assignment for your programming course or for other course? I understand that for a beginner understanding a, b, c of programming is tough. If it is for programming class, you better start digging more hard. This is an easy problem and you may get tougher questions in your mid terms and finals! (and who is gonna help then, unless it is take home exam ;-) ? ).  Now for your program,  you have a class "Triangle" and you need to write a public function reportTriangleType(..... ... .. ) that returns type "string" and takes three integer arguments angleA, angleB and angleC. The return string that you want probably is the string that tells whether the triangle formed from those passed angles is isosceles or not.   dalle_momo did the work, but in a little different way. Well, there are many ways to do this... and do you need it to be done in the very specific method you mentioned? Even so, it is easy.... and the work is almost done, just put things together...  Just defining functions does not execute the program. You need to have main function and call the necessary functions. After you write the function to check the triangle, what you need is the main function where you get the angles (hard code it or get it from the user  at run time? ) to pass to the function and call the function passing those values. Finally, print the string you receive. Hope this helps...  Last edited: 30-Sep-11 08:12 PM

LiveToRock · Sep 30, 2011 8:11 PM

@ ktm4 !! dherai dukkha bhayo jasto cha timi lai ,,below is the complete code in java.. for your inf. implementing OO (good practice) i have two separate classes...... first class (here you have all ur logic): public class Triangle {   public void reportTriangleType(int angleA, int angleB, int angleC) { if (angleA== angleB ||angleA== angleB ||angleA== angleB ) System.out.println("Is Issoceles"); else System.out.println( "Is not");   } } second class: (driver class) import java.util.Scanner;   public class DriverClass { public static void main(String[] args) { System.out.println("enter angle 1"); Scanner sajha = new Scanner(System.in); int angleA= sajha.nextInt(); System.out.println("enter angle 2"); int angleB= sajha.nextInt(); System.out.println("enter angle 3"); int angleC= sajha.nextInt(); Triangle triangle = new Triangle(); triangle.reportTriangleType(angleA, angleB, angleC); } } good luck!  

maila_baa · Sep 30, 2011 9:25 PM

@Maila_Baa Thank you so much! How to test using JUnit  i.e. TriangleTest to test "reportTriangleType"? Thank you.

ktm_4 · Sep 30, 2011 9:50 PM

//Triangle class public class Triangle {  public static String reportTriangleType(int angleA, int angleB, int angleC) { if (angleA ==  angleB || angleA ==  angleB || angleA ==  angleB ) { return "ISOSCELES";                } else {                         return "NOT_ISOSCELES";                }    } } //TriangleTest class public class TriangleTest {           @Test      public void testReportTriangleType() {           //Simple test fixture           int angles[][] = {{45, 45, 90}, {30, 60, 90}};           String expected = "ISOSCELES";           String acutal = Triangle.reportTriangleType(angles[0][0], angles[0][1], angles[0][3]);           Assert.assertEquals(expected, actual);           expected = "NOT_ISOSCELES";           acutal = Triangle.reportTriangleType(angles[1][0], angles[1][1], angles[1][3]);           Assert.assertEquals(expected, actual);      } }

fuckeetow · Sep 30, 2011 10:38 PM

 @ktm,, [Disallowed String for - bad word]ee's test is very nice for 4.4. the only thing u could add in the testcase is making sure the return type is not null,,,,its obvios but just makes u r test case more versatile!!

Maila_Baa · Sep 30, 2011 11:00 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion