|
After leaving Learning Java for awhile i've come back to it and intend to take a brief exam quite soon but i've forgotten some of the basics, I've started this thread just to ask for help with some Questions i'm struggling with over the coming weeks. Any Contributions would be much appreciated.
Thank you.
How do i:
Write an integer to a label on a java form?
Read from a text field on a java form into a float variable? ( or other variable)
Also if someone could point me in the direction of a basic set of Rules to follow for Java then that would be great. Thank you for your help.
<Added>
How would i write pseudo code for the java required to read from an external data file?
|
|
|
After a few hours of going over some more questions i came across this one:
"Explain, with snippets of Java code, how threads can be used in a program which includes screen animations as well as user intereaction via GUI".
Here is my answer, let me know if it sounds about right:
" As threads are computational units waiting to be utlized by the processor it means instructions for programs can be stored in methods waiting to be executed by the processor when a user intereacts with a GUI to exectute the instruction/thread. In this example i will show a basic Paint method.
Public void Paint (Graphics g) {
g.setColor(Color.blue);
In the basic example above the user could then create a rectangle fill with the color blue and add further code to instruct the rectangle to move around the screen, this information is stored in the thread waiting to be executed. To exectute the instruction the user could create a GUI and add a Jbutton called "Run animation" to execute the instruction stored in the Animation thread, causing the Thread to be executed by the processor creating the Blue rectangle animation on the screen".
|
|
|
Write an integer to a label on a java form?
int n = 25;
label.setText(String.valueOf(n));
Read from a text field on a java form into a float variable? ( or other variable)
String text = textField.getText();
float f = Float.parseFloat(s);
Also if someone could point me in the direction of a basic set of Rules to follow for Java then that would be great Java Tutorial
How would i write pseudo code for the java required to read from an external data file?
File file
try
create reader for file
String line
while((line = reader.readLine) != null)
process line
close reader
catch exception(s)
|
|
|
Hey thank you, i've been away for sometime.
So if i wante to write an integer to a label on a Jform i could use this:
int f = 56;
label.getText(String.getValueOf(f));
or of a float
float a = 199;
label.getText(String.getValueOf(a));
What if i wanted to set the value of the label to a string?
String labelName = ("Testing";
label.getText(labelName);
Would that work?
Also is a loop an example of Recursion? a method that recalls its self?
<Added>
String labelName = ("Testing");
label.getText(labelName);
Forgot end bracked.
For float example change to 19.9 so it makes more sense.
<Added>
In your example:
String text = textfield.getText();
float f = Float.parseFloat (S);
Does this create a string variable called text? this varibable retrieves the text from the text variablle
Float F = Float.parseFloat (S);
This is what i dont understand?
A Float cannot be a Character?
What is (F);
What is (S);
Please help
|
|
|
What if i wanted to set the value of the label to a string?
String labelName = ("Testing";
label.getText(labelName);
|
|
Would that work?
Should be okay, without the typos
String labelName = "Testing";
label.setText(labelName);
// or
label.setText("Testing");
|
|
Also is a loop an example of Recursion? a method that recalls its self?
No. Using loops is an alternative to recursion. Think about the factorial operation in math. A number times one less than itself, over and over until it reaches one. 3! = 6
String labelName = ("Testing");
label.getText(labelName);
Forgot end bracked.
They are not required.
For float example change to 19.9 so it makes more sense.
19.9 is a double.
19.9f or 19.9F or (float)19.9 is a float.
Does this create a string variable called text?
Yes. this varibable retrieves the text from the text variablle
This variable ("text") is assigned the value of the String which is retrieved from the textField with the getText method.
This is what i dont understand?
A Float cannot be a Character?
My error. You want to parse the text to get a float value.
String text = textField.getText();
float f = //Float.parseFloat(s);
Float.parseFloat(text);
|
|
"f" is a newly declared float variable.
System.out.println(f.getClass().getName());
|
|
|
|
|
This may be a silly question, but is Mergesort an example of recursion?
Also, im struggeling to explain how to add data to a linked list in the correct place if the link listed is sorted ascendingly.
What would that be in Psuedo code?
The only way i can decsribe it is:
"A linked list is a series of nodes in a datastructure which point to the next element of the same type (Singular)"
and then im stumped, not even sure how to being the pseudo code on it.
A Second question i dont seem to grasp is:
Score [] is an array of 200 integers, write the code to initialise each element of the array to 0.
Does this mean that every number in the array 0-200 needs to be set to Zero? in affect creating an array of 0's? or am i being completely stupid and missing the point.
Any help would be much appreciated.
<Added>
If you could take a look at this binary tree it would be much appreciated, wikipedia gave the simplest definition, everyone elses is to hard to follow.
Add the numbers 2 7 5 6 4 2 1 7 9 in that order to a binary tree.
In my example below ive gone left to right, left smallest right highest each time from number to number.
(2)
/ \
/ \
/ \
(5) (7)
/ /
/ /
(4) (6)
/ \ / \
/ \ (2) (7)
(1) (9)
|
|
|
Not very strong on pseudo code so i'll leave that out..
2)
int Score[];
Score = new int[200];
for (int i = 0; i < 200; i++) {
Score = 0; // set all values to 0
}
3) 2 7 5 6 4 2 1 7 9
Find this difficlut to draw out, since the indents go away..
|
|
|
Does this mean that every number in the array 0-200 needs to be set to Zero? in affect creating an array of 0's?
Yes. sam_02 showed it. The sub script got misinterpreted for an italics tag.
Actually, when you instantiate a primitive array each element is given a default value automatically by the jvm. So the array is already initialized to all zeros. Another way to initiaize all elements to the same value is to use Arrays.fill
java.util.Arrays.fill(Score, 0);
|
|
everyone elses is to hard to follow
Try this one Binary Tree - Data Structures Visualization 2 7 5 6 4 2 1 7 9
start with 2
add 7: add 7 to se of 2
add 5: add 5 to sw of 7
add 6: add 6 to se of 5
add 4: add 4 to sw of 5
add 2: oops
add 1: add 1 to sw of 2
add 7: ouch
add 9: add 9 to se of 7
|
|
I don't know that you can add duplicates to a binary tree.
<Added>
Let's see if the quote tags will preserve the subscript "i".
The sub script [i] got misinterpreted for an italics tag.
|
|
How about this: [j] ?
|
|
|
Thank you once again crwood and thank you Sam.
Its starting to make a bit more sense to me now, can someone explain to me why http://en.wikipedia.org/wiki/Binary_tree (Look at there diagram) shows a 7 on the left of a 2, and a 5 on the right?
|
|
|
The discussion on the page seems to be fairly general in scope.
So, maybe this: the root node 2 joins two subtrees and all below the root of each subtree
(7 and 5) illustrates a lower node value as the left child and higher value node as right
child.
|
|
|
Whats Java skeleton?
My current question is: how could an exception handler trap two types of common file IO Exception?
If you could reply ASAP that would be great, thank you.
|
|
|