In your main method when you call
Rfc rfc =new Rfc(ip,80);
You mean the ip you have declared inside the method but it is not reachable since it is inside the try code block - move all of the code inside the try block.
In your main method when you call
Rfc rfc =new Rfc(ip,80);
You mean the ip you have declared inside the method but it is not reachable since it is inside the try code block - move all of the code inside the try block.
Then the least you can do is to mark this one as solved.
No problem, please mark the thread as solved.
After you have finished adding all the contacts, then convert it into array, outside the loop is the right place :)
EDIT: masijade beat me to it while I was typing :)
Your avatar was made in Paint.NET, since you don't have after effects :)
Setter and Getter methods are for all types. Just like a getter method for variable x of type y will be
public y getX()
{
return x;
}
A setter method for the x will be:
public void setX(y newX)
{
x = newX;
}
Bonesawed, let's take a look at a part of your code:
public class EmployeePayroll
{
public double hours, payrate, grosspay, netpay, taxrate;
String firstname, lastname, id;
// ...
public double payrate()
{
double payrate;
return payrate;
}
//...
} // end of class
Line 11 declares a new variable called payrate. The next line returns it back to the user. This means that every time the method is called a new variable will be sent back to the user. You wanted something else though - the method needs to return the payrate variable declared on line 4. As you probably know, global variables (the one that you have declared in the class outside of the methods, in the beginning) are visible to all the methods. This means that the method payrate() can see the payrate variable declared on line 4, and therefore can return it to the caller of the payrate() method:
public double payrate()
{
return payrate; //the method knows the payrate variable and can send it back to the user.
}
Can you see other places in your code with the same problem?
You have one :)
I remember that I tried to search for a solution but eventually excepted the warnings, and ignored them :) I understand how annoying those warnings can be, but if the code ensures that the types are the same, I don't think you should break your head to much around it.
You are very welcome, and it's not a dumb error at all - generics can be extremely confusing.
Please mark the thread as solved.
So
if(num1 > num2) {
is correct? If I assumed correctly, we're only using Integer num1 and num2 to compare in if statement, but using node1 and node to swap? Am I on the right track in terms of swapping goes (I know the swap is wrong but I just wanted to know if I'm on right track). Thanks!
You are definitely on the right track :)
In a loop you can use the keyword continue
to stop the current loop iteration and proceed to the next one, and the keyword break
that simply exits the loop.
Well, the problem is because those are two different T's :) One is the class T, and the other one is the insert method's T. Let me explain - when you used the syntax
public <T> void insert(T item)
You actually declared a generic method that uses the parameter T - but not the same T as the class. Any class, generic or not, can have generic methods, declared by using the same syntax
public <T> returnType methodName(<parameters that can use T>)
You wanted to use the same T as the class, you needed to write
public void insert(T item)
I think at line 17 my drawing from before confused you - you cannot assume that head is the node that you need to change. You need to change the element that is before the first element that you are trying to swap. As kramerd said - if node1 is the 49th and node2 is the 50th, you will have to change the (node 48th).next
to point at node2, and you will not touch head during the entire swap process. I suggest that you draw the drawing again, using different places for the nodes - at the head, tail and in the middle, to be sure that you are not stuck on only one scenario.
apines, in order for this approach of setting a boolean to work, you need to put a break statement just after setting the boolean to true inside the loop. Otherwise it can just get set to false again in a different loop iteration and you won't have the value you want at the end.
Also, in your second code snippet, you have a void method that returns a boolean value.
Well, you are right about the void method returning a boolean, but I don't need a break since the boolean is set to false at the beginning and set to true if there is an empty sit. Can we settle on just half my bad and not a full one? :) I didn't put the break statement because it can confuse and this is not the point of the explanation :)
The method should be public static boolean makeFirstReservation(int[][] totalSeats, Scanner input)
Let's take a look at makeFirstReservation
method:
public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
row = input.nextInt();
col = input.nextInt();
System.out.println(totalSeats[row][col]);
}
}
}
Every iteration of the inner loop you are asking the user again for row and col, and then print the number inside that specific 2d array cell - not exactly what you wanted. You want the user to decide where he wants to sit, and then you want to check if you have any sits left. Let's assume that the user chose first class. You want to go over the matrix cells until you encounter an open sit (the first cell with 0) - if there is such a cell, then success, the user have a boarding pass and you need to change that cell to 1, otherwise the user will be back to the menu to choose another class.
So in the makeFirstReservation
method we basically want to check if there is a cell with value 0 in the range of the first 3 rows, all four columns. We will use a boolean variable to tell us whether there is an empty sit or not - we will scan the matrix, and when (and if) we will find an empty sit, then the variable will be equal to true:
public static void makeFirstReservation(int[][] totalSeats, Scanner input)
{
boolean emptySit = false; …
This is like assigning a number to a char variable - it will interpret it as the ASCII value. As kremerd said - you need to convert them into a String.
Another common interview question - write a method for swapping variables without using a temp variable (I know of two variations for this method).
Two extremely common questions are: Given a binary tree, reverse it, such that for every node x, swap x.leftChild and x.rightChild - analyse the complexity of your solution. Another one is similar - given a singly linked list, reverse the list. Analyse your complexity.
A non-programming question - you are given a pizza, using only 3 cuts, slice it into 8 slices. There is more than one solution.
I will think of more and will post them to you. Good luck ;)
a=a+b;
b=a-b;
a=a-b;
Will indeed swap to variables without a temp. Notice that you have to make sure that a+b does not exceed the maximum allowed integer. There is another way of swapping the variables without these constraints:
void swap (int *x, int *y)
{
if (x != y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}
First, excellent post - every experienced programmer is doing all of above and more after learning it the hard way - you cannot force your brain to work and solve all your problems, sometimes you need to rest, sometimes you need a different approach, and almost all of the time you can't assume that you know what your program is doing - you need to verify it.
Regarding the prints - I usually override the toString() method in order to print all the material that I want to know about the class and print that information completely in other parts of the code where I use instances of that class, I find it very helpful.
In addition to the prints, it is important to learn how to use the debugger, since it will give you more information in a more convenient fashion then just printing it.
JamesCherrill - I agree with you regarding testing each class, and I have a small main method in every class to check that the class itself doing all the it suppose to do (and does not do what it does not suppose to do).
Try to do one swap as showed in the diagram and as kramerd elaborated, and than think about special cases that need your attention, such as what if the length of the linked list is 1, or 2?
Ok, I am sorry, early in the morning - my mistake. Thanks for the correction :)
In your get_hours
and get_payrate
, you are creating a new variable and sending it back. First, as kremerd said, you don't need to do that, since your intention is to return the global variable _hours
/ _payrate
that you have already declared and initialized. What happens is called Shadowing Declarations, you can read about it more here, and a shorter version here.
Your method:
static int random(int die1, int die2)
{
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
From the header static int random(int die1, int die2
) we know that the method takes two int parameters, die1 and die2, and returns an int. This is not exactly what you want, right? You want the method to simply return a number that will represent the sum of the two die throws. Meaning you want the method to return an int, but you don't need any parameters sent to it. We should write:
static int random()
{
int die1 = (int)(Math.random()*6) + 1;
int die2 = (int)(Math.random()*6) + 1;
}
Now, this method computes two int random values, but doesn't do nothing with them - we need to explicitly tell the method what we want to return. Since you stated the int in "static int random", the compiler will shout at you if you won't return an int in the end of your method. We need to use the return keyword in order to return a value from the method. We said that we want to return the sum of the two die throws, so we can write:
static int random()
{
int die1 = (int)(Math.random()*6) + 1;
int die2 = (int)(Math.random()*6) + 1;
int dieSum = die1 + die2; //calculating the sum
return dieSum; //returning the sum;
}
Or, in the same way, we can simply write:
static int random()
{
int die1 = …
When do you increment? if you need only the number of rows of the matrix, you need to count only when you finish with a row, and not every cell (which will increment every column and every row, more than you need).
Basic info is basic. ;) Since this is now a sticky, I'd like to see the addition of a common class of examples that tend to cause confusion:
for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) System.out.println("What be the complexity?"); }
It's O(N^2), but the reason isn't immediately obvious because those not comfortable with Big O focus on the variance of the inner loop rather than the upper bound. A thorough analysis would be an amusing aside, if you feel so inclined.
You probably meant incrementing the j at the second loop :) Let's look at the code:
for (int i = 0; i < N; i++) {
for (int j = i; j < N; i++)
System.out.println("What be the complexity?");
}
The outer loop is going from 0 to N-1, meaning N iterations. In the first iteration i=0, the inner loop will iterate from j=i=0 to N, meaning N-1 iterations. In the second outer loop iteration i=1, and the inner loop will iterate from j=i=1 to N, meaning N-2 iterations. For iteration number k we get i=(k-1) and the inner loop will iterate from j=i=(k-1) to N-1, meaning N-(k-1) iterations. Let's sum all the iterations that the inner loop is doing: (N-1) + (N-2) + (N-3) + ... + 2 + 1. Using the sum of an arithmetic progression we can calculate this sum: (N-1) + (N-2) + (N-3) + ... + 2 + 1 = …
The method is PlayerWins(diceSum)
- but you are calling PlyerWins(diceSum)
.
FYI - the Java naming conventions encourage methods and variables to start with a lowercase letter, and classes with an uppercase letter.
My guess is that your compiler errors are from not recognizing Console.readString() - I am not familiar with that method in the Console class. Second thing is that your method is called PlayerWins but you are invoking on line 37 PlyerWins.
Are there any other compiler errors?
Take a look at the equals method - public boolean equals(Object obj)
. This method only gives true or false depending on whether the two objects are equal to one another. In this case - if they are the same object. You want to check the age variable in both nodes, and not just if they are equal - if they are bigger, equal or smaller - I think the compareTo method is better for that purpose :)
++(++(610)) = 612.
RMSD_Final = R2 / coordinatesRotZ.length;
just do RMSD_Final = R2 / counter;
I am not sure that I fully understood - can you please give me an example for visible text that you cannot parse?
Also, a search in DaniWeb revealed this post, which contains more information and other links to tutorials regarding web crawlers and Java. Might help you as well.
Well, if your code works, there is no reason to post it again I guess.
Glad I could inspire :) Please mark the thread as solved.
It states that the class TextBox does not have a method named Display(). In the MSDN they have an example for using C# text boxes:
private void CreateMyMultilineTextBox()
{
// Create an instance of a TextBox control.
TextBox textBox1 = new TextBox();
// Set the Multiline property to true.
textBox1.Multiline = true;
// Add vertical scroll bars to the TextBox control.
textBox1.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key to be entered in the TextBox control.
textBox1.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
textBox1.AcceptsTab = true;
// Set WordWrap to True to allow text to wrap to the next line.
textBox1.WordWrap = true;
// Set the default text of the control.
textBox1.Text = "Welcome!";
}
Take a look at the last line:
textBox1.Text = "Welcome!";
Perhaps this is what you need?
After you finish the inner loop, you need to erase the information inside the String Builder, or to adjust the loops to append just the needed string. Right now you are appending "Bark" at the first line, then in the second line you append to the same string "Bark Meow" and so on - it will create a long string.
For sorting the arrays you can choose one of several sorting algorithms. Simple ones will be bubble sort, selection sort and insertion sort. Try implementing one of these. The links above contain all the information you need regarding those sorts :)
When you do PersonNode x = current.getAge()
you are trying to insert an int into a variable that can store PersonNode. The correct syntax should be PersonNode = current
- that states that x, which is a PersonNode, points to the same place in the heap that current is pointing to. After that, doing x.getAge()
is the same as current.getAge()
, and doing node.next = x
is the same as node.x = current
.
If I have confused you, or you don't know what is the heap, tell me and I will explain it to you :) There is a big difference between trying to swap primitive types such as int, boolean, char etc' to objects, which are by reference in Java such as your PersonNode.
Can you please include the entire code?
You are welcome :) Please mark the thread as solved.
Did you fix the printf method?
What is the problem? The program works. If you are worried about the extra zeros - it is because your array is of size 20, and you are inputting only 3 numbers to it.
Since you are retrieving the HTML code, you can parse the <a href = "HTML SITE> </a> tags in order to get the hyperlinks. What exactly do you mean by "parsing all the visible text from the particular web page"?
this.getName() will call the getName() method of the calling instance. Square gets its getName() method from NamedShape, so it will print its name - "Square"
kremerd is right - drawing is very helpful. I will try to help you out with the pictures. We will do together one scenario, you will try and translate into code. The rest of the scenarios will be easier then I believe. Let's have this linked list:
We want to swap node1 and node2, meaning that the list should go head [TEX]\rightarrow[/TEX] node2[TEX]\rightarrow[/TEX] node1[TEX]\rightarrow[/TEX] the rest of the list as usual. First thing let's change node2.next() so it will point to next1, the next in line in the new order:
Now, node1.next() should point to where node pointed before, because now this is his new place in the list:
And eventually we need head, who pointed at node1, to point now at node2:
Now, note that you need not to "lose" your nodes, for example after changing node2.next() to point to node1, we make the older node2.next() unreachable:
We can make a temp node that will point to node2.next() before changing it to node1:
You need to be sure that during the entire swap you don't lost any nodes.
This was one swap scenario - can you think about more scenarios that you need to check and handle as part of the swap?
The file - is it binary or text? You want the first number to go into the int array, the next to one double array and the third to the second double array, and so on for each line?
The first loop is O(n), agreed. The nested loops: The outer loop is going from 0 to n-1, meaning n iterations. The first iteration, where i=0, j will iterate from i+1=1 to n-1, meaning n-2 iteration. The second outer loop iteration, j will iterate from i+1=2 to n-1, meaning n-3 iterations. When i=n-2, j=i+1=n-2+1=n-1, meaning it will iterate precisely once. This will give us (n-1)+(n-2)+...+2+1=\frac{n*(n-1)}{2} = O(n^2). You don't need to multiply by n since you have already counted the outer loop iterations with the sum. Think about it like this, if you had the code
for (int i = 0; i < n; i++) {
for (int j = 0, j < n; j++) {
int d = Math.abs(list[j] - list[i]);
if (m > d)
m = d;
}
}
The result will be O(n^2) right? The calculation would have been similar only n+n+n+...+n=n*n=n^2=O(n^2). So it can't be that when the inner loop is doing less iterations, it will have bigger complexity. Besides - you have two loops with O(1) operations inside, it cannot be bigger than O(n^2).