Object Oriented Programming
(JAVA WORKSHOP)
Lecture 2
SOFTWARE
• Program is the set of instructions that tells a computer what to do.
• Software is a single program or a set of programs.
Software
Application System
software Software
• Application software are useful programs that a user might need. Example word
processors
• System software are special programs that help the computer to do its job.
Example Windows
PROGRAMMING
• Programming is the task of writing instructions for the computer.
• Programming Language is a special language used for writing instructions for
the computer.
• Programming Languages have a very strict set of rules called Syntax.
COMPILING PROGRAMS
• Program Code or Source Code are the set of instructions written in a
programming language.
• Binary instructions are the only instructions the computer understands and they
are written in the form (0's and 1's);
• Machine code is often referred to as the language of the computer
• Compiler translates source code to machine code.
• Compiling is the process of translating Source code into Machine Code
consisting of 1s and 0s
JAVA
• Java is an object oriented programming language.
• Java is platform-independent
• Java is case-sensitive
• Java can work within the World Wide Web of computers via browsers, such as
Netscape and Internet Explorer;
• Java programs that run on the Web are called applets;
PROGRAMMING IN JAVA
Other Programming Languages:
Programmer Machine
Source Code Compiler Program User
code
Java Programming Language:
Machine
Java Virtual Code for PC,
Java Java Java byte Machine
Source Code Mac, Unix,
programmer Compiler code
(JVM) Windows
etc.
How can it do this?
• Java compilers do not translate the program into machine code, but into special
instructions called Java byte code;
• Java Byte Code (which still consists of 0's and 1's), contains instructions that are exactly
the same irrespective of any computer;
• Java Byte Code is universal, whereas machine code is specific to a particular type of
computer;
• Java Virtual Machine (JVM) is a special used to translate each instruction for the
computer it is running on, before the instruction is performed.
INTEGRATED DEVELOPMENT
ENVIRONMENTS (IDEs)
• IDEs are special programs used for compiling and running programs.
• IDEs provides the user with an easy-to-use window into which code is types, other
windows will provide information about the working file and a separate window
provide shows errors.
• Common IDEs include NetBeans and Eclipse
A Typical IDE SCREEN
FIRST PROGRAM
when your program runs you will see the words
"Hello world" displayed.
ANALYSIS OF THE FIRST PROGRAM
• First Line is called the Header
public class Hello
• public: This word makes the class accessible to the outside world and to other classes.
A public class should be saved in a file with the same name as the class. Example
Hello.java
• class: Object Oriented languages require the program to be written in separate unit
called classes
• Hello: is the name of the class.
ANALYSIS OF THE FIRST PROGRAM contd.
class Name Guidelines
The name must not be a Java keyword such as void, static
The name must not have space in it
The name must not include operators or mathematical symbols
such as +, -
The name must start with either a letter, an underscore(_), or a
dollar sign($)
The convention in Java programs is to begin the name of a class
with upper case letter
ANALYSIS OF THE FIRST PROGRAM contd.
• Second and last lines are called braces { }
• Everything in the class has to be contained between these two braces; these tells the
compiler where the class begins and ends
• Third line is called the main method header
public static void main (String[ ] args)
• public and static are referred to as keyword
• Fourth and Sixth line braces {} mark the beginning and end of the instructions in a
method
ANALYSIS OF THE FIRST PROGRAM contd.
• Method Instructions
System.out.println(“Hello world”);
• System.out.println: This is used to get stuff printed on a screen.
• println: is short for print line
• ( ): Whatever is inside the bracket is displayed exactly on the screen.
• ; : Every Java instruction has to end with a semi-colon.
ADDING COMMENTS TO A PROGRAM
OUTPUT in JAVA
OUTPUT
System.out.println(“message”); System.out.print( (“message”);
• System.out.println( “message”) :The effect of this statement is to start a new line
after displaying whatever is in the brackets
• System.out.print(“message”): The effect of this statement is to display whatever is in
the bracket and continue on the same line.
System.out.println( “message”)
output :
Hello world
Hello world again!
System.out.print(“message”)
output
Hello worldHello world again!
INTRODUCING Strings
• Strings are called collections of characters such as "Hello world“.
• in Java, literal strings like this are always enclosed in speech marks;
• two strings can be joined together with the plus symbol (+);
• when using this symbol for this purpose it is known as the concatenation operator.
For example
Output: Hello World
Note: spaces are included inside the
“ ” else they have no effect
EXPRESSION
Object Oriented Programming
(JAVA WORKSHOP)
Lecture 3
Data Types
Declaring variables in Java
• Data types are used in programs to create named locations in the computer's
memory that will contain values while a program is running;
• Declaring is the process of creating named locations in the computer's memory
that will contain values while a program is running ;
• Variables are the named locations .
variable Name Guidelines
The name must not be a Java keyword such as void, static
The name must not have space in it
The name must not include operators or mathematical symbols such as +, -
The name must start with either a letter, an underscore(_), or a dollar sign($)
the convention in Java programs is to begin the name of a variable with a
lower case letter.
Variables that comprise of two words (E.g difficulty level) can be written as
difficulty_level or difficultyLevel
variable Declaration
Format
dataType variableName ;
if they are all of the same type:
several variables can be declared on a single line
if they are all of the same type:
Assignments in Java
• Assignments allow values to be put into variables;
• they are written in Java with the use of the equality symbol (=);
• this symbol is known as the assignment operator;
Format
variableName = value;
For example
Declaring & Assignments in Java
Declaring & Assignments in Java
• This will not compile in java
int score=2.5;
• This will compile in java
double score = 20;
Creating constants
• Constants are values that remain constant throughout a program (as opposed
to variable);
• there are occasions where data items in a program have values that do not change.
• constants are declared much like variables in Java except that they are preceded by the
keyword final, and are always initialized to their fixed value.
For example
Java Convention for naming constant is upper case
Arithmetic operators
For example
terms on the right-hand side of assignment operators
(like 10 + 25) are referred to as expressions.
double cost;
Cost=500 * (1+17.5/100)
Output = 587.5
Order of evaluation
brackets
division
multiplication
addition
subtraction
Expressions in Java
• The expression on the right-hand side of an assignment statement can itself
contain variable names;
• if this is the case then the name does not refer to the location, but to the contents of the
location.
For example
Variable & Expression
• You can use the name of the variable you are assigning to in the expression
itself
• this means that the old value of the variable is being used to calculate its new value.
For example
A special shorthand
• if a variable x has been declared as an int, then the instruction for incrementing
x would be : x = x + 1;
• this is so common that there is a special shorthand for this instruction: x++;
• the '++' is therefore known as the increment operator;
• similarly there exists a decrement operator, '--':
x--;
is shorthand for:
x = x - 1;
Input in Java: the Scanner class
• in order to use the Scanner class we have to place the following line at the
beginning of our program:
• having imported the util package, you will need to write the following
instruction in your program.
• this declares a Scanner object
Input methods of the Scanner class
• if we want a user to type in an integer at the keyboard:
• in the case of a double, y:
• in the case of a char, c:
Program design
• Implementation is writing the actual code.
• Java programs consist of one or more classes, each with one or more methods.
• Pseudocode is used to sketch out the code for your methods especially if there
are complex.
Example
EXERCISES
• What will be the output? • What will be the output?
public class Practice1{ public class Practice2{
public static void main(String[ ] args) public static void main(String[ ] args)
{ {
System.out.print(“Hello, how are you? ”); System.out.println(“1+2 ” + “+ 3” + “= 6”);
System.out.println(“Fine thanks”); }
} }
}
EXERCISES Contd.
Identify syntax error Identify syntax error
public class { Public class {
public static void main(String [ ] args) Public static void main(string [ ] args)
{ {
system.out.println(I want this program to system.out.Println(where are the errors);
run)
EXERCISES Contd.
• Write a program that displays your name, address and telephone number, each on
a separate line.
• Adapt the above program to include a blank line between your address and
telephone number
• Write a program that displays your initials in big letters made of asterisks
• What would be the appropriate java data type to use for the following items
• The number of students in a class.
• The height of each student in the class
• The grade obtainable in CSC3302
EXERCISES Contd.
• Write a program to calculate and display the cost of a product after a tax has been
added.
• Given that price=500, tax=17.5 and cost= price * (1+ tax/100)
• Modify the program above so that the user enters the value for price and tax.
• Write a program that asks the user to enter the values for the length and height of a
rectangle and then displays the area. (Hint: A= l *b. )
• Design and implement a program that asks a user to enter values for the radius of a
circle; then displays the area and circumference of the circle (Hint Area= ∏r^2,
Circumference = 2 ∏r)
EXERCISES Contd.
• Convert the pseudocode below into a program
Begin
prompt for values in pounds
enter values in pounds
set value to old value / 2.2 ( to convert it to kilo; hint 1 kilo=2.2 pound)
display value in kilo
End
• The students in CSC3302 have been asked to form teams of a specific size
for their coursework. Design and implement a program that ask for the
number of students in the group and the size of the teams to be formed. The
program should display how many teams can be formed and how many
students are left without a team
The END