Question

In: Computer Science

Refactor the code below so that it isn't awful! Justify your design decisions! public class BrownBear...

Refactor the code below so that it isn't awful! Justify your design decisions!

public class BrownBear {
  public String roar(){
    return "bear";
  }
}

public class SilentBrownBear {
  public String roar(){
    return "";
  }
}

public class PolarBear {
  public String roar(){
    return "brrrr";
  }
}

public class SilentPolarBear {
  public String roar(){
    return "";
  }
}

Solutions

Expert Solution

Based on the names of the class it looks like these are types of different Bears. Each class's Roar method either return something or return empty string.

You can use abstract class as below

    abstract class Bear

    {

        // Abstract method (does not have a body)

        public abstract string Roar();

    }

    class BrownBear : Bear

    {

        public override string Roar()

        {

            return "bear";

        }

    }

    class SilentBrownBear : Bear

    {

        public override string Roar()

        {

            return "";

        }

    }

    class PolarBear : Bear

    {

        public override string Roar()

        {

            return "brrrr";

        }

    }

    class SilentPolarBear : Bear

    {

        public override string Roar()

        {

            return "";

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Bear b = new BrownBear();

            Console.WriteLine(b.Roar());

            b = new SilentBrownBear();

            Console.WriteLine(b.Roar());

            b = new PolarBear();

            Console.WriteLine(b.Roar());

            b = new SilentPolarBear();

            Console.WriteLine(b.Roar());

            Console.ReadLine();

        }

    }

Here I have Choose abstract class because

1- You can not instantiate the abstract class.

2- Here every class is having Roar method which is either returning something or empty string. So we defined Roar method as abstract method in abstract class and implemented Bear class by all other classes.

3- As Roar method is abstract in Bear class then all the classes which are implementing the Bear class should override the Roar method.

4- Even further you can add static method to the abstract class.

5- The performance of the abstract class is faster.

6- As Roar is common behaviour in all the classes, then abstract class is best suitable to use.

Hope this will help you. I would really appreciate if you give thums up. Thank you :)


Related Solutions

----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
public class Sum2 { // TODO - write your code below this comment. } Download the...
public class Sum2 { // TODO - write your code below this comment. } Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4: Sum: 7
public class FirstChar { // TODO - write your code below this comment. } Download the...
public class FirstChar { // TODO - write your code below this comment. } Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line argument foo is shown below: First...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
Module 2 Programming Assignment – Battleship Refactor Refactor your Battleship drawing code from Module 1. Download...
Module 2 Programming Assignment – Battleship Refactor Refactor your Battleship drawing code from Module 1. Download the OO template for the basic battleship game, BattleshipRefactor.zip (refer below) The template has the outline of a battleship game using OO constructs. You need to add the Grid class and complete the code so that it runs correctly. At a minimum, you need to complete the Draw and DropBomb methods with code similar to the previous assignment. There will be changes due to...
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private String firstName; private String lastName; private int age; private String email; private String phone; private String address;       public Person(String firstName, String lastName, int age, String email, String phone, String address){ setFirstName(firstName); setLastName(lastName); setAge(age); setEmail(email); setPhone(phone); setAddress(address); } public Person(String firstName, String lastName, String email){ setFirstName(firstName); setLastName(lastName); setEmail(email); }    public String getFirstName(){ return firstName; }    public String getLastName(){ return lastName; }...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT