Question

In: Computer Science

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

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.

Solutions

Expert Solution

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);
  
  
}
  
}


Related Solutions

In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts...
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
Using the object-oriented programming principles, write a currency exchange converter in Java. You should also use...
Using the object-oriented programming principles, write a currency exchange converter in Java. You should also use 3 different classes it could be currency1 and currency2 and currencytest, and set() and get() method and tostring() method. A person will be presented with 3 currencies (i.e., USD, AED, MYR) that they can convert to/from AED Dirham. The program will allow the user to input the amount of Dirham or other currency to be converted. The Money changer will take AED50 commission if...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
Why is it more feasible to use Objects and object oriented programming as compared to using...
Why is it more feasible to use Objects and object oriented programming as compared to using method based programs? What are the disadvantages of using only methods in your programs.
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop...
Using the various Object Oriented Programming concepts and skills learnt in this course, design and develop a Java Application to compute an individual student’s GPA and store the records in a database. The application should have two components i.e. The student and the course components. The following should be the minimal operations on the course component: – Set course information – Print course information – Show credit hours – Show course number The following should be the minimal operations on...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
Provide a screenshot of your script code and a screenshot of running the scripts with test...
Provide a screenshot of your script code and a screenshot of running the scripts with test inputs. Write a script to check command arguments (3 arguments maximum). Display the argument one by one. If there is no argument provided, remind users about the mistake.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT