Created
April 18, 2023 07:30
-
-
Save L8RFN/81d766e3ab9f1f3e072d56d334f4fa31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.util.Scanner; | |
//Question One (Random): | |
//1. Write a method calculateRightAnswer that takes two integers and returns their summation. | |
//2. Write a method checkAnswer that takes three integers. The method should check whether | |
//the summation of the first two integers equals the third. If so, the method should return | |
//(true), otherwise, the method should return (false). | |
// Hint: use the calculateRightAnswer method. | |
//3. Implement the main() method as follows: | |
//a. Generate two integer values randomly in the range [1-10]. | |
//b. Input the guessed user answer using Input Dialog: | |
// Message: "What is the result of "+ r1 + " + "+ r2+"???!!!! " | |
// Title: "Guess??!!!" | |
// Icon: QUESTION_MESSAGE | |
//c. Check whether the sum of the two generated numbers equals the number | |
//entered by the user. | |
// Hint: Call the checkAnswer method. | |
// If so, display a Message Dialog | |
// Message: "WOW!!! Right!!!" | |
// Title: "Result" | |
// Icon: "INFORMATION_MESSAGE" | |
// Otherwise, display a Message Dialog | |
// Message: "Sorry!!! Wrong" | |
// Title: "Result" | |
// Icon: "ERROR_MESSAGE" | |
public class Main { | |
public static void main(String[] args) { | |
int random1 = (int) (Math.random()*(10-1+1) + 1); | |
int random2 = (int) (Math.random()*(10-1+1) + 1); | |
int answer =Integer.parseInt(JOptionPane.showInputDialog(null,"What is the result of "+ random2+ " + " +random1+ " ????!!!","Guess??!!", JOptionPane.QUESTION_MESSAGE)); | |
if (checkAnswer(random1,random2,answer)) | |
JOptionPane.showMessageDialog(null,"WOW!!!Right!!!","Result",JOptionPane.INFORMATION_MESSAGE); | |
else | |
JOptionPane.showMessageDialog(null,"Sorry!!!Wrong","Result",JOptionPane.ERROR_MESSAGE); | |
} | |
public static int calculateRightAnswer (int x, int y) | |
{ | |
return x+y; | |
} | |
public static boolean checkAnswer(int x , int y, int z) | |
{ | |
return calculateRightAnswer(x, y) == z;//nice way to simplify if statement. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment