BestJewSinceJC 700 Posting Maven

How can I access variable in the frame from a component 2-3 levels bellow?

The class that contains the user ID and password should have a get method. Just call it and return the relevant info to caller. No need to pass it to things that don't need it.

BestJewSinceJC 700 Posting Maven

try this

File file = new File("C:/theFile.txt");
		file.delete();
BestJewSinceJC 700 Posting Maven

What is binary float?

BestJewSinceJC 700 Posting Maven

Its only called a no-argument constructor if it is a constructor with no arguments. What you posted is a no-argument method. And the purpose of a no-argument method can be almost anything. For example, lets say you wanted to print out a menu to the user.

public void printMenu(){

System.out.println("Type 1 for Whatever");
System.out.println("Type 2 for Whatever Else");
}


Obviously, no-argument methods aren't only for printing menus, they can be used for lots of different things, but that's one example.

BestJewSinceJC 700 Posting Maven

I'm not sure what you're asking, so here's two examples.

Animal myAnimal = new Animal(2);
Animal otherAnimal = new Animal(2);

If we call myAnimal.getDifference(otherAnimal), if you recall, the method parameter was simply called 'other'. So every time it says 'other' in the method's code, it will refer to otherAnimal. We could also say

Animal myAnimal = new Animal(2);
Animal other = new Animal(2);

myAnimal.getDifference(other), and it would not make a difference. The only important thing to remember is that whatever is between the () is going to be referred to in the method by 'other'.

It's probably above what you're studying at this point, but once you fully understand that, you might want to look into how Java passes-by-value.

BestJewSinceJC 700 Posting Maven

You can't use "==" as a comparator on Objects. On Objects, "==" will only tell you if the two Objects are at the same memory location, I believe. In any case, it will not compare the two things for equality like it would if you said "does int 1 == int 2?"

edit: I'm looking into this a little more, but I tried it with '==' and it does work, because of how getSource works. I'll run your code and get back to you.

BestJewSinceJC 700 Posting Maven

What is the point of a no argument?

public getDifference(Animal other){
int difference = this.height - other.height;
return difference;.


}

Where is "Animal other" from?

How would you compare the heights of two Animals if you didn't have two Animal Objects?

and for the "this" how would it know which of the 2 it refers too, as it was not stated.
'this' always refers to the calling Object (if the method isn't a constructor. If the method is a constructor, then this refers to the Object you are in the process of creating/modifying). So if you had two Animals called myAnimal and other, and you said other.getDifference(myAnimal), then inside the getDifference method, 'this.height' would refer to other's height.

I don't think I should use the this. anyways, since our teacher doesn't like us going ahead and using unlearned code

That's fine. I mentioned it because I'm positive that you will see it over and over again in Java. I doubt your teacher would have a problem with you using it, but it's up to you.

Yeah I understand the topic mainly now, just need to work around the small details that confuse Java.

fghgfdfgh

BestJewSinceJC 700 Posting Maven

Oh, and I have read, but do not understand what, how, or the purpose of a no argument constructor.

What exactly is it? a method containing no body?

No, it is a constructor that had no arguments/parameters. The method can contain whatever body/code you want it to. A no argument constructor is just, for example, if we had a constructor with the method header: public Animal()

yeah, I'll begin writing and practicing these using what I know, but I still need to further understand private, no-argument.

So doing calculations would be something like:

public getDifference (int height){
 heightDifference = myAnimal2.height - myAnimal1.height;
 return heightDifference;
}

would that be correct?

You have the right idea. But keep in mind that if you actually wrote that exact code, Java would not know what 'myAnimal2' and 'myAnimal1' are. See my example of how to do it below.

Rewriting your code above so that it would work:

public getDifference(Animal a, Animal b){
int difference = a.height - b.height;
return difference;
}

And rewriting it again, so that it is even better

public getDifference(Animal other){
int difference = this.height - other.height;
return difference;
}

^ Note the 'this' again. 'this' refers to the Animal Object that called the method. If that doesn't make sense, consider the way that you actually would call the getDifference method. You would have some code like this:

Animal myAnimal = new Animal(2);
Animal otherAnimal = new Animal(1);

int theDifference = myAnimal.getDifference(otherAnimal);
System.out.println(theDifference); // Prints "1"

jasimp commented: Nice job helping him out +9
BestJewSinceJC 700 Posting Maven

Also, this.whatever is usually not necessary. It is just a way to clarify what you are referring to. For example, consider the confusion that the following code might produce:

public class Animal{
[U][I]int height = 0;[/I][/U]

public int doSomethingPointless(int height){
int temp = [B]height [/B]- 5;
return temp;
}

}

Which height does 'height' (where I bolded) refer to? The parameter (int height), or the instance variable? Obviously, this is confusing. So 'this.' refers to the instance variable (underlined & in italics)

BestJewSinceJC 700 Posting Maven

Your first code box, if you put
...new Animal(2); and
...new Animal(2, 4);

the second one would automatically acess the one that also contains the int w, as there are 2 values, and skip over the first method right? while as the first one would skip the second method?

Or does the first one just have a w value of 0 as it is default?

Considering the class,

public class Animal{
int height = 0;
int weight = 0;

// ... our methods down here 
}

Think of it like this. Every time you construct a new Animal, that Animal initially has a height = 0 and a weight = 0. Then, if you say Animal myAnimal = new Animal(2), what Java does is it looks for the method that matches. So it will look for a method called "Animal" that has one integer as its parameter. It will find this method:

public Animal(int h){
height = h;
}

Then, it will call that method. It will set height to 2, but it will not change weight, since there is no code there to change weight. So now we will have height = 2 and weight = 0.

BestJewSinceJC 700 Posting Maven

Yes, it seems like you are starting to understand everything. If you had an Animal called myAnimal, myAnimal.height would let you access the height. So if you had the following class

public class Animal{

int height = 0;
int weight = 0;

public Animal(int h){
    height = h;
}

public Animal(int h, int w){
   height = h;
   weight = w;
}

}

And you did the following:
Animal myAnimal1 = new Animal(2);
Animal myAnimal2 = new Animal(2, 4);

myAnimal1 and myAnimal2 would have the same height. However, they would not have the same weight, since myAnimal1 uses a constructor which does not change the Animal's weight, whereas myAnimal2 uses a constructor which does change the weight.

Here are some things you can do w/ the height and weight of the Animals I just created.

//If you run this code, it prints "We have equal heights"

if (myAnimal1.height == myAnimal2.height)
System.out.println("We have equal heights");

//Print myAnimal1's height

System.out.println("Animal's height is " + myAnimal1.height);

As a side note, the two constructors I am writing below do the same exact thing:

public Animal(int h, int w){
   height = h;
   weight = w;
}

public Animal(int height, int weight){
   this.height = height;
   this.weight = weight;
}

You should practice writing some simple classes where you make the instance variables private and use setters and getters to access and change the values. An example of an instance variable, in the class we've been talking about, is …

BestJewSinceJC 700 Posting Maven

IE, Animal is now, like an int, or a float, but as what the method Animal states it is?

You were correct in your connection between Animal and int/float/etc. Yes, they are similar in some ways. But note that there are also very important differences. Animal, and all other types (anything that is declared with the word class) are Objects whereas int/float/etc are not Objects and do not have a class declaration. There are more differences than these, but I'm straying from your question. The other statements you made in the same post as this one, i.e. about it being an array, were not correct.

public class Animal{
int height;

[B]public Animal(int h){
height = h;
}[/B]


}

In this example, I created a class called 'Animal'. That means that when I say Animal myAnimal = new Animal(2), I am invoking the method that I bolded in my code. You'll notice that the method I'm invoking, Animal, has one parameter: "int h". So when I say new Animal(2), the Animal method is invoked, and 2 is passed in. This means that what will end up happening is height = 2. So after I say Animal myAnimal = new Animal(2), I have created an Animal Object with a height of 2. If I wanted to create an array of Animals, I would have to use the following code:

Animal[] arrayOfAnimals = new Animal[[B]10[/B]];

There are a couple of other ways to make an array in Java, but that is …

BestJewSinceJC 700 Posting Maven

A constructor is a name for a method that 'constructs' an Object, or creates an instance of its class type. The name of a constructor has to be the same of the name as the class it is in (although it can have any parameters). For example, if my class is as follows

public class Animal{

int height = 0;

   public Animal(int h){
      height = h;
   }

}

The method "Animal(int h)" is a constructor. If you say Animal myAnimal = new Animal(2); you are creating an Animal object, also referred to with various other terminology...


I just gave you the most basic description of what a constructor is. If you want to know more specific details then google Java Sun constructor or ask more specific questions here.

BestJewSinceJC 700 Posting Maven

Yes, we're definitely agreed that understanding the basics is the most important thing. Otherwise, one wouldn't understand the information the IDE spits out at all, which is why I only half agree with what you said about people who can "only use the IDE". Also, forgive the freepost, just making my previous statement clear - we're agreed on the first and half agreed on the second.

(feel free to erase and apologies if this is clouding the thread)

BestJewSinceJC 700 Posting Maven

> whenever you access a method, it gives you a summary of what the
> method does, as given in the API

...so do the online java docs. But in most of the cases we end up with a bunch of CTRL + SPACE programmers who find it too troublesome to even remember the method signatures of the most commonly used methods out there.

As someone who earns from programming, I find such tools to be really useful in boosting ones' productivity and getting the job at hand done faster, it can't be denied that knowing the basics of the language you develop in of prime importance. I am pretty sure none of us would want to hire someone who is completely lost without an IDE; after all, it's a Java developer we are looking for and not just a Eclipse/Netbeans/IntelliJ user.

I don't think that anyone in academics would agree with such a broad statement. Of course, it is important to be knowledgeable without the use of resources - otherwise, you're useless - but knowing how to properly use your resources is probably just as important.

BestJewSinceJC 700 Posting Maven

I just did a quick google search on your problem and I found the following information that seems like what you're looking for:

"The trick is to pass an array of message that contains both the message and the input components in the call to JOptionPane.showOptionDialog."

BestJewSinceJC 700 Posting Maven

Does it have to be a JOptionPane? JOptionPane's are usually supposed to be used to display messages, not get them, I think. They can also display a list of choices and get the user's choice. Here's the Sun tutorial for JOptionPane: http://www.j2ee.me/docs/books/tutorial/uiswing/components/dialog.html

If you're open to using different types of GUIs I can help you make a custom GUI that has multiple text fields where the user can enter text. Or, if you have to use JOptionPane, you'll have to wait and see if this is possible - maybe someone else here knows how to do it. Let me know.

BestJewSinceJC 700 Posting Maven

Couldn't you also convert the char to an integer, then compare the two chars? Being careful, of course, for case considerations?

BestJewSinceJC 700 Posting Maven

^ That code looks wrong. Who modifies main to throw an Exception? Also, since his Exception is printing "Invalid ID", your code would print the same thing twice. And most importantly, when you declare a method as 'throws Exception' in the method header, the method that calls it has to handle the exception by using try/catch. So, for example, Mr.Diaz, see my code below

public static void checkNumber (int number) throws InvalidIDException{
-loop through the array, find out if the ID is valid
if ID is not valid, throw new InvalidIDException
else do nothing, since the ID is valid
}

main method{
   try{
    checkNumber(number);
    System.out.println("Valid ID!");
   } catch(InvalidIDException e){
      System.out.println(e.getMessage());
   }
}

Mr Diaz, please note that this line of code: System.out.println("Valid ID!"); will never be reached if checkNumber throws an Exception. That is because if checkNumber throws an exception, then the code in our main method will 'go' to the catch portion, and it will never execute anything after checkNumber(number). Otherwise, if there is no exception, it will do the print statement and it will never enter the catch portion.

quuba commented: Who modifies main...? - No one, thank you for assertive post. quuba +1
BestJewSinceJC 700 Posting Maven

S.o.S - My code may have been wrong, but I got this discussion going, which is good enough for me

:)

~s.o.s~ commented: Heh, nice one. :-) +25
BestJewSinceJC 700 Posting Maven

It doesn't seem like your maxHeapify method has a base case. Meaning, even when it finds the values you are looking for, it will keep looking for those values. You need a base case so that once the method finds what its looking for, it no longer continues calling itself. Try adding this:

if (largest != i){
			int tmp = A[i];
			A[i] = A[largest];
			A[largest] = tmp;
}
else{ //putting return here prevents the method from calling itself again
return;
}
BestJewSinceJC 700 Posting Maven

If I'm not mistaken, she is just looking for a way to "pause" after each word is displayed, but she doesn't know it. She thinks what Stul said above - that it's just not working - but it probably is working and just is disappearing too fast to be seen.

Now, someone else may have a better suggestion, but I seem to remember something like this to pause. If she adds it in her loop she might get what she wants.

Thread.getThread().sleep(3000); //Pause for 3 s

BestJewSinceJC 700 Posting Maven

loop through each index of the array{
loop from 0 to the number at the current index of the array - 1{
print the star
}
}

For the first loop, consider that you want to print the stars for each index of the array. One way to do this is to go through each index of the array. At each step of the way, look at the index you are on and print the same number of *'s. For the second loop, notice that I say star, not stars. Think about the relationship between the loop and the print statement for the star. I didn't really add anything to what anyone else said - just put it in a different format in case you were having trouble thinking about it. Good luck

:)

BestJewSinceJC 700 Posting Maven

Making a class serializable in java means that you can read it from and write it to a file in binary form. So when your program exits, that is when you should be writing all of your objects to the file. When your program starts, you read them in from the file. When you read from a "serializable file", it creates your Objects for you. When you write to a "serializable file" you are essentially storing your Objects in a special form so that they can be read back in, as Objects, when you start your program the next time. I don't know a whole lot about the details of how it works (for example, I'm not sure when or how exactly the Objects are created), but that's the general idea. Hope that helps. If not, and you were already aware of that, I'll be glad to help you work out the details once you reply.

BestJewSinceJC 700 Posting Maven

His post was still a personal attack - a personal attack is judged by intent, regardless of whether there's "meant to be a lesson" in it. I agree with you that my comment was unnecessary and I wish I could withdraw it. However, WaltP should consider that haughtiness in any form is a personality flaw. Also, my original post was pointing out that in the real world, nobody would look at the code and attempt to apply a pattern if they knew they only needed that specific series of *'s. I assumed that someone else would give the kid the pattern or that he would do the logical thing and ask his teacher. Maybe its my own fault, but that's how I interpreted Walt's attitude. Consider this issue done.

BestJewSinceJC 700 Posting Maven

Actually, it was you who went on a personal attack with your above comment about never passing a programming course. If you aren't prepared to take it, don't dish it out.

BestJewSinceJC 700 Posting Maven

You've obviously never passed a programming course.

You've obviously never had friends.

BestJewSinceJC 700 Posting Maven

The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it. The code to write the loops alone is probably longer than the code to just print the '*'s' out. And the code definitely makes more sense at first glance hard coded than if it is put in for loops. Also, you might want to consider that the sentence 'nested is the term' only makes sense to Yoda. Learning you are.

BestJewSinceJC 700 Posting Maven

There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop, unless your teacher is trying to make you use a math formula to calculate the number of '*' on each line. If I were you I'd just ask my teacher about this.

BestJewSinceJC 700 Posting Maven

Use % and / to do this.

BestJewSinceJC 700 Posting Maven

My point is that its not a queue, then. Just take an ArrayList, use int index = Math.Random(n) where n is the # of items in your ArrayList, then index is the random index you want.

BestJewSinceJC 700 Posting Maven

What do you mean? A queue means its FIFO - first item put in the queue is the first item that comes out of the queue. So its not random.

BestJewSinceJC 700 Posting Maven

^^ Isn't that exactly what I just said? Lol

BestJewSinceJC 700 Posting Maven

This is just a guess, but I'm somewhat certain that if you don't explicitly call the constructor, the compiler will put in a super() call for you, which calls the default constructor (the constructor with no parameters). Since your Base class does not have a constructor w/ no parameters, that is why you are getting the error. Either add a constructor with no parameters, or in the class where you get the error, add a call to an existing constructor (like Base(int i) constructor)

BestJewSinceJC 700 Posting Maven

I think you could use a 3D KD tree for that. Well, actually, not really, since a KD tree works by analyzing the first key, then second, then third, etc, depending on what level you're on. But I don't see why you can't put all the data into one array, then simply search through the data according to whatever characteristic you're looking for. And Ezzaral, my professors always use Global to refer to what would be the same as a 'static' member of a class in Java. But if you're saying an instance of a class doesn't contain Global variables (I mean, that Global variables aren't class instances), I agree.

BestJewSinceJC 700 Posting Maven

@ Vernon: I got the impression that he isn't creating a new class, he was just giving an example of what he thought the concepts were, and he's going to use those concepts in the class he currently has. No?

BestJewSinceJC 700 Posting Maven

Identify the portion of the code that isn't working and explain what it is supposed to do and I will gladly try to help you. Also, if we're supposed to identify your problem based on that 100 lines of input & output and that small segment of code, how are we going to do so if you didn't post the code for your functions that were called in that segment of code?

BestJewSinceJC 700 Posting Maven

Yes, the above example will work, as long as you declare x properly. It should say public static int x = 37;

BestJewSinceJC 700 Posting Maven
public class anExample{
private Object obj; 
private static Object obj2;
public Object obj3;
public static Object obj4;

public static void main(String[] args){
}

//other methods

}

From a method inside the class with an instance of anExample:
You can refer to (use) obj and obj3.

From a static method inside the class without an instance of anExample:
You can use obj2 and obj4. Being static means the class has one copy of them that is shared by the entire class.

From a method inside a different class, with an instance of anExample:
You can refer to obj3

From a method inside a different class, without an instance of anExample:
you can refer to obj4


So the answer to your question would be the following, although I didn't look at your code. Either use a public or protected variable in your main driver class (the one thats run when you first start the program), use a private instance variable and provide accessors and mutators, or you could use a static variable. In either case, if you wanted easy access to the variable in both of your classes, consider using constructors that take the parameter as an argument.

BestJewSinceJC 700 Posting Maven

edit again... disregard

BestJewSinceJC 700 Posting Maven

Its because hasNextLine() will return true if there is a next line, even if that line does NOT contain any information. In other words, if there is a newline character, hasNextLine() will return true. Whereas hasNext() will only return true if there is a next token. A token is defined as anything that is preceded and followed by the delimiter pattern, (which is whitespace by default). In other words, change it to hasNext() and it will work. Also, read this...


http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#hasNext()

BestJewSinceJC 700 Posting Maven

I'd be willing to bet that if you changed your code to this, then your code would work. I've never actually seen somebody use "File" as an argument to a Scanner, so excuse me if I'm wrong, but I don't think it should be used. File is a class that represents a File, is it not? A File and a FileInputStream are not the same thing, I don't think. Try this..

Scanner whatever = new Scanner(new FileInputStream(yourFileHere));

BestJewSinceJC 700 Posting Maven
//Fig.2.7:Inventory.java
//Inventory program

public class Inventory
{
	//main method begins execution of Java application
public static void main(String[] args) {
	
 //You need to create a Scanner object. Below, we create a Scanner object called 'input'.
 //We use System.in because we want to read input from the keyboard.
		Scanner input = new Scanner(System.in);

       //To declare the variables below, we do NOT need a new class. 
       //We can just declare them in this method.
       //Each declaration should only have one type, as I edited below.
	String productName;
	int productNumber;
	int productStockAmount;
	int productPrice;
	int stockValue;

        while(true){
	System.out.print(Enter product name:); //prompt
	productName = input.nextLine(); // gets the line of input the user typed

        //Note: this code is wrong: the format ###-##-### is NOT an integer
        //therefore, you can't save it into an integer variable! number1 is an 
        // integer variable, and this code will not work. You can, however,
        //declare number1 as a 'String' and it will work. 
	System.out.print(Product number: ###-##-###:); //prompt
	number1 = input.nextLine(); 

	System.out.print(Product stock amount:); //prompt
	number2 = input.nextLine(); 

        //Again, this will not work because ##.## is not an integer. 
        //It is a decimal number, so use float or double. 
        // Go back to where number3 is declared in your code and change it from int
        // to either float or double.
	System.out.print(Product price: $##.##:); //prompt
	number3 = input.nextLine(); 

       //Amount has to be a float or a double for the same reason listed above.
	amount = number2 …
BestJewSinceJC 700 Posting Maven

To restate my earlier post, what I said only applies if the variable is static. Otherwise, you need to create a new object in order to refer to 'q' -- which is what javaAddict did. (In other words what javaAddict did is what you have to do if its a public, non-static variable. What I did in my first post is what you'd do for a static variable.)

BestJewSinceJC 700 Posting Maven

If it is public, then you can use

Classname.variableName to refer to it. In this case, that means saying C.q
This only applies if they are in the same package, which in your case, they are. If they were in a different package, you could say packageName.Classname.Variablename

BestJewSinceJC 700 Posting Maven

Step one: Read the whole string in all at once. This can be accomplished in a lot of different ways. If you're getting it from a file, you could use Scanner input = new Scanner(new FileInputStream(yourFile.txt))); Otherwise, if you're reading it from the keyboard, Scanner input = new Scanner(System.in);
Step two: Read in the entire String using String wholeLicense = input.nextLine();
Step three: (There are a number of ways to do this). Make a for loop that goes through every character from the String, then read it into a new String IF the character isn't whitespace. You can figure out if the character is whitespace by making a new Character object, then using the isWhitespace method.


Now you have a String with all the chars you need but without whitespace

BestJewSinceJC 700 Posting Maven

Your question is very confusing. Here is an example of how to set up a Socket, which is one end of a connection/pipeline to send stuff back and forth, basically. If you want the client to be on your own computer, then it would be 'localhost'. Otherwise, the String hostname would be set to the web address of wherever.


int port = 1235;
String hostname = "localhost";
Socket connectionSock = new Socket(hostname, port);
out = new ObjectOutputStream(connectionSock.getOutputStream());
in = new ObjectInputStream(connectionSock.getInputStream());


Also, google 'Java Socket' and 'Java ServerSocket'