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

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...
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 program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT