please solve this question It is a discrete math problem Not
please solve this question. It is a discrete math problem. Not computer science.
Prove that it is possible to write a program P which: takes as input M, a java program runs forever, and prints out strings to the console for every x, if M(x) halts, then P(M) eventually prints out x for every x, if M(x) does NOT halt, then P(M) never prints out xSolution
import java.util.*;
class Print
{
String st;
public static void main(String ss[])
{
Scanner sc = new Scanner(System.in);
Print pp = new Print();
char ch;
System.out.println(\"Enter a string: \");
pp.st = sc.nextLine();
System.out.println(\"Enter a Character: \");
//Accept a character
ch = sc.nextLine().charAt(0);
//Calls the M method to check character halts
pp.M(ch);
}
void M(char cha)
{
int c;
System.out.println(\"Result: \ \");
//Loops till the length of the string
for(c = 0; c < st.length(); c++)
{
//If the character is available calls the P() method
if(st.charAt(c) == cha)
P(cha);
}
}
//Display the character if it is available
void P(char cc)
{
System.out.print(\" \" + cc);
}
}
Output:
Enter a string:
This is a demo
Enter a Character:
s
Result:
s s
