-OUTPUT-
Requirements/Prequisites:
Installed JDK 1.7 (Java Development Kit) for compilation of source code
JRE (java Runtime Environment) for execution of source code
Installed JDK 1.7 (Java Development Kit) for compilation of source code
JRE (java Runtime Environment) for execution of source code
SOURCE CODE
FOR Triangle.java
Copy the code given below into notepad and save it as "Triangle.java"
FOR Triangle.java
Copy the code given below into notepad and save it as "Triangle.java"
import java.util.Scanner;
public class Triangle{
/** constructors */
public Triangle(){ //default constructor
a=b=c=0;
}
public Triangle(int a,int b,int c){ //parametrized constructor
this.a = a;
this.b = b;
this.c = c;
}
/** methods */
public boolean inputAndDetermine(int triNo){ //get the input from user at run time and return true if triangle can be constructed or false if not constructed from the sides entered by the user at run time.
a=getIntValue(1,triNo);
b=getIntValue(2,triNo);
c=getIntValue(3,triNo);
if((a+b)>c&&(b+c)>a&&(a+c)>b){
return true;
}else{
return false;
}
}
public void determineType(){ //determine type of triangle and set the "type" variable of int type to 1 if equilateral , 2 if isosceles, 3 if scalene, -1 if not constructed
if(a==b){
if(b==c){
type = EQUILATERAL;
ttype = "EQUILATERAL";
}else{
type = ISOSCELES;
ttype = "ISOSCELES";
}
}else if(b==c){
type = ISOSCELES;
ttype = "ISOSCELES";
}else if(a==c){
type = ISOSCELES;
ttype = "ISOSCELES";
}else{
type = SCALENE;
ttype = "SCALENE";
}
}
public void calculatePerimeter(){ //calculate perimeter of triangle and store it in "perimeter" variable of int type
perimeter = a+b+c;
}
public void calculateArea(){ //calculate Area of triangle and store it in "area" variable of in type
float s = (float)(a+b+c)/2;
area = (float)Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public void displayResult(){ //display result i.e . area , perimeter and type of triangle
Triangle.display(5,70,' ');
display(5,70,String.format("Area : %.2f, Perimeter : %d, type : %s ",area,perimeter,ttype));
Triangle.display(5,70,' ');
}
public void displayErrorMessage(){ //display error i.e. triangle cannot be constructed
Triangle.display(5,70,' ');
display(5,70,"Triangle cannot be constructed ");
Triangle.display(5,70,' ');
type=-1;
}
public void display(int n){ //display a formated output showing 3 sides of triangle, perimeter, area, and type
String etc="";
int space=5;
int width = 70;
String format="";
if(type!=-1){
format = "%8d%4d%4d%14d%15.2f%20s";
display(space,width,String.format(format,a,b,c,perimeter,area,ttype));
}else{
format = "%8d%4d%4d%14s%15s%20s";
display(space,width,String.format(format,a,b,c,"N/A","N/A",ttype));
}
}
/**helper methods */
public int getType(){ //return the value of "type" variable stored in it
return type;
}
public static void display(int start,int len,char c){ //diplay a row of character 'c' of length 'len', leaving 'start' spaces in beginning
for(int i=0;i<start;i++){
System.out.print(" ");
}
System.out.print("*");
for(int i=0;i<len;i++){
System.out.print(c);
}
System.out.println("*");
}
public static void display(int start,int total,String str){ //diplay a string "str", whose position is centered between the length "total" and leaving 'start' spaces in beginning
int len = str.length();
if(len%2!=0){
len++;;
}
int spac = total-len;
if(spac<0){
spac = 0;
}
spac = spac/2;
if(spac==0){
spac = 1;
}
String format = "%"+start+"s*%"+spac+"s%s%"+(spac+1)+"s*\n";
System.out.printf(format,"","",str,"");
}
public int getIntValue(int side,int triNo){ //used to get the positive int value from the user, until he donot enter correct value and display the apporpriate messages and return it back
int side_;
int space = 5;
int w = 70; //width
String tempSide="";
String str="Enter the length of "+((side==1)?side+"st":((side==2)?side+"nd":side+"rd"))+" side of Triangle "+triNo+" : ";
System.out.printf("%5s*%2s%s","","",str);
while(true){
tempSide=s.nextLine();
tempSide=tempSide.trim();
while(tempSide.equals("")){
str="!!Enter the length of "+((side==1)?side+"st":((side==2)?side+"nd":side+"rd"))+" side of Triangle "+triNo+" : ";
System.out.printf("%5s*%2s%s","","",str);
tempSide=s.nextLine();
tempSide=tempSide.trim();
}
s1 = new Scanner(tempSide);
tempSide="";
try{
side_ = s1.nextInt();
}catch(java.util.InputMismatchException e){
System.out.printf("%5s*%2s!!Please Enter Integer Value : ","","");
continue;
}
if(side_<0){
System.out.printf("%5s*%2s!!Please enter positive Value : ","","");
continue;
}
return side_;
}
//return side_;
}
/** attributes */
private int a,b,c; //sides of triangle
private float area; //store area
private int perimeter; //store perimeter
private int type; //to store the type of triangle, -1 invalid triangle, 1 means EQUILATERAL,
//2 means ISOSCELES, 3 means SCALENE
private String ttype ="";
static final int EQUILATERAL=1;
static final int ISOSCELES=2;
static final int SCALENE=3;
static Scanner s = new Scanner(System.in);
static Scanner s1;
}
FOR "TriangleTest.java"
Copy the code given below into new notepad file and save it as "TriangleTest.java"
public class TriangleTest{
public static void main(String[] args){
Triangle[] t = new Triangle[N]; //creating array of triangles
int i = 0;
displayHeader(); //display the header "TRIANGLES"
while(i<N){
t[i] = new Triangle(); //creating a Triangle object
Triangle.display(5,70,'='); //displays a row of character filled with '='
if(t[i].inputAndDetermine(i+1)){ //get input from the user
t[i].determineType(); //determine the type of user
t[i].calculatePerimeter(); //calculate the perimeter of triangle
t[i].calculateArea(); //calculate the area of triangle
t[i].displayResult(); //display the result of triangle
}else{
t[i].displayErrorMessage(); //displays error message that triangle cannot be constructed, as sides entered are unable to do so
}
i++;
}
displayTriangles(t); //display a table showing sides, area, perimeter and type of each triangle
displayBarChartOf(t); //display the bar chart
}
public static void displayHeader(){ //display the topmost header in the program once on starting "TRIANGLES"
Triangle.display(5,70,'=');
Triangle.display(5,70,' ');
Triangle.display(5,70,"TRIANGLES");
Triangle.display(5,70,' ');
}
public static void displayBarChartOf(Triangle[]t){ //display the bar chart at the end of program
int k = 0;
for(int i=0;i<N;i++){
if(t[i].getType()==-1){
k++;
}
}
int space = 5;
int width = 70;
Triangle.display(space,width,'=');
Triangle.display(space,width,' ');
Triangle.display(space,width,"Bar Chart of Number of Triangle Constructed");
Triangle.display(space,width,' ');
Triangle.display(space,width,'-');
Triangle.display(space,width,' ');
StringBuffer sb1 = new StringBuffer(String.format("%35s"," Constructed triangles: "));
StringBuffer sb2 = new StringBuffer(String.format("%35s"," Not Constructed triangles: "));
for(int i=0;i<N;i++){
if(i<k){
sb2.append("*");
}else{
sb2.append(" ");
}
if(i<N-k){
sb1.append("*");
}else{
sb1.append(" ");
}
}
Triangle.display(space,width,sb1.toString());
Triangle.display(space,width,sb2.toString());
Triangle.display(space,width,' ');
Triangle.display(space,width,'=');
}
public static void displayTriangles(Triangle[]t){ //display table of showing sides(a,b,c), area, and perimeters of allthe entered N triangles
int space = 5;
int width = 70;
Triangle.display(space,width,'=');
Triangle.display(space,width,' ');
Triangle.display(space,width,"Summary of Computing Triangle Perimeter & Areas");
Triangle.display(space,width,' ');
Triangle.display(space,width,'-');
//Triangle.display(space,width,' ');
String str="";
Triangle.display(space,width,"Sides(a,b,c) Perimeter Area Type");
//Triangle.display(space,width,' ');
Triangle.display(space,width,'-');
//Triangle.display(space,width,' ');
for(int j=0;j<N;j++){
t[j].display(10);
}
//Triangle.display(space,width,' ');
Triangle.display(space,width,'=');
}
final static int N = 8; //Number of triangles to be stored
}
Open Command prompt and type following commands
javac TriangleTest.java
Now you will get two class files.TriangleTest.class
Triangle.class
Execution
At command prompt type :
java TriangleTest
after this command you will get output as shown at the top of this blog. Enter the sides of triangle, when asked and then you will get the results i.e. area, perimeter, type etc.
Comments
Post a Comment