For working professionals
For fresh graduates
More
Step by Step Java Tutorial Con…
1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
Now Reading
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Java is an object-oriented programming language developers widely use for developing software applications.
Java offers platform independence, vast libraries, and frameworks, making it a popular choice among developers, especially for building robust, scalable, and portable applications for web, enterprise, mobile, and embedded systems.
You can easily compute the area of triangle in Java. This could be done using formulas such as area = (base * height) / 2. For example, you can write a Java program that accepts user input for the base and height values and computes the triangle's area with these values' help.
In this tutorial, we’ll discuss several methods of finding the area of a triangle in Java. Keep reading to know more.
If we know the base (b) and the height (h) of the triangle, we can use the formula:
Area = (b * h) / 2
If we know the lengths of all three sides (a, b, c), we can use Heron's formula:
Area = sqrt(s * (s - a) * (s - b) * (s - c))
where s = (a + b + c) / 2
If we know the coordinates of three vertices (x1, y1), (x2, y2), (x3, y3), we can use the shoelace formula:
Area = 0.5 * |(x1y2 + x2y3 + x3y1) - (x2y1 + x3y2 + x1y3)|
Here is an example of calculating the area of a triangle using the height and base of the triangle (base x height) in Java:
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
double area = (base * height) / 2;
System.out.println("The area of the triangle is: " + area);
}
}
The program begins by importing the Scanner class from java.util package, allowing us to read user input. Next, a class named TriangleAreaCalculator is declared, which contains the main method, serving as the program's entry point.
Inside the main method, an instance of the Scanner class is created and associated with the standard input stream (System.in), enabling input reading. The program then prompts the user to enter the base of the triangle by printing the corresponding message. The entered value is read as a double using the nextDouble() method of the Scanner class and assigned to the base variable.
Similarly, the program prompts the user to enter the height of the triangle, reads the input value as a double, and assigns it to the height variable. The area of the triangle is calculated using the formula (base * height) / 2 and assigned to the area variable.
Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "The area of the triangle is: " with the value of the area variable.
Here is an example of calculating the area of a triangle using the lengths of its three sides (Heron's formula) in Java:
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of side a:");
double a = scanner.nextDouble();
System.out.println("Enter the length of side b:");
double b = scanner.nextDouble();
System.out.println("Enter the length of side c:");
double c = scanner.nextDouble();
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("The area of the triangle is: " + area);
}
}
In the above example, the program prompts the user to enter the lengths of the triangle's three sides. It reads these values as double using the nextDouble() method of the Scanner class and assigns them to the variables a, b, and c.
Next, it calculates the semi-perimeter of the triangle (s) using the formula (a + b + c) / 2. Then, it applies Heron's formula to calculate the triangle area using the Math.sqrt() function to compute the square root.
Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "The area of the triangle is: " with the value of the area variable.
Here is an example of calculating the area of a triangle in Java using a constructor:
import java.util.Scanner;
public class Triangle {
private double base;
private double height;
private double area;
public Triangle (double base, double height) {
this.base = base;
this.height = height;
}
public void calculateArea() {
area = (base * height) / 2;
}
public double getArea() {
return area;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
Triangle triangle = new Triangle(base, height);
triangle.calculateArea();
System.out.println("The area of the triangle is: " + triangle.getArea());
}
}
This example defines a Triangle class with private fields for base, height, and area. The class has a constructor that takes the base and height as parameters and assigns them to the corresponding fields.
We also have two methods within the Triangle class: calculateArea() and getArea(). The calculateArea() method calculates the area of the triangle using the formula (base * height) / 2 and stores the result in the area field. The getArea() method returns the value of the area field.
In the main method, we prompt the user to enter the base and height of the triangle and read those values using the Scanner class. Then, we create an instance of the Triangle class, passing the base and height as arguments to the constructor.
Next, we call the calculateArea() method on the triangle object to calculate the area. Finally, we display the calculated area of the triangle using the getArea() method. When the program runs, it asks the user to input the base and height of the triangle, creates a Triangle object, calculates the area using the provided values, and displays the resulting area of the triangle.
import java.util.Scanner;
public class upGradTutorials {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
double area = calculateTriangleArea(base, height);
System.out.println("The area of the triangle is: " + area);
}
public static double calculateTriangleArea(double base, double height) {
return (base * height) / 2;
}
}
In this program, we define a class called TriangleAreaCalculator. The main method prompts the user to enter the base and height of the triangle, reads those values using the Scanner class, and assigns them to the variables base and height.
The program then calls the calculateTriangleArea method, passing the base and height as arguments. The calculateTriangleArea method calculates the triangle area using the formula (base * height) / 2 and returns the result.
Finally, the calculated area is stored in the area variable and displayed to the user using the System.out.println statement.
Here is an example of calculating the area of a triangle in Java using object-oriented programming:
import java.util.Scanner;
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
public Triangle(double a, double b, double c) {
sideA = a;
sideB = b;
sideC = c;
}
public double calculateArea() {
double s = (sideA + sideB + sideC) / 2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of side A:");
double sideA = scanner.nextDouble();
System.out.println("Enter the length of side B:");
double sideB = scanner.nextDouble();
System.out.println("Enter the length of side C:");
double sideC = scanner.nextDouble();
Triangle triangle = new Triangle(sideA, sideB, sideC);
double area = triangle.calculateArea();
System.out.println("The area of the triangle is: " + area);
}
}
In the above program, we define a Triangle class representing a triangle object. The Triangle class has private fields for sideA, sideB, and sideC, representing the lengths of the triangle's three sides.
The Triangle class has a constructor that takes sideA, sideB, and sideC as parameters and initializes the corresponding fields.
Additionally, the Triangle class has a calculateArea() method that calculates the area of the triangle using Heron's formula: Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)), where s is the semi-perimeter of the triangle.
In the main method, we prompt the user to enter the lengths of the triangle's sides (sideA, sideB, and sideC) and read those values using the Scanner class. We then create a Triangle object by invoking the constructor with the provided side lengths.
Next, we call the calculateArea() method on the triangle object to calculate the area and store the result in the area variable. Finally, we display the calculated area of the triangle using the System.out.println statement.
The numerous ways of calculating the area of a triangle in Java have been discussed in this tutorial. You must enroll in a certified course if you want to use Java for more advanced functions. You could look for software development courses by upGrad. The courses on upGrad are well-designed for professionals and ensure you develop a holistic understanding of the subject. Enquire today to know more.
The base and height values can be represented using the double data type in Java, which can be decimal numbers.
In Java, the constructor of the area of a triangle would typically be defined within a Triangle class and would take the base and height values as parameters. It would initialize the instance variables of the Triangle object with the provided values.
The Math class in Java provides various mathematical functions. However, you cannot use it directly to calculate a triangle's area. You will need to implement the formula manually.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.