Reputation: 117
public class FavNum {
public static void isEven(int x){
boolean b;
if (x%2==0) {
b=true;
}else{
b=false;
}
}
public static void isSingle(int x){
boolean b;
if (x > 0 && x < 10){
b=true;
}else{
b=false;
JOptionPane.showMessageDialog(null, "Your favorite number is not a single digit!");
}
}
public static void all(int x){
isEven (x);
//What should I add here to use isEven and isSingle in conditionals?
//For example, I want to say something like if isEven and isSingle are true, then say this.
//Or if isEven is true and isSingle is not, say this. But I don't know how to properly write those conditionals.
}
public static void main(String[] args) {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?"));
}
}
The directions for this are as follows "
So far, I have done the first 4 bullets. I am having trouble (and don't think I completely understand) the 5th bullet. I tried to do it by creating a new method called all where I planned to call isSingle and isEven and use if else statements to compare them and return messages accordingly. But I'm stuck and have left comments in my above code that explain my issues.
Can someone help me with this assignment please? Or at least point me in the right direction?
Thank you!
Upvotes: 0
Views: 2696
Reputation: 311
public class Joption {
// declared outside so easier to be accessed by other methods in the class
private static int x;
// is the number passed into this method even?
private static boolean isEven(int x) {
if (x % 2 == 0) {
return true;
}
return false;
}
// is the number passed into this method single?
private static boolean isSingle(int x) {
if (x >= 0 || x < 10) {
return true;
}
return false;
}
// is the number passed in even AND single?
// notice how the number passed in flows into the two methods you just created. Their returns help you determine if they are even AND single.
private static void isEvenAndSingle(int x) {
if (isEven(x) && isSingle(x)) {
JOptionPane.showMessageDialog(null, x + "is Even and Single!");
}
}
// same as above method but now it's checking to see if it's even and NOT single.
private static void isEvenAndNotSingle(int x) {
if (isEven(x) && !(isSingle(x))) {
JOptionPane.showMessageDialog(null, x + "is Even but NOT single!");
}
}
// your main running method
// here you actually need to call the isEvenAndSingle() and isEvenAndNotSingle() methods with the retrieved x value from the JOptionPane so that you can actually print the appropriate messages in the responding JOptionPane
public static void main(String[] args) {
x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?"));
isEvenAndSingle(x);
isEvenAndNotSingle(x);
}
}
Your problem, as stated earlier, is in the way you retrieve the value from your JOptionPane. You get it as an int. But you pass it into your methods as a boolean. What you actually need to do is pass in the values as an int (see isEven() and isSingle() method) and have them return a boolean.
Then you are asked to create a comparison with the two methods you just created. One for if the number is even and single and one for if the number is even and NOT single.
I've annotated the code with comments to help you understand better! Good luck with your future java coding! :)
Upvotes: 1
Reputation: 134
Looks like you have the algorithm section figured out From you methods, you need to return boolean instead of void
public static boolean isEven(int x){
return (x % 2) == 0;
}
Upvotes: 0
Reputation: 59
You are comparing booleans with numbers:
boolean x;
if (x%2==0) { //Error here
b=true;
}
if (x > 0 && x < 10){ //Error here
b=true;
you cannot do that.
If x is true
or false
you cannot check if it is bigger than 0
or do arithmetic operations with it.
Upvotes: 2