Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Joel Christophel

Ranch Hand
+ Follow
since Apr 20, 2011
Joel likes ...
Eclipse IDE Chrome
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joel Christophel

I am trying to use JavaFX to stream audio content from the Internet using both HTTP and HTTPS (support for the latter was added as of 8u72). However, the playback is very unreliable and different each time. There are usually short spurts of sound, and then silence. I know my Internet connection is fine because the streaming the same file works flawlessly when done via a web browser. What's going on?

4 years ago
The compiler is confused because you are passing in non-existent variables to the constructor. What you need to do is first create your variables and populate them with data from user input as you did before. After you've done that, then you can pass these variables to your constructor, and the compiler won't freak out. Note that this time, you shouldn't need to call your set methods, because all that will be handled within the constructor.
5 years ago

Amie Mac wrote:
So, that's what I'm confused about. Is it possible to put in the variable names as the parameters?

Such as:

Although, I've tried this and get compiler errors...

Yes, you can most definitely do that. Any place where you can use a literal value (e.g. "Hello", 7.34), you can use a variable instead.

You just have to make sure you refer to the variables after they're created of course.

Post your updated Payroll class with any errors or questions you may have.
5 years ago
Your first code snippet is golden. (Although when dealing with doubles and ints, make sure they are lowercase until you know what uppercase does).

Amie Mac wrote:
I would edit line 16 in the file to read this:

Passing in those non-values achieves the same thing as not having a constructor. You want to be passing meaningful information to your constructor.

Amie Mac wrote:
I would do this because I don't want to put actual values in the employee instance because I want to get them from the user input.

Who's to say that you can't pass values to your constructor that you received from user input?
5 years ago
Well let's see your attempt at a constructor. And by the way, you are using a constructor when you do new Employee(). This is the default constructor, but nothing special happens when you call it other than an Employee being created.

So think about what a more useful constructor would do. It would not only create an Employee, but would also start your employee out with some meaningful properties.
5 years ago

Travis Roberts wrote:
If the main driver class was longer, am I right to assume that the anonymous class on line 4 becomes eligible for garbage collection starting on the next line, line 5?

Anonymous classes are a very different thing. But yes, the anonymous object would immediately become eligible for garbage collection.

Travis Roberts wrote:
Also, I am uncertain as to why the toString method prints anything at all since it was not called? I tested by adding another method below that one which returns an integer

When an object is passed to println, the return value of its toString method is what gets printed. All classes have a default toString method, but you can override this method as shown in the problem.
5 years ago
I'd assume that your teacher doesn't want you using the parseInt method.

So how will you write a method to do it yourself? Well first, you need to sit down and think how you yourself would convert a binary number to decimal. How did you get 53 from 110101?

It might help if you wrote down the steps in English.
5 years ago

C C Campbell wrote:
for (int i= 0; i<= grades [i]; ++i)

How many times do you want your loop to iterate? If you look at this expression, you're saying to continue the loop while i is less than or equal to grades[i], which is 100. I'm guessing you only want your loop to iterate 10 times, which is the length of the array rather than a value in the array.

Inside your loop, you want to be getting values from your array, rather than just indexes. So you can't just use i. You must use grades[i].
5 years ago
For my speech class in college, I am going to be delivering a seven-minute informative speech about computer science. Over the years, I've been very impressed by the wisdom displayed by JavaRanch's experienced programmers, so I thought you guys could help me out.

My goal is to inform my class about programming languages and the job of programmers in Lehman's terms. I could focus on the modern state of computer programming or I could also delve into the evolution of programming (e.g. procedural to object-oriented, command line to IDEs). It could be fun to have volunteers participate somehow (I have access to a whiteboard).

No matter what I end up doing, I want to keep it interesting; it doesn't have to be super-technical. Are there any things that you guys think I should include? You guys always have great ideas.

P.S. This may not seem like quite the right forum for this thread. However, I figured I would get the most helpful responses here, as I will be addressing "beginners." If I am in error, feel free to move my post.
5 years ago
You can initialize, assign, and modify multiple variables in a for statement.
5 years ago

Charles Todd wrote:Putting the suggested \n in my print statement in the for loop isn't an option because i'll get:


which isn't desirable so I'll have to insert somewhere else later in my code. Thanks everyone for help.

I never suggested you put \n in your print statement because you only need one additional '\n' whereas the print() statement gets iterated many times. You are correct in that you'll have to insert it later, but where exactly do you think that would be?

5 years ago
The method println(x) (with some String variable x) prints out x and then a newline character. print(x) doesn't prints out just x. In Java, the newline character is '\n'. So print("\n") and println() are equivalent.

In your example, the for loop iterates three times and print(b) prints out E, B, and O. Because you are using the print() method, no newline characters are involved, and thus all the characters remain on the same line. After the loop is done, you go right to println("Please Enter Text to Decrypt: "). We are still on the same line as EBO because we had been using the print() method, so the entire line becomes "EBOPlease Enter Text to Decrypt: \n". Note the newline character at the end of the line that came from the println() method. As you can see, you should also have a '\n' character between the O and the P.
5 years ago
Consider what you know about the differences between print() and println(). You never print out a newline character after printing out your encrypted message.
5 years ago

Louis Lewis wrote:As a question for Joel though, what would it look like if I didn't want my chessboard class to extend JPanel, and I instead wanted to create a JPanel inside it? I tried this briefly -- ChessBoard didn't extend anything, but I created a JPanel within it, but then when I tried to instancize it in my main GUI class, I got an illegal argument exception (presumably because it didn't extend anything, but I'm not sure how to fix this)

Yes, if your Chessboard object doesn't extend a GUI component, you will not be able to add it to another GUI component.

Instead of gui.add(chessboard), you would create a getJPanel() method in Chessboard and do gui.add(chessboard.getJPanel()).
5 years ago
What is the purpose of the line scan.nextLine()? Also, your if/else statement doesn't seem to be necessary.
5 years ago