IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Java Screen Output

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Create a Java Program
  • Print text to a Text Only Console

Writing a Java Program

Java programs can be written as either a structural program or an object-oriented program

  • Using a procedural approach, the program consists of specific functions called to accomplish specific tasks
  • Using an object approach, the program consists of individual objects that interact with each other as the program executes.

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

  • Linear Execution, where instructions are executed 1 by 1 in the order they are written
  • Event Driven Execution, where the program waits until certain events occur before executing instructions. Events could be Mouse Clicks, Key Presses, Timers, etc. Once an event occurs the instructions for that event are executed in a linear fashion.

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

 

IB-Computer-Science-Screen-Output-Java (1)

ProgramName is a name you choose for your class.

  • It should start with an Upper Case Letter.
  • The Class Name and the File Name must be the same.
  • The name you pick should be somewhat descriptive
  • So in this case you will be working with a file called ProgramName.java

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

  • Class indicates that this file is a template that could be used to create an object
  • Public indicates that this class is allowed to be used by other classes in different programs.

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.

IB-Computer-Science-Screen-Output-Java (2)

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

IB-Computer-Science-Screen-Output-Java (3)

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.

Output Text Messages to a Text Console

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.

  • The print method will output text and keep the cursor on the same line, while the println method will output text but move the cursor to the next line

Here is an example of how to make that work

 

IB-Computer-Science-Screen-Output-Java (5)

This is what was output on my screen

IB-Computer-Science-Screen-Output-Java (4)

Notice the following about the code:

  • The message you output must be a text message and must be inside a pair of “ “ marks.
  • Methods are called with a . notation: objectName.method();
  • Don’t forget the ; at the end of the line or you will get an error.

Special Output Characters

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

 

IB-Computer-Science-Screen-Output-Java (7)

Here is the output that showed up on my screen

IB-Computer-Science-Screen-Output-Java (6)

Execution of a Java Program

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

IB-Computer-Science-Screen-Output-Java (8)

The .java files are in the “src” folder and the .class files are in the “build” folder.

IB Computer Science Practice Questions

1. Write a Java Program that displays

  • Your Name
  • Your School and School Board
  • Your Favorite Music, TV Show, Movie, Color
  • Two Additional Pieces of Information about yourself that you want your classmates to know about.

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!!!)

IB-Computer-Science-Screen-Output-Java (1)
IB-Computer-Science-Screen-Output-Java (2)

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

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