Java Lesson 05 – Inheritance

In this lesson we are going to continue with the classes from the last lesson. Do create a new project called Lesson 05.

Object instances

Look at the following code. What do you expect the System.out.println to print out? Now run the code. Where you wrong or correct? Why do you think that happened?

With the Animal b = a; you give b a reference to a, making both a and b point toward the same Object instance. So when you change a, b will also change.

Now look at the following code and try to guess what will happen again and try it out.

When a gets a new instance that doesnt mean b will also get a reference to that new instance. The b object will keep having the Miranda instnace that’s later renamed to Miranda.

Like I’ve said before all classes inherit the Object class this means any class can be cast to Object without any problems.


To make one class inherit another class you use the extends keyword. Now lets create a Horse class which inherits the Animal class. In this class also @Override the getType functions and directly return the correct type, so they dont have to be set anymore.

Now you should change the Animal class to an abstract class, because when you need Animal classes they should just create seperate classes for them.  You can also delete the setType function as that shouldnt be used anymore and change getType into a abstract function that should be overriden by classes that extend Animal. So far we used the toString() function to print out our animals, we are now going to create a print function in Animal instead.

An abstract function can not have any functionality so it doesn’t have curly brackets. Abstract can only be created in abstract classes and when a class inherits an abstract class it will be forced to create all abstract functions in that class otherwise it will give errors. Give it a try by creating a Cat class which extends the Animal class. Also give this class two new functions isClipped that returns a boolean and a setClipped which sets if the cat has been clipped. Also @Override the print function to print out whether or not the cat has been clipped.

Result SelectShow

Now if you create an instance of the Cat class and print it, it will show whether or not your cat has been clipped.

Create two new functions in the Animal class called setAge and getAge. These functions will take an int. Also create an abstract function called getAverageAge with an int as return type. Next create some more animals and like the cat try and think of unique functions and variables to give them and override the getAverageAge and getType correctly in them.
The examples and the solutions to the Assignment you can <a href=””>download here</a>

Java Lesson 04 – Classes

















Result SelectShow




Now when you run the following code in your Main class:





The examples and the solutions to the Assignment you can download here

Special note



Java Lesson 03 – User input / Ifs

User input

Now that we had some of the basics lets do some ‘real’ programming. We’ll start with something simple: accepting user input. As with everything there are multiple ways of doing this. We’ll do it with the Scanner class. The Scanner class is a java class used to get user input lines in the console. The constructor of the Scanner class needs an InputStream. This means that to create a Scanner object an InputStream object is needed. In our case to ‘listen’ to what the user is typing we will use the This will give us the InputStream of the system, which is in the console.

Lets try that with an example. Create a new project called Lesson 03, create a Main class and copy in the following code:

When you copy this code in and press ctrl + s, to save, there will be a red line under Scanner meaning it has an error. This is because it doesnt recognize the class Scanner. To fix this we need to import it into our Main class. An easy way todo this is to stand with your cursor on the error and hit ctrl + 1 (or right click and press quick fix), this will bring up the quick fix menu. In this menu you should be able to choose to create your own Scanner class or to import the existing java.util.Scanner class. Choose to import and hit ctrl + s again to save. Now you shouldnt have any errors left. (I wont be telling you to save again, you need to save your code to run it and you need to save it for eclipse to compile and check if you have errors)

When you run this program it will say Firstname: and then pause. Now put your cursor in the console and type your first name. When you hit enter the program will continue and ask for your lastname. Fill it in and hit enter again and it will print out your full name.

As you might have guessed scanner.nextLine() will pause your program and wait for you input and when you have have typed something and have pressed enter it will put what you typed in the lastname String. How it does this I dont know. All I know is that it listens to the InputStream we gave in the Scanner constructor. Its kind of like using a tv. We know how to turn it on and we know how to change channels, but we dont know how it does this. Same goes with many java classes. You dont have to know how they do it, you only need to be able to work with them.

If Statements

The programming we have done so far is pretty liniar. Most of the time you will want certain pieces of code only to execute when a certain comdition is met. (.lenght() gives the number of characters in a String)

Those are all the basic ifs. Now the firstname and lastname program we had and print out a message if they are shorter than 3 characters or longer than 16. Do this in 4 different ifs and try to use 4 different conditions.

Result SelectShow

Our current program has 4 ifs it will always check, which isnt really necessary. If firstname < 3 is true it will still check if firstname > 16 too. You can improve this with an if else construction.


Besides making it ask for lastname and firstname make it also ask for your adress, city and phonenumber. Also create an integer error that tracks how many things are filled in wrong. If there were no errors print out that everything was filled out succesfully.


The examples and the solutions to the Assignment you can download here

Java Lesson 02 – Numbers

Learning about numbers




Try the following example:


Basic Calculations



Answers SelectShow


Answers SelectShow


Answers SelectShow




Try get the following results:1) 9.0
2) 3
3) 2
4) 12.0
5) 7
6) 1.0
7) 4
8) 5

Answers SelectShow


The examples and the solutions to the Assignment you can download here

Java Lesson 01 – String basics

Creating a new Project

note: everything about programming is case sensitive make sure to use the right case.

In eclipse at the top right click File -> New -> Java Project. You will now have a New Java Project wizard. Fill in the Project name: Lesson 1 and press finish. Is that it? What about the other options? Don’t worry about the other options for now. It’s all fine. You now have created your first project.

Ok so now you might be wondering thats nice and all, but what is it for? A project is the shell of your programm. Its the container of your class files. In it self it does nothing. Its mostly made to make your improve your workflow and keep things organized. Lets continue into making this project a runnable program.

Go into your Lesson 1 project and right click the src(source) folder -> New -> Class. This will open up a New Java Class wizard. Fill in the name: Main and put a tick infront of public static void main(String[] args). When you’ve done that press Finish.

Inside your src folder should now be a (default package) and inside that a For now you can ignore the (default package) we’ll go into packages later on you can ignore that for now. Open the and you should see:

Everything that starts with // or is inbetween /** and */ are comments, remove these for now. Now than lets starts simple and add to the main:

Ok let me explain this piece of code. String s = “Hello World!”; is how you define a variable. String is the variable type, s is the variable name and “Hello World!” is the variable value. And in java you have to end every line of code with a semicolon. Strings are used to store text in, values of a String variable have to be inbetween double quotes. The s is a variable name you can basically call it whatever you want as long as its not a known variable type, something like String String = “Hello World!” would give you errors also variable names have to start with a letter and may only contain letters, numbers or underscores, so no spaces or starting with numbers. There are more variable type, we’ll get into them later.

The System.out.println(); is javas way to print stuff to the console. In the above sample we will be printing the String s. Its not really something you need to understand, just know how to use it. Its mostly used to debug your code.

Now lets teach you how to run this. Before running your code you always need to save your code, use ctrl + s. You should save quite often, because after you save it recompiles internally and shows you if you have any errors. What happens if you run your code is that it compiles and runs your code. Eclipse does this all automatically for you.

There are multiple ways to run your code:

  • Right click -> Run As -> Java Application
  • Select the and hit the Green circle with an arrow inside in the eclipse toolbar
  • Select the and hit ctrl + F11
  • Select the and go to Run -> Run in the eclipse top menu bar.

After you’ve run it, your console log should dsiplay that is was <terminated> and there should be Hello World! in the log. Congratulations you have now created your first program.

Playing with strings

This bit is where you get to try out code yourself. What do you expect the results of the following pieces or code are? Be sure to try them out one by one.

Besides System.out.println there is also System.out.print which prints everything on the same line.

Read after trying out all the examples SelectShow


This assignement is optional. You can move on to the next Lesson if you want.

Assignment: Create me some ASCII Art. Before you ask me what ASCII Art is do some research yourself, it’s pretty easy to find out.

Warning: Try not to use a \ in your string it will give you errors. If you want a \ you will have to do a double \\. Ill explain why that happens some other time.


If you want my project with the Hello World and my ASCII Art download it here.

Unzip it somewhere on your pc, not in your workspace. In eclipse: File -> Import -> General -> Existing project into workspace -> Browse to where you stored the Project -> Tick copy into your workspace -> Press finish.

You can now delete where you stored the project.


Java Getting started

This getting started is windows specific, but Im sure if you google, you’ll be able to find out how to do this stuff on your OS (operating system). I wont completely be holding your hand with these tutorials. Im going to asume you know how to install things and how to unzip and stuff like that. If you dont, programming basically all about problem solving, you will have to learn how to solve your own problems, usually googling your problem helps a lot.

To get started with coding java, you need a couple of things first:

Installing JDK

JDK is short for Java Developers Kit and you will need to have this installed if you want to develop java. To download you have to go to the Oracle Download page and press download. That will bring you to a download selection page. There you have to tick Agree Lisence Agreement. If you are on a 64 bit pc be sure to download the x64 version. If you have a 32 bit pc or dont know what you have, download the x84 version.

Well I take you know how to install things so just run the exe you just download and install it. during installation you will probably be asked to install the JRE, this is the Java Runtime Environment. You most likely have this installed this already since that is whats needed to run java programs on your pc. Doesnt hurt to install it again though. At the time of writing this you will also asked to install JavaFX, feel free to skip this. If you want to know what it is use google.

So you have installed the JDK now, but you arent done yet. Now comes the tricky part making windows be able to use it. You have to let windows know where you intalled the JDK otherwise it wont be able to use it. You do this by adding the JDK bin location to your Windows Path. Now why this isnt done automatically when you install it I dont know. Anyway you have to go to your Environment Variables. Where these are located slightly variables per Windows. Ill explain it for Windows 7, but Im sure if you use google you’ll be able to find how its done for your windows.

Go to Control Panel -> System -> Advanced System Settings -> Enviroment Variables. That wasn’t so hard right? Now be careful these are settings your windows needs to run properly so no messing around. Now look at the System variables and find the path and double click it. This should look something like:

Now go the end of the Variable value and if there isnt a semicolon there add one. Now find out where your JDK installed. This is probably in your C:\Program Files\Java and go into your JDK and than bin folder and copy the path. At the time of writing this is C:\Program Files\Java\jdk1.7.0_05\bin. Now take that path and copy it after the semicolon you placed in the Path varuable value. Once again make sure you dont delete anything in it, this could mess up your windows. After doing this press ok.

Now you should have succesfully installed the JDK. You can test this by hitting your windowskey + r -> type in cmd and hit enter, a command promt should now pop up. Type in the command prompt: java and hit enter. If it gives you a bunch of text with explanations how to use it than its installed correctly. If it says ‘java’ is not recognized as an internal or external command, operable program or batch file. it didnt install correctly.

Installing Eclipse

Eclipse is a free IDE for java. An IDE is an Integrated Development Enviroment. Its basically a tool made to make programming easier. Ill be using eclipse for the upcoming tutorials. Ofcourse there are more IDEs for java or if you are really ‘hardcore’ you can ofcourse not use an IDE and just use notepad to code in. That is your own desision. Though if you are a starter I suggest you start with eclipse.

Go to the eclipse download page and download the standard eclipse. Reminder if you downloaded the 64 bit JDK you have to use the 64 bit eclipse and 32 if you got the 32 bit JDK. This will give you a zip file. Unzip the folder where you want. I ususally do it to my c:/ or c:/program files. Now go into the folder and run eclipse.exe. If it doesnt run you either installed the wrong version of eclipse or didnt install your jdk. Either way go to the eclipse forum to get help.

When you start eclipse it will ask what workspace you want to use. I for example have different workspaces for: school, work, private, testing etc. You can make as many workspaces as you want. For these tutorial workspaces I suggest you create a WorkspaceTutorials or something similar like that.

When you start ecplise it might get a welcome screen. Feel free to close it. Your start screen should look something like: