8 Replies - 385 Views - Last Post: 02 August 2012 - 09:08 AM Rate Topic: -----

#1 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:22 AM

I need logic to check if (for example: 2 * 2 = 4 is true or false.). The two values are random values between 1 and 4.

import java.util.Random;

public class ComputerTuts {

	public static void main(String[] args) {

		Random random = new Random();
		int num = 1 + random.nextInt(4);
		int num1 = 1 + random.nextInt(4);
		
		int result = 0;
		boolean correct = true;
		
		System.out.printf("How much is %d times %d", num, num1);
		result = num * num1;
		System.out.println("Result: " + result);
		result = correct;
		
		//Error: Type mismatch: cannot convert from int to boolean

		if(result = correct){
			System.out.println("Very Good.");
		}
		else
			System.out.println("No! Please Try Again");
		
		
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Logic to check if value of 2 multiple is true or false?

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:25 AM

use the == operator to compare two values and return a boolean result. For example,

int i = 5;
if (i * i == 25)
  System.out.println("double equals returns a boolean");
else
  System.out.println("oh no! math is broken!");

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:29 AM

You'll need a Scanner to read the answer from the user
Then you can just compare result with the value read. You do not need a boolean correct
Was This Post Helpful? 0
  • +
  • -

#4 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:34 AM

I'm not asking the user... randomly generating 2 values.

Is this logic correct?

if(result == num * num1){
			System.out.println("Very Good.");
		}
		else
			System.out.println("No! Please Try Again");
		
		
	}



Output:
How much is 3 times 3
Result: 9
Very Good.


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:39 AM

Looks good to me.

Of course, you shouldn't trust me, I make mistakes all the time. What would you do to test it?
Was This Post Helpful? 0
  • +
  • -

#6 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:43 AM

Haha, you're being humble. But yea, i have no idea how to test this ...but i tried

if(result != num * num1){


I got the else statement printed out.

How much is 4 times 1
Result: 4
No! Please Try Again


Doesn't look right to me.

This post has been edited by rjohn5854: 02 August 2012 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 07:56 AM

Anyone?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 08:36 AM

Not being humble - I really do make mistakes, ask anyone. In this case I'm pretty sure you're good, but it's a good chance to think about testing a function.

The first question is, what sorts of test cases would it take to convince you that this operator returns what you want? You might want to think about classes of values - where might this fail?

Generally, we'd like to test positive and negative and zero values, we'd like to test extremes, and if there are "edge cases" and "corner cases" we want to test those as well. We want to make sure we test both true and false cases - ie, both equal and unequal quantities. Integer equality, frankly, doesn't make a good example, because there aren't really a whole lot of ways to break it, but you can certainly come up with a list of values that will convince you that == does return what you want it to return.
You can and should use this sort of logic any time you're asking "does this do what I want it to do?". Not that you shouldn't ask here as well, but if you've done a little exploration, you can answer a lot of questions for yourself.

You should also think about this sort of logic as a way to ensure your programs work correctly. For this, google "test-driven development" and "junit" and do some reading.

Quote

How much is 4 times 1
Result: 4
No! Please Try Again



Doesn't look right to me.


Since you were asking for the negative:


if(result != num * num1){


This is actually correct, in that it's doing what you told it to do. In this case, it's not the case that 4 * 1 is not-equal-to 4. That is, 4 does in fact equal 4 * 1. So it correctly responds as if the test came back false, which it did.

This post has been edited by jon.kiparsky: 02 August 2012 - 08:39 AM

Was This Post Helpful? 1
  • +
  • -

#9 rjohn5854  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 172
  • Joined: 04-October 11

Re: Logic to check if value of 2 multiple is true or false?

Posted 02 August 2012 - 09:08 AM

Thank You Jon :online2long:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1