Question

In: Computer Science

Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...

Write a simple java class that contains the following three methods:

1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise.

2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle.

3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula. You must call the perimeter method from this method.

4. maxSide -- accepts 3 integers, which represent the sides of a triangle. Returns the maximum side.

Solutions

Expert Solution

Code:

import java.util.Scanner;
import java.lang.Math;
class triangle
{
   public boolean isosceles(int a,int b,int c)
   {
       if(a==b||b==c||c==a)
       {/*If 2 sides are equal then we return true*/
           return true;
       }
       else
       {/*Else we return false*/
           return false;
       }
   }
   public int perimeter(int a,int b,int c)
   {/*Return the perimeter which is sum of sides*/
       return a+b+c;
   }
   public double area(int a,int b,int c)
   {/*Area */
       double s=perimeter(a,b,c)/2;/*Calculating s*/
       return Math.sqrt(s*(s-a)*(s-b)*(s-c));/*Returning the area*/
   }
   public int maxSide(int a,int b,int c)
   {
       return (a>b && a>c)?a:(b>c)?b:c;/*Returning the max side*/
   }
   public static void main(String[] args)
   {
       int a,b,c;
       Scanner scnr=new Scanner(System.in);/*Scanner object*/
       System.out.print("Enter the three sides of the triangle:");
       a=scnr.nextInt();
       b=scnr.nextInt();
       c=scnr.nextInt();/*Reading the three sides of the triangle*/
       triangle tr=new triangle();/*Triangle object tr*/
       if(tr.isosceles(a,b,c))
       {/*If isosceles we print isoscles triangle*/
           System.out.println("The triangle is a isosceles triangle");
       }
       else
       {/*else we print not a isoscles triangle*/
           System.out.println("The triangle is not a isosceles triangle");
       }
       System.out.println("Area of the triangle:"+tr.area(a,b,c));/*Printing the area*/
       System.out.println("Maximum side:"+tr.maxSide(a,b,c));/*Printing the maximum side*/
   }


}

Output:

Indentation:


Related Solutions

Write a Calculate class which contains methods that accepts two integers as arguments and return the...
Write a Calculate class which contains methods that accepts two integers as arguments and return the results of Addition, Subtraction, Division and Multiplication. Call these methods though main()and print the results.
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiusToFahrenheit(double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheitToCelsius(double fahrenheit) The formula for the conversion is: Celsius = (5.0 / 9) * (Fahrenheit – 32) Fahrenheit = (9.0 / 5) * Celsius + 32 Program output should be like: If you want to convert from Fahrenheit To Celsius press 0...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Question 3: Write the following Java program. A. Create a class called Quadrilateral that contains the...
Question 3: Write the following Java program. A. Create a class called Quadrilateral that contains the following: [4 Marks] • Five double type data fields for: side1, side2, side3, side4, and perimeter • A no-argument constructor that creates a Quadrilateral with all sides equal to 1 • Methods named setSide1(double s1), setSide2(double s2), setSide3(double s3), and setSide4(double s4) to set the values of the four sides • A method named calculatePerimeter( ) to calculate and return the perimeter of the...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT