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

Solve the following programming problems using object-oriented programming in Ja

ID: 3850792 • Letter: S

Question

Solve the following programming problems using object-oriented programming in Java. Provide the problem code and screenshot of the test run.

COIN TOSSING: Write a program that simulates the toss of a coin. Provide a menu of two options: toss and quit. Count the number of times each side of the coin appears and display the results after each toss. The program should have a method called flip() that is called, takes no arguments and returns a Coin enum value (HEADS or TAILS).

GUESS THE NUMBER: Write a program that plays a game of guess the number. Generate a random integer between 1 and 500. When the player inputs a number, evaluate if the number is too low or too high and let the user know. When the user guesses the number correctly, display how many tries it took for the user to correctly guess the number.

Create a method that receives the guess number as a parameter and returns true if the user guessed the number correctly and false if the user hasn’t guessed the number. The evaluation of high or low should occur in the method.

You can pass the value of the randomly generated number to the method or declare it as static before the main method.

Explanation / Answer

COIN TOSSING: Write a program that simulates the toss of a coin. Provide a menu of two options: toss and quit. Count the number of times each side of the coin appears and display the results after each toss. The program should have a method called flip() that is called, takes no arguments and returns a Coin enum value (HEADS or TAILS).

import java.io.*;
import java.util.Random;
public class tosscoinpgm
{
public static void main (String args[])
{
  
try
{   
int choice = 1,tails = 0,heads = 0;
   boolean frntvalue;
  
//user input for different choice
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
//creating object of class so that we can access flip method
   tosscoinpgm obj = new tosscoinpgm();
do
{
   System.out.println(" Enter your choice");
   System.out.println(" 1 Toss Coin ");
System.out.println(" 2 Show Results");
   choice = Integer.parseInt(b.readLine());//reading choice of user using readline() method

   if (choice == 1)
   {
   frntvalue = obj.flip();//flip method return boolean value true or false

   if (frntvalue == true)//if condition check fro true or false

       {
       heads++;
       }
       else if (frntvalue == false)
       {
       tails++;
       }
   }
else if (choice == 2)
       {
       System.out.println(" No.Of Heads = "+heads);
       System.out.println(" No.Of Tails = "+tails);
       }
   }
while (choice != 2);

   }
catch (Exception e)
{
System.out.println("Error"+e.getMessage());
}


}
  

boolean flip ( )
{
   int front;
  
   Random randnum = new Random();

   front = 1 + randnum.nextInt( 2 );

   if (front == 1)
   {
   return true;
   }
   else if (front == 2)
   {
   return false;
   }
return true;
  
}   
}

GUESS THE NUMBER: Write a program that plays a game of guess the number. Generate a random integer between 1 and 500. When the player inputs a number, evaluate if the number is too low or too high and let the user know. When the user guesses the number correctly, display how many tries it took for the user to correctly guess the number.

Create a method that receives the guess number as a parameter and returns true if the user guessed the number correctly and false if the user hasn’t guessed the number. The evaluation of high or low should occur in the method.

You can pass the value of the randomly generated number to the method or declare it as static before the main method.

import java.util.Random;
import java.util.Scanner;

public class guessgame
{
public static void main (String args[])
{
Random randnum = new Random();
int guesno=randnum.nextInt(500);//generate random guess no
int num_try=0;

Scanner s=new Scanner(System.in);//read user value
int guess;
boolean win=false;
while(win== false)
{
System.out.println("Guess no between 1 to 500");
guess=s.nextInt();
num_try++;
  
if(guess==guesno)
{
win=true;
}
else if(guess<guesno)
{
System.out.println("guess is too low");
}
else if(guess>guesno)
{
System.out.println("guess is too high");
}
}
System.out.println("winner!!");
System.out.println("No was :"+guesno);
System.out.println("trial of yours :"+num_try);
  
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote