Play

In this episode of the How to Program with Java podcast I will be covering two topics which I haven’t specifically focused on before.

Encapsulation

Java is an Object Oriented Programming language, and along with it comes the four major principles of Object Oriented Programming. Encapsulation is one of these major principles and in the podcast I explain exactly what it is, and why you are (likely) already familiar with this concept.

Final Keyword

I’ve touched on the final keyword before in a previous podcast but I did not go into depth on the subject. There are some interesting things that you’ll need to be aware of when you use this keyword. The main thing you’ll need to remember is that the final keyword refers to an Object’s reference. So declaring a variable as final will mean that once the reference is assigned, you won’t be able to change it… but this does not mean that you won’t be able to change the properties WITHIN that object. This is very important to remember and can cause some confusion.
[click to continue…]

{ 0 comments }

Java Practice Assignment – High Stakes Roulette

by Trevor Page on April 20, 2013

rouletteKeeping on the gambling theme, I’ve decided to create another assignment centered around the game of Roulette.

Before we jump into the details of the assignment, I’d like to share my solution for the Texas Holdem Poker assignment.

Click Here to Download the solution

This was a very complex assignment and it even took about 4 hours of video to explain the solution in its entirety. This video is available for members of Java Video Tutorials (Video #22 and #23), so if you’re interested in seeing them feel free to signup for a membership.

High Stakes Roulette – Rules

Now let’s get on with the next Java practice assignment! If you’re not familiar with the game of roulette, it’s fairly simple to understand. Basically it’s a game of selecting a random number between 1 and 36; this is done by spinning a roulette wheel while a ball bounces around before finally landing on one number. The numbers 1 through 36 have alternating colors (black and red), and this allows for certain betting to occur.

Before each spin of the roulette wheel, you are allowed to bet on a wide variety of things, but for this assignment you only need to concern yourself with the fact that you can bet on either black or red.

If you bet on red, and the roulette ball lands on red, then you win twice the amount that you bet (eg. if you bet $10 on red, and the ball lands on red, you will win $20)! If you bet on red, and the ball lands on black, then you lose your entire bet.

Note: There are also two “green” numbers (0 and 00), if the ball lands on one of these two numbers, then you will lose your bet regardless on which color (red or black) you had bet on.

The Requirements

Okay, now that you understand the pertinent aspects of roulette let’s talk about what this assignment will involve.

I’m interested in cheating the system and guaranteeing that I’ll win money at roulette. Here’s how I think it can be done:

If you consistently bet on one color (either red or black), you’ll eventually win right? The trick will be to keep doubling your bet until you actually win. So I want to know how much money you need to have in order to guarantee that you’ll be able to win $10 for each “cycle” of continuous betting.

Here’s an example of what I mean:

  1. Choose a color to bet on and STICK with it for the entire “cycle” of betting
  2. Place your bet of $10 and spin the wheel
  3. If you lose, bet twice the amount of your last bet
  4. Repeat step 3 until you win then go to step 5
  5. If you win, record the amount you needed to bet in order to win

So in the end, what I’m interested in knowing is the MAXIMUM amount of money needed to guarantee that out of 10,000 spins of the roulette wheel, I’ll win my $10 every time.

What you’ll need to implement

In a word: everything!

I think if you’ve come this far in your Java programming journey, you should be able to create this assignment from scratch. I won’t leave you completely high and dry though, here are the basic aspects you’ll need to implement:

  • Roulette Wheel (for spinning)
  • Roulette Number (represents a number with a color)
  • Random Number Generator (used to generate a random number from 1 to 38; includes values for 0 and 00)
  • You may want to implement an Enum to represent the Colors (up to you)
  • Test class or Runnable class

Remember: the point of the assignment is to determine how much money you’ll need to have in your pocket to guarantee that you’ll always win $10 at the game of roulette by consistently betting on the same color until you win.

Now, if you are struggling to learn Java, then I will have to point you in the direction of Java Video Tutorials. I launched this service back in November 2012, and since then I have helped tons of new students learn Java. The feedback I have received from these students has been overwhelmingly positive and I’m so excited to be providing such a valuable platform for learning Java programming. What’s incredible is I’ve heard the same story over and over again:

“I have tried to learn Java a bunch of times before and never got anywhere… until I signed up to Java Video Tutorials. I’ve learned so much more than I ever have before and I’m so excited!”

Seriously! So if you are in the same boat, sign up and try it out, you’ve got nothing to lose :)

{ 0 comments }

Play

In this second iteration of our Java interview questions, I have pulled some other commonly asked questions from the following website: http://www.allapplabs.com/interview_questions/java_interview_questions.htm

  1. What is the difference between HashMap and HashTable?
  2. What is the difference between Vector and ArrayList?
  3. What is the difference between a constructor and a method?
  4. What is an Iterator?
  5. Do I need to import java.lang package any time? Why ?
  6. Does importing a package via the * operator also import all the subpackages? For example, does importing src.net.* also import src.net.javavideotutorials.*?
  7. What are wrapper classes?
  8. How are this() and super() used with constructors?
  9. Is it safe to use an instance variable inside of a singleton class?

Bonus Content

One extra piece of advice that I would like to give that I didn’t mention in the podcast recording is how to act during the interview. If you are asked a question, and you don’t know the answer off the top of your head and just need time to think about it, then say so! Don’t be afraid to say “That’s an excellent question, I’m pretty sure I know the answer but just give me a moment to think about it”. And then proceed to THINK about it in complete silence.

I’ve been in interview situations before and have had to do this myself. It feels a bit awkward sitting in silence, but believe it or not (as having been someone on the other side of the table), it’s not a huge pain in the butt as an interviewer. With the silence, it gives the interviewer time to do things like:

  • Review your resume further and think up more questions
  • Review your previous answers to ensure nothing was missed
  • Or just sit back and relax

So by all means, take the time to think things through and don’t just blurt out the first thing that comes to mind. If you blurt out the incorrect answer without thinking, then you will not stand out as a strong candidate to be hired. So collect your thoughts and take the time to think your answers through first!

{ 0 comments }

Play

List of Java interview questions

Here’s the list of questions that will be covered in this episode of the How to Program with Java podcast:

  • What is the difference between the JDK and the JRE?
  • What is the difference between checked and unchecked exceptions?
  • What is the difference between final, finally and finalize?
  • What is the difference between an Inner Class and a Sub-Class?
  • What are the various access specifiers for Java classes?
  • What is data encapsulation and why is it important?
  • What is a singleton and when is it typically used in Java?
  • What is the difference between the continue and break statements?
  • What is the base class in Java from which all other classes are derived?
  • What packages in Java? Why are they used?
  • What is the difference between an Abstract Class and Interface in Java?
  • Does Java pass by reference or by value?
  • Is it mandatory for a try block to be followed by a catch block in Java’s exception handling?

{ 0 comments }

Podcast Episode 22 – Mocking

by Trevor Page on March 29, 2013

Play

mockingUnit testing your code is all about being able to confidently test one particular piece of code without worrying about dependencies causing problems. If you get a failure in a unit test, you can to be sure that the failure was caused by bad code INSIDE of the unit of code you were testing (not because some dependency has failed)

What you’ll Learn

In this episode you’ll hear all about these things called “dependencies” and how they can be minimized by using mocking.

You’ll learn what mocking does to Objects in your unit tests.

You’ll learn what kinds of frameworks exist out there and how you can obtain them for you own personal use… don’t worry, they’re all free!

You will also learn just how powerful these mocking frameworks are in what they allow you to do with methods of dependent classes… really, really cool stuff guys/gals.

Links Mentioned in the Show

Here’s a list of the links that I mentioned in the show:

  • Mockito – Grab the JAR file here
  • Mockito Tutorials – Here’s what I used to learn Mockito (very simple stuff)
  • PowerMock – Can be used WITH Mockito to extend the functionality of your unit tests (i.e. mocking static/final methods)
  • jMock – Another mocking library (just like Mockito). I’ve never used it, but it’s another popular choice for mocking!

Listener Questions

We had a question come in from a listener who wanted to know what they could do to gain some real world experience without having to work a full-time job as either an entry level developer or an intern. You can hear all my comments on how I think this can be achieved in the last part of this podcast episode, so be sure not to miss it, as I’m sure this topic applies to MANY of you out there today :)

As always, if you do have any questions for me to answer on the podcast, feel free to shoot me an email and let me know you want the question answered on the Podcast. Be sure to leave your name so I can mention you!

{ 0 comments }

Podcast Episode 21 – Unit Testing

March 21, 2013

Podcast: Play in new window | Download In this week’s episode, I will be talking about a topic that I think is crucial when programming. This important concept was only revealed to me after I had already been programming professionally for 3 years. Unit Testing in Java Just this past week I was working on [...]

Read the full article →

Java Practice Assignment #6 – Texas Holdem Poker

March 19, 2013

Before we launch into this assignment, let’s take a look at the solution for assignment 5: Assignment #6 – Fun with Enums In this assignment we are going to use our knowledge of Enums to create a Texas holdem poker game! If you aren’t familiar with Texas holdem poker, you can become acquainted with the [...]

Read the full article →

Podcast Episode 20 – Casting

March 14, 2013

Podcast: Play in new window | Download Let’s kick off today’s podcast episode with a quick explanation of what ‘casting’ is in general. Casting exists in today’s Object Oriented Programming languages, as it’s the means by which we turn one Object type into another Object type. Think of it like turning a caterpillar into a [...]

Read the full article →

CSS 101 Tutorial

March 13, 2013

Cascading Style Sheets are a bit of a tricky beast in HTML. Given this fact, I wanted to dedicate an entire section to just this topic. Let’s start with the basics, shall we? What are Cascading Style Sheets (CSS)? Well, we’ve already talked a little bit about HTML formatting tags (like <H1>), but that kind [...]

Read the full article →

Getting a Job as a Self-Taught Programmer

March 8, 2013

In my time as a teacher of the Java programming language I have heard one question over and over again without fail: “Can I get a job as a self-taught programmer?”… My short answer to that question: YES! Here’s why: Fewer and Fewer Companies are Requiring Post-Secondary Education I did a search for Java programming [...]

Read the full article →