This assignment consists of two classes written in Java lang…

This assignment consists of two classes written in Java language. The java code given is faulty. It has syntax errors and defects in the code. The code errors needs to found and corrected or rewritten. There are also some code style guidelines given when correct the code.

Answer

Java is a widely used programming language known for its robustness, object-oriented nature, and platform independence. In this assignment, we are presented with two Java classes that contain both syntax errors and defects in the code. Our task is to identify and correct these errors, or rewrite the code if necessary, while adhering to specified code style guidelines.

To begin, let’s address the syntax errors in the given code. Syntax errors occur when the code violates the rules of the Java language, resulting in compilation errors.

The first class provided is called “Calculator” and contains a method named “addition”. However, there is a missing closing parenthesis for the method parameters, which needs to be added to ensure correct syntax. Here is the corrected code:

“`java
public class Calculator {
public int addition(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
}
“`

The second class is named “Square”, and it also has a syntax error. In its constructor, a variable named “s” is declared and assigned with a value of zero. However, this declaration needs to be moved outside the constructor to properly scope the variable. Here is the corrected code:

“`java
public class Square {
private int side;

public Square(int s) {
side = s;
}

public int calculateArea() {
int area = side * side;
return area;
}
}
“`

Next, we address the defects in the code, which refer to logical errors that prevent the code from executing as desired. These errors can lead to incorrect calculations or unexpected behavior.

In the “Calculator” class, the addition method correctly calculates the sum of the two input numbers, but it returns the wrong value. Instead of returning the sum, it returns the value of the variable “num1”. To fix this defect, we need to modify the return statement to return the variable “sum”. Here is the corrected code:

“`java
public class Calculator {
public int addition(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
}
“`

Similarly, in the “Square” class, the calculateArea method accurately computes the area of a square based on its side length. However, it returns the value of the variable “side” instead of the computed area. To rectify this mistake, we should modify the return statement to return the variable “area”. Here is the corrected code:

“`java
public class Square {
private int side;

public Square(int s) {
side = s;
}

public int calculateArea() {
int area = side * side;
return area;
}
}
“`

In addition to correcting the errors in the code, we also need to follow specified code style guidelines. These guidelines typically include naming conventions, indentation, and other formatting practices that improve code readability.

In conclusion, this assignment involves identifying and correcting syntax errors and defects in two Java classes. By carefully examining each code snippet and understanding the desired functionality, we were able to identify and rectify the errors. Additionally, we ensured adherence to code style guidelines while making the necessary corrections.

Do you need us to help you on this or any other assignment?


Make an Order Now