76 lines
4.4 KiB
HTML
76 lines
4.4 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Triangle.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/Triangle.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.mycompany.lab6.exceptions_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Triangle <span class="literal">implements</span> Geo{
|
|
<span class="literal">private</span> <span class="literal">double</span> side1 = <span class="number">1.0</span>, side2 = <span class="number">1.0</span>, side3 = <span class="number">1.0</span>;
|
|
|
|
<span class="literal">public</span> Triangle(<span class="literal">double</span> side1, <span class="literal">double</span> side2, <span class="literal">double</span> side3) {
|
|
<span class="literal">this</span>.side1 = side1;
|
|
<span class="literal">this</span>.side2 = side2;
|
|
<span class="literal">this</span>.side3 = side3;
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">double</span> getArea() {
|
|
<span class="literal">double</span> s = (side1 + side2 + side3) / <span class="number">2</span>;
|
|
<span class="literal">return</span> Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
|
|
}
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">double</span> getPerimeter() {
|
|
<span class="literal">return</span> side1 + side2 + side3;
|
|
}
|
|
@Override
|
|
<span class="literal">public</span> String toString() {
|
|
<span class="literal">return</span> <span class="string">"</span><span class="string">Triangle: side1 = </span><span class="string">"</span> + side1 + <span class="string">"</span><span class="string"> side2 = </span><span class="string">"</span> + side2 + <span class="string">"</span><span class="string"> side3 = </span><span class="string">"</span> + side3;
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
|
Scanner input = <span class="literal">new</span> Scanner(System.in);
|
|
System.out.println(<span class="string">"</span><span class="string">Enter the three sides: </span><span class="string">"</span>);
|
|
<span class="literal">double</span> side1 = input.nextDouble();
|
|
<span class="literal">double</span> side2 = input.nextDouble();
|
|
<span class="literal">double</span> side3 = input.nextDouble();
|
|
Triangle triangle = <span class="literal">new</span> Triangle(side1, side2, side3);
|
|
System.out.println(<span class="string">"</span><span class="string">The area is </span><span class="string">"</span> + triangle.getArea());
|
|
System.out.println(<span class="string">"</span><span class="string">The perimeter is </span><span class="string">"</span> + triangle.getPerimeter());
|
|
System.out.println(triangle);
|
|
}
|
|
}
|
|
<span class="literal">interface</span> Geo {
|
|
<span class="literal">double</span> getArea();
|
|
<span class="literal">public</span> <span class="literal">double</span> getPerimeter();
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|