Q1: THE CALCULATOR CLASS: public class Calculator { /** Main method */ public st
ID: 3543063 • Letter: Q
Question
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());
}
Listing 9.5, Calculator.java, is a simple command-line calculator. Note that the program terminates if any operand is nonnumeric. Write a program with an exception handler that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective. Your program should display a message that informs the user of the wrong operand type before exiting (see Figure 14.12). (IllegalTriangleException) Programming Exercise 11.1 defined the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class, and modify the constructor of the Triangle class to throw an Illegal TriangleExcept ion object if a triangle is created with sides that violate the rule, as follows:Explanation / Answer
public class IllegalTriangleException extends Exception { public IllegalTriangleException() { } public IllegalTriangleException(String message) { System.out.println(message); } } public class Triangle { public Triangle(int i, double d, int j) throws IllegalTriangleException { // TODO Auto-generated constructor stub if(i+d
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.