Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming: DUE: tel: Chapter 11,15, 6.6, 6.12 Liang: Chapter 12, 7.13 Name you

ID: 3859788 • Letter: P

Question

Programming: DUE: tel: Chapter 11,15, 6.6, 6.12 Liang: Chapter 12, 7.13 Name your file for PAM: P7 . java Tuesday August 1, 2017 6am You will write an application program tor fundasental command line arguments file will be specified on the comma Eile. Refer to Exceptions and St ram for fundanental File 1/0t Data will be read from File 1/0 Input/Output) using a simple plain text file. This The command line argument will nd line Use se command line ream L/0 handout a be found as an i argunents to open the ndex in the String args[]. This application program wil handle unchecked exceptions ArrayIndexOutofBoundstxception and un and also handle checked exception FileNotFoundException is application program will also use fundamental Exception handling. You wi You will declare (not handle) the checked exception 10Exception An acray of triangle sides is allocated, filled with input values, prints type of triangle and the perimeter. Avoid hardcoding class P7 private statie final int ASIE 3: private static int aTri new int (ASIZE]: 3 elements of triangie sid 1/ Array size public static void main(String (1 args) throws IOException public static void typeTriangle () public static void calcPerimeter () a) main ) will open args t01 using FileReader and ButeReader construators BufferedReader readLine ( ) will read in triangle sides values tro.the-tut tokenize the input with String method splito". Reter to Exceptions handout Allow your application program to repeat this calculation until no moze input is in the text file. See code below Convert the token into an integer using Integer-parseInt (. Catch the NumberFormatException if the user enters an invalid error message, print a stack trace ex.printStackTrace),and loop back to read the next line of input. integer v // string for input line // Array of strings for each word // readtineD input froa file String inputStr=""; String si BufferedReader in; in-new BufferedReader (new FileReader (args101)) // try block for other Exceptins // Parse into tokens 11 n, readLine() )null ) // Read entire line whil. ( ( inputStr try sinputstr. split( ") Iraverse array foro s.length Int(s[)); // Convert triangle sade // w. aTrii -Integer , parse print an error sessage array, ck and read the next line of input. It there ba and call calcerineter) 25 elements than the size of the If there are less e loop typed in, then assign only the first 3 into the array

Explanation / Answer

Given below is the code for the question. Please rate the answer if it helped. Thank you.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class P7 {
   private static final int ASIZE = 3;
   private static int []aTri = new int [ASIZE];
  
   public static void main(String[] args) {
       String inputStr = "";
       String [] s;
       BufferedReader in;
       if(args.length != 1)
       {
           System.out.println("Usage: java P7 [file]");
           System.exit(1);
       }
       try {
           in = new BufferedReader(new FileReader(args[0]));
          
           while((inputStr = in.readLine()) != null){
              
               s = inputStr.split(" ");
               try{
                   for(int i = 0; i < s.length; i++){
                       aTri[i] = Integer.parseInt(s[i]);
                   }
                   System.out.println(" Input: " + inputStr);
                   if(s.length < 3){
                       System.out.println(" ERROR! Entered less than 3 sided. Enter again.");
                       continue;
                   }
               }
               catch(NumberFormatException e){
                   System.out.println(" Input: " + inputStr + " are not valid sides of a triangle.");
                   e.printStackTrace();
                   continue;
               }
               catch(ArrayIndexOutOfBoundsException e){
                   System.out.println(" Input: " + inputStr +" Entered more than 3 sides. First 3 sides accepted. Continuing...");
               }
              
              
               typeTriangle();
               calcPerimeter();
              
           }
           in.close();
          
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }
  
   public static void typeTriangle(){
       String type;
       String sides ="";
       boolean error = false;
       for(int i = 0;!error && i < ASIZE; i++){
           if(aTri[i] < 1 || aTri[i] > 100)
               error = true;
           if(i != 0)
               sides += " + ";
           sides += aTri[i];
       }
      
       if(error){
           System.out.println(" ERROR! Out of range 1-100. Enter again");
       }
       else{
              
          
           if(aTri[0] == aTri[1] && aTri[1] == aTri[2]) //all 3 sides equal
               type = "Equilateral Triangle";
           else if(aTri[0] == aTri[1] || aTri[1] == aTri[2] || aTri[0] == aTri[2]) //any 2 sides equal
               type = "Isoceles Triangle";
           else
               type = "Scalene Triangle";
          
           System.out.println(type + ": " + sides);
       }
   }
  
   public static void calcPerimeter(){
       if((aTri[0] + aTri[1] > aTri[2]) &&
               (aTri[1] + aTri[2] > aTri[0] ) &&
               (aTri[0] + aTri[2] > aTri[1])){ //sum of any 2 sides should be greater than third side to form a triangle
           int perimeter = aTri[0] + aTri[1] + aTri[2];
           System.out.println("Perimeter: " + perimeter);
       }
       else{
           System.out.println(" ERROR! Sum of 2 sides < 3rd side");
       }
          
   }
}

input: triangle.txt

4 4 5
4 4 4
4 5
4 5 6 7
4 5 0
4 5 ?

output


Input: 4 4 5
Isoceles Triangle: 4 + 4 + 5
Perimeter: 13

Input: 4 4 4
Equilateral Triangle: 4 + 4 + 4
Perimeter: 12

Input: 4 5
   ERROR! Entered less than 3 sided. Enter again.

Input: 4 5 6 7
   Entered more than 3 sides.
   First 3 sides accepted. Continuing...
Scalene Triangle: 4 + 5 + 6
Perimeter: 15

Input: 4 5 0
   ERROR! Out of range 1-100. Enter again
   ERROR! Sum of 2 sides < 3rd side

Input: 4 5 ? are not valid sides of a triangle.
java.lang.NumberFormatException: For input string: "?"
   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
   at java.lang.Integer.parseInt(Integer.java:580)
   at java.lang.Integer.parseInt(Integer.java:615)
   at P7.main(P7.java:27)