Java

Java

Re: Java Posted by Windows 98 on Fri Mar 17th 2006 at 8:57pm
Windows 98
757 posts
Posted 2006-03-17 8:57pm
757 posts 86 snarkmarks Registered: Apr 25th 2005 Occupation: Student Location: USA
Ok, for my mapping team to calculate the price of a map, so I wrote an
bassic Javaprogram I want them to run off of their Command Line. I want
to be able to have them just click a file (A JAR or BAT) and then have
the command line pop up and let it go. So I wrote this
import java.util.Scanner;

public class MappingFormula {

//Mapping formula calculation for a custom map.

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

MappingFormula.formula();

}

public static void formula(){

Scanner keyboard = new Scanner(System.in);

int sizeW = 0;

int sizeL = 0;

int sizeH = 0;

double price = 0;

int levelOfDetail = 0;

int levelOfDetailPrice = 0;

double time = 0;

double timeOff = 0;

System.out.println("Enter the map width: ");

sizeW = keyboard.nextInt();

System.out.println("Enter the map length: ");

sizeL = keyboard.nextInt();

System.out.println("Enter the map height: ");

sizeH = keyboard.nextInt();

int size = (sizeW/2048*5 + sizeL/2048*5 + sizeH/2048*5);

price = size;

System.out.println(price);

System.out.println("Enter the level of detail, Low = 1, Medium = 2, High = 3)");

levelOfDetail = keyboard.nextInt();

if (levelOfDetail == 1)

{

levelOfDetailPrice = size+size/300;

price = levelOfDetailPrice + price;

}

if (levelOfDetail == 2)

{

levelOfDetailPrice = size+size/200;

price = levelOfDetailPrice + price;

}

if (levelOfDetail == 3)

{

levelOfDetailPrice = size+size/150;

price = levelOfDetailPrice + price;

}

time = price*.25+10;

price = time + price;

System.out.println("Your base price of the map is " + price + " with a default time limit");

System.out.println("Enter the number of days you want taken off");

timeOff = keyboard.nextInt();

price = timeOff+price;

System.out.println("The Final Price Is: " + price);

}

}
Now, that works fine and dandy in the program writer (Eclipse). But when I made a batch file that was
cd C:\

java MappingFormula
It should open the thing if i just put the Class file in the C:. So I
did, and then I tried opening it elsewhere. But anytime I try to open
it I get an error

"Exception in thread "main" java.lang.NoClassDefFoundError: MappingFormula"

Any ideas why?

Also, if someone can help me make a JAR file that someone can just
click and have the program run in the command line thatd be awesome.
http://img362.imageshack.us/img362/8521/windows981dk.jpg

Nickelplate is my dad
Re: Java Posted by Captain P on Fri Mar 17th 2006 at 9:15pm
Captain P
1370 posts
Posted 2006-03-17 9:15pm
1370 posts 1995 snarkmarks Registered: Nov 6th 2003 Occupation: Game-programmer Location: Netherlands
I'm sure there's some good tutorials on jarring Java classes around. What I personally found more interesting is that there are programs that allow you to package it all into an .exe. You may want to look for that rather than hassling with batch files.
Create-ivity - a game development blog
Re: Java Posted by wil5on on Fri Mar 17th 2006 at 10:26pm
wil5on
1733 posts
Posted 2006-03-17 10:26pm
wil5on
member
1733 posts 570 snarkmarks Registered: Dec 12th 2003 Occupation: Mapper Location: Adelaide
I think for that bat file to work, youd have to make it go to the folder where the .class files are (presumably you dont put them in the root directory). So it would be something like:

cd C:\java\
java MappingFormula

That said, you should probably put it all in a .jar for simplicity. Theres a command-line program for doing this that comes with the JDK, but what its called and how to use it I dont know. Like P said, look for tutorials.
"If you talk at all during this lesson, you have detention. Do you understand?"
  • My yr11 Economics teacher
Re: Java Posted by Crono on Sat Mar 18th 2006 at 1:33am
Crono
6628 posts
Posted 2006-03-18 1:33am
Crono
super admin
6628 posts 700 snarkmarks Registered: Dec 19th 2003 Location: Oregon, USA
You should look into switch statements.

If you're already in the current class don't you use the "this" pointer? Or just call the func ... I mean method, directly.

You could say "this.formula();" or "formula();" instead of "MappingFormula.formula();" ?

I'm not sure, since I haven't used Java in ... 2 years ... Monkey works with it on a daily basis though (I think), he could help if he actually still lurks around.

But, from the look of it, it looks like scope is the issue ...

I'm a C/C++ guy myself. Java is nice for multiplatform stuff though. Oh, and people who refuse to manage their own memory :rolleyes:
Blame it on Microsoft, God does.
Re: Java Posted by omegaslayer on Sat Mar 18th 2006 at 3:38am
omegaslayer
2481 posts
Posted 2006-03-18 3:38am
2481 posts 595 snarkmarks Registered: Jan 16th 2004 Occupation: Sr. DevOPS Engineer Location: Seattle, WA
You should look into switch statements.
Im not into Java, im just learning C++, but arent switch statements bad? (or maybe I should say obsolete)

Anyways I read it, and what is "price" exactly? FPS?
Posting And You
Re: Java Posted by Crono on Sat Mar 18th 2006 at 3:41am
Crono
6628 posts
Posted 2006-03-18 3:41am
Crono
super admin
6628 posts 700 snarkmarks Registered: Dec 19th 2003 Location: Oregon, USA
Obsolete? No, they're for the purpose of replacing many if-else statements that check one variable against several values.

More efficient, easier to program and read.

Postfix incrementing is bad (for general purpose), but people use that more than pre-fix :\
Blame it on Microsoft, God does.
Re: Java Posted by wil5on on Sat Mar 18th 2006 at 4:17am
wil5on
1733 posts
Posted 2006-03-18 4:17am
wil5on
member
1733 posts 570 snarkmarks Registered: Dec 12th 2003 Occupation: Mapper Location: Adelaide
Switch statements would work in this case, but I dont like them in general since they only accept integral values (int, long, char etc, theyd be much more useful if they accepted strings).

I didnt look at your code before since thats not the problem, but Crono is right, you only need to say formula(); in the main method. I've been using Java for a year now as part of my uni degree, it does what it does fairly well.
"If you talk at all during this lesson, you have detention. Do you understand?"
  • My yr11 Economics teacher
Re: Java Posted by Crono on Sat Mar 18th 2006 at 5:01am
Crono
6628 posts
Posted 2006-03-18 5:01am
Crono
super admin
6628 posts 700 snarkmarks Registered: Dec 19th 2003 Location: Oregon, USA
He is using integers :razz:

I noticed some things with your calculations (ignoring your types being wrong half the time.)

If the map's height, width, or length is below 10240 units (I assume that's the measurement) then you will get 0. Period. You want to ceiling that function, so the price will be at least 1. Ceiling this function will also eliminate large number trails after the decimal.

And, yeah ... what is "the price" exactly?

I wrote this up in C++ (~5 minutes) it's online here. Command line only, of course :smile: [edit]: Apparently, it's unix only too. Don't bother downloading it, look at the code if you want, the same rules (mostly) apply in Java.
Blame it on Microsoft, God does.
Re: Java Posted by Windows 98 on Sat Mar 18th 2006 at 5:23am
Windows 98
757 posts
Posted 2006-03-18 5:23am
757 posts 86 snarkmarks Registered: Apr 25th 2005 Occupation: Student Location: USA
I'm not here to discuss which way is more efficient, because i rally
dont care. I'm looking how I can make this run the command line without
that damn error! I've had programs run off the command line fine, but
this is being stupid for some reason.
http://img362.imageshack.us/img362/8521/windows981dk.jpg

Nickelplate is my dad
Re: Java Posted by wil5on on Sat Mar 18th 2006 at 5:39am
wil5on
1733 posts
Posted 2006-03-18 5:39am
wil5on
member
1733 posts 570 snarkmarks Registered: Dec 12th 2003 Occupation: Mapper Location: Adelaide
Crono: I know hes using integers, thats why I said switch would work in this case.

98: Have you tried changing the cd command like I said?
"If you talk at all during this lesson, you have detention. Do you understand?"
  • My yr11 Economics teacher
Re: Java Posted by omegaslayer on Sat Mar 18th 2006 at 7:08am
omegaslayer
2481 posts
Posted 2006-03-18 7:08am
2481 posts 595 snarkmarks Registered: Jan 16th 2004 Occupation: Sr. DevOPS Engineer Location: Seattle, WA
Obsolete? No, they're for the purpose of replacing many if-else
statements that check one variable against several values.

More efficient, easier to program and read.

Postfix incrementing is bad (for general purpose), but people use that more than pre-fix :\
I suppose its useful if you want to execute separate actions based off
of exact values (like x == 1 && x == 2). if/else would be for
ranges correct? (like x >= 1 && x <= 5).

/still learning when/when not to use stuff.

BTW I get marked down if I use switch statements in my class >_>.
Posting And You
Re: Java Posted by Forceflow on Sat Mar 18th 2006 at 11:01am
Forceflow
2420 posts
Posted 2006-03-18 11:01am
2420 posts 451 snarkmarks Registered: Nov 6th 2003 Occupation: Engineering Student (CS) Location: Belgium
Try exporting it to Jar, there must be such functionality.
Your code looks ok, and if it compiles well, there shouldn't be any problems running it.

You're not declaring any fields, which might be odd, but shouldn't keep the program from running.

Or this:

MappingFormula.formula()

You're already in the class MappingFormula, so there's no need to do an external method call. Shouldn't this be:

formula()
:: Forceflow.be :: Nuclear Dawn developer
Re: Java Posted by Captain P on Sat Mar 18th 2006 at 10:04pm
Captain P
1370 posts
Posted 2006-03-18 10:04pm
1370 posts 1995 snarkmarks Registered: Nov 6th 2003 Occupation: Game-programmer Location: Netherlands
Postfix incrementing is bad (for general purpose), but people use that more than pre-fix :\
Care to explain why it's worse or more specifically, the advantages/disadvantages of both?
Create-ivity - a game development blog
Re: Java Posted by ReNo on Sat Mar 18th 2006 at 10:14pm
ReNo
5457 posts
Posted 2006-03-18 10:14pm
ReNo
member
5457 posts 1991 snarkmarks Registered: Aug 22nd 2001 Occupation: Level Designer Location: Scotland
I believe it has to do with postfix needing to copy the data to a new register and incrementing that (then copying back), because the very point of postfix is that the operation is only carried out after the data is used. Prefix increments before using the data, so it just increments the data where it is rather than making any copies of it. I'm sure Crono will come along and debunk / correct my theory soon enough though :smile:
[img]http://card.mygamercard.net/sig/Default/reno84.png[/img]
Designer @ Haiku Interactive | ReNo-vation.net
Re: Java Posted by Crono on Sat Mar 18th 2006 at 10:26pm
Crono
6628 posts
Posted 2006-03-18 10:26pm
Crono
super admin
6628 posts 700 snarkmarks Registered: Dec 19th 2003 Location: Oregon, USA
Windows, your program doesn't give a decent output, that's what I was pointing out. Anything below 10240 and the program gives 0 as the price. :rolleyes: If it's a "price" as implying it's something that'd cost you something valuable, you want to round up. As for the way you wrote the program, it was just a little sloppy, that's all. There's not really a way you could know that if you're just starting out unless someone tells you.

Did you try changing the method call, since that's been suggested about 5 times now?

Reno, no, that's exactly right.

If you want to know why is makes three copies, do some research into operator overloading. (Since to overload the operator's you have to have the same declaration structure)

Note*: The function uses pass by value and return by value and makes a copy in the function body too!

Also, the crappy thing is, if it's a higher language, there's a chance it DOESN'T copy to a register, but memory.
Blame it on Microsoft, God does.
Re: Java Posted by Windows 98 on Sat Mar 18th 2006 at 11:24pm
Windows 98
757 posts
Posted 2006-03-18 11:24pm
757 posts 86 snarkmarks Registered: Apr 25th 2005 Occupation: Student Location: USA
Yes i made it formula rather MappingFormula.forumula. I have been just used to typing in the both
http://img362.imageshack.us/img362/8521/windows981dk.jpg

Nickelplate is my dad
Re: Java Posted by Orpheus on Sat Mar 18th 2006 at 11:51pm
Orpheus
13860 posts
Posted 2006-03-18 11:51pm
Orpheus
member
13860 posts 2024 snarkmarks Registered: Aug 26th 2001 Occupation: Long Haul Trucking Location: Long Oklahoma - USA
I wish that I knew more about Java than simply drinking the stuff. It would be nice to know how to take a good cup of Jo and making a web page outa the stuff.

You guys are simply amazing. :eek:

The best things in life, aren't things.
Re: Java Posted by Crono on Sun Mar 19th 2006 at 12:09am
Crono
6628 posts
Posted 2006-03-19 12:09am
Crono
super admin
6628 posts 700 snarkmarks Registered: Dec 19th 2003 Location: Oregon, USA
Orph, that's JavaScript and even then it doesn't "make" the page, it just allows you to do some gritty calculations. That's a different language anyway, this is Java, it's a system programming language, not a browser programming language :smile:

Also, take into consideration that pretty much everyone who's responded has some educational and/or hobbyist background in this. (I should post up some Asm programs ... if I can find them) I'm working on some SML stuff now too for compilers. (It compiles the Mini-Java language, which is an sub-set of Java, just not everything ... since that'd be a pain in the ass. I don't know if we're writing the garbage collector and stuff next term or not, but it was mentioned)

Windows, There's a step that hasn't been done then. I could be wrong, but don't you have to have the JRE running for java programs to work? Is that running? Or do you have to define something in the program to make it run? I don't particularly remember, however, but just some things to look at.

Anyway, what error does it give once you take out the "MappingFormula" part? Same error? More information you give, the easier someone could recognize the problem and help you out.
Blame it on Microsoft, God does.
Re: Java Posted by Orpheus on Sun Mar 19th 2006 at 1:17am
Orpheus
13860 posts
Posted 2006-03-19 1:17am
Orpheus
member
13860 posts 2024 snarkmarks Registered: Aug 26th 2001 Occupation: Long Haul Trucking Location: Long Oklahoma - USA
Orpheus said:
You guys are simply amazing. :eek:
I meant it.

The best things in life, aren't things.
Re: Java Posted by fraggard on Sun Mar 19th 2006 at 3:36am
fraggard
1110 posts
Posted 2006-03-19 3:36am
fraggard
member
1110 posts 220 snarkmarks Registered: Jul 8th 2002 Occupation: Student Location: Bangalore, India
Win98: What does your CLASSPATH contain? The last entry in the
environment variable CLASSPATH should be for the current directory,
i.e. "." .

Just to check, run the program with

c:\>java -classpath . MappingFormula

If it works then you know where the problem is.
Re: Java Posted by Windows 98 on Sun Mar 19th 2006 at 4:39am
Windows 98
757 posts
Posted 2006-03-19 4:39am
757 posts 86 snarkmarks Registered: Apr 25th 2005 Occupation: Student Location: USA
Thanks everyone, but fraggard is heling me over ICQ, and for the most part is helping ALOT.
http://img362.imageshack.us/img362/8521/windows981dk.jpg

Nickelplate is my dad