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   ...
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/
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...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java. PART 1: Implementing BubbleSorter Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on. This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name,...
Analyzing a Health Science Article Topic: COVID 19 Your review should include: Title, author, and other...
Analyzing a Health Science Article Topic: COVID 19 Your review should include: Title, author, and other components of an APA citation A brief overview of what the article is about (2 -3 sentences) When the article was written (you want to find an article from 2014 or later) The gap identified by the writer in the research Review of the results and discussion of results - provide a summary of what was found in your own words. (about half a...
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario:...
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario: Please write a few lines describing your scenario and the four variables (in addition to income) you have selected. Use Table 1 to report the variables selected for this assignment. Note: The information for the required variable, “Income,” has already been completed and can be used as a guide for completing information on the remaining variables. Table 1. Variables Selected for the Analysis Variable...
Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file...
Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file Geometry.java. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create it. 2. Below the main method, but in the Geometry class,create a static method called printMenu that has no parameter list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT