Question

In: Computer Science

public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...

public static int punishOrMercy(char direction, int reward)
Output: int which will be the value of zero or the initial reward.
Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up. This is common practice in Artificial Intelligence (AI) to reward more or penalize less to create incentive to move towards a certain direction, in our case, up.
What does it do (a more detailed explanation): It will first check the sign of the reward value, if it was negative, then it will check if the direction is equal to ‘u’, if that is the case, then it will flip a coin and for zero (tail) it will return zero, and for one (head) it will return the initial reward (the same reward that the function received as an input).
When a coin is generated, this function also prints one of the following messages:
“Coin: tail | Mercy, the negative reward is removed.”
“Coin: head | No mercy, the negative rewarded is applied.”

(in java)

*note (do just complete this specific method as this is just one of the methods apart of this.)

Solutions

Expert Solution

In case of any query do comment. Please rate answer as well. Thanks

You can use below code for your method. You have to import to toss a coin to get either 0 or 1 for tail and head simulation.

import java.util.Random;

Code:

    public static int punishOrMercy(char direction, int reward){
            if (reward < 0 && direction == 'u'){
                //flip a coin here
                Random rnd = new Random();
            int coin = rnd.nextInt(2);
            
            if (coin == 0) {
                //if tail, print the message and return 0
                System.out.println("Coin: tail | Mercy, the negative reward is removed.");
                return 0;
            }
            else{
                //if head then return the same reward
                System.out.println("Coin: head | No mercy, the negative rewarded is applied.");
                return reward;
            }
            } 
            //if your direction is not u or reward is not negative then return the same reward
            return reward;
        }

Output:

Full Sample Code for above output:

import java.util.Random;

public class Reward
{
        public static void main(String[] args) {
                System.out.println(punishOrMercy('u',-1));
                System.out.println(punishOrMercy('u',-1));
                System.out.println(punishOrMercy('u',2));
                System.out.println(punishOrMercy('d',-1));
        }
        
        public static int punishOrMercy(char direction, int reward){
            if (reward < 0 && direction == 'u'){
                //flip a coin here
                Random rnd = new Random();
            int coin = rnd.nextInt(2);
            
            if (coin == 0) {
                //if tail, print the message and return 0
                System.out.println("Coin: tail | Mercy, the negative reward is removed.");
                return 0;
            }
            else{
                //if head then return the same reward
                System.out.println("Coin: head | No mercy, the negative rewarded is applied.");
                return reward;
            }
            } 
            //if your direction is not u or reward is not negative then return the same reward
            return reward;
        }
}

Related Solutions

public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up,...
public static char inputDirection() : Functionality: The user will be asked to input ‘u’ for up, ‘d’ for down, ‘l’ for left or ‘r’ for up. And in case the user enters anything except one of these four letters, it will be kept asked to enter a new char until the entered char matches one of them. (in java) *note (do just complete this specific method as this is just one of the methods apart of this.)
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT