JAVA: Compute The Area Of Triangle
Tarih: 7 Kasım 2013 Yorum: 0

(Compute the area of a triangle)  Write a program that reads three edges for a triangle and computes the perimeter if the input is valid.  Otherwise display that the input is invalid.  The input is valid if the sum of every pair of two edges is greater than the remaining edge. (You must use JOptionPane class to get input from user.)

 

 

Triangle.java

/*
 Compute the area of a triangle.
*/

 // Import JOptionPane.
 import javax.swing.JOptionPane;

public class Triangle {

    public static void main(String[] args) {

    // Create variables.
    double edge1	= 0;
    double edge2	= 0;
    double edge3	= 0;
    double result	= 0;
    String msg		= "You can not enter the number\n which is less then or equel zero.";

    	// Getting first edge length.
      	edge1 = Double.parseDouble(JOptionPane.showInputDialog("Enter the length of the first edge."));

      	// Checking first edge length. (length<0)
      	if(edge1 <= 0){

      		// First edge length<=0 => STOP
      		JOptionPane.showMessageDialog(null,msg,"Error!",JOptionPane.ERROR_MESSAGE);

		}else{

      		// Getting second edge length.
      		edge2 = Double.parseDouble(JOptionPane.showInputDialog("Enter the length of the second edge."));

      		// Checking second edge length. (length<0)
      		if(edge2 <= 0){

      			// Second edge length<=0 => STOP
      			JOptionPane.showMessageDialog(null,msg,"Error!",JOptionPane.ERROR_MESSAGE);

      		}else{

      			// Getting third edge length.
      			edge3 = Double.parseDouble(JOptionPane.showInputDialog("Enter the length of the third edge."));

      			// Checking third edge length. (length<0)
      			if(edge3 <= 0){

      				// Third edge length<=0 => STOP
      				JOptionPane.showMessageDialog(null,msg,"Error!",JOptionPane.ERROR_MESSAGE);

      			// Checking conditions to being triangle.	
      			}else if(edge3 > edge1+edge2 || edge2 > edge1+edge3 || edge1 > edge2+edge3 || edge3 < edge1-edge2 || edge3 < edge2-edge1 || edge1 < edge2-edge3  || edge1 < edge3-edge2 || edge2 < edge1-edge3 || edge2 < edge3-edge1){

      				// One of the conditions is not TRUE => STOP
      				JOptionPane.showMessageDialog(null,"One of the conditions to being triangle is not confirmed!","Error!",JOptionPane.ERROR_MESSAGE);

      			}else{

      				// All conditions is OK.
      				result = edge1+edge2+edge3;
      				JOptionPane.showMessageDialog(null,"The primeter of triangle is "+result,"Result",JOptionPane.INFORMATION_MESSAGE);

      			}		
      		}
      	}	
    }
}



Yorum Yok:


Yorum Yap:

Yorum yapabilmek için giriş yapmalısınız.




tema yapımcısı wordpress alexa bilgileri Webmaster Creative Commons v3 ile Lisanslanmıştır!


Akif ARSLAN © 2012 - 2019
Sitede bulunan istediğiniz cümleyi veya içeriği, istediğiniz gibi, istediğiniz yerde, istediğiniz zaman ve istediğiniz kişilerle paylaşabilirsiniz.