Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ComptimusPrime
Jan 23, 2006
Computer Transformer
Hey guys, I need some help with some code I am writing for my intro to programming class.

The problem involves analyzing the vertical position of a cannonball. We are supposed to take the input of the initial velocity (and for the purposes of this assignment I guess we are not paying attention to angles and for all intents and purposes the cannon is firing straight up, the physics don't really matter). We are supposed to find its position every 1/100th of a second, and print to the screen its position every one second. I have gotten everything down to where it will print out EVERYTHING but cannot get it to print just every one second. So here is the code I have currently.

code:
import java.util.*;

class CannonBall

{ public static void main(String[]args)
	{Scanner in = new Scanner(System.in);


double deltaT=0.01 , s=0 , so = 0 , T= 0, g = 9.8;

System.out.print("We're simulating a cannon ball flight today - Please enter the initial speed of the canon ball (in meters/sec) : ");

double v = in.nextDouble();

double Vo = v;

while(s>=0)

{

	s = s + v * deltaT ;

	v = v - g * deltaT ;

	T=T+deltaT;

	if(T%1==0)
	System.out.println("The cannonball is at " +s);
}
}
}
I introduced the variable T and set it equal to itself + deltaT (which equals 0.01). And I figured that whenever T was equal to a whole number (and thus a whole second) it would be divisible by 1 with no remainder. So the if statement (and I'm obviously wrong here) should make it print out the position "s" at every 1 second interval. However, every time I do this the program just ends with no printout. Can someone help me see what I'm doing wrong here?

PS
I hope I didn't break any rules for the forum or anything. I tried to get in to the right area and don't have enough time right now to read all of the full rules. Sorry in advance if I screwed up.

Adbot
ADBOT LOVES YOU

ComptimusPrime
Jan 23, 2006
Computer Transformer

rjmccall posted:

Much like decimal arithmetic isn't exact when dividing by numbers that aren't powers of 10 (or factors thereof), binary floating-point arithmetic is not exact when representing numbers that can't be represented as an integer multiplied by a power of 2. Thus adding .01 to 0 100 times is not guaranteed to produce exactly 1.

The easiest way to solve this is to count iterations using a separate, integer counter, and just print out every 100 iterations.

Thank you. That did the trick. And I'm sure that tidbit will help me a whole hell of a lot in the future.

ComptimusPrime
Jan 23, 2006
Computer Transformer

MEAT TREAT posted:

Just so that you know, if you really do need that kind of precision in your calculations checkout the BigDecimal class.

Thanks, I appreciate it. I don't quite need that much precision. It just said to use double precision fps so I went with it. I am not quite sure why you would want them in a problem like this where the actual level of accuracy doesn't extend beyond 0.1 with a nice estimated hundredth. Of course, they have not done anything in our book with single precision fps. I imagine it is just to keep it simple.

ComptimusPrime
Jan 23, 2006
Computer Transformer

mustermark posted:

I've only started to get into Java programming for work, and I gotta say that JDBC makes me physically ill and depressed. Are there any good modern guides on it, or am I stuck with having to write twenty lines of ps.setHate(..., "Fury");?

If you are already familiar with SQL then start at the official tutorial.

http://download.oracle.com/javase/tutorial/jdbc/

JDBC is (can be) very easy to use as long as you already have a decent understanding of DBMS.

ComptimusPrime
Jan 23, 2006
Computer Transformer
I believe he really meant == 0 or < 1.

ComptimusPrime
Jan 23, 2006
Computer Transformer

MrZig posted:

What's with Java and null pointer exceptions, even though I check to see if the object is a null?

Ie check this code out:

code:
if (selected != null && mouseButton == 1 && (leftMouseClickPosition.x != mousePosition.x || leftMouseClickPosition.y != mousePosition.y))
{
 if (!selected.getDragging() && mousePosition.distance(Camera.getRelativex(selected.getDragArrowPoint().x),Camera.getRelativey(selected.getDragArrowPoint().y)) <30)
  {
   selected.setDragArrowMoving(true);
  }
{
It gives me a Null Pointer Exception on the second if line. Camera is a class being accessed in a static way, and the only object is selected yet I've specifically checked to see if it's null or not before using it. Any ideas?

Also, is there any way to go back to previous errors in Eclipse? Every time I run a new build it erases the old log.

How exactly are you initializing Camera? I don't want to make any assumptions, but you are saying you are trying to access Camera in a static way...

I am assuming when you call Camera.getRelativex(int x) you are doing some conversion between the camera position and the position that you pass it. When are you initializing the Camera? I am guessing that may be where your problem lies.

ComptimusPrime
Jan 23, 2006
Computer Transformer

Doctor w-rw-rw- posted:

Play framework is a really cool non-Servlet app framework. Klout and some other companies use it in production and its Scala support was good enough for Typesafe to anoint it officially as...something. I forgot. But its Java bindings are great, and I find it a lot of fun.

Agreed, the Play framework is easily the best framework I have worked with. It is so good that it makes me sad when I have to go to work and use ASP.

ComptimusPrime
Jan 23, 2006
Computer Transformer

I am in posted:

For one, it uses Object-Graph Navigation Language (OGNL), which makes it easy to parse input to Java objects and get values from Java objects using strings like "session.user.name.firstName". OGNL can also parse and execute arbitrary Java code.

Yep, nothing can go wrong with that.

I think it is worth adding that there is no point picking up something like Struts right now if you are starting from scratch. The world of web development has largely moved on.

Struts can be used to create a great website that is secure and full featured, but there is no point to using it. It isn't higher performing that other frameworks, it isn't more inherently secure, and most importantly, it is not as easy to create things with as one of the more modern frameworks.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Creating a new Scanner will likely work.

I would really recommend that you take a look at InputStreamReader or FileReader.

Each of these will do what you need them to do, and you can call reset() to go back to the beginning of the file. Not as friendly as Scanner, but it is the more correct way of handling the problem.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Are they loading and just not showing? You may need to apply some sizing info to the img tags there.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Regarding the img tags.

First, I would recommend opening up the developer tools in chrome, or installing firebug in ff, and looking at the errors that are being logged.

The first thing you need to find out is if it is loading or not. And if it is logging a not found then you know at least that much.

Also, I am not familiar really with tomcat, but I would make sure that requests for /src/images are really serving that directory.

Also, I don't think this is likely the case, but if you are using XHTML, then you need to get that img tag to be lower case.

ComptimusPrime fucked around with this message at 08:57 on Feb 6, 2012

ComptimusPrime
Jan 23, 2006
Computer Transformer

Sinestro posted:

I am a Python programmer, and I need to learn semi-basic Java. All the books I see online are really old, designed for people that have no idea how to program, and/or designed for people who are already know Java to hone their skills.

What do you mean by basic? Core Java is a pretty good place to go if you want something that will get you up to speed. Just skip the parts you don't need.

ComptimusPrime
Jan 23, 2006
Computer Transformer

fletcher posted:

code:
List<String> myList = new ArrayList<String>();
myList.add("value 110");
myList.add("value 1");
myList.add("value 50");
myList.add("value 5");

Collections.sort(myList);

System.out.println(StringUtils.join(myList, ","));
This spits out "value 1,value 110,value 5,value 50". How can I get it to sort it as "value 1,value 5,value 50,value 110"?

Well... When you are doing something like this you could just change the code to...

code:
List<Integer> myList = new ArrayList<Integer>();

myList.add(110);
myList.add(1);
myList.add(50);
myList.add(5);

Collections.sort(myList);

String myString = "value " + StringUtils.join(myList, ", value ");
System.out.println(myString);

ComptimusPrime
Jan 23, 2006
Computer Transformer

Newf posted:

Does day-to-day programming ever get over this kind of boneheadedness? I'm sure that I spend half of my 'coding time' on these types of errors.

Yes and no. They will become less frequent but there will always be times when something obvious becomes incredibly difficult due to either being tired, a cache bug, or something else miniscule that will cost you a couple of hours.

ComptimusPrime
Jan 23, 2006
Computer Transformer

Sedro posted:

Crappy plugins drove me away from eclipse.


In eclipse the build process was:
1. Make change
2. Maven build
3. Refresh all projects
4. Clean all projects
5. Clean maven
6. Clean web app
7. Clean TomCat working directory
8. Some other "clean" options I forgot?
9. Start server
10. Server didn't start? goto 2
11. Server started but I can't see the change at runtime? goto 2

After switching to netbeans:
1. Make change
2. Build and run

They made the assumption that no third-party tool (including their own plugins) will ever modify the workspace. Since that's obviously not true, they added a million "clean" options to compensate.

Process for merging conflicts with Git and Netbeans:

Close Netbeans.
Start merge process.
Merge all conflicts.
Add/commit changes.
Finish pull.
Open Netbeans.
Netbeans now thinks you have duplicate class files.
Bang head against wall until finding Netbeans hidden cache folder.
Delete cache folder.

Repeat process for every merge until you remember Netbeans cache folder.

ComptimusPrime
Jan 23, 2006
Computer Transformer
I don't use the IDE for source control. I use git proper. And NetBeans does not like the way it handles merging files.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Scala IDE 2.0.0 was just released. You should probably check it out.

ComptimusPrime
Jan 23, 2006
Computer Transformer

thegasman2000 posted:

I have been given a nice problem to solve and have no idea what I am going to do... I need to us the JustGiving Javascript API to enable functionality to a flash app. Is there a way for me to utilise JS in actionscript 3? Here is a link to the API if anyone fancies a look. https://api.justgiving.com/docs

The project is for a major charity who have zero technical people to help me out, so its goon power or nothing.

Perhaps you will have more luck asking about this in the JavaScript thread.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Think about using a temporary in that leave method.

ComptimusPrime
Jan 23, 2006
Computer Transformer
You're not even addressing his problem. You're suggesting a beefy architectural style to solve his inability to reason about how to both set the person to null and also return the person in the leave method.

ComptimusPrime
Jan 23, 2006
Computer Transformer

pigdog posted:

Any particular reason you wouldn't just use LinkedList? That is, if you really need a linked list in the first place, as opposed to normal ArrayList.

He is in an intro to data structures class. I am pretty sure.

ComptimusPrime
Jan 23, 2006
Computer Transformer
readLine will only return once it encounters a new line (which happens in the terminal when you press enter). New lines are not automatically entered in to an output stream unless you have a method called something like "writeLine". BufferedWriters have a method "newLine", which you can call after each write. Don't close the socket and reopen it. Hopefully this will get you on your way.

ComptimusPrime
Jan 23, 2006
Computer Transformer
You can do master slave to break down the 4 sections. But if you want to make it become truly scalable to as many cores as are present in the system then it will be a bit more complex of an algorithm.

One question would be this: Does each node only care about where the other nodes are at the beginning of each calculation? At the end? Or at some point in between? (e.g. if node 1 carries out the distance computation, does the result effect node 2?)

I would first write it out linearly and then look for sections of the code that can be broken out in to separate tasks.

ComptimusPrime
Jan 23, 2006
Computer Transformer
Are you buy chance trying to carry this out on the root drive of your Windows install (a.k.a C:\)?

If so, I suggest placing the file in your User directory and trying to run your program again.

I also suggest you trim the ends off of that input.

Edit:

Also, if you want, google "java FilePermission".

ComptimusPrime fucked around with this message at 02:37 on Apr 26, 2012

Adbot
ADBOT LOVES YOU

ComptimusPrime
Jan 23, 2006
Computer Transformer
Firstly, you are decrementing front after you take its modulus.

Secondly, this

code:

s=d[front];

I assume is reversed.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply