Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 769931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:25:01+00:00 2026-05-14T18:25:01+00:00

If anyone familiar with Rebecca Wirfs-Brock, she has a piece of Java code found

  • 0

If anyone familiar with Rebecca Wirfs-Brock, she has a piece of Java code found in her book titled, Object Design: Roles, Responsibilities, and Collaborations.

Here is the quote >Applying Double Dispatch to a Specific Problem
To implement the game Rock, Paper, Scissors we need to write code that determines whether
one object “beats” another. The game has nine possible outcomes based on the three kinds of
objects. The number of interactions is the cross product of the kinds of objects. Case or switch statements are often governed by the type of data that is being operated on. The
object-oriented language equivalent is to base its actions on the class of some other object. In Java,
it looks like this
Here is the piece of Java code on page 16 ‘

  import java.util.*;     
  import java.lang.*;   

 public class Rock
{
public static void main(String args[])
{

}

public static boolean beats(GameObject object)
{
    if (object.getClass.getName().equals("Rock"))
    {
        result = false;
    }
    else if (object.getClass.getName().equals("Paper"))
    {
        result = false;         
    }
    else if(object.getClass.getName().equals("Scissors"))
    {
            result = true;
    }
    return result;
}

}’

===>This is not a very good solution. First, the receiver needs to know too much about the argument.
Second, there is one of these nested conditional statements in each of the three classes. If new
kinds of objects could be added to the game, each of the three classes would have to be modified.
Can anyone share with me how to get this “less than optimal” piece of code to work in order to see it ‘working’. She proceeds to demonstrate a better way, but I will spare you.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:25:01+00:00Added an answer on May 14, 2026 at 6:25 pm

    So here’s how I fixed it. I first made a new interface called GameObject since they refer to it!

     public interface GameObject
     {
     public boolean beats(GameObject g);
     }
    

    The type didn’t exist so referring to it wasn’t going to work so great.

    Here’s my new code for Rock with comments on changes:

     import java.util.*;
     import java.lang.*;
    
     public class Rock implements GameObject //Need to be an instance of GameObject somehow!
     {
     public static void main(String args[])
         {
    
         }
    
     public boolean beats(GameObject object) //This isn't static anymore
     {
     boolean result = false; //Need to declare and initialize result
     if (((Object)object).getClass().getName().equals("Rock")) //getClass should have ()
         { 
         result = false;
         }
     else if (object.getClass().getName().equals("Paper")) //getClass should have ()
         {
         result = false;
         }
     else if(object.getClass().getName().equals("Scissors")) //getClass should have ()
         {
         result = true;
         }
    return result;
     }
     }
    

    EDIT: You seemed to be asking for how to fix the code and not the better way to do it. I believe this should be good to go for you now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure anyone familiar with Axis2 has seen this page a gazillion times: http://axis.apache.org/axis2/java/core/docs/http-transport.html#timeout_config
Is anyone familiar with a Java library that helps with parsing the fields (date,
Is anyone familiar with a tool that generates code stubs with meaningful names from
Is anyone familiar with Native Code in OS X Safari (Version 3 and WebKit)?
Q1) Does anyone familiar with the Java Disruptor pattern know the size of messages
Anyone familiar with a way how can I can i write something to the
For anyone familiar with lambdaj (not I) you will have seen this stacktrace, or
I'm wondering if anyone is familiar with designing UIs to be scalable to any
I am not familiar with using event handlers, and I was wondering if anyone
Anyone familiar with the differences in starting with Webstart(javaws.exe) compared to starting the app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.