Point p = ...; if (p.getX() == 0 && p.getY() == 0) // test if p is the origin p.draw(); p.move(1, 2);
Java interprets the above code as three separate statements: a declaration statement, an if statement, and finally, an assignment statement. The fact that you indented your two statements to show that they "belong" together has no effect on how Java will interpret the logic of your code. This is what Java sees:
Point p = ...; if (p.getX() == 0 && p.getY() == 0) // test if p is the origin p.draw(); p.move(1, 2);
If you want to communicate the meaning that you indicate with your indentation, then you must group the two assignments into a single statement, like this:
Point p = ...;
if (p.getX() == 0 && p.getY() == 0) { // test if p is the origin
p.draw();
p.move(1, 2);
}
You still want to indent things nicely, because even though it doesn't matter to the Java compiler, it sure does matter to your AI!
These braces are not required if we only need to do one thing in the particular clause. For example,
if (today.isTuesday())
System.out.println("Yeah! I get to go to an A201 lecture!");
else
if (today.isThursday() && !today.isDuringExamWeek())
System.out.println("Yeah! I get to go to an A201 lecture!");
else
System.out.println("Bummer...");
It does no harm to put the braces in, though, and it's probably a good idea to do so when you're just getting started:
if (today.isTuesday()) {
System.out.println("Yeah! I get to go to an A201 lecture!");
}
else {
if (today.isThursday() && !today.isDuringExamWeek()) {
System.out.println("Yeah! I get to go to an A201 lecture!");
}
else {
System.out.println("Bummer...");
}
}
This makes it easier to modify your program if you later decide to add some additional statements to your clauses:
if (today.isTuesday()) {
System.out.println("Yeah! I get to go to an A201 lecture!");
System.out.println("Just kidding...");
}
else {
if (today.isThursday() && !today.isDuringExamWeek()) {
System.out.println("Yeah! I get to go to an A201 lecture!");
System.out.println("Just kidding...");
}
else {
System.out.println("Bummer...");
}
}
We do recommend using the curly braces in every if statements,
even if it only has one statement inside.
if-else statements in Java have the same syntactic requirement as in English: you have to use an if before you use else. In other words, each else must have a matching if that preceeds it.
The "else without if" error is a violation of this rule. (In English, you don't begin a conversation with "else so and so", do you? The same is true in Java. You cannot begin a statement with the word else.)
Without knowing anything more about your program, I would say that you have made one of two possible errors. Either you have forgotten to enclose multiple statements in the true clause of an if-else, like this:
if (today.isSunny())
System.out.println("Let's go swimming!"); // missing {}'s around
sunnyDays = sunnyDays + 1; // these two statements
else
System.out.println("Let's go to the movies!");
or you have a "chain" of nested if's and you forgot to include the keyword if after some else, like this:
if (today.isSunny())
System.out.println("Let's go swimming!");
else if (today.isRainy())
System.out.println("Let's go to the movies!");
else (today.isCloudy()) // missing if
System.out.println("Let's go for a short walk!");
In Java, the equality operator == is (primarily) used in comparisons between primitive data types such as integers. It is bad practice to use it to compare objects like Strings or Points because it just tests whether or not the two objects occupy the same space, i.e. it determines if your two names are just aliases for the same object. If you want to compare the contents of two distinct objects to see if they are equivalent, then you must use the equals methods provided by the class. For example, the following code will print the Yum message,
String dinner = "hot dog";
if (dinner.substring(0, 3).equals("hot"))
System.out.println("Yum...");
but this code will not print anything:
String dinner = "hot dog";
if (dinner.substring(0, 3) == "hot")
System.out.println("Yum...");
There is an equals method defined for all of the graphical objects:
Point origin = new Point();
if (origin.equals(new Point(0, 0)))
System.out.println("This message WILL be printed!");
I think my diskette is full because I have a problem opening it. I keep getting this message: "The Drive cannot find the sector requested". I can't delete or extract anything. What should I do?
It sounds like the directory on your disk is damaged. I doubt you'll be able to retrieve any information from that disk. You'll have to start from scratch. Hopefully, you've got some printouts lying around of the work you've been doing recently... In the future, you should always make a back up of your programs on a separate disk. Your disk can fail unexpectedly at any time!
Sure thing. Here they are. Note that the following tests only show the first and the last words.
as Bm => piglet sentence: Asbay Mbay
Ann Joe => piglet sentence: Annbay Oejay
a b. => piglet sentence: Abay bay.
a A. => piglet sentence: Abay Abay.
Man HH. => piglet sentence: Anmay Hhay.
A A. => piglet sentence: Abay Abay.
Joe HH. => piglet sentence: Oejay Hhay.
B Joe. => piglet sentence: Bay Oejay.
Actually, take that sigh back. You don't need to. One straightforward way is to check if the character is greater or equal to "A" and less or equal to "Z", as follows:
String firstChar = word.substring(0,1);
if ((firstChar.compareTo("A")>=0) && (firstChar.compareTo("Z")<=0))
...
There is actually a more elegant solution - here is a hint. If
a character is in upppercase, then converting it into lowercase
will result in a different character! (unlike a lowercase
character, which will stay the same, if converted into
lowercase). What would this be in an if condition?
String letterGrade;
letterGrade = Console.in.readLine();
letterGrade.toLowerCase();
if (letterGrade.equals("a"))
System.out.println(4.0);
else
System.out.println("Illegal grade");
The letterGrade string is not actually being updated when you do this:
letterGrade.toLowerCase();
Therefore, letterGrade is the same as it was before, i.e. "A". Since upper and lower case letters are considered entirely different things in Java, the string "A" is not equal to the string "a".
Remember that there are no mutating methods for the String class. All the methods return something. Thus, the only way that you can change an existing String is to assign to it a different object.
F+ and F- are illegal grades, so they do not have any value. F has a value of 0.0, but do not try and assign values for F+ and F-. If you do the error checking part of the asignment, make sure you do error checking that takes care of F+ and F- too, since they are invalid inputs.
You should perform some initial error checking to make sure that the user has given you a valid grade, and, if it is fine, then go ahead with the normal processing of the input. If the input is invalid, you should print an appropriate error message and stop.
Note that the error message is required to include the actual string the user entered without any modifications that you might have made. For example, you might convert the user input to all lower case for the purpose of conducting a case-insensitive comparison. But, if the input is found to be incorrect, you should not print this modified input (i.e. your lower case version of it) in the error message; you should print the original input in the message.
The example on the lab web page is:
Error: A+++ is an illegal grade.
This does not mean that you should check for this particular illegal input in your program. Do not include code that looks like this:
if (letterGrade.equals("A+++"))
System.out.println("Error: A+++ is an illegal grade.");
if (first.equals("a") || first.equals("b") || first.equals("c") ||
first.equals("d") || first.equals("f") &&
second.equals("+") || second.equals("-") || second.equals(""))
...
The && operator has higher precedence than the operator, so a condition of the form:
will be interpreted as:
which is not what you want!
if (second.equals("_"))
base = base - .3;
You have mistakenly typed an underscore character rather than the minus character! That's very easy to do because <shift>- is _.
In the Circle class there is a constructor that makes a circle that will be filled in with the current pen color when it is drawn. All you do is specify "fill" as the third argument.
Here's an example:
Point p = new Point(); // the origin (0, 0) Circle c = new Circle(p, 4, "fill"); setPenColor(green); c.draw();
Make a line between them (don't draw it, just create it), and then find out the length of the line. The other option is to do the math to find the distance between two points: sqrt((x1-x2)2 + (y1 - y2)2)
You need to make, and then draw, a Message object. Like all of our graphical objects, a Message object is created with new. The Message constructor takes a Point and a String. For example,
Point p = ...; Message m; m = new Message(p, "hello"); m.draw();
You should just experiment with what the starting point of the message should be so that it comes up near the center of the screen.
No, the two circle actually have to cross into each other in order for them to intersect.
In GraphicsApplet, the colors are defined as special constants. So, setPenColor(red) will work fine, but setPenColor("red") will not. Why is this?
Well, to explain, notice that in order to assign a boolean constant, you would say boolean foo = false and not boolean foo = "false". This is somewhat like that. red here is a special constant referring to the red color. But "red" is a regular string, and does not really have a meaning to the setPenColor() method.