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
carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Yeah, the only time I've ever seen it is when a parameter and an instance variable have the same name, or an instance variable and a local variable. (edit: I guess a parameter is just a local variable, isn't it?)

code:
public class MyClass {
    int number;

    public MyClass(int number) {
        this.number = number;
    }
}
I'm sure with a bit of work you could always make sure the names are different. What's the consensus on which one is best?

Adbot
ADBOT LOVES YOU

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Scanner will throw an InputMismatchException if the input the user gives is not an integer. Make sure you put the catch for that ahead of the one for Exception, since all Exceptions are a subclass of Exception, and the InputMismatchException would just get caught as an exception.

http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Has anyone here worked with JavaFX? If so, what is it meant to be? I tried a tutorial that drew some blended circles and moved them, but I don't understand what it's really designed to do (well.)

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

String also has the valueOf() method which is overloaded to take any of the primitives and return a string representation.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

But the problem is that in the case of primitives you're still not getting the string representation of the object you're dereferencing, but of the parameter you're passing in. Probably just a case of use what's been used earlier in the project or use whichever you prefer if there's no precedent; certainly the JDK documentation doesn't comment on either or.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Harold Ramis Drugs posted:

I'm working on a program to calculate Pascal's Triangle up to 16 rows, and then print it out as a (mostly) perfect isosceles triangle. I'm having trouble coding a formula to do this cleanly though, and the only appearant solution I can think of is to write a method that will print each line of the triangle with unique directions for spacing. This is what I have so far:

code:
-snip-
Which prints this...

code:


                                1  
                              1  1  
                            1  2  1  
                          1  3  3  1  
                        1  4  6  4  1  
                      1  5  10  10  5  1  
                    1  6  15  20  15  6  1  
                  1  7  21  35  35  21  7  1  
                1  8  28  56  70  56  28  8  1  
              1  9  36  84  126  126  84  36  9  1  
            1  10  45  120  210  252  210  120  45  10  1  
          1  11  55  165  330  462  462  330  165  55  11  1  
        1  12  66  220  495  792  924  792  495  220  66  12  1  
      1  13  78  286  715  1287  1716  1716  1287  715  286  78  13  1  
    1  14  91  364  1001  2002  3003  3432  3003  2002  1001  364  91  14  1  
  1  15  105  455  1365  3003  5005  6435  6435  5005  3003  1365  455  105  15  1  
If you guys can give me any pointers on how to format my printing so that the correct numbers line up, I would be greatly appreciative. Thanks
In your output, look at the lines which seem to increase the length the most. What's different about the contents of these lines compared to the line above? Think of ways you can adjust for that difference. If you want to see how it's built, replace each space in your spacing println() statements with a different, visible character.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Have you looked at The Java Tutorials? From when I've come across them, they seem oriented at someone who is looking for examples of Java features but isn't necessarily completely new to programming.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You have to cast prime.data back to "Jobs" in order to get at its methods.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

According to that, the node's data is... a node. The next two lines indicate that there's a Job somewhere down the chain. What happens if you do prime.data.data.getClass()? Also, this indicates there may be a problem when these Jobs are being inserted.

carry on then fucked around with this message at 05:13 on Apr 23, 2012

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Hidden Under a Hat posted:

I can't find this folder anywhere. I'm using Mac OS X 10.6.

In Finder just hit CMD+Shift+G and type in ~/.netbeans, should take you right to it.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

edit: beaten :v: ^^^

Arguments made to the call to "java Calc" go in the args parameter of public static void main(String[] args), so if that's how you're doing it, the array is already made. That only works for starting the program; you can't do something like that if it's already running.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Worth noting that the forums now have syntax highlighting for new languages including Java. You just put "code=java" instead of "code" in the square brackets.

Java code:
public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        System.out.println("Hello Syntax Highlighting!");
    }
}
e: CHOO IS LORD

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Is there even a decent JavaFX UI designer? The tutorials I tried with it were all written by hand.

In other words, no, it's not going to be as good.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

There shouldn't be anything special, even the package declaration is a part of the Java language. I'm sure if you had some issues showing up in NetBeans that didn't happen in JGrasp we could help figure it out.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

nbzl posted:

I've also got a recursion based question:

code:
    public boolean find(String t) {
        if (text.length() <= 1) {
            return false;
        } else if (text.equals(t)) {
            System.out.println("Found it!")
            return true;
        } else if (text.length() > 1) {
            text = text.substring(0, (text.length() - 1));
            System.out.println(text + " " + t);
            find(t);
        } 
            return false;      
    }
This method seems to always return false even though I get the 'Found it!' to display. Is there something I'm missing here? I was under the impression that it would end the method and return the true value.

Think about what you'd have to do with the result of find() to punt the answer up the call stack.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

== only tests for reference equality. You'll want input.equals("File") for string equality.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Yeah but all the examples would use butts and farts instead of foos and bars.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You call setFocusable() on the JPanel, which allows the component to be focused, but does not focus it. Looking through the documentation, you will probably need to call requestFocus() on the same JPanel whenever the reset button is pressed.

http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#requestFocus()

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Are you creating a new project for each problem? If not, it thinks the new file you've started is going to be called from some other file. In Netbeans you can right-click on the file and choose Run, as long as it has a public static void main method.

It would probably be worthwhile teaching yourself how to compile and run java from the command line, as an IDE is probably overkill if you're just learning.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

The problem I have with BlueJ is that, unless you really have problems grasping object oriented fundamentals, it doesn't offer much that other IDEs don't while teaching you some processes that don't really exist outside of BlueJ.

That's not to say it's a bad program (I learned on it back in my first CS course) but I would just be aware that there really isn't anything like it in wide use.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Hand Model posted:

Oh, no I was creating new files within the same project. And yeah that works. Thanks!

I think compiling isn't too hard, but an IDE looks so much nicer! But fine, you're right, I should just practice doing everything.

I'm poo poo with computers, unfortunately.

I'm not saying you should drop Netbeans right now if it works for you, more that it sounds like you're working with smaller problems, and something as monolithic as Netbeans can seem to be in your way at times.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Class<C extends Class>?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

I don't know specifically, but in html don't you usually use <span> for that?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You can reassign a variable after it is created. z = input.next(); is valid even if z is already set to something else. After that statement, z will be set to the result of input.next(), which will be what the user input that time.

Think about how often you need to get input from the user compared to how often you check the value of that input in those if statements.

Also, it would probably help to clean that code up a bit:
Java code:
import java.util.Scanner;

public class project
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner( System.in );
        String z;        
        System.out.print("Enter a number between 1 and 100 to quit type a negative number: ");
        z = input.next( );
       
        Double x = Double.parseDouble(z);
        while( x > 0)
        {
            if ( x > 0  && x < 59)
            {
                System.out.print("you typed a number!");
            }
       
            if ( x > 59 && x < 100)
            {
                System. out.print("you typed another number");
            }
            if (x < 0 )
            {
                System.exit(1);
            }
        }
    }
}
Tell me, where else in this code should you be asking for input (and converting it!), do you think?

carry on then fucked around with this message at 22:06 on Sep 9, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

= makes the left hand side take the value of the right hand side. which of x or sortingArray[0] gets the value from the scanner?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Almost. The items in your third level are also classes, don't forget. An object would be an instance of any of those classes, so Knife is a class, but if you give a knife to the player you would make an instance of the Knife class which would have values for its member variables (and the member variables of its superclasses.) This is where it gets tricky, since I don't think Classes are considered objects in OOP, yet most programming languages will have some method of obtaining an object representing a class in the system (and even modifying that class.)

The next concept you will likely read about will be interfaces and, if the text is Java oriented, abstract classes, which you may find better fit your modeling of an RPG.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

supermikhail posted:

The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming :rolleyes:).

Pointless snark aside, if you're that worried about it, have backup names and check if a file exists by the first name and open it, and if it doesn't look like your file then choose a different name.

"Adult" apps probably use either the Preferences API or the Properties API, and the former seems like overkill for storing one list of paths, while the latter is usually placed relative to the working directory.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

KildarX posted:

Still learning the language so not really sure what to call things, thanks a lot.

Here's a list of all the operators in Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html You probably won't use some of these unless you're doing a lot of operations on raw bits, but it's good to have if you every come across a use of >>> and want to know what it does.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You're on the right track with creating a method to search for a words, but I think it needs to encapsulate a simpler functionality than to check for all words. I would actually create two methods: one to scan through the puzzle in order until it comes to a letter that is the first letter of a word (or multiple words!) in the list (that hasn't been found yet.) When a matching letter is found, the method can call another method, which takes in a word to look for, coordinates to start with, and a direction to look in, and returns whether the word is found in that direction. That latter method could be called up to 8 times, one for each direction, until one returns true or you've exhausted directions to look, then you move on to the next word that starts with that letter, or if no other words are found, move to the next letter in the puzzle. Just remember to handle what happens if you hit the edge of the puzzle.

The only edge case I can think of to handle would be if one of the words in the list was also a substring of another word in the list (think "the" and "there", for instance), which could be taken care of by searching for longer words before shorter ones.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

MyFile(file.getName())?

EDIT: Nope. It's this(file.getName())

carry on then fucked around with this message at 17:20 on Oct 14, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Constructors always return a reference to a new instance of the class, so no.

Proof:


One last thing I should add, according to http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java you can only chain once and it has to be the first line in the constructor.

carry on then fucked around with this message at 17:47 on Oct 14, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

It's more than just a good habit, it's required.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Tusen Takk posted:

1) Do I make class Quadrilateral extends Object or am I misunderstanding something?

All classes implicitly extend Object if no extends is specified, meaning all classes are a subclass of object, even if they may be a distant descendant.

Tusen Takk posted:

2) if I make class Square extend Rectangle then use super( x1, y1, ... , x4, y4 ) will that be inheriting those variables from class Quadrilateral or from class Rectangle? I'm assuming it'd be from Rectangle and ultimately Quadrilateral but I just wanted to double check.

Instance variables with the same name are overridden by subclasses, which means that for any variable that exists in several classes that are in the same inheritance hierarchy, the lowest one is used. The same goes for methods. In this case, because Rectangle has a constructor accepting the same number of arguments of the same type in the same order, you will use Rectangle's constructor. However, if Rectangle's constructor did not take exactly 8 doubles, but you called the 8 double super(), it would use Quadrilateral's constructor directly.

Tusen Takk posted:

3) How do I go about fixing the error in the test class? I explicitly defined the constructors in their classes (or at least as far as I understand, I did ), so if they're not inherited from their original classes, do I have to redefine them in class QuadrilateralTest

Quadrilateral and all subclasses have a constructor taking 8 double variables. You are trying to call it and supply 4 locationPoint variables. Java has no way of knowing how to convert the two, so you should specify the points as doubles and let the constructor make the locationPoints objects. Your other option is to create another constructor which takes four locationPoints variables and just store them in the instance variables; this is called overloading.

carry on then fucked around with this message at 22:03 on Oct 27, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

This is the signature of your Quadrilateral constructor:

Java code:
public Quadrilateral( double x1, double y1, double x2, double y2, double x3,
            double y3, double x4, double y4)
It takes 8 different variables, all typed as double. That's what you need to supply in order to create a Quadrilateral.

As far as taking the four locationPoints vars instead of 8 doubles, look at the types of your instance variables and the types of the proposed arguments, and think about what (if anything) you need to do with the parameters to make the constructor have the same functionality as the one you already have.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Tusen Takk posted:

OHHH okay so I have to either change the signature of Quadrilateral to something like

Java code:
public Quadrilateral( locationPoints( x1, y1 ), locationPoints( x2, y2 ), 
	locationPoints( x3, y3 ), locationPoints( x4, y4 )

More like
Java code:
public Quadrilateral(locationPoints p1, locationPoints p2, locationPoints p3, locationPoints p4)
But as you have it working as is it's not a major concern.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

LoneGun posted:

its been about 7-8 years since I have done any coding and my day to day job has numbed my mind when it comes to java.

What I am trying to do is examine a text file, search for a string and then examine the following 8 characters. If what I find is higher than a certain number I would like to move the file to a new folder. If anyone can help me out with this or give me some very basic pointers it would be much appreciated.
thanks in advance.

How big are these files? Do they have any line breaks or other delimiters anywhere? If they're reasonably small, you could probably get away with reading each file's contents into a string and calling fileString.indexOf("my string") to get the start of your string, then do a substring and convert to an integer. If you know there is a delimiter (so that your string and the number would be between two delimiters) you could go delimiter to delimiter to save memory. java.nio.Files in Java 7 can move the file pretty concisely, but you could probably get away with opening a stream to a new file in the destination directory, write everything out, then delete the source file, all of which can be done with java.io.File if you're stuck on Java 6.

carry on then fucked around with this message at 17:28 on Nov 15, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Definitely do the brackets every time. Not only does it make it easier to add a second statement (since you already have the brackets) it reduces ambiguity when reading the code.

Edit: ambiguity like this

carry on then fucked around with this message at 05:35 on Dec 8, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Not in that program I used, nor in many text editors which just maintain the indentation of the previous line.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Sab669 posted:

C# guy trying to help my roommate with Java; are variables not referred to as "fields" in a class in Java? He's a first-semester CS student and his vocabulary is pretty poor, which makes it difficult when I try to give him guidance on a lab.

They are usually "instance variables" (or "properties" if they are public) although I have also heard member variables as well. Ask if any of those terms sound familiar.

Adbot
ADBOT LOVES YOU

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

The difficulty is in the fact that each type may have properties or methods which do not exist in the other. To use a better example, if both Circle and Triangle extend Shape, but Circle has center and radius, while Triangle has point1, point2, point3, how could you possibly do a circleToTriangle() without programmer insight?

While duck typing systems let you stuff a triangle into a variable which before held a circle, true conversions like that aren't really possible in the language itself.

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