Hey, so I'll post what I have, the code is pretty self explanatory. I have a hunch that there are more efficient ways to code than using these long and repetitive if statements, so if you guys could help me in that direction that would be great.

I also am getting compile errors with my >= signs, saying there is no symbol for boolean. I was wondering if you guys could help me with what a boolean is and how it is used in Java as well.

thank you.

import javax.swing.JOptionPane;


public class HomeworkThree
{
	public static void main(String[] args)
	{



		JOptionPane.showMessageDialog(null, "Hello, my name is David");
		String junk = JOptionPane.showInputDialog(null, "What is your age?");
		int age = Integer.parseInt(junk);

	if (age >= 65)
	{
		  JOptionPane.showMessageDialog(null, "Time to retire");
	}
	else if (65 < age >=21)
	{
		  JOptionPane.showMessageDialog(null, "You're an adult");
	}
	else if (age == 20)
	{
		  JOptionPane.showMessageDialog(null, "Not a teenager but not quite an adult");
	}
	else if (13 > age <= 20)
	{
		  JOptionPane.showMessageDialog(null, "You are a teenager");
	}
	else if (0 > age < 13)
	{
		JOptionPane.showMessageDialog(null, "You are a mere child");
	}
	else if (age >= 0)
	{
		JOptionPane.showMessageDialog(null, "You do not excist");
	}


	}//end class
}//end main

The reason for your boolean errors is you need to compare 2 values on each condition. Meaning, you can't have a condition such as: 65 < age >= 21. You are trying to compare age to 2 different numbers to one number at the same time. You need to separate as follows:

if(age<65 && age>=21)

Also Notice the logical operator &&.
a && b means and- The result is true only if both a and b are true.
a || b means or- The result is true if either a or b is true.
!a means not- true if a is false and false if a is true.

Thank you.

Do you have any advice on making my mainline program more efficient? i.e. Less else if's.

The way you did it is the most efficient way that I know of also. Maybe someone more advanced will give you something, then we will both learn :)

I sent my final program to my professor and she replied with

"You should review your code with this in mind:

Am I asking the same question twice?

For example, at one point you say: is the age great than or equal to 65

At another point you say is 65 less than the age?

You're asking the same question, in a different way."

I'm a bit stumped but I am kind of getting the hang of it.

As a matter of coding style, you might start by making your boundaries into constants. In this simple little example, it doesn't look like it saves you anything, but suppose you were writing something where these values were used across multiple class files. You don't want to change three class files by hand... and miss the fourth.

So:

//constants specify minimum age for membership in group:
public static final int CHILD = 0;
public static final int TEENAGER= 13;
public static final int CUSP = 20;
public static final int ADULT= 21;
public static final int RETIREE= 65;

Now if the Republicans come in, you can easily change the last line to read

public static final int RETIREE= 85;

and the code will comply with the new rules. :) *.5


Now, you peel off groups with the if statements:

if (age >=RETIREE) { System.out.println("Retiree")};
else if (age>=ADULT) { System.out.println("Adult"));

(and so forth)

This doesn't really gain you any efficiency, but it'll be easier to read and easier to keep up to date as things change. You might also keep your text strings up in the declarations as well, so you have the external language separate from the code. If Bob from Marketing calls and says "I want it to say 'You're our prime market' instead of 'You are a teenager'" it's easier to deal with if you have all of that in one place. Later on, you'll maybe use Properties files to store those Strings, and then it's not in your code at all. That's really nice, because you can write a little tool for Bob to use to modify that language and he'll leave you alone.

What your teacher meant by "asking the same question twice" was that you wouldn't need to ask if age was less than 65, since you can't get to that line if age is greater than 65. So you don't need to make the compound if statements (although Akill's suggestion is the correct way to write those).

I can't think of any clever way to efficiently map an integer into a set of arbitrary ranges other than by a series of ifs. If you had a lot of ranges, you might use a sort of binary sort logic - start at the middle and branch your way down - but that would just be pointless for five or six categories, and I'm not sure I can imagine the circumstance where you'd really need such a thing.

However, your instinct is correct: when you see a series of if statements, you should really wonder if there's a better way to do things. Often there is. In this case, maybe not.

Edited 5 Years Ago by jon.kiparsky: n/a

Comments
good post

Thank you for your input. It's definitely giving me new ideas.

I have a question in regards to your declarations though. You declared it as public static final int. Why? What is the difference as oppose to just declaring it as an int child = 0 for example.

public: Public members are visible to all other classes, this means any class can access a public field/method.

static: creates a class variable as opposed to a member variable.Class variables share the same variable/method over every instance of that class, no instance/object will have a unique value for a static member. static variables/methods can be called directly using the class name and a "."

final: Means you cannot alter the value. It is constant.

int child = 0; On the other hand is only local to the class/method it was used in. As was said, it's not really that practical declaring constants for your small example, But just remember the different keywords above when you ever need to use the same variables over multiple classes.

Comments
Yep, that's about right.

To follow on to Akill's post, the idea is to make these values accessible throughout the program, so I know I'm always using the same value and I can change them when I want. To do that, I put them publicly in some class. Since they're not going to depend on the particular instance of that class, I make them static. Since I don't want them to change in the course of the program, I make them final. Notice how these characteristics interact: if I make a variable public, anything that can read it can write to it. That means that I only ever make variables public if they're final. It's pretty rare that you'd want to do this with anything but a program-wide constant, so it'll usually be static. I can think of some cases where you'd have an instance constant - you might have a model of the solar system, and a constant value for surface gravity for each Planet, for example, so it won't always be static, but most often it will.
Best of luck
-jpk

This article has been dead for over six months. Start a new discussion instead.