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

HELLO, I Need help with this plllllz anyone !!! Q1: THE CALCULATOR CLASS: public

ID: 3543051 • Letter: H

Question





HELLO, I Need help with this plllllz anyone !!!




Q1:



THE CALCULATOR CLASS:


public class Calculator {

/** Main method */

public static void main(String[] args) {

Check number of strings passed

if ( != 3) {

System.out.println(

"Usage: java Calculator operand1 operator operand2");

System.exit(0);

}


// The result of the operation

int result = 0;


// Determine the operator

switch( ) {

case '+': result = Integer.parseInt( ) +

Integer.parseInt( );

break;

case '-': result = Integer.parseInt( ) -

Integer.parseInt( );

break;

case '*': result = Integer.parseInt( ) *

Integer.parseInt( );

break;

case '/': result = Integer.parseInt( ) /

Integer.parseInt( );

}


// Display result

System.out.println( + ' '+ + ' ' + + " = " + result);

}

}




Q2:


THE TRAIANGLE CLASS :


double b;

double c;

Triangle my = new Triangle(1,1.5,1);

// display output

System.out.println("Sides of Triangle "+my.toString());

System.out.println("Area of Triangle "+my.getarea());

System.out.println("Perimeter of triangle "+my.getperimeter());

System.out.println("color of the Triangle is "+my.getcolor());

System.out.println("is triangle filled "+my.isFilled());

}

Explanation / Answer

USING EXCEPTION HANDLER DETECTING IF OPERAND IS NON NUMERIC: import java.util.Scanner; public class Calculator { /** Main method */ public static void main(String[] args) { System.out.println("Usage: java Calculator operand1 operator operand2"); Scanner in=new Scanner(System.in); String[] operands=in.nextLine().split(" "); if ( operands.length!= 3) { System.out.println( "Usage: java Calculator operand1 operator operand2"); System.exit(0); } double op1 = 0,op2=0; int temp=0; try{ op1=Double.parseDouble((operands[0])); } catch(NumberFormatException n){ temp++; System.out.println(operands[0]+" not a valid operand"); } try{ op2=Double.parseDouble((operands[2])); } catch(NumberFormatException n){ temp++; System.out.println(operands[2]+" not a valid operand"); } if(temp>0) System.exit(0); // The result of the operation double result = 0; // Determine the operator char c=operands[1].charAt(0); switch(c) { case '+': result = op1 +op2; break; case '-': result = op1 -op2; break; case '*': result = op1*op2; break; //EXCEPTION HANDLING COMES HERE case '/': if(Integer.parseInt(operands[2])== 0){ System.out.println( op1+ " "+ operands[1].charAt(0)+ " " +op2 + " = NA "); System.exit(0);} else result = op1/(double)op2; break; default: System.out.println("entered operator not supported by caluculator"); System.exit(0); } // Display result System.out.println( op1+ " "+ operands[1].charAt(0)+ " " +op2 + " = " + result); } } WITHOUT USING EXCEPTION HANDLER DETECTING IF OPERAND IS NON NUMERIC : import java.util.Scanner; public class Calculator { /** Main method */ public static void main(String[] args) { System.out.println("Usage: java Calculator operand1 operator operand2"); Scanner in=new Scanner(System.in); String[] operands=in.nextLine().split(" "); if ( operands.length!= 3) { System.out.println( "Usage: java Calculator operand1 operator operand2"); System.exit(0); } double op1 = 0,op2=0; int temp=0; if(!operands[0].matches("-?\d+(\.\d+)?")) {temp++; System.out.println(" wrong operand "+operands[0]); } if(!operands[2].matches("-?\d+(\.\d+)?")){ op2=Double.parseDouble((operands[2])); temp++; System.out.println(" wrong operand "+operands[2]); } if(temp>0) System.exit(0); op1=Double.parseDouble((operands[0])); op2=Double.parseDouble((operands[2])); // The result of the operation double result = 0; // Determine the operator char c=operands[1].charAt(0); switch(c) { case '+': result = op1 +op2; break; case '-': result = op1 -op2; break; case '*': result = op1*op2; break; //EXCEPTION HANDLING COMES HERE case '/': if(Integer.parseInt(operands[2])== 0){ System.out.println( op1+ " "+ operands[1].charAt(0)+ " " +op2 + " = NA "); System.exit(0);} else result = op1/(double)op2; break; default: System.out.println("entered operator not supported by caluculator"); System.exit(0); } // Display result System.out.println( op1+ " "+ operands[1].charAt(0)+ " " +op2 + " = " + result); } }