0% found this document useful (0 votes)
26 views

Com 323 Programming in Java Week 01

The document provides an overview of programming in Java for week 1 of a course. It discusses common programming terms, describes computer programming and the compilation process, and explains programming in Java. Key points include that Java source code is compiled into bytecode that can run on any system with a Java Virtual Machine (JVM), and the JVM allows Java programs to run across different operating systems. It also discusses Integrated Development Environments (IDEs) and practical exercises for the course.

Uploaded by

Wil Son
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Com 323 Programming in Java Week 01

The document provides an overview of programming in Java for week 1 of a course. It discusses common programming terms, describes computer programming and the compilation process, and explains programming in Java. Key points include that Java source code is compiled into bytecode that can run on any system with a Java Virtual Machine (JVM), and the JVM allows Java programs to run across different operating systems. It also discusses Integrated Development Environments (IDEs) and practical exercises for the course.

Uploaded by

Wil Son
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

COM323 Programming in Java

Week 01

© 2021. Cavendish University. Private and Confidential


Agenda

Learning outcomes of this module are to:

1. Explain common programming terms


2. Describe computer programming
3. Discuss programming in Java
4. Explain the compilation process
5. Describe the role of IDEs
6. Perform a practical exercise
7. Describe Java applications
8. Explain Java programming

17-Feb-22 © 2021. Cavendish University. Rights Reserved 1


1. Some common terms

• Any student starting a particular programming Language for the


first time would be anxious to do just one thing -get started on
their first program.

• Designing and writing computer programs can be one of the most


enjoyable and satisfying things you can do, although it can seem a
little daunting at first.

• Before you start writing programs, there's need to be sure that


you understand what is meant by important terms such as
program, software, code and programming languages.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 2


1. Some common terms | Software

• A computer is not very useful unless we give it some instructions


that tell it what to do.
• This set of instructions is called a program.
• Programs that the computer can use can be stored on electronic
chips that form part of the computer, or can be stored on devices
like hard disks, CDs, DVDs, and USB drives (sometimes called
memory sticks), and can often be downloaded via the Internet.
• The word software is the name given to a single program or a set
of programs.
• There are two main kinds of software: System software and
Application software.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 3


1. Some common terms | Applications Software

• This is the name given to useful programs that a user might


need; for example, word-processors, spreadsheets, accounts
programs, games and so on.

• Such programs are often referred to simply as applications and


perform specific tasks for the user.

• MS Word for creating documents like CVs, MS Excel for budgets,


computation and analysis of numbers.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 4


1. Some common terms | System Software

• This is the name given to special programs that make computer


hardware usable; for example, operating systems (such as UNIX™
or Windows™, which help us to use the computer) and network
software (which helps computers to communicate with each
other).

• Software is no longer restricted simply to computers themselves.

• Many of today’s devices—from mobile phones to microwave


ovens to games consoles—rely on computer programs that are
built into the device.

• Such software is referred to as embedded software.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 5


2. Computer programming

• Both application and system software are built by writing a set of


instructions for the computer to obey.

• Programming, or coding, is the task of writing these instructions.

• These instructions have to be written in a language specially


designed for this purpose. These programming languages include
C, Visual Basic, C++, Pascal, C#, Python and many more.

• Ultimately these instructions have to be translated into a


language that can be understood by the computer.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 6


2. Computer programming

• The computer understands only binary instructions—that means


instructions written as a series of 0s and 1s.

• So, for example, the machine might understand 01100111 to


mean add.

• The language of the computer is often referred to as machine


code.

• A special piece of system software called a compiler translates


the instructions written in a programming language into machine
instructions consisting of 0s and 1s.

• This process is known as compiling.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 7


2. Computer programming

17-Feb-22 © 2021. Cavendish University. Rights Reserved 8


2. Computer programming

• Programming languages have a very strict set of rules that you


must follow.

• Just as with natural languages, this set of rules is called the


syntax of the language.

• A program containing syntax errors will not compile.

• You will see when you start writing programs that the sort of
things that can cause compiler errors are the incorrect use of
special Java keywords, missing brackets or semi-colons, and
many others.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 9


2. Computer programming

• If, however, the source code is free of such errors the compiler
will successfully produce a machine code program that can be
run on a computer, as illustrated.

• Once a program has been compiled and the machine code


program saved, it can be run on the target machine as many
times as necessary.

• When you buy a piece of software such as a game or a word


processor, it is this machine code program that you are buying.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 10


3. Programming in Java | JVM

https://www.tiobe.com/tiobe-index/

17-Feb-22 © 2021. Cavendish University. Rights Reserved 11


3. Programming in Java | JVM

• How is this achieved? The answer lies in the fact that any Java
program requires the computer it is running on to also be
running a special program called a Java Virtual Machine or JVM
for short.

• This JVM is able to run a Java program for the particular


computer on which it is running.

• For example, you can get a JVM for a PC running Windows™;


there is a JVM for a MAC™, and one for a Unix™ or Linux™ box.

• There is a special kind of JVM for mobile phones; and there are
JVMs built into machines where the embedded software is
written in Java.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 12


3. Programming in Java | JVM

• This machine code would contain the particular instructions


appropriate to the type of computer it was meant for. Java compilers
do not translate the program into machine code—they translate it
into special instructions called Java byte code.
• Java byte code, which, like machine code, consists of 0s and 1s,
contains instructions that are exactly the same irrespective of the
type of computer—it is universal, whereas machine code is specific
to a particular type of computer.
• If you do not have a JRE on your computer (as will be the case with
any Windows™ operating system), then the entire Java Development
Kit (JDK), comprising the JRE, compiler and other tools, can be
downloaded from Oracle™, the owners of the Java platform.

NOTE: The original developers of Java were Sun Microsystems™. This company was
acquired by Oracle™ in 2010.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 13


3. Programming in Java | JVM

17-Feb-22 © 2021. Cavendish University. Rights Reserved 14


3. Programming in Java | JVM

17-Feb-22 © 2021. Cavendish University. Rights Reserved 15


3. Programming in Java | JVM

• Java is one of the most popular programming languages.

• It is a descendant of the programming language Cand is much


related to C++. Java, like C++, embodies the concept of object-
oriented programming, which allows a programmer to define a
type of data with its permissible set of operations.

• To execute a Java program on a machine, the machine needs an


installation of the Java Runtime Environment (JRE).

• A major part of JRE is the Java Virtual Machine (JVM).

• JVM creates an environment where Java programs interact with


the hardware.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 16


3. Programming in Java | JVM

• A programmer creates a Java program by writing its source code.

• A source code is a text file that describes the program according


to the syntax of Java and has the file name extension .java.

• An executable Java program generated from a source code is


called Java bytecode, and has the file name extension .class.

• To generate a Java program from a source code, the machine


needs an extension of JRE called the Java Development Kit (JDK).

17-Feb-22 © 2021. Cavendish University. Rights Reserved 17


3. Programming in Java | JVM

• The Java language that comes with JDK consists of two parts.
• The first part, called java.lang, is an essential component of Java.
• The second part is a collection of source codes that can be
selected and added to the Java program being written.
• To write and edit a Java source code, a text editor is needed.
Some editors understand the syntax of Java and offer a variety of
assistance.
• The process of generating Java bytecode from a Java source code
is called compilation.
• A primitive way to compile a Java source code is to execute a
compilation in a command line interface.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 18


3. Programming in Java | JVM

17-Feb-22 © 2021. Cavendish University. Rights Reserved 19


4. Source Code, Bytecodes and Compilation

• As mentioned earlier, all Java source files must have the file
name extension .java.

• These can be created using a text editor but must be saved with
the .java extension and are called Java files.

• To compile, for example, the source code cuz.java in a command


line interface, one must type the command:

javac cuz.java

NOTE: compiler takes note of case sensitivity of file names

17-Feb-22 © 2021. Cavendish University. Rights Reserved 20


5. Integrated Development Environments

• It is very common to compile and run your programs by using a


special program called an Integrated Development Environment
or IDE.

• An IDE provides you with an easy-to-use window into which you


can type your code; other windows will provide information
about the files you are using; and a separate window will be
provided to tell you of your errors.

• Not only does an IDE do all these things, it also lets you run your
programs as soon as you have compiled them.

• Examples of IDEs are NetBeans and Eclipse, among many others.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 21


5. Integrated Development Environments

• It is perfectly possible to compile and run Java programs without


the use of an IDE—but not nearly so convenient.

• You would do this from a command line in a console window.

• The source code that you write is saved in the form of a simple
text file which has a .java extension.

• The compiler that comes as part of the JDK is called javac.exe,


and to compile a file called, for example, MyProgram.java, you
would write at the command prompt:

javac MyProgram.java

17-Feb-22 © 2021. Cavendish University. Rights Reserved 22


5. Integrated Development Environments

• This would create a file called MyProgram.class, which is the


compiled file in Java byte code. The name of the JVM is java.exe
and to run the program you would type:

java MyProgram

• Whereas students of this class are at liberty to use an IDE of their


choice, demonstration is done to show how painstaking it is to
compile a Java program from the command prompt.

• Subsequently, it is recommended that students use an IDE such


as NetBeans™ or Eclipse™

17-Feb-22 © 2021. Cavendish University. Rights Reserved 23


6. Practical Exercise | Setup JDK and get your first java app running

For class exercise this👇will be useful:

1. Download JDK (Java Development Kit) which is your compiler


2. Install the JDK and trace the path in program files to the "bin"
folder within C:\Program Files\Java\jdk1.8.0_241\
3. Copy the full path C:\Program Files\Java\jdk1.8.0_241\bin on a
Windows 10 PC, right-click on 'This PC' and choose properties.
Next select 'Advanced system settings' then select 'Environment
Variables'
4. Under System Variables, select New. The type should be path
and the for value paste C:\Program Files\Java\jdk1.8.0_241\bin

17-Feb-22 © 2021. Cavendish University. Rights Reserved 24


6. Practical Exercise | Setup JDK and get your first java app
running

public class CUZ{


public static void main(String[ ] args)
{
System.out.println(“Hello CUZ Java Class 2022”);
}
}

17-Feb-22 © 2021. Cavendish University. Rights Reserved 25


7. Java Applications

• Java applications can run on a computer, on such devices as mobile


phones and games consoles, or sometimes can be embedded into
an electronic device.

• In the last case you would probably be unaware of the fact that the
software is running at all

• In the former cases you would be seeing output from your program
on a screen and providing information to your program via a
keyboard and mouse, via a touch screen, or via a joystick or game
controller.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 26


7. Java Applications

• The screen that provides output from your program, and


prompts you to enter information, is known as the user
interface. There are two principal types of user interface:

• Text based

• Graphics based

• With text-based user interfaces, information is displayed


simply as text—with no pictures.

• Text based programs make use of the keyboard for user input.

• Text based programs are known as console applications.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 27


7. Java Applications

If you are using an IDE, the console window is usually integrated


into the IDE.

Running a program from the command prompt you will see a


window similar to that shown below;

17-Feb-22 © 2021. Cavendish University. Rights Reserved 28


7. Java Applications

• You are probably more accustomed to running programs that


have a graphical user interface (GUI).

• Such interfaces allow for pictures and shapes to be drawn on


the screen (such as text boxes and buttons) and make use of
the mouse as well as the keyboard to collect user input.

• Eventually it is desirable for your programs to have graphical


interfaces, but these obviously require more programming
effort to create than simple console applications.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 29


7. Java Applications

• The introductory sections concentrate on getting the program logic


right and will typically be characterized by console style
applications as you learn the fundamentals of programming in Java.

• Once you have mastered these fundamentals, you will be ready to


create attractive graphical interfaces.

• An example of a Graphical User Interface (GUI) program is given in


the next slide.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 30


7. Java Applications

17-Feb-22 © 2021. Cavendish University. Rights Reserved 31


8. Java Programming | Visibility

• The three words in the first line: public class cuz state that:
a) this is a program unit of type class,
b) the unit is named cuz, and
c) the unit is accessible from all other Java program units.

• There are four kinds of program units: class, interface abstract


class, and enum.

• The keyword public specifies that the unit is accessible from other
program units. A keyword that specifies the accessibility of program
units and their components is called a visibility attribute.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 32


8. Java Programming | Declaration

• There are three explicit visibility types: public, private, and


protected.

• The default visibility type, when there is no explicit visibility


attribute, is the package visibility.

• The order of appearance of the three components,


VISIBILITY_TYPEUNIT_TYPENAME applies to all Java source code
files.

• Methods and their Declaration: curly brackets{ } are used to


enclose units, components, and code blocks. For example, the
declaration of each program unit (such as class and interface) should
be followed by a matching pair of curly bracket. Two matching pairs
appear either one after the other or one inside the other.
17-Feb-22 © 2021. Cavendish University. Rights Reserved 33
8. Java Programming | Declaration

• A method has a name and defines a set of actions needs to be


performed.
• Some methods process information given to the in the form of
parameters.
• Some methods report the results through the use of return values.
• A special kind of method is the method main.
• Each method main takes the form of: public static void main(
String[ ] args ) as its declaration.
• In the case of the method main, the attribute public states that the
method is accessible from outside, the attribute static states that
the method is part of some executable program, the return type
void states that the method has no return value, and String[ ] args
specifies that the parameter is args and its data type is String[ ].

17-Feb-22 © 2021. Cavendish University. Rights Reserved 34


8. Java Programming | Output

• This is the way we are always going to get stuff printed on a simple
text screen; we use System.out.println (or sometimes
System.out.print, as explained below) and put whatever we want
to be displayed in the brackets.

• The println is short for “print line” by the way.

• You won’t understand at this stage why it has to be in this precise


form (with each word separated by a full stop, and the actual
phrase in double quotes), but do make sure that you type it exactly
as you see it here, with an upper-case S at the beginning.

• Also, you should notice the semi-colon at the end of the statement.
This is important; every Java instruction has to end with a semi-
colon.
17-Feb-22 © 2021. Cavendish University. Rights Reserved 35
8. Java Programming | Classes

• The first, and most important, thing to pay attention to is the word
class.
• We noted earlier that Java is referred to as an object-oriented
programming language.
• Now, the true meaning of this will start to become clear in later
stages -but for the time being you just need to know that object-
oriented languages require the program to be written in separate
units called classes.
• The simple programs that we are starting off with will contain only
one class (although they will interact with other classes from the
“built-in” Java libraries).
• We always have to give a name to a class and in this case we have
simply called our class HelloWorld.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 36


8. Java Programming | Classes

• When choosing a name for a class, you can choose any name as
long as:

• the name is not already a keyword in the Java language


(such as static, void);

• the name has no spaces in it;

• the name does not include operators or mathematical


symbols such as + and −;

• the name starts either with a letter, an underscore (_), or a


dollar sign ($).

17-Feb-22 © 2021. Cavendish University. Rights Reserved 37


8. Java Programming | Classes

• So, the first line tells the Java compiler that we are writing a class
with the name HelloWorld.
• However, you will also have noticed the word public in front of the
word class; placing this word here makes our class accessible to the
outside world and to other classes—so, until we learn about
specific ways of restricting access (in the second semester) we will
always include this word in the header.
• A public class should always be saved in a file with the same name
as the class itself—so in this case it should be saved as a file with
the name Hello World.java.
• Notice that everything in the class has to be contained between
two curly brackets (known as braces) that look like this {}; these tell
the compiler where the class begins and ends.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 38


8. Java Programming | Methods

• Main Method. A method contains a particular set of instructions


that the computer must carry out.

• Our HelloWorld class contains just one method and this line
introduces that method.

• In fact it is a very special method called a main method.


Applications in Java must always contain a class with a method
called main: this is where the program begins.

• A program starts with the first instruction of main, then obeys each
instruction in sequence (unless the instruction itself tells it to jump
to some other place in the program). The program terminates
when it has finished obeying the final instruction of main.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 39


8. Java Programming | Keywords

• NOTE: public static void main (String[ ] args) could also be written
with the angle brackets after args i.e. public static void main (String
args [ ]).

• At the moment we will not worry about the words public static void
in front of main, and the bit in the brackets afterwards (String[]
args) —we will just accept that they always have to be there; you
will begin to understand their significance as you learn more about
programming concepts.

• The top line of a method is referred to as the method header and


words such as public and static, that are part of the Java language,
are referred to as keywords.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 40


8. Java Programming | Syntax Errors

• A syntax error is a part of source code that fails to conform to the


Java syntax.
• If a source code contains syntax errors, the Java compiler, instead of
producing the bytecode, produces an error message stating that
there are syntax errors.
• If there is a bytecode generated from the prior successful
compilation, that code remains the same without being updated.
• There are two other types of errors: run-time errors and logic
errors.
• Runtime errors are those that occur during the execution of code,
interrupting the execution.
• They often result in a premature termination of the program.
• Logic errors do not necessarily result in run-time errors, but occur
due to misconception or flaws in the logic of the programmer.
17-Feb-22 © 2021. Cavendish University. Rights Reserved 41
8. Java Programming | Comments

17-Feb-22 © 2021. Cavendish University. Rights Reserved 42


8. Java Programming | Comments

Adding Comments:
• It is possible to insert texts that have no relevance to how the code
runs. Such texts are called comments.
• Comments are free-form texts. Java compilers ignore comments
when producing class files and so they exist only in the source file.
• A programmer can use comments to make notes to him or herself.
For example, comments can be about the expected behaviorof the
program and about the underlying algorithms.
• (//) at the beginning of the line
• The comment is enclosed between two special symbols; the
opening symbol is a slash followed by a star (/*) and the closing
symbol is a star followed by a slash (*/). Everything between these
two symbols is ignored by the compiler.

17-Feb-22 © 2021. Cavendish University. Rights Reserved 43


Thank You

© 2021. Cavendish University. Private and Confidential

You might also like