Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Triangles program  RSS feed

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a Java program using JCreator called Triangles that asks the user to enter three whole numbers representing the angles of a triangle. The purpose of the program is to determine, based on these numbers if the triangle is equalateral, isosceles or right angles.

If all angles are equal then I want to output the sentence using System.out.print, stating that this is an equalateral triangle. If only two angles are equal I want the sentence to state that this is an isosceles triangle. If any of the angles are 90o then I want to output a sentence stating that this is a right angled triangle.

This is what ive got so far:

public class Triangles
public static void main (String [] args)

String triangle(double angle1, double angle2, double angle3) {
if( (angle1 + angle2 + angle3) !=180)
return "Not a triangle";
if(angle1 == angle2 || angle1 == angle3 || angle2 == angle3)
return "Isosceles";
if(angle1 == 90.0 || angle2 == 90.0 || angle3 == 90.0)
return "Right";
if(angle1 == angle2 && angle1 == angle3)
return "Equalateral";


but i dont know what else i need. Please help
[ October 28, 2006: Message edited by: Emma Elliott ]
author and iconoclast
Posts: 24217
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Welcome to JavaRanch!

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copy of this thread and your other one.

As far as your program goes: main() doesn't have a return value -- it returns "void", meaning nothing. Your problem description says to use System.out.println() to print the results -- so do so, rather than trying to use "return".
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need three values (angles), so let the user enter them first...
Hint: showInputDialog() method of JOptionPane class

you can ask for three values one by one or type all at once,
so, hint for second case: split() method of String class

You have three strings (or array of strings) but you
need, parseDouble() method of Double class.
Be careful: conversion of f.e. "blahblah" as user input
will hint: NumberFormatException class will be helpful

Of course, you can use Stdin class and getDouble() method instead
of input dialog and 'manual' you wish

Finally, having three numerical values (or array of values)
you can easy write methods answering simple questions:
boolean isIsosceles(double []ang),
boolean isRightAngled(double []ang)
boolean isEquilateral(double []ang)....

or isTriangleAtAll()

Having this stuff you can write: fact, equilateral triangle is also isosceles
and right-angled may be would be better to write:

[ October 28, 2006: Message edited by: Natasza Biecek ]
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!