Question

How should I refactor these code? We're supposed to refactor 5 things. Here's the conditon: A...

How should I refactor these code? We're supposed to refactor 5 things.

Here's the conditon:

A minimum of 5 refactoring operations must be performed. The refactoring operations must preserve the program’s functionality. For each refactoring operation, you must insert comments to -Give the refactoring operation a number, -describe what refactoring operation was performed, -give the rationale for your choice of the refactoring operation you have applied, and -put the original code (i.e. before refactoring) inside the comments

public class StringOperations {

   static final int N = 10000;

   public void usingPlus() {
       String s1 = "";
       for (int i = 1; i <= N; i++) {
           s1 = s1 + "*";
       }
   }

   public void usingStringBuffer() {
       StringBuffer sb = new StringBuffer();
       for (int i = 1; i <= N; i++) {
           sb.append("*");
       }
       String s2 = sb.toString();
   }

}

************************************************************************

public class CodeMotion {

    static final int ARLEN = 50000;
    static final double[] x = new double[ARLEN];
    static final double y = (new Random()).nextDouble();

    public void codeInsideLoop() {
        for (int i = 0; i < x.length; i++) {
            x[i] *= Math.PI * Math.cos(y);
        }
    }

    public void codeOutsideLoop() {
        double picosy = Math.PI * Math.cos(y);
        for (int i = 0; i < x.length; i++) {
            x[i] *= picosy;
        }
    }
}

************************************************************************

public class ArrayListvsLinkedList {

    private ArrayList<Integer> al = new ArrayList<Integer>();
    private LinkedList<Integer> ll = new LinkedList<Integer>();
    private static final int N = 100000; // 1000000

    public void arrayListCreation() {
        for (int i = 1; i <= N; i++) {
            al.add(0, new Integer(i));
        }
    }

    public void linkedListCreation() {
        for (int i = 1; i <= N; i++) {
            ll.add(0, new Integer(i));
        }
    }

    public void arrayListAccess() {
        Object o;
        for (int i = 0; i < N; i++) {
            o = al.get(i);
        }
    }

    public void linkedListAccess() {
        Object o;
        for (int i = 0; i < N; i++) {
            o = ll.get(i);
        }
    }
}

************************************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package mar21;

public class StringOperations {

   static final int N = 10000;

   public void usingPlus() {
   String s1 = "";
     
   /*for (int i = 1; i <= N; i++) {
   s1 = s1 + "*";
   }*/
   //Refactoring -1 : instead of starting from 1, I am starting from 0-11 so that it will behave same as original
   for (int i = 0; i < N; i++) {
       //Refactoring -2 : instead of using arithmetic,I am using combined arithmetic
       s1+="*";
   }
   }

   public void usingStringBuffer() {
   StringBuffer sb = new StringBuffer();
/*   for (int i = 1; i <= N; i++) {
   sb.append("*");
   }*/
     
   //Refactoring -3 : instead of starting from 1, I am starting from 0-11 so that it will behave same as original
   for (int i = 0; i < N; i++) {
   sb.append("*");
   }
/*   String s2 = sb.toString();*/  
   //Refactoring -4: creating string objecr using consructor
   String s2 = new String(sb);
   }

   }


   public class CodeMotion {

   static final int ARLEN = 50000;
   static final double[] x = new double[ARLEN];
   static final double y = (new Random()).nextDouble();

   public void codeInsideLoop() {
   for (int i = 0; i < x.length; i++) {
   x[i] *= Math.PI * Math.cos(y);
   }
   }

   public void codeOutsideLoop() {
   double picosy = Math.PI * Math.cos(y);
   for (int i = 0; i < x.length; i++) {
   x[i] *= picosy;
   }
   }
   }

   ************************************************************************

   public class ArrayListvsLinkedList {

   private ArrayList<Integer> al = new ArrayList<Integer>();
   private LinkedList<Integer> ll = new LinkedList<Integer>();
   private static final int N = 100000; // 1000000

   public void arrayListCreation() {
   for (int i = 1; i <= N; i++) {
   al.add(0, new Integer(i));
   }
   }

   public void linkedListCreation() {
   /*for (int i = 1; i <= N; i++) {
   ll.add(0, new Integer(i));
   }*/
  
       //Refactoring -5 : instead of starting from 1, I am starting from 0-11 so that it will behave same as original and adding i+1
       for (int i = 0; i < N; i++) {
   ll.add(0, new Integer(i+1));
   }

   }

   public void arrayListAccess() {
   Object o;
   for (int i = 0; i < N; i++) {
   o = al.get(i);
   }
   }

   public void linkedListAccess() {
   Object o;
   for (int i = 0; i < N; i++) {
   o = ll.get(i);
   }
   }
   }

Add a comment
Know the answer?
Add Answer to:
How should I refactor these code? We're supposed to refactor 5 things. Here's the conditon: A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds) // TODO (optional) refactor to DRY // TODO...

  • Please help complete the items marked TODO in the code and get the tests to pass:...

    Please help complete the items marked TODO in the code and get the tests to pass: Someone already answered it, but it was incorrect! import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestPerformance { // TODO run test and record running times for SIZE = 10, 100, 1000, 10000, ... // (choose in conjunction with REPS below up to an upper limit where the clock // running time is in the tens of seconds)...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • 1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer...

    1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer >(); ArrayList b = a; a.add(new Integer(4)); b.add(new Integer(5)); a.add(new Integer(6)); a.add(new Integer(7)); System.out.println(b.size()); A)1 B)2 C)3 D)4 E)5 2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code:     Person p1, p2, p3;     int m1, m2, m3;     p1 = new Person();     m1 = p1.getMoney();     // assignment 1...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and...

    Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and "import edu.princeton.cs.algs4.StdOut"imports: /* import java.util.Queue; import java.util.LinkedList; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdOut; public class BottomUpMergeSort { public static void main(String[] args) { int N=Integer.parseInt(args[0]); Double [] a = new Double [N]; for(int i=0;i<N;i++) {    a[i]=StdRandom.uniform();    } StdOut.println("Unsorted"); printArr(a); Queue que=sort(a); StdOut.println("\nSorted"); BottomUpMergeSort.printQue(que); } public static Queue sort(Double[] a) {    int N=a.length;       Queue->ques=new LinkedList>();       for(int i=0;i<N;i++) {       ...

  • I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String...

    I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank;    public Student() { this.name = this.major = this.status = ""; this.rank = 0; }    public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; }    public int...

  • I have a below codes with all details and need to answer this below question ONLY!...

    I have a below codes with all details and need to answer this below question ONLY! How to explain below codes with the two properties of a Greedy algorithm - Optimal Substructure and the Greedy Property line by line? ====================== public class TeamFormation { private static ArrayList buildTeam(ArrayList team, int skill) { ArrayList newTeam = new ArrayList(); newTeam.add(skill); for (int player : team) { if (skill == (player + 1)) { newTeam.add(player); } } return newTeam; } private static boolean...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT