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

s./lab01pl Use the \'script\' command to capture the program output into a text

ID: 3874965 • Letter: S

Question

s./lab01pl Use the 'script' command to capture the program output into a text file named lab01p1_output.txt' 7. Part 2 Write a program named 'lab01p2' that computes the area of triangles. The program should first prompt the user for the number of triangles to be computed. For each triangle, it should prompt the user for the base and height values. Output an error message if the input base or height is not greater than zero. The program should produce output format exactly as the example run below. Example run % ./labolp2 How many triangles? 3 --Iteration 0 -_-

Explanation / Answer

lab01p2

import java.util.*;

class CalcTrianglesArea{

public static void main(String[] args){

Scanner scn=new Scanner(System.in);

System.out.println("Enter the number of triangle's area you wants to calculate::");

int num=scn.nextInt();

while(num!=0){

System.out.println("Enter the height !!");

int height=scn.nextInt();

if(height>0){

System.out.println("Enter the width !!");

int width=scn.nextInt();

if(width>0){

System.out.println("Area for triangle having height "+ height+ " and width "+width+" is "+((height*width)/2));

}

}

num--;

}//while

}//main

}//class