1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Help a beginner with Java

Discussion in 'Programming & Webmastering' started by razaron, Jul 5, 2011.

  1. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    <Original question answered>

    Edit: I'll post more questions as I'm learning.
     
    Last edited by a moderator: Jul 5, 2011
  2. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    If you already have it working for classes why doesn't it work for javac? Maybe you just need to use something like "javac .\myfile.java"?

    All you should need to do is add the Java directory to the "path" system variable.
     
  3. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,461 (4.64/day)
    Thanks Received:
    3,264
    set the path for it i believe.
     
  4. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    How do you set the path for it? I've been reading around and am completely confused.
     
  5. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Another question.

    In the following code:
    is

    the method header

    the method body and
    the method call?
     
  6. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    Easiest way to do it is in System Properties (in Windows 7 you can use Start->Run->systempropertiesadvanced.exe).
    [​IMG]

    Then select Environment Variables and you'll see this window.
    [​IMG]

    Then click "Path" in the lower box and hit "Edit" and you'll get here:
    [​IMG]

    Then just add a semicolon and the path to your Java directory (with javac).
    I'm pretty sure that's correct, though my terminology is a bit rusty.
     
    Last edited: Jul 5, 2011
    razaron says thanks.
  7. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    I ignored path and thought I had to add SOURCEPATH :banghead:

    Edit: I've already done that what I meant was how do I set it (cmd) to know where my .java files are, not javac.
     
    Last edited: Jul 7, 2011
  8. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    Well it doesn't really work that way as far as I know, but it does work the other way.

    You have to type in the directory of your .java files at one time or another. The idea is usually to cd into the directory with your .java files and then all you need to do is type "javac myfile.java". You could also make a batch file which would simplify the process even further.
     
  9. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,461 (4.64/day)
    Thanks Received:
    3,264
    first off, not to be a dick, but you gotta correct your grammar. i had to read your question 3 times before i figured out what you were asking. better grammar means more people willing to help.

    a .java file is the source code. you run javac on the .java file to compile it into a class file which you then execute running the java <your java class file>.

    so you have to either be in the directory of your compiled java program or include the entire working directory.
     
  10. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    I just changed the default start directory for a shortcut of cmd. That solves that problem.

    I couldn't think of a better way to word it (otherwise I would've just googled it...)
     
  11. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    I popped my programing cherry.

    I'm going to practice with maths for now then do that archery thing Kreij mentioned in another thread.

    PS. Is it bad to have to regularly look up syntax?
     
  12. A Cheese Danish

    A Cheese Danish

    Joined:
    Nov 18, 2006
    Messages:
    2,963 (1.02/day)
    Thanks Received:
    413
    Location:
    your local vending machine
    Not at all for a beginner! :)
    I still have to look up syntax for some things. The more you do it, the easier it will become and the less you will have to reference
     
    razaron says thanks.
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.92/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    No raz, with 1000s of APIs and a jillion methods that are available in some of the modern programming languages, and the fact that with language revisions they will add/obsolete calls, you will be using references constantly.
     
    razaron says thanks.
  14. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    I was looking for java keyword definitions and found them on Wikipedia. The definition of "void" has me confused.
    Wikipedia defines void as, "The void keyword is used to declare that a method does not return any value.", if that's true how did the code I posted earlier return a value?:wtf:

    EDIT: Also, can any of you folks remember how long it took you to start using correct case letters (upper/lowercase) in code? That seems to be the commonest error I make.
     
    Last edited: Jul 7, 2011
  15. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,434 (1.30/day)
    Thanks Received:
    641
    Location:
    Geneva, FL, USA
    That's because it didn't return anything, unlike this (poor) example:

    Code:
    public int getTheNumberTen() {
    	return 10;
    }
     
  16. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Then would I be correct in assuming returning a value means "returning a value to the code"?



    Another question:
    The following code makes cmd say "Your target is x" where x is a value.
    However I want cmd to say "Your target is x meters away" where x is a value. The code I'm using to make this happen is,
    , which doesn't work/compile. What do I have to do to make it work/compile?
     
    Last edited: Jul 7, 2011
  17. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,434 (1.30/day)
    Thanks Received:
    641
    Location:
    Geneva, FL, USA
    Seems like you're missing a "+" after the x_target variable. You need it to concatenate " meters away" with the beginning portion.


    That's one way to describe it. It's going to return the result so you may do with it as you will.

    Ex: Lets say you have a function that returns a value describing how close a user got to the the target based on the angle, meters away, and the bow pull. It's something that you want to print out, but also save in an array so that the user can see their last ten moves or something like that.

    Code:
    proximity = getProximity(angle, meters, power)
    Now you can print out the variable proximity and store it.

    Note: Much more familiar with PHP, sorry if my Java syntax is off.
     
    razaron says thanks.
  18. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Is it better to have several small formulae,

    or one big formula,
    ?
     
  19. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    So far I have this for the archery game,

    How do I make it return to the bit just after it's worked out "x_target" if "d_target" > 1?
    Other than that it works, yay.

    EDIT: I can advertise this game as using real Newtonian physics, lol.
     
    Last edited: Jul 7, 2011
  20. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    Is this what you want?
    Code:
     . . .
    
    [B]double d_target=2;[/B]
    
     . . . 
    
    x_target = 1 + (Math.round(100 * Math.random() * 100)/100.0d);
    [B]while (d_target > 1) {[/B]
         System.out.println("Your target is " +x_target+ " meters away");
         System.out.println();
    
         System.out.println("How much power will you pull the bow with: ");
         P = input.nextDouble();
         System.out.println();
    
         System.out.println("At what angle will you shoot your arrow: ");
         theta = input.nextDouble();
         System.out.println();
         
         u = Math.sqrt((2 * P * t_bow) / m);
    
         t = (u * Math.sin(Math.toRadians(theta))) / 4.9;
    
         x = Math.round(t * u * Math.cos(Math.toRadians(theta)) * 100)/100.0d;
    
         s_target = x_target - x;
    
         if (s_target < 0){
              d_target = s_target * -1;
         }
         else{
              d_target = s_target;
         }
    
         if (d_target > 1){
              System.out.println("You were "+Math.round(d_target * 100) / 100.0d+" meters off");
         }
    [B]}[/B]
    System.out.println("Well done you hit the target");
    
     
    razaron says thanks.
  21. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    Couldn't get what you wrote to work. However that helped me learn the do-while thing, so thank you.:toast:

    Here's my awesome game:
     
    streetfighter 2 says thanks.
  22. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    What would be a logical step up from the archery game? Preferably something that doesn't require much imagination (so no text adventure).
     
  23. PopcornMachine

    PopcornMachine

    Joined:
    Aug 17, 2009
    Messages:
    1,563 (0.82/day)
    Thanks Received:
    459
    Location:
    Los Angeles/Orange County CA
    My advice is to learn Perl or Python or even C.

    I hate Java. It's buggy as hell.
     
  24. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    Seeing as how 99% of the world does not use console applications, I'd suggest making a GUI for your archery game. The structure of GUI code is fairly similar in most languages, meaning they're mostly event driven. Here's the (rather poorly written IMO) official tutorial.

    I'd create a simple window with a couple edit controls (text boxes) for inputting theta/P and a button to start the simulation. Then I'd plot the arrows path and show the origin and target.

    It sounds complicated but it's actually very easy.
     
    razaron says thanks.
  25. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.47/day)
    Thanks Received:
    177
    Location:
    london
    What am I doing wrong with this piece of code?
    I'm trying to make a 25 places big vector with each place containing an integer with the same value as the place itself (hence the (i, i)). Then it should display the 5th int in the vector.
    The errors:
    [​IMG]
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page