Java
Post Reply
Quote
Re: Java
Posted by Windows 98 on Fri Mar 17th at 8:57pm 2006


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

? quote:
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

? quote:
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.






Quote
Re: Java
Posted by Captain P on Fri Mar 17th at 9:15pm 2006


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.





Quote
Re: Java
Posted by wil5on on Fri Mar 17th at 10:26pm 2006


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



Quote
Re: Java
Posted by Crono on Sat Mar 18th at 1:33am 2006


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 image



Blame it on Microsoft, God does.



Quote
Re: Java
Posted by omegaslayer on Sat Mar 18th at 3:38am 2006


? quote:
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?






Quote
Re: Java
Posted by Crono on Sat Mar 18th at 3:41am 2006


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.



Quote
Re: Java
Posted by wil5on on Sat Mar 18th at 4:17am 2006


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



Quote
Re: Java
Posted by Crono on Sat Mar 18th at 5:01am 2006


He is using integers image

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 image [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.



Quote
Re: Java
Posted by Windows 98 on Sat Mar 18th at 5:23am 2006


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.






Quote
Re: Java
Posted by wil5on on Sat Mar 18th at 5:39am 2006


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



Quote
Re: Java
Posted by omegaslayer on Sat Mar 18th at 7:08am 2006


? quote:
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 >_>.






Quote
Re: Java
Posted by Forceflow on Sat Mar 18th at 11:01am 2006


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



Quote
Re: Java
Posted by Captain P on Sat Mar 18th at 10:04pm 2006


? quote:
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?






Quote
Re: Java
Posted by ReNo on Sat Mar 18th at 10:14pm 2006


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 <img src=" SRC="images/smiles/icon_smile.gif">





Quote
Re: Java
Posted by Crono on Sat Mar 18th at 10:26pm 2006


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. image 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.



Quote
Re: Java
Posted by Windows 98 on Sat Mar 18th at 11:24pm 2006


Yes i made it formula rather MappingFormula.forumula. I have been just used to typing in the both






Quote
Re: Java
Posted by Orpheus on Sat Mar 18th at 11:51pm 2006


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.





The best things in life, aren't things.



Quote
Re: Java
Posted by Crono on Sun Mar 19th at 12:09am 2006


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 <img src=" SRC="images/smiles/icon_smile.gif">

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.



Quote
Re: Java
Posted by Orpheus on Sun Mar 19th at 1:17am 2006


? quoting Orpheus
You guys are simply amazing.

I meant it.





The best things in life, aren't things.



Quote
Re: Java
Posted by fraggard on Sun Mar 19th at 3:36am 2006


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.







Post Reply