Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions LCAPP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Value
{
int a,b;
void setValue(int x,int y)
{ a=x;
b=y;
}
int getResult()
{ return 0;
}
}
class Add extends Value
{
int getResult(){
return a+b;
}
}
class Mul extends Value
{
int getResult(){
return a*b;
}
}
class Sub extends Value{

int getResult(){
return a-b;
}
}
class Div extends Value
{
int getResult(){
return a/b;
}
}
Comment on lines +30 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Add division-by-zero check.

The division operation will throw an ArithmeticException if b is zero. This must be handled to prevent runtime crashes.

Apply this diff to add validation:

 class Div extends Value 
 {
 	  int getResult(){
+		  if (b == 0) {
+			  throw new IllegalArgumentException("Cannot divide by zero");
+		  }
 		  return a/b;
 	  }
 }
🤖 Prompt for AI Agents
In LCAPP.java around lines 30 to 35, the Div.getResult() method performs a/b
without checking for division by zero; add a guard that checks if b == 0 and, if
so, throw a clear ArithmeticException (or IllegalArgumentException) with a
descriptive message; otherwise perform and return the division. Ensure the
method's visibility/signature remains appropriate and no unchecked divide
occurs.

class Calculator
{
void performOperation(Value v,String type)
{
int result= v.getResult();
System.out.println(type+"\t"+result);
}
}
public class LCAPP
{
public static void main(String x[])
{
Calculator c = new Calculator();
Value v=null;
v=new Add();
v.setValue(10,20);
c.performOperation(v,"Addition");
v=new Mul();
v.setValue(5,4);
c.performOperation(v,"Multiplication");

v=new Sub();
v.setValue(10,5);
c.performOperation(v,"Substraction");

v=new Div();
v.setValue(10,2);
c.performOperation(v,"Division");
}
}
Comment on lines +44 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Fix typo in operation label.

Line 59 has "Substraction" which should be "Subtraction". This is user-facing output and should be corrected.

Apply this diff to fix the typo:

 	  v=new Sub();
 	  v.setValue(10,5);
-	  c.performOperation(v,"Substraction");
+	  c.performOperation(v,"Subtraction");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public class LCAPP
{
public static void main(String x[])
{
Calculator c = new Calculator();
Value v=null;
v=new Add();
v.setValue(10,20);
c.performOperation(v,"Addition");
v=new Mul();
v.setValue(5,4);
c.performOperation(v,"Multiplication");
v=new Sub();
v.setValue(10,5);
c.performOperation(v,"Substraction");
v=new Div();
v.setValue(10,2);
c.performOperation(v,"Division");
}
}
public class LCAPP
{
public static void main(String x[])
{
Calculator c = new Calculator();
Value v=null;
v=new Add();
v.setValue(10,20);
c.performOperation(v,"Addition");
v=new Mul();
v.setValue(5,4);
c.performOperation(v,"Multiplication");
v=new Sub();
v.setValue(10,5);
c.performOperation(v,"Subtraction");
v=new Div();
v.setValue(10,2);
c.performOperation(v,"Division");
}
}
🤖 Prompt for AI Agents
In LCAPP.java around lines 44 to 65, the user-facing label passed for the
subtraction operation is misspelled as "Substraction"; change that string to
"Subtraction" so the output displays the correct spelling when calling
c.performOperation for the subtraction case.