Java boolean int comparable - java

hi can anyone tell me why this isn't working or point me in the right direction all i am trying to do is take a users input of 3 numbers compare them if they are the same which would be 3 of a kind or whether they are all different which would make it all different or if it is a run of 1 2 3 etc
error code i get is incomparable types boolean and int
public boolean different() {
do {
System.out.println ("All results are different" + "\n" + rollone + "\n" +
rolltwo + "\n" + rollthree);
}
while (rollone != rolltwo && != rollthree) ;
}
any ideas much appreciated in advance

Probably the best way to do this would be a set of if statements. Usage of a do/while loop is a bit confusing in your example code. I would start by breaking up the logic into multiple parts. So for example: Let's say I want to know if a number is even or odd.
What would I normally do if I didn't know how to tell if a number was even or odd just by looking at it? Well, I know that any number that's a multiple of 2 is even. And anything that isn't is odd. How can I translate that into code?
if(x % 2 == 0) { return true; }
This is using the modulo operator. It's essentially, "Given that I have the number x, what remains if I divide it by 2?" If this is 0, x is divisible by 2. Ergo, even.
else { return false; }
If it's not even, it must be odd. Simple stuff.
Now for your problem, you've described a lot of use cases. I would say write down each use case and try to think of the logic in your mind of how -you- would manually do it. (i.e. Someone hands you 3 numbers on pieces of paper. How would determine if they were the same? If they were a run? If they had no relationship?) And then think of how you can apply that logic with if / else statements.
Good Luck.

Related

Understanding if statements

I have done thorough research on If statements in java, but I can't for the life of me understand them.
I get that you're supposed to put in a sort of true or false statement and if it meets the requirements of the if Statement then it does something. I however do not fully understand how to write them. If someone can possibly write an example and explain it that would be much appreciated.
If statements are sometimes hard to understand if they aren't explained well. In hopes to help I'll show you some examples and explain how they work.
First you need to understand that an if statement is not as simple as True or False. It works more as if the given situation in code meets the requirements you set for it between the parenthesis it will run the code inside the brackets of the if statement.
Example 1:
int x = 5;
if(x == 5)
{
System.out.println(" X is equal to 5");
}
Whats happening hear is you are initializing the int x to be 5 and then the if statements runs to see if x is equal to 5. Since it is the code inside the if statement is run and it prints out the sentence. X is equal to 5.
If the the int x was equal to anything but five in this situation the if statement would not run the code inside because it failed to meet the requirements. Similarly you can use different operators such as; does not equal(!=), greater than or less than(<, >), and greater to or equal to and less than or equal to(<=, >=).
If you want to get more complex you can go as far as to add and or or operators in(||, or &&). This allows you to set multiple requirements in one if statement.
Example 2:
int x = 5;
int y = 2;
if(x == 5 || y == 5)
{
System.out.println("I <3 if statements");
}
What is happening in this example is the if statement is checking to see if either y or x is equal to 5. Since one of them is (x) it runs the code inside the if statement printing out the sentence. I <3 if statements.
With the use of the or operate only if neither of the requirements are met will it not operate the code inside. Having both the requirements be met is fine because at least one of them are.
Also when using || you are not limited to only 2 requirements you can go on to make as many as you desire.
Example 3:
int x = 5;
int y = 2;
if(x == 5 && y == 2)
{
System.out.println("Coding is fun");
}
With the and operator, the if statement checks to see if both the requirements are met. In this example since x is equal to 5 and y is equal to 2 the code in the if statement will run printing the text. Coding is fun.
If you look to get more in depth you can also use else if and else statements. How the work is simply if the requirements of the previous if statements were not met than it will either run the code if it is an else statement or check to see if the next set of requirements are met in the else if statement.
Example 4:
int x = 5;
if(x == 1)
{
System.out.println("X is 1");
}
else if(x == 3)
{
System.out.println("X is 3");
}
else
{
System.out.println("X is unknown");
}
What is happening is that the original if statement is checking to see if x is equal to 1. Since it is not the code inside the if statement is not run and it moves on to the else if statement. The else if statement is checking to see if x is equal to 3. Once again since it is not it skips over the code inside the else if statement and moves the the else statement. With the else statement since there is no requirements it runs the code inside no matter what and finally prints out the sentence. X is unknown.
In the event that the requirements in one of the previous statements(if, or else if) is met it will run the code inside the given one and terminate there. In other words it won't run the else regardless, only if everything else fails.
I hope I was able to help with your problem. Enjoy your coding experiences! :)
I'm not sure what you mean by "thorough research on If statements", but the principle is simple.
There's always some kind of expression that might turn out to be true or false. It's written in parentheses. Then there's a bunch of statements that are usually (but not always) written between curly braces. Some naughty developers sometimes omit the braces, if there's only one statement, but it's arguably best not to do this.
The computer starts by working out whether the expression in parentheses is true or false. If it's true, the statements in the braces are run. If it's false, the statements in the braces are ignored.
For example,
if (today.equals("Friday")) {
developers.goHome("early");
managers.stay("late");
}
Here, if today.equals("Friday") turns out to be true, the two statements inside the braces are run. Otherwise, they are not.
Let's say you have a value x that you want to check if it's bigger or smaller than five for example you can do this :
int x ;
if(x>5) { // you're checking if x is bigger than 5
// If true print it's bigger...
System.out.print("It's bigger than five");
}else{ // anything else (if not bigger) print it's not bigger...
System.out.print("It's not bigger than five");
}
So if x was 6 for example output would be
It's bigger than five

I need help in trying to fully understand the concept of recursion [duplicate]

This question already has answers here:
Understanding recursion [closed]
(20 answers)
Closed 5 years ago.
Before you get started, I have used google countless times in hopes of searching for a very brief and simple explanation of how recursion works when it has a return type. But I guess I'm not as bright as I thought since i still cant understand it quite well.
Take the following code snippet (in java) as an example
public static int recursion(int num)
{
int result;
if (num == 1)
result = 1;
else
result = recursion(num - 1) + num;
return result;
}
I grabbed this code from my professors lecture slide and he said this will return 1 + 2 + 3 + ... + num.
I just need someone to explain how the process works in the method that i provided. Maybe a step by step approach might help me understand how recursion works.
recursion(5) = recursion(4) + 5, let's figure out recursion(4) and come back to this later
recursion(4) = recursion(3) + 4, let's figure out recursion(3) and come back to this later
recursion(3) = recursion(2) + 3, ...
recursion(2) = recursion(1) + 2, ...
recursion(1) = 1, we know this!
recursion(2) = 1 + 2, now we can evaluate this
recursion(3) = (1+2) + 3, and now we can evaluate this
recursion(4) = (1+2+3) + 4, ...
recursion(5) = (1+2+3+4) + 5, the answer to our original question
Note: Without knowing recursion(1), we'd have gone to 0, -1, -2, and so on until forever. This known quantity is called the base case and it is a requirement for recursion.
Basically when there is a stack buildup for each item that is created beyond the last iteration. (Where num=1)
When n>1 the if statement kicks the iteration to the else which 'saves' the result in a stack and calls the same funtion again with n-1
what this effectively does is keep calling the same function until you hit your designated 'base case' which is n=1
Recursion is all about solving a problem by breaking it into a smaller problem. In your case, the question is "how do you sum the numbers from 1 to n", and the answer is "sum up all the numbers from 1 to n-1, and then add n to it". You've phrased the problem in terms of a smaller or simpler version of itself. This often involves separating out a "base case"—an irreducibly simple problem with a straightforward answer.
public static int recursion(int num)
{
int result;
if (num == 1)
result = 1; // Base case: the sum of the numbers from 1 to 1 is 1.
else
result =
// This is the sum of numers from 1 to n-1. The function calls itself.
recursion(num - 1)
// Now add the final number in the list, and return your result.
+ num;
return result;
}
You're defining the unsolved problem in terms of itself, which works because the solution always involves either the base case or a simpler version of the problem (which itself further involves either the base case or an even simpler version of the problem).
I'll close with one of my favorite jokes:
How do you explain recursion to a five-year-old?
You explain recursion to a four-year-old, and wait a year.
Going by the classic code example you posted. if you call your method like so with number passed in as 5:
recursion(5);
In layman terms just to understand, your function will create & call another copy of your function in the else block as below:
recursion(4);
and then
recursion(3);
recursion(2);
recursion(1);
as the number keeps decrementing.
Finally it will call the if part in the final copy of the method as num will satisfy num == 1. So from there it starts unwinding & returning each value to the previous call.
As each method call has its own stack to load method local variables on, there will be n number of stacks created for n calls. When the deepest call in recursion is made, then the stacks start unwinding. Hence recursion achieved
The most important thing however to note is that there is a base-most call in your code, which is done at 1 just because you have the check if (num == 1). Else it would be infinite recursion & of course a fatal & wrong program to write. The base-most call is from where its called as stack unwinding in recursion terms.
Example: Finding the factorial of a number is the most classic examples of recursion.
Performance: Do look into recursion vs iteration and recursion vs looping to see what are the performance impacts of recursion

Coding Mastermind in Java

I was wondering how to code the game, Mastermind, in Java, but with things up a notch (I want to inform the user not only how many pegs they got right or wrong, but also how many they guessed correctly in the wrong slots).
For instance, say the RNG answer of 5 digits of numbers 1-6 is:
22354
... and the user's guess is:
32624
Resulting in:
two guessed correctly (2 and 4)
two guessed partially correct (2 and 3)
one guessed incorrectly (6).
Here's my code for informing the user what they got correct:
String answer = "22354";
String guess = "32624";
int correctPegs = 0;
for (int i = 0; i < 5; i++) {
char a = answer.charAt(i);
char g = guess.charAt(i);
if (a == g) {
correctPegs++;
}
}
System.out.println(correctPegs);
How would I find the partially correct ones?
... and for calculating how many the user guessed incorrectly, I was thinking of using basic algebra to find the remaining characters after finding the correct and partially correct ones.
I would put '0' into the strings where they match. Then for every char that is not a zero, I would look through the answer string again. If any character in the answer string matches, I would make them both zero, and increment outOfPlacePegs.
This guards against two corner cases I am assuming you do not want:
One is when you have '323' as the answer and '223' as the input. You don't want the first '2' to be recorded as out of place.
Second is when you have '223' as the answer and '442' as the input. You don't want this recorded as two numbers out of place.

Clarification on Mystery

I have just started taking a Computer Science class online and I am quite new to Programming(a couple of week's worth of experience). I am working on an assignment, but I do not understand what a mystery method is. I have yet to find an answer that I can wrap my head around online, in my textbook, or from my professor. Any explanation using this code as an example would also be greatly appreciated!
This is the equation where I saw it in:
public static void mystery1(int n) {
System.out.print(n + " ");
if (n > 0) {
n = n - 5;
}
if (n < 0) {
n = n + 7;
} else {
n = n * 2;
}
System.out.println(n);
}
If anybody can help, that would be amazing! Thank you!
First of all, I voted your question up because I think it's a valid question for someone who is just beginning in computer programming, and I think that some people fail to understand the significance and purpose of Stack Overflow, which is to help programmers in times of need.
Secondly, I think that the couple of users that have commented on your post are on the right track. I have personally never heard of a mystery method, so I think the goal here is for you to simply figure out what the method does. In this case, the method takes a parameter for int 'n'. This means that if, at any point in the application, the 'mystery1()' method is called, an integer will have to be passed as the variable.
Let's say that a user enters the number '9'. The method would be called by the code mystery1(9). This would then run the first part of the 'if' statement, because n is greater than 0. So, n would be equal to n - 5, or 9 - 5, which is 4. (So, n=4.)
I hope my answer was somewhat helpful to you. Take care.
Your assignment is probably to figure out what this method does. More specifically, what does it print to the screen. I'll walk you through how to figure this out.
You have a function, also called a methood, called mystery1. A function is just a named block of code that you can use throughout other pieces of code. This function takes an integer argument called n. Let's assume n=12 for this example.
The first thing that happens in your function when it is called is that n is printed out via the System.out.print method. Notice that it prints a blank space after it. Notice also at the end it prints another value of n that gets assigned within the method. So the method is going to print "12 ?" without the double quotes. The question mark is what we have to figure out. The code says if n > 0 then n = n-5. Since 12 is greater than 0, n gets the new value of 7. The next if statement says if n is less than 0, n gets assigned n+7. But it is not less than zero, it is 7 at this point, so we move to the else statement. In this statement n gets multiplied by 2 which is 14. So the last statement prints 14.
So for an input value of 12 this method prints:
12 14
I hope this helps. If not, please give more detail about your assignment and what you don't understand about my explanation.
The point of this kind of exercise is that you are given a method, but they don't tell you what it does (hence the "mystery"). You are supposed to figure out what it does on your own (like "solving the mystery"). It doesn't mean that the method is special in any way.
Say I give you a "mystery" method like this:
public static void mystery(int n) {
System.out.println(n+1);
}
You would "solve the mystery" by telling me that this method prints out the number that comes after n. Nothing else is special here.
In the example you gave, your job would be to tell me why the method prints out 0 0 when n = 0, or 6 2 when n = 6.
I think the usage of the term "mystery method" is rather misleading, as it has clearly made you (and many, many, many others) believe that something about these methods is special and something that you need to learn about. There isn't anything special about them, and there's nothing to learn.
I think a lot of people would understand this better if instructors just said "tell me what this method does" instead of trying treat students like 5 year olds by saying "Here's a mystery method (ooh, fancy and entertaining). Can you play detective and solve the mystery for me?"

Game logic issue

i am making a noughts and crosses game (tic tac toe) and in my logic class i represent the state of the game with a 2d array, but this is the problem, im checking the array like so
if(gameModel[0][0] == gameModel[1][1] && gameModel[0][0] == gameModel[2][2]){
return true;
}
if(gameModel[2][0] == gameModel[1][1] && gameModel[2][0] == gameModel[0][2]){
return true;
}
and so on for all 8 conditions, however, the array is initialised with all values of 0 at the beginning, so it always finds three matching values, how can i get round this problem without having to change the whole of my code
thanks
In that case you just have to add a check if a value is set:
if ( gameModel[0][0] == gameModel[1][1]
&& gameModel[0][0] == gameModel[2][2]
&& gameModel[0][0] != 0) {
return true;
}
One thing that jumps out at me with this is... why are you using ints instead of a class to represent this? True this is a simple game, but a Piece class seems to jump out as a fairly obvious class to have.
Also, with int you really have 3 states, presumably something like:
0 = empty
1 = X
2 = Y
So you should check for 0 (empty) before bothering to check for if they are the same value, it will be quicker (who really cares though, this doesn't need to be fast), and make more logical sense (is the square empty? if so then do not bother checking if the squares hold the same values).
Even for simple things like this, especially when you are just starting out, try to embrace OOP, it is a different way of thinking, and it takes practice, so practice as much as you can!

Categories

Resources