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) {           }
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.
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 " +...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT