What is the interpretation of "p[z] <-- y" pseudocode in java? - java

This is for a red black tree.
For the pseudocode "p[z] <-- y", would the interpretation in java be:
z.getParent() = y;
or
z.setParent(y);
Thanks :)

The correct code would be z.setParent(y);
Be aware that z.getParent() = y; is invalid code. The assignment operator = stores the result of evaluating the expression to the right of the = into a variable, attribute or array position to the left of the =. In an expression like the first one in your question, you'd be trying to assign the value y into the result of calling z.getParent() - that is, trying to assign a value into another value, and that won't work.
The right way to change an attribute is by calling the corresponding setXXX() method, or by directly assigning to the attribute if it was declared non-private, like this: z.parent = y;

Related

can any one help me in this error in java?

i create hash table in java but there is aproblem in it when adding elements in it.
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<sum2 , g>;
the error in second line where define sum2 is variable contain integer value and g variable contain string value. i dont know where the problem in putting elements in hash table.i want to add the values of this variables in hash table each time as the values change.
To mutate an object, you have to call its properties. An object property is either a field or a method. In your case put is a method of the hashT object. A method call is done by writing the object name, then the dot operator, then the method name and finally the arguments surrounded by parenthesis:
objectName.methodName(argument1, argument2, ...);
The problem is here:
hashT.put<sum2 , g>;
put is a method and to call it you have to surround the arguments (sum2 and g) with parenthesis:
hashT.put(sum2, g);
You need to follow the comments and edit your title so its a meaningful title that will help other people understand what your question is about.
Also, when you get an error, copy the big red text that is displayed in the console (color depends on which editor you are using), also known as the error's "Stack Trace" and past it into your question somewhere. This will help us pinpoint what is going on, and the error title itself will likely give away to us exactly what's wrong.
However, without any context on what the error is and what is before or after those two lines of code it is difficult to determine if you have previously defined sum2 or g as a variable that stores values. I am going to assume you haven't assigned at least one of them, likely g as a variable.
For experimental purposes, try replacing those two lines of code with:
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<0 , "g">;
That is putting zero (0) as an explicit integer and g as an explicit string into the hashtable. If you need to put variables into there, then you need to define them before hand, like this:
int sum2 = 3 + 4;
String g = "Some String";
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<sum2 , g>;
Now the integer value 7 is stored as the hash and the string Some String as the mapped value.

Mapping Color Codes To Int in Java

I'm trying to write a change color method in Java that accepts an int parameter and changes that color based on that int. Valid colors will be in the range 1 - 6 for the six colors. You may decide which number (1-6) maps to which color. If the value is not in this range, make the circle red.
I am trying to do this without the use of an array list, but I am unsure how. Any ideas?
I've tried:
if(newColor == 1) {
newColor = "yellow";
}
And I get an error message saying 'incompatible types: java.lang.String cannot be converted to int.'
I've also tried:
if(newColor == 1) {
newColor.equals("yellow");
}
And I get an error message stating 'int cannot be dereferenced'.
1 is an int literal. If the compiler allows you to test newColor ==, then that means newColor must be an int variable. Being an int variable, it is only allowed to hold int values.
"yellow" is a String literal. The compiler will not allow you to assign a String value to an int variable. You may only assign String values to String variables.
You're going to need two variables: One to hold the given int value, and one to hold the String result.
Other languages (e.g., Ruby) might let you do it differently, but if you're going to use Java, then you are going to have to work within the Java rules to solve your problem.

error while using math.pow in java code

i have a java code for finding out netsalary . I keep Getting "bad operand type for binary operator ' / '" error . The Line goes like this
netSalary = Double.parseDouble(principle2*rate2/12*Math.pow(rate2/12+1))/(Double.parseDouble(Math.pow(rate2/12+1)-1));
Could this be solved . Thanks in advance!
It should be commas you use, not slashes.
Math.pow(rate2/12+1) should syntactically be in the form of Math.pow(x,y) where both x and y are doubles. The first arg is the base, and second arg is the index you're raising it to. As the comment below mentioned, it's difficult to understand what you're trying to achieve, and you'll have to substitute x and y for the correct values - make sure they're of type double (you cannot use, for example, 12+1 as a parameter because it is an integer). If it is an integer, then type cast it using (double) in front of the value.
You also do not need to parseDouble everywhere since Math.pow will return double values anyways; it is redundant.
I agree with the comment above; please read the javadocs for any problems you're having with a method before posting here.
You mistake in using power function your power function should be like this
Math.pow((rate2/12+1),1)

Difference between declaring variables

I am a little confused in declaring variable in android, I know two ways of doing it.
1. int EC, CC, PT, HC = 1;
2. int EC = 1, CC = 1, PT = 1, HC = 1;
I want to know the difference between 1 & 2.
Are both the ways same ?
This question is not really related to Android, it is only dependent on Java.
Anyway,
the first approach creates the variables EC, CC, PT and HC. HC gets the value 1.
In the second approach you create all the variables and give them the value 1.
Just do a search for Java and Variables if you want to find some information about the subject. Here are two links for you:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
http://www.roseindia.net/java/master-java/variables-in-java.shtml
On a side note, you should follow the Java naming conventions and keep your variables lowercase, or capitalize the first letter of any subsequent words in your variable name.
No, it's not the same.
In the first case, only HC will have the value 1 assigned. The other variables will be left unassigned.
If these are instance or static variables, that means they will get the default value 0. If they are local variables, you will need to assign them before you can use them.
only HC will be initialized
all variables will be initialized (with value 1)
Assuming instance variables, frst way, EC,CC,PT are ZERO. Second way all are ONE.
Instance variables are assigned to default values, if nothing assigned.
If you define first one as local variable (inside any method), you will get compilation error.
If you declare int EC,CC,PT,HC=1;
The values for EC,CC and PT are Zero(0) and HC value is 1,
If you decalre int EC=1,CC=1,PT=1,HC=1 means all EC,CC,PT and HC values are one.
In the first case, the first 3 variables are uninitialized, that means when object is created compiler will initialize those uninitialize variables with the default values hich is ZERO in case of integer type and the last variable will be initialized with ONE.
In the second case each variable will be initialized with ONE. You can also do it as follows
First declare all variables
int EC, CC, PT, HC;
Then assign value
EC =CC = PT = HC = 1;
The first example is like :
int EC;int CC;int PT;int HC = 1; // and only hc gets the value of 1
The second example is like :
int EC=1;int CC=1;int PT=1;int HC = 1; // all of the vars get the value of 1
In first case, except EC, all are uninitialized.
In second case, all are initialized.

Unexplained parenthesise in Java

What do parenthesis do in Java other than type casting.
I've seen them used in a number of confusing situations, here's one from the Java Tutorials:
//convert strings to numbers
float a = (Float.valueOf(args[0]) ).floatValue();
float b = (Float.valueOf(args[1]) ).floatValue();
I only know only two uses for parenthesis, calls, and grouping expressions. I have searched the web but I can't find any more information.
In the example above I know Float.valueOF(arg) returns an object. What effect does parenthesize-ing the object have?
Absolutely nothing. In this case they are not necessary and can be removed. They are most likely there to make it more clear that floatValue() is called after Float.valueOf().
So this is a case of parenthesis used to group expressions. Here it's grouping a single expression (which does obviously nothing).
It can be shortened to:
float a = Float.valueOf(args[0]).floatValue();
float b = Float.valueOf(args[1]).floatValue();
which can then be logically shortened to
float a = Float.parseFloat(args[0]);
float b = Float.parseFloat(args[1]);
I dont believe they serve any purpose here. Maybe left over after some refactoring
None other than to confuse you. It's as good as saying
float a = Float.valueOf(args[0]).floatValue();
directly.
I suspect the programmer just found it more readable. I don't agree with him in this particular case, but often use parentheses to make it clearer. For example, I find
int i = 3 + (2 * 4);
clearer than
int i = 3 + 2 * 4;
The extra parentheses in your code sample do not add anything.
//Your example:
float a = (Float.valueOf(args[0]) ).floatValue();
// equivalent:
float a = Float.valueOf(args[0]).floatValue();
It could be that the original programmer had done something more elaborate within the parentheses and so had them for grouping, and neglected to remove them when simplifying the code. But trying to read intent into old source is an exercise in futility.
The extra space in args[0]) ) is pretty odd looking, too, as it is unmatched in the opening paren.
Here they are used for grouping. What's inside one of those expression if of type Float, so you can apply the method floatValue() to the whole content of the parenthesis.
They could be removed here as there is no ambiguity. They would have been mandatory with an expression using another operator of higher preseance order. But according to the docs, there is no such operator, the dot/projector has highest priority. So they are really useless here.
Regards,
Stéphane

Categories

Resources