Question

In: Computer Science

/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import...

/* 
 * Assignment: #3
 * Topic: Identifying Triangles
 * Author: <YOUR NAME>
 */

package edu.depaul.triangle;

import static edu.depaul.triangle.TriangleType.EQUILATERAL;
import static edu.depaul.triangle.TriangleType.ISOSCELES;
import static edu.depaul.triangle.TriangleType.SCALENE;

import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A class to classify a set of side lengths as one of the 3 types
 * of triangle: equilateral, isosceles, or scalene.
 *
 * You should not be able to create an invalid Triangle.  The
 * constructor throws an IllegalArgumentException if the input
 * cannot be interpreted as a triangle for any reason.
 */
public class Triangle {

  private static final Logger logger = LoggerFactory.getLogger(Triangle.class);
  private int[] sides = new int[3];
  
  /**
   * Define as private so that it is not a valid
   * choice.
   */
  private Triangle() {}
  
  public Triangle(String[] args) {
    validateArgs(args);

    parseArgs(args);

    validateLength();
  }

  public TriangleType classify() {
    TriangleType result;
    if ((sides[0] == sides[1]) && (sides[1] == sides[2])) {
      result = EQUILATERAL;
    } else if ((sides[0] == sides[1])
        || (sides[1] == sides[2])) {
      result = ISOSCELES;
    } else {
      result = SCALENE;
    }
    logger.debug("classified as: " + result);
    return result;
  }

  private void parseArgs(String[] args) {
    // throws IllegalArgumentException on a failed parse
    for (int i = 0; i < args.length; i++) {
      sides[i] = Integer.parseInt(args[i]);
    }
  }

  private void validateArgs(String[] args) {
    if ((args == null) || (args.length != 3)) {
      throw new IllegalArgumentException("Must have 3 elements");
    }
  }

  private void validateLength() {
    for (int i = 0; i < sides.length; i++) {
      if (sides[i] > 400) {
        throw new IllegalArgumentException("max size is 400");
      }
    }
  }
  
}
/*
 * Assignment: #3
 * Topic: Identifying Triangles
 * Author: <YOUR NAME>
 */
package edu.depaul.triangle;

import static org.junit.jupiter.api.Assertions.*;
import static edu.depaul.triangle.TriangleType.ISOSCELES;
import static edu.depaul.triangle.TriangleType.EQUILATERAL;
import static edu.depaul.triangle.TriangleType.SCALENE;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class TriangleTest {

  @Test
  @DisplayName("Remove me and add proper tests")
  void testPlaceHolder() {

  }
}

Need to write tests to see if the program works correctly. (using assert)

Solutions

Expert Solution

Here's the Another easy code for identify Triangle Type

import java.util.*;
public class MainClass {
public static void main(String[] args) {

int sides[] = new int[3];
for(int i = 0; i<sides.length; i++){
if(i == 0){
System.out.println("What is the first side Measure?");
Scanner side1 = new Scanner(System.in);
sides[i] = side1.nextInt();
}
else if(i == 1){
System.out.println("What is the second side Measure?");
Scanner side2 = new Scanner(System.in);
sides[i] = side2.nextInt();
}
else{
System.out.println("What is the third side Measure?");
Scanner side3 = new Scanner(System.in);
sides[i] = side3.nextInt();
}
}
if(sides[0] == sides[1] && sides[1] == sides[2]){
System.out.println("It's an equilateral triangle.");
}
else if(sides[0] == sides[1] || sides[1] == sides[2] || sides[0] == sides[2]){
System.out.println("It's an isosceles triangle.");
}
else{
System.out.println("It's a scalene triangle.");
}
}

}


Related Solutions

package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number...
package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number between 0 and 9, and challenge the user to guess the number. Write a loop that asks the user to guess a number that the computer is thinking of. Print a success message if they guess correctly. If the user does not guess correctly, tell the user that they need to guess higher, or lower, and ask the user to try again. The user...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
JAVA SHOW YOUR OUTPUT package exampletodo; import java.util.Arrays; import stdlib.*; /** * Edit the sections marked...
JAVA SHOW YOUR OUTPUT package exampletodo; import java.util.Arrays; import stdlib.*; /** * Edit the sections marked TODO * * Unless specified otherwise, you must not change the declaration of any * method. */ public class example {        /**        * valRange returns the difference between the maximum and minimum values in the        * array; Max-Min. Precondition: the array is nonempty. Your solution must go        * through the array at most once.        *        * Here are...
with java code write The method prints a meaningful title with your name as the author....
with java code write The method prints a meaningful title with your name as the author. The method then generates THREE random numbers 0 - 4 and reports how many of the numbers are the same. 2-Use a loop to repeat 10 times. the output should be like this Title title title by Your Name 4, 3, 4 two are the same 4, 0, 0 two are the same . . . 2, 0, 4 none are the same 4,...
How do I use the meta element to sepicify your name as the document author and...
How do I use the meta element to sepicify your name as the document author and as kansas and elections as key words for web search engines? How do I create semantic link in the document head linking this document to the office of the kansas secretary of state at the following address: ( http://www.kssos.org/elections_statistics.html ) use the property attribute value of external for the link.
In your reading assignment, “Engaging With Children and Young People” by Mary Kellett, the author suggested...
In your reading assignment, “Engaging With Children and Young People” by Mary Kellett, the author suggested methods for more effective communication. Mention three “pearls” that you will utilize in practice involving general and/or specific pediatric populations. https://epubs.scu.edu.au/cgi/viewcontent.cgi?article=1029&context=ccyp_pubs/
I have questions in regards to the following code 1. package Week_5; 2. import java.util.InputMismatchException; 3....
I have questions in regards to the following code 1. package Week_5; 2. import java.util.InputMismatchException; 3. import java.util.Scanner; 4. public class Customer { 5. public String firstName; 6. public String lastName; 7. public String FullName() 8. { 9. return this.firstName+" "+this.lastName; 10. } 11. } 12. public class ItemsForSale { 13. public String itemName; 14. public double itemCost; 15. public boolean taxable; 16. public void PopulateItem(String iName, double iCost, boolean canTax) 17. { 18. this.itemName = iName; 19. this.itemCost =...
For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment....
For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment. Considering the specific requirements of your scenario, complete the following steps using Excel. The accuracy of formulas and calculations will be assessed. Select the appropriate discrete probability distribution. If using a binomial distribution, use the constant probability from the collected data and assume a fixed number of events of 20. If using a Poisson distribution, use the applicable mean from the collected data. Identify...
You are to name your package assign1 and your file Palindrome.java. Palindrome Class Palindrome - testString...
You are to name your package assign1 and your file Palindrome.java. Palindrome Class Palindrome - testString : String + Palindrome (String) + isPalindrome (): boolean The method isPalindrome is to determine if a string is a palindrome. A palindrome, for this assignment, is defined as a word or phrase consisting of alphanumeric characters that reads the same frontwards and backwards while ignoring cases, punctuation and white space. If there are no alphanumeric characters, the string is considered a palindrome. The...
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of...
Test Your Understanding Create a JAVA package by name ClassEx2 and in the main() method of the class, assign your name to a String object and your father name to a different String object and do the following; Concatenate your name and your father name. Get the length of your full name. Find the index of any character in your full name. Replace the occurrence of any lower case character in your full name to capital character. Compare your first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT