Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write comments on the code Thank you! ...o AT&T; 9:06 PM 53% Use at least

ID: 3674188 • Letter: P

Question

Please write comments on the code Thank you!
...o AT&T; 9:06 PM 53% Use at least one user defined class in your program. Write a program to have the computer generate a digit between 100 and 999. Then the user will try to guess the number. Every time a guess is made, the computer responds with the number of red, yellow, and green digits in the guess A guess digit is green if the guess digit and the corresponding digit are the same. A guess digit is red if it does not correspond to any of the digits. A yellow guess is if it is a correct digit but not in the correct place Suppose the computer's number is 123. Your output may look as follows: Please enter a guess? 422 You have: 2 red 1 green 0 yellow Enter the next guess? 459 e ex You have: 3 red 0 green Oyellow Enter the next guess? 122 You have: 1 red 2 green 0 yellow Enter the next guess? 123 VERIFIED BY: DATE:

Explanation / Answer

Since you did not mentioned about programming language I have did it in java under netbeans.


package guess;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Guess {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Random r=new Random();
  
int red,green,yellow;
int number,Rnumber;
int random;
int upperBound=999;
int lowerBound=100;
int a1[]=new int[3];
int a2[]=new int[3];
int i=10,j=0,temp,factor=1;
  
  
public Guess() throws IOException
{
  
  
}
public void input() throws IOException
{
random=lowerBound + (int)(Math.random() * ((upperBound - lowerBound) + 1));;

do{   
System.out.println("Please GUESS a number between 100 to 999");
number=Integer.parseInt(br.readLine());   
}while(((number < 100)||(number > 999)));

computationOfInput(number);

System.out.println("Given Input = "+number+"and random number is = "+random+" ");
for(i=0;i<3;i++){
System.out.print(" "+a1[i]);
}
System.out.println();
computationOfRandom(random);

for(i=0;i<3;i++){
System.out.print(" "+a2[i]);
}
System.out.println();
match(a1,a2);
System.out.println(green+" greens");
System.out.println(yellow+" yellow");
System.out.println(red+" red");
}
public void computationOfInput(int number)
{
int j=0;
temp=number;
while(temp>0 && j<3){
temp=temp/10;
factor = factor*10;
}
while(factor>1 && j < 3){
factor = factor/10;
a1[j]=(number/factor);
number = number % factor;
j++;
}
  
}
public void computationOfRandom(int random)
{
temp=random;
while(temp>0){
temp=temp/10;
factor = factor*10;
}
while(factor>1){
factor = factor/10;
a2[j]=(random/factor);
random = random % factor;
j++;
}
}
public void match(int a[],int b[])
{
int ii=0;
while(ii<3)
{
for(i=0;i<3;i++){
if(a[ii]==b[i] && ii==i)
green++;

}
  
ii++;
}
ii=0;
while(ii<3)
{
for(i=0;i<3;i++){
  
if(a[ii]==b[i] && ii!=i)
yellow++;
}
  
ii++;
}
ii=0;
while(ii<3)
{
for(i=0;i<3;i++){
if(a[ii]!=b[i] && ii==i)
red++;
}
  
ii++;
}
  
}
  
  
  
public static void main(String[] args) throws IOException {
Guess g=new Guess();
g.input();

  
}
  
}