|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Use the [code] forum tag to get verbatim indentation.
|
On October 12 2011 07:47 Ripense wrote:Show nested quote +On October 12 2011 07:17 Quasimomo wrote:Derp! Of course hahaha xD Here is the whole code that's causing the problems ( I think ) I'm working with a few other .java files. These include the GUI file and the file that takes care of linking vlc to java. + Show Spoiler + package businessunits;
import com.phidgets.*;
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import org.jdesktop.application.Application; import org.jdesktop.application.SingleFrameApplication;
public class BusinessUnitsApp extends SingleFrameApplication { @Override protected void startup() { show(new BusinessUnitsView(this)); }
/** * This method is to initialize the specified window by injecting resources. * Windows shown in our application come fully initialized from the GUI * builder, so this additional configuration is not needed. */ @Override protected void configureWindow(java.awt.Window root) { }
/** * A convenient static getter for the application instance. * @return the instance of BusinessUnitsApp */ public static BusinessUnitsApp getApplication() { return Application.getInstance(BusinessUnitsApp.class); }
/** * Main method launching the application. */ VLCv2 vlc = new VLCv2("localhost", 1234); private Phidget phidget; private BusinessUnitsView guiobj; private InterfaceKitPhidget interf;
/** * constructor van deze Main klasse, maak GUI aan en * maak verbinding met VLC */
public long getsensor(int a) { long sensor = readSensorValue(a);
return sensor; }
public boolean getdeurbel() { boolean deurbel = Inputstate(0); return deurbel;
}
//Vanaf 120 public long StartMove() {
long sensor7 = readSensorValue(7); return sensor7;
}
public boolean deurbel() { while (true) { try { boolean deurbel = Inputstate(0); Thread.sleep(250); System.out.println(deurbel); if (deurbel) { return deurbel; } } catch (Exception e) { e.printStackTrace(); } } }
public static void main(String[] args) throws InterruptedException { launch(BusinessUnitsApp.class, args);
BusinessUnitsApp m = new BusinessUnitsApp(); VLCv2 vlc = new VLCv2("localhost", 1234);
vlc.connect(); vlc.rc.fullscreen(); vlc.rc.play(); vlc.rc.pause();
while (true) { try {
Robot rob = new Robot(); Thread.sleep(250); long sensor7 = m.StartMove();
if (sensor7 >= 70) { vlc.rc.play();
for (int i = 0; i <= 110; i++) { Thread.sleep(1000); System.out.println(i); if (i == 110) { vlc.rc.pause(); } } }
if (m.getsensor(0) < 100) { rob.mouseMove(1550, 340); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(1) < 100) { rob.mouseMove(1550, 410); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(2) < 100) { rob.mouseMove(1550, 470); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(3) < 100) { rob.mouseMove(1550, 535); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(4) < 100) { rob.mouseMove(1550, 600); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(5) < 100) { rob.mouseMove(1550, 650); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getdeurbel()) { vlc.rc.play();
for (int a = 0; a <= 126; a++) { Thread.sleep(1000); System.out.print(a + " ");
if (a == 44) { vlc.rc.pause(); while (true) { try { boolean deurbel = m.deurbel(); if (deurbel == true) { vlc.rc.play(); break; } } catch (Exception e) { e.printStackTrace(); } } } if (a == 126){ vlc.rc.pause(); } } }
} catch (AWTException e) { e.printStackTrace(); } } }
public void makeConnection() {
try { //interf.openAny("localhost", 5001); // alleen voor webservice-versie interf.openAny(); // werkt ook zonder webservice interf.waitForAttachment(1000); // 1 sec, anders exception } catch (PhidgetException e) { e.printStackTrace(); } }// end of phidgetmakeConnection() //public native boolean getInputState(int i) throws PhidgetException;
public boolean Inputstate(int nr) { boolean value = false; try { value = interf.getInputState(nr); } catch (PhidgetException e) { e.printStackTrace(); } return value;
}
public long readSensorValue(int nr) { long value = -1; try { value = interf.getSensorValue(nr); } catch (PhidgetException e) { e.printStackTrace(); } return value; }
public void setOutputValue(int nr, Boolean state) { try { interf.setOutputState(nr, state); } catch (PhidgetException e) { e.printStackTrace(); } } }
As far as I can see you call the method getSensorValue on the interf variable which is no initialized and therefore throws a NullpointerException. You need to do something like inter = new InterfaceKitPhidget(); before calling methods on it.
I'm glad you see what's wrong with it :D but... could you perhaps explain in more detail please? I've only been working with java for a couple of weeks so I don't really know where look for things and where to put them =(
|
anyone know how to call the b2Vec2 function Multiply(a:number :void in Objective-C++? Been bugging me all day since I can't find out how
|
On October 12 2011 08:08 Quasimomo wrote:Show nested quote +On October 12 2011 07:47 Ripense wrote:On October 12 2011 07:17 Quasimomo wrote:Derp! Of course hahaha xD Here is the whole code that's causing the problems ( I think ) I'm working with a few other .java files. These include the GUI file and the file that takes care of linking vlc to java. + Show Spoiler + package businessunits;
import com.phidgets.*;
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import org.jdesktop.application.Application; import org.jdesktop.application.SingleFrameApplication;
public class BusinessUnitsApp extends SingleFrameApplication { @Override protected void startup() { show(new BusinessUnitsView(this)); }
/** * This method is to initialize the specified window by injecting resources. * Windows shown in our application come fully initialized from the GUI * builder, so this additional configuration is not needed. */ @Override protected void configureWindow(java.awt.Window root) { }
/** * A convenient static getter for the application instance. * @return the instance of BusinessUnitsApp */ public static BusinessUnitsApp getApplication() { return Application.getInstance(BusinessUnitsApp.class); }
/** * Main method launching the application. */ VLCv2 vlc = new VLCv2("localhost", 1234); private Phidget phidget; private BusinessUnitsView guiobj; private InterfaceKitPhidget interf;
/** * constructor van deze Main klasse, maak GUI aan en * maak verbinding met VLC */
public long getsensor(int a) { long sensor = readSensorValue(a);
return sensor; }
public boolean getdeurbel() { boolean deurbel = Inputstate(0); return deurbel;
}
//Vanaf 120 public long StartMove() {
long sensor7 = readSensorValue(7); return sensor7;
}
public boolean deurbel() { while (true) { try { boolean deurbel = Inputstate(0); Thread.sleep(250); System.out.println(deurbel); if (deurbel) { return deurbel; } } catch (Exception e) { e.printStackTrace(); } } }
public static void main(String[] args) throws InterruptedException { launch(BusinessUnitsApp.class, args);
BusinessUnitsApp m = new BusinessUnitsApp(); VLCv2 vlc = new VLCv2("localhost", 1234);
vlc.connect(); vlc.rc.fullscreen(); vlc.rc.play(); vlc.rc.pause();
while (true) { try {
Robot rob = new Robot(); Thread.sleep(250); long sensor7 = m.StartMove();
if (sensor7 >= 70) { vlc.rc.play();
for (int i = 0; i <= 110; i++) { Thread.sleep(1000); System.out.println(i); if (i == 110) { vlc.rc.pause(); } } }
if (m.getsensor(0) < 100) { rob.mouseMove(1550, 340); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(1) < 100) { rob.mouseMove(1550, 410); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(2) < 100) { rob.mouseMove(1550, 470); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(3) < 100) { rob.mouseMove(1550, 535); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(4) < 100) { rob.mouseMove(1550, 600); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getsensor(5) < 100) { rob.mouseMove(1550, 650); rob.mousePress(InputEvent.BUTTON1_MASK); rob.mouseRelease(InputEvent.BUTTON1_MASK);
}
if (m.getdeurbel()) { vlc.rc.play();
for (int a = 0; a <= 126; a++) { Thread.sleep(1000); System.out.print(a + " ");
if (a == 44) { vlc.rc.pause(); while (true) { try { boolean deurbel = m.deurbel(); if (deurbel == true) { vlc.rc.play(); break; } } catch (Exception e) { e.printStackTrace(); } } } if (a == 126){ vlc.rc.pause(); } } }
} catch (AWTException e) { e.printStackTrace(); } } }
public void makeConnection() {
try { //interf.openAny("localhost", 5001); // alleen voor webservice-versie interf.openAny(); // werkt ook zonder webservice interf.waitForAttachment(1000); // 1 sec, anders exception } catch (PhidgetException e) { e.printStackTrace(); } }// end of phidgetmakeConnection() //public native boolean getInputState(int i) throws PhidgetException;
public boolean Inputstate(int nr) { boolean value = false; try { value = interf.getInputState(nr); } catch (PhidgetException e) { e.printStackTrace(); } return value;
}
public long readSensorValue(int nr) { long value = -1; try { value = interf.getSensorValue(nr); } catch (PhidgetException e) { e.printStackTrace(); } return value; }
public void setOutputValue(int nr, Boolean state) { try { interf.setOutputState(nr, state); } catch (PhidgetException e) { e.printStackTrace(); } } }
As far as I can see you call the method getSensorValue on the interf variable which is no initialized and therefore throws a NullpointerException. You need to do something like inter = new InterfaceKitPhidget(); before calling methods on it. I'm glad you see what's wrong with it :D but... could you perhaps explain in more detail please? I've only been working with java for a couple of weeks so I don't really know where look for things and where to put them =(
VLCv2 vlc = new VLCv2("localhost", 1234); private Phidget phidget; private BusinessUnitsView guiobj; private InterfaceKitPhidget interf;
In this part you declare some variables. The declaration alone does not assign an object to the variable. For the variable vlc you assign an object (new VLCv2("localhost", 1234). However the other variables are still null. At some point in you code you need to have the other variables on the left side of an assign operation before you use them otherwise, or the will still be null. That's what I think is the problem, because at
public long readSensorValue(int nr) { long value = -1; try { value = interf.getSensorValue(nr); } catch (PhidgetException e) { e.printStackTrace(); } return value; }
you call the function getSensorValue(int) on the interf variable, but this variable is still null (not defined) as there isn't a point in the code, where interf is assigned an object. Similar to your vlc variable you need to assign an object to the interf variable before calling the method. Depending on your code this could be as easy as assigning the object during declaration as you did with the vlc variable
private InterfaceKitPhidget interf = new InterfaceKitPhidget();
See the following code snippet for a simplified version of the problem in your code. Analog to your readSensorValue method in this case the testproc method throws a NullPointerException, because the String variable a is not defined.
public class test { static String a; /** * @param args */ public static void main(String[] args) { testproc(); } public static void testproc() { a.length(); } }
I hope this clarifies my earlier statement. To avoid such problems, always make sure, that a value or object is assigned to your variables before using them.
|
I need to make a really quick and simple website. What is the best tool available? I am talking about making a website that takes between 1-2 hours from scratch at most. Drag-and-drop stuff which then produces a .html document would be sweet.
|
On October 13 2011 01:45 KaiserJohan wrote: I need to make a really quick and simple website. What is the best tool available? I am talking about making a website that takes between 1-2 hours from scratch at most. Drag-and-drop stuff which then produces a .html document would be sweet.
Wordpress offers pretty good starting points, where you can change it almost entirely from a blog to anything you want.
|
I have a hard time understanding recursion. Could someone please explain what it is/what it does?
|
On October 13 2011 01:55 Butterednuts wrote:Show nested quote +On October 13 2011 01:45 KaiserJohan wrote: I need to make a really quick and simple website. What is the best tool available? I am talking about making a website that takes between 1-2 hours from scratch at most. Drag-and-drop stuff which then produces a .html document would be sweet. Wordpress offers pretty good starting points, where you can change it almost entirely from a blog to anything you want.
Is there any other tools that you can work with offline?
|
On October 13 2011 01:58 Necosarius wrote: I have a hard time understanding recursion. Could someone please explain what it is/what it does? A recursive function might call itself with different parameters to compute its return value, that's all.
For example, factorial calculation:
public class Main {
public static void main (String[] args) { System.out.println(factorial(5)); }
public static int factorial (int n) { if( n == 0 ){ return 1; } return n * factorial(n - 1); }
}
Another common example, calculating the Fibonacci number:
public class Main {
public static void main (final String[] args) { System.out.println(fibonacci(7)); }
public static int fibonacci (int n) { if( n == 0 ){ return 0; } if( n == 1 ){ return 1; } return fibonacci(n - 1) + fibonacci(n - 2); }
}
|
On October 13 2011 01:58 Necosarius wrote: I have a hard time understanding recursion. Could someone please explain what it is/what it does? Recursion is a general idea for creating methods. A recursive method has a simple answer for some base case, and calls itself for any other cases. The recursive calls generally try to whittle the problem down to a base case, then merge the returns of each call.
The Fibonacci and the factorial example, while being relatively easy to understand, are computationally very slow. There are much faster algorithms that don't use recursion.
|
On October 13 2011 01:58 Necosarius wrote: I have a hard time understanding recursion. Could someone please explain what it is/what it does?
Recursion just means a method calls itself until it reaches some sort predetermined value. In this case, the method calls itself until it reaches zero. Since it calls itself there's only ever one of it, so it's static.
public class Main { public static void main (final String[] args) { GetToZero(7); }
public static void GetToZero (int n) { if(n == 0) { System.out.println("Reached Zero"); } else if (n > 0) { System.out.println("Subtracting 1 - " + n); n--; GetToZero(n); } else if (n < 0) { System.out.println("Adding 1 - " + n); n++; GetToZero(n); } } }
|
On October 13 2011 01:58 Necosarius wrote: I have a hard time understanding recursion. Could someone please explain what it is/what it does?
First thing to recognize: programming is problem solving by decomposition. Given a problem, you decompose that problem repeatedly into smaller problems until the subproblems are manageable to solve on their own. For example, the problem of cooking spaghetti involves several subtasks such as buying the ingredients, preparing the sauce, preparing the meat, and the mixing it all together. In code, you typically use functions/methods to realize this decomposition, e.g.,
void cookSpaghetti() { buyIngredients(); prepareSauce(); prepareMeat(); mixIngredients(); }
With that in mind, recursion is a problem solving technique in which you recognize that the subproblems that you create are actually smaller versions of the original problem that you are trying to solve.
To make this more concrete, imagine a line of folk waiting for the release of heart of swarm:
front <(^^)> <(^^)> <(^^)> back
Now imagine that the person in the back of line is trying to figure out what place they are in line. One thing they can do is step out of line and count the number of people in front of them directly. However, instead of risking losing their place in line, they can ask the person in front of them:
front <(^^)> <(^^)> <(^^)> back |-- "What place are you in line?"
Now, the person in front of them is posed with a similar question, so they can ask the person in front of them the same question:
front <(^^)> <(^^)> <(^^)> back | |-- "What place are you in line?" |-- "What place are you in line?"
Finally the question is posed to the person in the front of the line. He's know he's #1, so he tells the person behind him.
front <(^^)> <(^^)> <(^^)> back | | |-- "What place are you in line?" 1 |-- "What place are you in line?"
With that answer, the person behind the first in line now knows what place they are in line. They know they're second by adding one to the place of the person in front of them.
front <(^^)> <(^^)> <(^^)> back | | |-- "What place are you in line?" 1 2
And finally the person in the back knows their place in line given the position of the person in front of them by adding one.
front <(^^)> <(^^)> <(^^)> back | | | 1 2 3
This is the essence of simple recursion:
- A base case that handles the situation when the problem we're trying to solve is directly solvable.
- A recursive case that solves the current problem by solving an identical subproblem of a smaller size.
Let's extend this to a code example that people have mentioned already, factorial(n).
int factorial(int n) { // Base case, i.e., front of the line: 0! = 1 if (n <= 0) { return 1; // Recursive case, i.e., the middle or the line: n! = n * (n-1)! } else { return n * factorial(n-1); } }
|
I'm having a problem getting this chunk of code to work, I cannot seem to get the array from function int main() to pass correctly to int show()...
The compile has no problem with it, but for some reason it is not outputting anything. Please point out my mistake.
+ Show Spoiler + #include <iostream> using namespace std;
const int MAXELS = 9; void show(int [MAXELS]);
int main () { double temps[MAXELS] = {6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8, 10.0}; return 0; }
void show(int temps[MAXELS]) { int i; for (i = 0; i < MAXELS; i = (i + 1))
cout << temps[i] << endl; return;
}
NEVERMIND FOUND MY ERROR!!! :DD
|
Well you don't call show from main.
|
Yay! I finally have a use for this thread. Taking a Computer Science course in C (C++ next term ^^).
All the numbers to be entered will have two decimal places, and the total of the numbers entered won't be greater than 1 000 000.00 . I have to split this total into equal parts and print out the integer total and the cents since this is formatting for money.
My problem is that the precision of the numbers is really terrible so entering 999 999.99 three times and dividing it equally gives me 999 999.98 for some reason. I'm using double which is supposed to be more precise than float... so I'm confused when stuff like this comes out.
Any suggestions on how to preserve precision when working with large decimal numbers?
I'm also outputting with printf and %f. It's not very clear to me what's the best way to output decimal numbers or store them.
|
Very simply. Never use floating point numbers (doubles count) for money. :-)
(I'm not really kidding either. That really is something that basically no one should ever do.)
|
On October 13 2011 15:25 EvanED wrote: Very simply. Never use floating point numbers (doubles count) for money. :-)
(I'm not really kidding either. That really is something that basically no one should ever do.)
What should I use for money then? The input has two decimal places... ?
I don't remember getting these kinds of errors in Python, but I'm using C now, and the lesson is supposed to be learning how to deal with the doubles I think...
|
On October 13 2011 17:16 Blisse wrote:Show nested quote +On October 13 2011 15:25 EvanED wrote: Very simply. Never use floating point numbers (doubles count) for money. :-)
(I'm not really kidding either. That really is something that basically no one should ever do.) What should I use for money then? The input has two decimal places... ? I don't remember getting these kinds of errors in Python, but I'm using C now, and the lesson is supposed to be learning how to deal with the doubles I think...
Floating point numbers have problems with precision. On the first look they work, but then you end up having customers pay only 0.99 cent instead of 1 euro.
For money you have to use a fixed point type (or in C you usually calculate with cents instead of euros/dollars) as they have exact instead of approximate representations.
|
Dennis Ritchie dies at age 70.
A quote from another forum:
#include <stdio.h>
int main(void){ printf("goodbye world :( \n"); return(70); }
each of us has a return value, but only very few of us are able to know what the return value we pass back is when our run-time ends and we hand ourselves over to the system. - TheFloorIsNowBacon
|
United States17233 Posts
Monetary institutions do track decimals beyond the hundredths place in transaction. The best way is to use a variable that actually has precision and then display it by formatting however is appropriate to the organization (I'm not really sure if they truncate or round, tbh).
|
|
|
|