01
  • Intro
  • 1.1
  • 1.2
  • 1.3
  • 1.4
  • 1.5
  • 1.6
  • 1.7
  • 1.8
  • 1.9
  • 1.10
  • 1.11
  • 1.12
  • Summary
  • 1.1 Programs

    To get a computer to do a job, you need to give it instructions in a language that it can understand. Computers are able to understand, a range of languages that have more limited vocabulary and stricter rules than the natural languages that humans speak. These are known as programming languages. The language you will be learning to use in this module is called Java.

    You often want a computer to do the same job over and over again. Most jobs require a sequence of many instructions to be carried out. Rather than requiring a user to issue these instructions every time, it is better to assemble them into a collection of instructions, known as a program, which can be executed, or run, each time the job needs to be done. The text that defines the instructions is known as program code. Programs are often known as software, in contrast to the physical components of a computer, which are known as hardware.

    Programming is the process of developing a program by writing code in a programming language, and is one of the key skills needed by a Computing professional.

  • 1.2 Computers everywhere

    In the early 1960’s there were a few thousand computers in the world, and these were large systems that would each fill an entire room. Nowadays, as well as the billions of personal computers and laptops which are in use, computers are all around us in places which are not all obvious. Many people carry smartphones which are actually sophisticated computers which can run a huge range of programs, or apps. Computers are embedded in many everyday products, such as cars, washing machines, smart televisions and so on, and these computers have been programmed to provide ways of controlling the functions of these items. When you browse the web, for example to shop online, you are interacting, not just with the device you are holding or sitting in front of, but also over the internet with powerful computers known as servers which are programmed to provide the functions of the web sites you use.

    In all these cases, the computer will not do anything useful unless it is programmed to do so. For example, when you use the on-screen schedule guide on a modern television you are running a program which a programmer has written to instruct the computer that is built into the television.

  • 1.3 Computers and programs

    Every computer is designed to provide a basic set of capabilities:

    • Executing instructions
    • Storing information needed while executing instructions
    • Communicating with external devices, such as keyboards, screens, permanent storage devices, networks

    A programming language lets you, as a programmer, write program code which makes use of these capabilities to perform some useful function. You are likely to learn about the components that computers have in order to provide these capabilities elsewhere in your course (for example in the module Fundamentals of Computing).

    A program can be just a few lines of code that perform a simple task. However, many of the programs that are written today are very complex. Building complex software is in some ways like building a complex physical structure such as a bridge or an aircraft, and should be done similarly using an engineering process involving: analysing what the software needs to be able to do; designing a solution to meeting those needs; implementing the software according to the design; and testing to make sure it all works.

    This process is called software engineering, and you may learn more about this elsewhere (for example in the module Fundamentals of Software Engineering). Programming is essentially one – a very important aspect! – of the process of implementing software. However, programming is also closely related to other parts of the software engineering process, and in this module you will learn how to apply some important software engineering techniques.

  • 1.4 Objects

    Java, like many of the most widely-used languages, is an object-oriented programming language, and a program written in Java is an object-oriented program. This means that Java allows you to create in the computer a model of some part of the world. This model is built up from objects, and is at the core of an object-oriented program.

    Objects in many cases represent “things” which exist in the real world. For example, a program that allows customers to buy books online will use objects representing customers, books and orders. A word processor program will deal with objects representing words and paragraphs. A computer game may deal with objects representing characters and scenes.

    When an object-oriented program is running, it creates objects which do their own jobs and also work together, or collaborate, to perform the required actions. In the real world there are many examples of components that work together like this, whether it is parts of a machine or people in a team. Just like in the real world, the success of a program depends on all the objects doing their job, and also, crucially, on the communication between them.

  • 1.5 Classes

    An OO program creates the objects it needs as it runs. Often there will be more than one object of the same type created. A game may have several characters of the same type, for example, and a word processor will need to deal with many words. These objects will be similar, and will be able to do the same things, but will also have properties that distinguish each one from others of the same type.

    In order to know how to create an object of some type, the program needs to have a template which specifies what an object of that type can do. This template is called a class. In fact, when you write the code for a program, you are actually writing the classes that are used to create objects. An object is a single instance of a class – there can be many objects of the same type.

    A class specifies the following for the objects it is used to create:

    • The name of the type of object (e.g. Customer)
    • The properties which each object will have (e.g. name)
    • The actions which each object can perform (e.g. change password)
    • The way in which each object is linked with other objects (e.g. with Order objects)

  • 1.6 Working with objects

    Let’s see how this works by watching some demonstrations of creating and working with some actual Java objects. For now we will use classes that have already been written – from week 3 you will learn to create your own classes in Java.

    Note that these demonstrations introduce quite a few of the really important ideas of object-oriented programming, and there is a lot to take in! Don’t worry, we will look again at all these ideas and how you make use of them to write your own programs as we go through the module.

    Throughout this module I will use the BlueJ Integrated Development Environment (IDE) throughout this module to create objects, classes and programs in Java. You will also use BlueJ yourself. You can watch the demonstrations and then try the activities in them for yourself, and each week you will have a practical lab activity to do using BlueJ.

    In the first demo you will see how to open an existing project in BlueJ. You can download the project here and you can follow the steps of this demo and the ones that follow on your own computer. Click the link below to watch the demo.

    📹 WATCH DEMO: First look at BlueJ

    The link below will show you more information about how you can get hold of BlueJ for your own computer. While viewing the presentations throughout the module, any time you see a link with a lightbulb symbol you should click it to see important extra information

    💡 Getting hold of BlueJ

    X


    BlueJ is installed on the PCs in the computer lab on campus (it may be in a virtual machine). If you want to use it on your own computer it is easy to install. You will also need to install the Oracle Java SDK which BlueJ needs to create and run Java programs (and to run at all, as it is a Java program itself). You can find the installer and full instructions at www.bluej.org

    X
  • 1.7 Creating an object

    The presence of a Bicycle class in the project means that objects of that type can be created (and the same applies to Rider). But why would we want to create a bicycle object in a program? As we said earlier, objects often model some part of the world. What do we mean by “model”? An object which models a real-world “thing” (or entity) should behave in some way like the real thing. It doesn’t have to do everything that the real thing can do, but needs to model the behaviour that is relevant to the program that will need to use the object.

    Software objects representing bicycles might be useful in, say, a bike simulator program or a game program that involves bikes. The user or player would use some input device to give commands corresponding to the actions, changing gear for example, of a real rider on a bike, and the “model” bike should respond like a real bike would to those actions.

    Not everything about a real entity will be relevant. To take a different example, a customer object in an online bookstore program represents a real person, and it is not necessary to include, say, the customer’s favourite colour in the model as it is not relevant to buying books. The customer’s favourite author might be included in the model, though, as this might help the program recommend new books to buy.

    In the example you are looking at here, our Bicycle objects will simply model the way that the road speed of a real bike responds to change in pedalling speed (revolutions per minute, or RPM) and gear selection. This is a very simple model, which doesn’t include complications such as steering (everything in this particular “world” happens in a straight line!)

    📹 WATCH DEMO: Creating objects
    X
  • 1.8 Calling a method

    Now that an object has been created we want to make it do something useful. You usually make a real bicycle change gear by moving a lever – how do we make this model object change gear? In the demo you have seen that right-clicking on an object lets you inspect its properties. The right-click menu also gives you the option to make the object perform one of its actions. These actions are performed by calling methods, and each menu choice is the name of a method. Calling the method changeGear is the model equivalent of moving a real gear lever.

    Parameters

    If you want to change how fast you pedal a real bicycle, you need to know just how fast you want to pedal. If you want to change gear, you need to know whether move the gear lever to change up to a higher gear or down to a lower gear. When you call a method to model one of these actions, you need to provide information on how to perform the action. You can provide information to a method using parameters. A parameter is a piece of information that a method needs in order to perform its action. setPedalRpm needs to be told what value to set, while changeGear needs to be told in what direction to change.

    Return values

    If you pedal a real bicycle faster you immediately notice a change as the bicycle speeds up. The bicycle doesn’t actually tell you it’s going faster though – you would only know this because you see your surroundings going past you faster! Similarly, calling the setPedalRpm method of a Bicycle object gives no immediate response. In fact, you would have to inspect the object to see that it had made a difference. The method is essentially an instruction to the object. The object carries out the instruction but does not respond to the user to say it has been done. Sometimes you want an object to respond to a method call, so that the method call is more like a question than an instruction. Let’s say you want to know the speed of the bicycle object. This will depend on how rapidly it is being pedaled and also what gear it is in (increasing the gear will cause the road wheel to turn more times for each rotation of the pedals, so that the bicycle goes faster). Our bicycle can calculate its speed by using the values of its pedalRpm and gear properties. You can ask it to do so by calling the method speed.

    📹 WATCH DEMO: Calling methods

    Sometimes it makes sense for a method to have both parameters and a return type. When you change gear on a real bicycle you may be able to get an immediate response to the action that tells you what gear the bicycle is, such as a numeric indicator or simply the physical position of the lever. It might improve our model of the bicycle if the changeGear method returned a value that indicated the new value of gear. What would be the return type of the method if it were modified to do this?

    X
  • 1.9 Data types

    What kind of information is the direction parameter that was entered in the demo? The method call dialog asks you to enter an int value, which means the value should be a whole number, or integer. int is the type of the parameter data. When you inspect the object, you can see that most of the properties are also int values. However, thewheelDiameter is a different type, as the size of a wheel should not necessarily be an integer. The data type of wheelDiameter is called double, which means the value can be a floating-point number with a fractional part. Java supports a number of data types for the information used in programs, including numbers, characters, true/false, and many others, and you will learn more about these shortly.

    💡 What's going on in the computer?
    X

    How is Java making use of the capabilities of the computer while you manipulate the bicycle object?

    Executing instructions – a method is a set of instructions, and the computer executes these instructions when the method is called

    Storing information needed while executing instructions - parameter values and the properties of the object need to be stored so that they can be used by the instructions as the method runs

  • 1.10 Objects working together

    Most object-oriented programs need more than one type of object. So far you have only created bicycles. In our BlueJ project e there is another class, Rider, so it looks like you can put a rider onto each bicycle and make the rider and bicycle work together somehow. This interaction between different types of objects is a crucial part of making object oriented programs work. The next demo will show how to create two objects and make them interact.

    📹 WATCH DEMO: Interacting objects
    X
  • 1.11 Programming Objects

    So far you have seen how to create objects and call their methods using BlueJ’s menus. This is useful when learning about object-oriented programming and creating and testing classes.

    However, as we said earlier, a Java program creates and uses objects as it runs. A program will do so as a result of running instructions written in Java code. You can see the equivalent Java code when you are working in BlueJ by opening the Terminal window, and switching on the Record Method Calls option. You will learn to write your own code using the Java language soon.

    📹 WATCH DEMO: Creating objects and calling methods with code
    X
  • 1.12 Source code

    You have seen how to create objects in BlueJ as instances of classes. The classes define what properties and actions these objects can have. Sometimes when you are writing a program you can take classes that someone has created and use them, as you have seen in these examples. Most often, though, you need to create your own classes which allow you to define exactly what kind of objects your program can use, and what properties and actions they can have. In many cases you will use a combination of your own classes and classes that have already been written.

    You create a class by writing Java source code. In fact, when you write an object-oriented program you are mainly writing or modifying classes. You will learn in detail how to write source code to define a class shortly. For now, let’s try to make a simple change to the Bicycle class.

    📹 WATCH DEMO: Looking at Java source code

    Why do you need to compile the code? Well, we said earlier that to program a computer you need to give it instructions in a language it understands. Unfortunately, the computer doesn’t actually understand Java (or any other programming language, in fact). It only really understands very simple instructions which consist of binary numbers, called machine code. It is very difficult for human programmers to create useful programs by writing these instructions. Programming languages such as Java are known as high-level languages, which can be translated, or compiled, by another program called a compiler into machine code that the computer can execute. Because programming languages have strict rules (syntax) it is feasible to translate source code to machine code – it would be unfeasible to compile instructions given in natural language. It is much easier to write useful programs using a high-level language than using machine code.

    The compiler doesn’t just translate – it also checks that your source code obeys the rules of the programming language. If your code breaks any rules it can’t be translated into machine code successfully, and the compiler will give you one or more error messages. Seeing a compiler error message is not something to get upset about. All programmers, from novices to professionals, encounter compiler errors as they write code. An important part of programming is reading error messages, identifying the errors and fixing them. This can seem frustrating to beginners, but in fact is a process of the programmer working together with the compiler to craft perfect code!

    Let’s try to break a rule and see how the compiler helps.

    📹 WATCH DEMO: Compiling source code
    X
    X
  • Summary

    You’ve been introduced in this lecture to the following concepts:

    Computers and Programming, Objects, Classes, Methods, Parameters, Data types, Return values, Object interaction, Source code and Compilers

    In the next lecture you will start to learn how to write your own Java code.


    Download PDF version