Difference between revisions of "Lie Detector"

From CompSciWiki
Jump to: navigation, search
(wrote problem)
 
(Solution added)
Line 18: Line 18:
 
*printing out the result.
 
*printing out the result.
  
HAVEN'T FIXED ANYTHING BELOW THIS YET
+
The first step is to read in the input. You are going to need to use an input dialog box.
 
+
<pre>String statement = JOptionPane.showInputDialog(null, "Enter a statement");</pre>
The first step is to read in the input. For each of the two inputs, you are going to need to use an input dialog box.
+
<pre>
+
String input;
+
 
+
// Get input 1
+
input = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle");
+
</pre>
+
  
 
Don't forget your import statement.
 
Don't forget your import statement.
 
<pre>import javax.swing.*; //needed for JOptionPane</pre>
 
<pre>import javax.swing.*; //needed for JOptionPane</pre>
  
The input you receive is going to be a string. You will need to convert that input into a double before using it.
+
The next step is to calculate the truthiness of the statement using the random method. The method is part of the Math class, so make sure you import it.
<pre>// Convert string to double
+
<pre>import java.lang.Math;//needed for Random</pre>
sideA = Double.parseDouble(input);</pre>
+
Math.random() will return a decimal value between 0 and 1. By comparing the value to a constant value, we can calculate the truthiness.
 +
<pre>boolean truthiness = Math.random() < 0.5;</pre>
  
Do this for both sets of input. There is a reason to use doubles instead of integers. Using two integer inputs, the output can result in a decimal value. Your output shouldn't be more precise than your inputs, so you should allow decimal values for input as well.
+
I am using 0.5 so that ~50% of statements will be true. If you are testing the program on someone who is more or less trustworthy, you may want to adjust the value accordingly.
  
The next step is calculating the length of the hypotenuse. Recall, the formula is <math>a^2 + b^2 = c^2</math>. The user has supplied <math>a</math> and <math>b</math>, so the first thing you need to do is calculate <math>a^2</math> and <math>b^2</math>. Use Java's built-in method for calculating powers. The method is part of the Math class, so make sure you import it.
+
With the truthiness calculated, all that is left is to output the result. This should be done using System.out
<pre>import java.lang.Math;//needed for pow, sqrt</pre>
+
<pre>System.out.println("The Statement " + quote + statement + quote + " is " + truthiness);</pre>
Once the Math class is imported, calculating <math>a^2</math> is as simple as calling Math.pow with an exponent of 2
+
<pre>//calculate A^2
+
double sideA2 = Math.pow(sideA, 2);</pre>
+
  
Once you have calculated <math>a^2</math> and <math>b^2</math>, <math>c^2</math> can be calculated by adding the two values together
+
You should now have a program that divines truth from statements. For the entire code solution, see below.
<pre>//calculate C^2
+
double sideC2 = sideA2 + sideB2;</pre>
+
 
+
Now that you have <math>c^2</math>, you just need to take the square root of that value, then you will have your solution. Luckily, the Java Math class has a built-in square root function.
+
<pre>//calculate C
+
double sideC = Math.sqrt(sideC2);</pre>
+
 
+
With the length of the hypotenuse calculated, all that is left is to output the result. This should be done using System.out
+
<pre>//output solution
+
System.out.println("The length of the hypotenuse is: " + sideC);</pre>
+
 
+
You should now have a program that calculates the hypotenuse of a right-angled triangle. For the entire code solution, see below.
+
 
|SolutionCode=
 
|SolutionCode=
/* Class Pythagoras
+
/* Class Lie Detector
 
  * Created by Brendan Curran-Johnson
 
  * Created by Brendan Curran-Johnson
 
  * Made in COMP3040
 
  * Made in COMP3040
  * Takes the two sides of a triangle and outputs the hypotenuse
+
  * A lie detector
 
  */
 
  */
  
 
import javax.swing.*; //needed for JOptionPane
 
import javax.swing.*; //needed for JOptionPane
import java.lang.Math;//needed for pow, sqrt
+
import java.lang.Math;//needed for Random
  
public class Pythagoras
+
public class LieDetector
 
{
 
{
         public static void main (String args[])  
+
         public static void main (String args[])
{
+
        {
// These variables are used for input
+
                // Get the statement
String input;
+
                String statement = JOptionPane.showInputDialog(null, "Enter a statement");
double sideA;
+
double sideB;
+
+
// Get input 1
+
input = JOptionPane.showInputDialog(null, "Please enter the first side of the triangle");
+
  
// Convert string to double
 
sideA = Double.parseDouble(input);
 
  
// Get input 2
 
input = JOptionPane.showInputDialog(null, "Please enter the second side of the triangle");
 
  
// Convert string to double
+
                //calculate truthiness
sideB = Double.parseDouble(input);
+
                boolean truthiness = Math.random() < 0.5;
  
//calculate A^2
+
                char quote = '"';
double sideA2 = Math.pow(sideA, 2);
+
  
//calculate B^2
+
                //output solution
double sideB2 = Math.pow(sideB, 2);
+
                System.out.println("The Statement " + quote + statement + quote + " is " + truthiness);
 +
        }
 +
}
  
//calculate C^2
 
double sideC2 = sideA2 + sideB2;
 
 
//calculate C
 
double sideC = Math.sqrt(sideC2);
 
 
//output solution
 
System.out.println("The length of the hypotenuse is: " + sideC);
 
System.out.println("End of program.");
 
}
 
}
 
  
  
  
 
}}
 
}}

Revision as of 14:29, 9 April 2010

Back to the Program-A-Day homepage

Problem

Create a program that will allow the user to enter in a statement, and output whether the statement is true or false. The statement will be inputted using JOptionPane.showInputDialog. The program will evaluate the truthiness of the statement randomly (just like a real lie detector). The result should be given using System.out.println.

To solve this problem, you will need to understand:

 

Primitive Data Types

Wiki chars03.jpg

Solution

There are three distinct steps in this program:

  • reading in input from the user
  • calculating the truthiness
  • printing out the result.

The first step is to read in the input. You are going to need to use an input dialog box.

String statement = JOptionPane.showInputDialog(null, "Enter a statement");

Don't forget your import statement.

import javax.swing.*; //needed for JOptionPane

The next step is to calculate the truthiness of the statement using the random method. The method is part of the Math class, so make sure you import it.

import java.lang.Math;//needed for Random

Math.random() will return a decimal value between 0 and 1. By comparing the value to a constant value, we can calculate the truthiness.

boolean truthiness = Math.random() < 0.5;

I am using 0.5 so that ~50% of statements will be true. If you are testing the program on someone who is more or less trustworthy, you may want to adjust the value accordingly.

With the truthiness calculated, all that is left is to output the result. This should be done using System.out

System.out.println("The Statement " + quote + statement + quote + " is " + truthiness);

You should now have a program that divines truth from statements. For the entire code solution, see below.

Code

Solution Code

Back to the Program-A-Day homepage