In this IB Computer Science lesson you will be learning about:
Java programs can be written as either a structural program or an object-oriented program
Most programs you write in Grade 11 will be procedural in nature, but since Java was designed to be an object-oriented language you will be using objects to help you write those programs, but you will not be creating your own objects while writing those programs. That is handled in the Grade 12 course.
The java programs we write will execute in the following ways
Java is based on the idea of creating objects. A template for defining how that object behaves is called a Class. So all programming in Java, no matter what the type, must be done inside a Class.
A Typical Java Program will have the following form
ProgramName is a name you choose for your class.
Notice there is a pair of { } braces. You WILL get an error if you fail to include these in your program. Any code you write must go in between the pair of { } braces.
There are two Java keywords used in the program: class and public
Java Project can contain multiple classes that work together to make a complete program. As the programmer you must indicate where you want the program to start executing. A main method is used to accomplish this task.
The details of why the main method is written like this goes back to when programs were executed from a command line interface. You don’t need to be concerned about it right now. Again, notice the pair of { } braces. You will get an error if you don’t include them and any code you want to execute must go between those { } braces.
Putting everything together will mean that your simple java programs will always look something like this at the start.
Notice
Notice how everything is indented inside a pair of { } braces. This greatly improves the readability of your program. If you want help with your programs, I will expect them to be properly indented.
Keep in mind that java is a case sensitive language. It treats uppercase letters different from lower case letters and won’t execute if things are not typed correctly. For example if in the above code you capitalized the word public like Public. Your code won’t run.
Messages can be sent to the screen using a text based console in Java. Most software nowadays uses Graphical Interfaces, and we will get into those later, but for simple programs the easiest thing to do is just have our programs display to a Text Console Window.
Remember that java is object oriented and thus has many premade classes that can help us accomplish certain tasks. Often times we create objects from those classes and each of those objects has certain methods that accomplish certain tasks.
To output to a Text Window we need to use the System Class with an object named out with methods named print or println.
Here is an example of how to make that work
This is what was output on my screen
Notice the following about the code:
In Java there are special characters that can be used in output statements. The backslash \ is called an escape character. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence
/n New line – Position the screen cursor to the beginning of the next line
/t Horizontal tab – Move the screen cursor to the next tab stop
/r Carriage return – Set the screen cursor to the beginning of the current line. Any characters output after the carriage return overwrite the previous characters output on that line
// Backslash – Used to print a backslash character
/” Double quote – Used to print a double quote character.
The following example illustrates some of the special output characters in Java
Here is the output that showed up on my screen
Recall that in order for the computer to understand your programming, it must be translated to machine code using either an interpreter or a compiler. Java is a compiled language, however it does not compile your program into machine code directly.
The Java compiler, creates a Java Virtual Machine Code that can be run on any computer that contains the Java Virtual Machine and thus allowing it to be platform independent. When you execute a Java program, you will notice that a second file has been created with the same name as your program but with the file extension .class
This file contains the Java Virtual Machine Code and it is the only file that needs to be distributed to other computers in order to execute the program on them.
If you look at where you created your project in the file system, it will likely look as follows
The .java files are in the “src” folder and the .class files are in the “build” folder.
1. Write a Java Program that displays
Your program must demonstrate the use of both the print and println methods, and use 3 of the escape characters mentioned in the notes.
2. Draw a cool picture using characters like the example below. (They don’t have to be the pictures below, remember it is just practice!!!)
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page