In: Computer Science
Question 1) Identify the type of bad smells and refactor the given code.
1. Code 1:
public double sumSquaresArea ( L1, L2 )
{
area1 = smallSquareArea(L1);
area2 = bigSquareArea(L2);
return area1+area2
}
public double smallSquareArea ( L1)
{
area = L1 * L1;
return area;
}
public double bigSquareArea ( L2 )
{
area = L2 * L2;
return area;
}
2. Code 2:
class Student
{
private string name;
public void getStudentMarks ( );
}
class Course_Marks
{
private int course_id;
private string semester;
public void getStudentMarks ( );
}
3. Code 3:
public void accountInfo(string account_title, string account_number, string account_type)
{
// Generate Monthly Statement
if(account_type == “Current”)
{
// do something
}
else if (account_type == “Saving”)
{
// do something
}
else
{
//do something
}
//Account information
System.out.println(“***** Account Information *****”);
System.out.println(“Account_Title : ” , account_title);
System.out.println(“Account Number : ” , account_number);
System.out.println(“Account_Type : ” , account_type);
System.out.println(“Monthly Statement : ”, monthly_statement );
}
Question 1 :-
Explanation :-
Return type and data type of the arguements are not present and semi color during return was not written due to which error raised. Corrected code
Program :-
public double sumSquaresArea
(double L1,double L2 )
{
double area1 = smallSquareArea(L1);
double area2 = bigSquareArea(L2);
return area1+area2;
}
public double smallSquareArea (double L1)
{
double area = L1 * L1;
return area;
}
public double bigSquareArea (double L2 )
{
double area = L2 * L2;
return area;
}
Question 2:-
Explanation :-
There were two issues. One was string data type. It should be "String". And, second as there is no function definition we need to set class and methods as abstract.
Program :-
abstract class Student
{
private String name;
public abstract void getStudentMarks ( );
}
abstract class Course_Marks
{
private int course_id;
private String semester;
public abstract void getStudentMarks ( );
}
Code 3 :-
Explanation :-
Many errors :-
1) monthly_statement variable is not defined but used for printing the value. So, I have commented it
2) string variables should be declared by String
3) In printing statement, two elements are merged through "+" symbol not with "," symbol
Code :-
public void accountInfo(String account_title, String
account_number, String account_type)
{
// Generate Monthly Statement
if(account_type == "Current")
{
// do something
}
else if (account_type == "Saving")
{
// do something
}
else
{
//do something
}
//Account information
System.out.println("***** Account Information *****");
System.out.println("Account_Title : " + account_title);
System.out.println("Account Number : " + account_number);
System.out.println("Account_Type : " + account_type);
//System.out.println("Monthly Statement : "+ monthly_statement
);
}