QuestionQuestion

Lab 1 – Triangle
What: a java file containing your solution
In this lab, you will learn about variables, arithmetic, assignments, basic input-output, and built-in mathematical functions in java.
Problem: Three points on a plane determine a triangle, this could be a degenerate triangle. You need to determine the area, perimeter and heights of this triangle.
1/ Describe how you want to solve this problem: write this description as a comment at the top of your java file. The description should be free of typos and grammatically correct.
2/ Put together a Java code that addresses the above problem. Each instruction of your code should be commented to explain what it does and why the whole piece of code is an actual solution to the above problem.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

public class Triangle {

    // x and y coordinates of three points.
    float x1, x2, x3;
    float y1, y2, y3;

    /**
    * This method will calculate the area
    * of the triangle using heron's formula.
    * @return the area
    */
    public float calculateArea() {
       float area = 0.0f;

       float a = calculateDistance(x1, y1, x2, y2);
       float b = calculateDistance(x2, y2, x3, y3);
       float c = calculateDistance(x3, y3, x1, y1);

       float s = (a + b + c)/2;

       area = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c));

       return area;
    }

    /**
    * This method finds the length of each side using coordinates points.
    * Then, it simply adds all side to calculate the perimeter.
    *
    * @return the perimeter
    */
    public float calculatePerimeter() {
       float perimeter = 0.0f;

       float a = calculateDistance(x1, y1, x2, y2);
       float b = calculateDistance(x2, y2, x3, y3);
       float c = calculateDistance(x3, y3, x1, y1);

       perimeter = a + b + c;

       return perimeter;
    }

    /**
    * This method utilises the calculateArea method to get
    * the area, then applying, Area = base*height/2
    * formula to calculate the height of the triangle.
    * @return the height of the triangle.
    */
    public float calculateHeight() {
       float height = 0.0f;

       float a = calculateDistance(x1, y1, x2, y2);
       float b = calculateDistance(x2, y2, x3, y3);
       float c = calculateDistance(x3, y3, x1, y1);

       float area = calculateArea...

By purchasing this solution you'll be able to access the following files:
Solution.png and Solution.java.

$24.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats