BestJewSinceJC 700 Posting Maven

Your class has the following variable:

BankAccount ba = account;

That doesn't make any sense. You never declared "i", so your program doesn't know what "i" is. In addition, lets imagine that you had declared i. For example, consider the following code:

BankAccount []account = new BankAccount[10];
BankAccount ba = account[0];

That assigns the value at account[0] to ba. Since the account array is empty, ba probably now contains null. Regardless of what ba contains, it is useless.

BestJewSinceJC 700 Posting Maven

I'm not upset, there just wasn't anything to be gained from your previous post. And its certainly not serious enough to waste a mod's time with removing one from your solved threads count, which they probably cannot do anyway.

BestJewSinceJC 700 Posting Maven

My question was about linear probing, not about another form of hashing. And your 'random number generator' comment doesn't make sense to me, because if you hash an element using randomness, then you cannot ever get back to that square in O(1) time, which is the goal of hashing.

Also, my lecture notes say that

f(i) = c * i, and that h(h(k), f(i)) is the hash function. My question is the following: How is an "i" value chosen for the first hash attempt? And if the first hash attempt is unsuccessful, do you simply increment "i" and try again?

BestJewSinceJC 700 Posting Maven

If I have a hash function h(k, f(i)) and

f(int i){
return c * i;
}

And the first index that it tells me to insert the element at already has an element there, what do I do? Would the proper way to insert an element the first time be

h(k, f(0)) = the index I try to insert the element at.

Then, if there is a collision/an element is already at that index, I would do i += stepwise, and call h(k, f(i)) again?

BestJewSinceJC 700 Posting Maven

Ok, I kind of understand that. But it doesn't guarantee that it will read to the end of the stream, so isn't there a possibility that input will be available, but not all of the input? If so, how would that occur? Is there some buffer to be considered or something?

BestJewSinceJC 700 Posting Maven

I just spent 5 minutes looking over your code and I have no idea what you're talking about. Could you explain what you want each one of your classes to do? Then, explain which of these classes is not working like you want it to, and explain how you want it to work. You only posted two classes, neither of which contains a main method. And you're talking about FunctionPanel, but you didn't say what FunctionPanel does. Also, post the code for any classes that you want help with that currently aren't working.

Btw, to further explain what others have said, here is what you want to do:

If you want class ATM to be able to update class BankGUI, then the constructor for ATM would look like this: 

public ATM(BankGUI theGUI, ....){
atmsBankGUI = theGUI; //ATM has to have a variable called atmsBankGUI
}

And lets say ATM lets you withdraw money. 

public void withdraw(){
int amount = dollarAmount; //get the dollar amount the user wants to withdraw
atmsBankGUI.withDrawCash(amount);
}


//Now, this method would be in the BankGUI class

withDrawCash(){
//Do whatever you have to, to make the GUI display the withdrawal
//For example, you could use JLabel.setValue to set it to the new amount of money the user has.
}

If you wanted to do the opposite, the BankGUI class to be able to get input from the user, but you wanted the ATM class to do the calculations (for example, the user types the amount …

BestJewSinceJC 700 Posting Maven

Use code tags when you post code. Also, all you have to do to figure out if your code is correct is print out the array after you read it in. If it doesn't print out the same thing as the input file, then it isn't correct. To refer to an array index, yourArray[0][1] is the first row, second column. So if you want to print out your array, use nested for loops, like this:

for (each row, i){
for (each column in the row, j){
print the integer contained in the array at i,j
}
}

Also, what you posted clearly looks like a school project. Not that I care, because my personal philosophy is that sometimes, a little bit of help will make you learn more. But your teacher might not share that philosophy - so you might not want to post your code, including a copied version of the project directions, on the web.

:)

BestJewSinceJC 700 Posting Maven

Use the power of the Internet to find it out....

That's exactly what I'm doing by posting on Daniweb. Not out of laziness, but I was short on time and I was fairly sure I would have follow up questions, so I chose to post here. Since I probably answer more questions than I ask, I'm sure you guys will forgive me. :) Its a shame that when I mark this thread solved, you will now get credit for it, although you didn't answer the question or offer an alternate solution. Either way, thanks to Masijade.... an alternate solution is just as good.

BestJewSinceJC 700 Posting Maven

Do you not understand Vernon's point about the variable ba? Learn the basics first, then work your way up.

Btw, what are you referring to when you say 'input name'? Also, post your code that you think is incorrect, or ask a specific question. And make sure to use code tags

BestJewSinceJC 700 Posting Maven

I'm trying to read in the whole file at once. If this code ever fails, my whole project will fail. I'm using the following:

byte[] theFile = new byte[input2.available()];
then, input2.read(theFile);

The API says something about blocking. What exactly is blocking, when will it happen, and how will it affect the fact that I want the whole file to be read in at once?

BestJewSinceJC 700 Posting Maven

Actually, I just realized that if the first things put in are 0's, it will seem like they have the same bit sequences, when in reality, one bit sequence might be 0001 and another might be 001 or something similar. I'll investigate some more and come back if I have more problems.

BestJewSinceJC 700 Posting Maven
public void generateBits(Node root, int current, int counter){
		if (root== null) return;
			
		if (root.left == null && root.right == null){
			list.add(new HuffCode(root.theChar, current, counter));			
		} else{
			current<<= 1; //move all the bits 1 spot to the left
			generateBits(root.left, current, counter+1);
			generateBits(root.right, (current | 1), counter+1);
		}
	}

Somehow, this method is generating wrong bit sequences for the Huffman Code. Specifically, the SAME bit sequences are being generated for different Nodes, which should be impossible... the way I'm trying to generate the bit sequences is "if you move left in the tree, add a 0 to the bit sequences, if you move right in the tree, add a 1 to the bit sequence. If you're at a leaf node, stop, and create a new HuffCode". Regardless of how the tree was created, there can only be one Node at any given position. Therefore, I should never have the same bit sequence twice, but I do. So there has to be something wrong with my method, right? I can't see what.

BestJewSinceJC 700 Posting Maven

Yeah, thats great - thanks guys. Leave the error handling to me. Lol. :)

BestJewSinceJC 700 Posting Maven

Its cool, Antenka. Everybody makes mistakes. What I usually do before responding is quickly check the API to make sure my response coincides with what I see there.

Antenka commented: Thanks for correction and moral support :) +1
BestJewSinceJC 700 Posting Maven

http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintWriter.html

Look at the API. Do you see a constructor for PrintWriter that takes a String as an argument? (Change your code so that your PrintWriter takes the correct argument)

You might want to look at this link, since from the API, it isn't immediately obvious which type of Object to create: http://java.sun.com/docs/books/tutorial/essential/exceptions/putItTogether.html

Or, here's a quick example,

PrintWriter writer = new PrintWriter(new FileWriter("yourFile.txt"));

BestJewSinceJC 700 Posting Maven

Actually, wait lol. After all this time spent trying to figure this out, I stopped thinking about it and I think it just came to me. If I do

byte current = 00000000;
current = current | 1;
that will make current = 00000001, right?

Then, the next time I wanted to add a bit to the front, I could shift the current bits over 1 and do another OR. No?

BestJewSinceJC 700 Posting Maven

I'm having trouble with storing bits as a byte. I've read probably 30 different sources about this already. I'm doing Huffman Compression. Lets say I read in the character "c" and I am supposed to output "1010". How would I go about doing that? I know how to write bytes to an output stream, my problem is generating the bytes in the first place. My question is very similar to a question I found elsewhere on the web that received this response:

"Don't do that. Use the bit manipulation operations to create an integer - you need to store the accumulated bits, and the number of bits you've written to it. When you have 8 bits, write those out as a byte, and shift the stored value right by 8 and reduce the count by 8. That way you can write irregular numbers of bits, and don't even have to convert numbers and strings. "

I understand the need for 'waiting until you have 8 bits' to write the byte. However, what would a method look like that recursively changed the bits 0000 to 1111, one at a time? The problem is - if we have 0000, in order to change the second bit to a 1, don't I have to do

0000 | 0010

So this causes issues if I don't know what bit I'm currently "on", and just want to recursively "add" a series of bits and create a byte out of it. What …

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

Why would you use a loop to search? Look at the methods for the Vector class. Particularly, look at the indexOf method. If you override the equals method from the Object class, then use indexOf, it will return the index of the thing you are looking for. So your "search" code would look like this:

while there are still names to search for{
get the next name from the file
check to see if the name you just got is in your Vector using indexOf
if the index != -1 {
Customer customer = Vector(theIndexIndexOfReturned);
}

}

Now, your customer variable will contain the Customer that you were looking for in the Vector.

BestJewSinceJC 700 Posting Maven

Thanks. The app I am currently working on is a single thread so I'll just use whichever looks easier, I guess.

BestJewSinceJC 700 Posting Maven

Can someone please help with this project of mine? I am not really good at GUI...

create a new account, check account status. and make purchase with a credit card.

Create a New Account
You must ask the user to input some information: applicant's name (e.g. "John Smith"), application date (e.g. to make it more user friendly you may want to use combo boxes so that a user can choose the year, month and date), and a password for accessing the account (e.g. you may want to use password fields for security reason, and ask the user to input his password twice for verification).

Type combo box into google and read the Java tutorial that pops up. Modify the example code they give; make sure to cite!

When the application is confirmed (the two passwords match), display a credit card number with four randomized digits (take care that any two credit card numbers of two different accounts must not be the same), an expiration date (set it to 3 years after the application date, need only year and month), and the available credit (US$1000 initially, also, available credit can not be negative).

Use a random function, class: Random for starters, to get the four random digits. Build an integer out of the four digits. Keep an array of "previously used" card numbers. Make sure that the card number you just generated wasn't previously used. If it was, generate a different one and perform the same check. Once you find …

BestJewSinceJC 700 Posting Maven

I had a problem w/ efficiency - The problem was that using String concatenation was inefficient for large text files, when reading the whole file into the String. I've since thought about it, and I guess this is because Strings are immutable - so every time concatenation is done, a new String object has to be created and the underlying char array has to be copied? After some research, I came across StringBuilder and String___ (I forget the rest of the name of the class). My question is, how can a text file be read in as efficiently as possible? I don't really care what type its read into. Also, a lot of sources I found said StringBuilder was the best way, is that true?

BestJewSinceJC 700 Posting Maven

Your professor already gave you basic steps on how to begin. Try those and when you get stuck, tell us what you don't understand. We won't do your assignment for you.

BestJewSinceJC 700 Posting Maven

To demonstrate the second point I made above, consider what happens when you move the mouse. Lets say you move the mouse down, but you do not move it in any other direction. The mouseMoved method is called, and mouseLeft is set to true. Now, lets say you eventually make it down to mouseDown = true. Then, repaint is called where you have if (Y > 250). Now, you enter the repaint method. Since mouseLeft is true, and mouseRight is true... both of those things will be repainted.

Now, another thing will happen: since you never reset mouseLeft, mouseRight, mouseUp, and mouseDown to "false"... every time you enter the repaint method, these things might be repainted. Consider using "else if" statements inside of mouseMoved to call repaint, so that repaint is never called when you don't want it to be. Also, make sure that at the end of mouseMoved, you reset all of the mouseUp, down, left, and right variables to false. Also, do not set these variables to "true" until you have determined that they should be true. In other words, setting them to true should be done inside the if statement, not outside of it.

Also, I'm not exactly sure how your assignment works, but you should consider that any time you color something, you are never un-coloring it. So lets say you color something because of mouseDown. What happens when the thing you colored isn't in the "mouseDown" sector anymore? Shouldn't you un-color it?


       
Ezzaral commented: Helpful. +15
BestJewSinceJC 700 Posting Maven

You should only be setting mouseLeft, mouseRight, etc to true if, in fact, the mouse was moved in that sector. When your mouseMoved method gets called, you are setting ALL of them to true, then calling repaint. The way you're currently doing it, if mouseLeft were to be set true, it would always remain true, which is one problem. Also, currently, if mouseDown were true, it would call repaint. However, the mouseMoved method just set mouseUp, mouseDown, mouseLeft, and mouseRight ALL to true - so all of them will be repainted.

hope that helps.

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

Why would you post something in German. Lol

BestJewSinceJC 700 Posting Maven
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PostOffice extends JFrame implements ActionListener
{
    //This appears on the screen as a clickable button
    private JButton zipCodeButton;
    //This appears as two text fields where you can type
    private JTextField zipCodeField, barCodeField;
    //This appears as text, a label next to the zip code field
    private JLabel zipCodeLabel;
    
    //This is the variable your professor has created to get input
    //from the GUI. 
    private PostalZipCode myZip;
    
    public static void main ()
    {
        //All of this code creates the GUI and shows it to you
        //You shouldn't be concerned with it, and you do not
        //need to alter it or add to it in any way. 
        PostOffice myApplication = new PostOffice();
        myApplication.setSize(400,140);
        myApplication.setTitle("Zip Code");
        myApplication.createGUI();
        myApplication.setVisible(true);
    }
    
    //Again, more code to create the GUI. You do not need to
    //change any of this code either
    private void createGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());
        
        zipCodeLabel = new JLabel(" Zip Code ");
        window.add(zipCodeLabel);
        zipCodeField = new JTextField(5);
        window.add(zipCodeField);
        
        barCodeField = new JTextField(20);
        barCodeField.setHorizontalAlignment(JTextField.CENTER);
        barCodeField.setFont(new Font("Sans erif", Font.BOLD, 20));

        window.add(barCodeField);
        
        zipCodeButton = new JButton("Display Postal Bar Code");
        zipCodeButton.addActionListener(this);
        window.add(zipCodeButton);
    }
    
  //The code that you are primarily concerned with is in here.
    public void actionPerformed (ActionEvent e) 
    {
        //Your professor reads in the zip code
        String userInput = zipCodeField.getText();
        //Here, your professor takes a zip code, "userInput", and 
        //calls a constructor that converts it to a PostalZipCode.
        //You need to write that constructor. Doing so involves creating a new class called PostalZipCode, writing a constructor …
BestJewSinceJC 700 Posting Maven

Your professor gave you whats called a GUI. A GUI is just a window that, in this case, provides the user with a way to enter data. Now, to elaborate on that idea, I'll comment some of your professor's code to help you understand what he wants you to do.

BestJewSinceJC 700 Posting Maven

there is no way to use the return value of a method for creating an int variable

That is not true.

BestJewSinceJC 700 Posting Maven

In case this is helpful (for what you mentioned above)

//By default, can get input typed from the keyboard
//This would change if System.in was redirected
Scanner keyboard = new Scanner(System.in);

//Get input from a file
Scanner fileInput = new Scanner(new FileInputStream("yourFile.txt"));


Also, following up on what S.o.s said, keep in mind that there is a method called parseInt (use: Integer.parseInt(String anIntString);). This method allows you to attempt to parse Strings into Integers. For example, if you read in everything from your file as a String, but you expect that they are actually ints, you could use

int myInt = Integer.parseInt(StringReadIn);

Also, for testing (and for good implementation) you should surround that with a try catch and report any errors (parseInt will throw an exception if the String you pass in can't be converted to an int).

What I said above is just another thing that you can do. The way S.O.S suggested, with using hasNextInt(), is probably better because it will tell you right off the bat whether or not whats next in the file is an integer or not.

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

So you guys are suggesting reading the entire file into Objects and then editing and writing it back out? Seems inefficient to me - there's no better way? I'm sure there's a way to just have a carat position and then write things there. Not saying your way isn't good, just curious.

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

Programmers are never in shape... always either skinny or fat or pudgy... hardly ever muscular or physically active

BestJewSinceJC 700 Posting Maven

ehh, forget this question. I was asking about communicating between two classes in the same application. I already know ways to do this, but I was trying to figure out if there was a way for the non-event driven code in the one class to 'wait' for the GUI to be clicked before continuing to execute. I accomplished the same merely by calling a method from within actionPerformed, but I was looking for a cooler way

BestJewSinceJC 700 Posting Maven

I will write it for you for one milliiiiiiioon dollars

(Austin Powers anyone?)

BestJewSinceJC 700 Posting Maven

But I never said I was using netbeans or otherwise, and I'm not, lol. Sorry for the lack of information. And I'm pretty tired right now, I'm not sure about the threads thing. The 'main' class, when run, calls createAndShowGUI in the other class.

Side note; isn't the only way a new thread is created if

1. the main method is called
2. a new runnable is declared.

My 'main' class uses new runnable when it creates the gui so I guess they're in separate threads.

BestJewSinceJC 700 Posting Maven

What you posted above doesn't really make sense. Make sure to clearly explain what you mean. On one hand, your thread title makes it seem like you are having trouble reading stuff in from the file. If that is the case, then why are you talking about indexOf()? indexOf() is not used to read from the file.

An example of how to read from a file which contains:

an integer on the first line
a sentence (String) on the second line

Scanner readFile = new Scanner(new FileInputStream("filename.txt"));
int firstInt= readFile.nextInt();
String secondLine = readFile.nextLine();

Obviously, I didn't just solve your problem for you, I gave you an example of how to do it. With that, you should be able to figure it out. If you aren't sure what methods to use (for example, how would you read in a decimal value such as 3.556?) then google "Java Scanner" and read the documentation for the Scanner class.

BestJewSinceJC 700 Posting Maven

Just use google. Type System.out.println() java, or you could also try Java IO. Also, an easy way to 'count' would be

// Read in the amount of time you want to wait in seconds, call it "time"
Thread.getThread().sleep(time * 1000); //waits for 1 second if time = 1.
//Print out the time you waited.

Edited.

BestJewSinceJC 700 Posting Maven

A stack overflow error is caused when you run out of stack space. Stack overflow error can mean that you have an infinitely recursive method, since this will cause you to run out of space eventually. The stack is just a block of memory that's made available to your program. I'm pretty sure that block of memory is in RAM but it doesn't matter where it is. If you want to know more about the stack, I'd look up call stack or google stack and look for results pertaining to functions/methods using the stack. Also, you didn't post your code. When you do, make sure its in code tags.

BestJewSinceJC 700 Posting Maven

Just use google. Type System.out.println() java, or you could also try Java IO. Also, an easy way to 'count' would be

// X
Thread.getThread().sleep(1000); //waits for 1 second.
//If you print out right here, you can be sure that the time is extremely close to 1 second from X

BestJewSinceJC 700 Posting Maven

Thank you, much appreciated

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

Does the "table size should be a good choice, such as a prime number" property of hash tables always need to be preserved, or does it only apply to when the table is first created? In other words, when rehashing, should the new hash table size be a prime number? From what I've read about hash tables, I think it should always be a prime number, but my lecture notes say otherwise - they say to "double the size of the old hash table and re-insert the items from the old hash table".

BestJewSinceJC 700 Posting Maven

I have two programs, one is a GUI and the other is a normal (non GUI) program. How do I make a method in the non-GUI program wait until a JButton on the GUI gets clicked before continuing?

(No while loops)

This is frustrating since I'm fairly certain somebody here has answered this question for me before, but I can't find the post, if so.

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.