Download presentation
Presentation is loading. Please wait.
1
SSD1 – Introduction to Information Systems
Fundamentals of Java Lecture 5
2
Data Types Any objects in the real world have attributes. Attributes are characteristics of objects Each attribute may take on specific kinds of information. In programming terms, we would say that an attribute has a specific data type Data types – are the specific kinds of information
3
Data Types One of the defining characteristics of a data type is its size in computer's memory. Memory is measured in bits (binary digit). A binary digit can take two values, 0 or 1. A memory bit can store two states. Bits can be combined to store information that is more meaningful.
4
Floating Point (from +-4.9E-324 to +-1.7976931348623157E+308)
Primitive Data Types Type Represents Size Example boolean Boolean value (true or false) 1 bit true double Floating Point (from +-4.9E-324 to E+308) 64 bits 3.14 int Integer value (from -2,147,483,648 to 2,147,483,647) 32 bits 6
5
Reference Data Types Reference Types are more advanced types then primitive types Every Java class is a reference type Reference types range from very simple classes built solely from primitives to very complex types One relatively simple reference type is the String class
6
Variables Variables are a fundamental building block of programming.
A variable represents a placeholder or container for information A Java variable can be of certain type but every variable in Java must be of a certain type. Strict typing also has an effect on memory use and allocation
7
Declaring Variables Java, as a strongly typed language, requires the explicit designation of a data type when declaring a variable This data type cannot change within the program Similarly, if a Java program expects to receive data of a specific type, it will not accept data of any other type
8
Declaring Variables Declaring a variable is specifying a data type and an identifier (a name) for that variable Declaring a variable will allocate enough space in memory to store the variable's data type.
9
DataType variableName;
Declaring Variables The generic declaration of a variable follows the following format DataType variableName; For example, the following are valid variable declarations int age; String name; double salary;
10
Naming Variables Rules: Conventions
Identifiers can contain letters, underscores ( _ ), or the dollar sign ( $ ) Identifiers cannot start with a number Identifiers may not contain spaces or other special characters or punctuation marks Identifiers cannot be a Java keyword Conventions Identifiers should begin with a lower-case letter Identifiers should be descriptive nouns, adjectives, or noun/adjective phrases
11
Valid Variable Identifiers Invalid Variable Identifiers
Naming Variables Valid Variable Identifiers Invalid Variable Identifiers x y personalGreeting _userID $backgroundImage double 3sisters State Capitol
12
Using Variables Assignment String myStr; int x; int y;
The following assignments are possible myStr = "This is a string"; x = 5; y = 3; x = y;
13
Using Variables Initialisation String myStr = "This is a string";
Default Values If a variable does not have a value assigned, Java helps by initializing the variable to a default value (whatever value is specified as the default for that type) Reference variables are initialized to null which means that the variable does not refer to any object.
14
Using Variables Scope
15
Arithmetic Operators and Expressions
int x = 100; Operation Operator Example Evaluates to: Addition + x + 5 105 Subtraction - x - 5 95 Multiplication * x * 2 200 Division for Quotient / x / 2 50 Division for Remainder % x % 2
16
Arithmetic Operators and Expressions
Casting Casting is converting a value of one type to a similar value of another type at run time. To cast, specify within parentheses the type you want to cast to. The casting is acceptable for both primitive and reference types int x; int y; y = (int) x * 0.5;
17
Arithmetic Operators and Expressions
Increment Operator It is a shortcut in Java to increment the numeric value stored in a variable by one. The two statements below are equivalent: age = age + 1; ++age;
18
Greater than or equal to
Relational Operators Operator Symbol Greater than > Less than < Greater than or equal to >= Less than or equal to <= Exactly equal to == Not equal to != instance of instanceof
19
Boolean Operators Operator Symbol AND && OR || NOT !
20
AND Operator The Boolean AND expression evaluates to true only when both the component Boolean expressions evaluate to true Receives discount: (Is the customer a student) && (Is the age of the customer less than 24)
21
OR Operator A Boolean OR expression evaluates to true only when either or both of the component Boolean expressions evaluate to true Cannot play: (Is it raining) || (Is it snowing)
22
NOT Operator If your height is NOT less than five feet you can join the police force can join: !(height less than five feet)
23
Boolean Type and Boolean Variables
True False
24
If-Else Statement
25
If-Else Statement Syntax if (booleanExpression) statement
The statement can be a single statement, a block of statements (enclosed by curly braces) a null statement (just a semicolon)
26
If-Else Statement Examples if (isRaining) canPlay = false;
if (isRaining); // Does nothing. if (order > 50.00) {freeShipping = true;} else {freeShipping = false;}
27
If-Else Statement Examples if ((isStudent) && (age <= 24))
{discount = order_price * 0.1;} else if (order_price > 50.00) {discount = shipping;} else {discount = 0;}
28
If-Else Statement Examples if ((isStudent) && (age <= 24))
{ discount = discount + order_price * 0.1;} if (order_price > 50.00) { discount = discount + shipping;}
29
Nested If-Else Statement
Examples if ((isStudent) && (age <= 24)) { if (order_price <= ) {discount = order_price * 0.1;} else if (order_price > ) {discount = order_price * 0.15;} } else if (order_price > 50.00) {discount = discount + shipping;}
30
Iterations Iteration allows the program to repeat statements until some condition is met. Java offers three forms of iteration for the programmer to us. With these forms of iteration or loops, programmers can create applications that repeat one or more Java statements as many times as required
31
The While-Loop while (loop guard expression) loopBody
32
The While-Loop Examples int i = 1; while (i < 5)
{out.println("value of i is " + i); i = i+1;} The while loop follows the "test then execute" pattern; it tests the value of i before printing it
33
The For-Loop for (initialization; loopGuard; update) loopBody
34
The For-Loop Examples int i; for (i = 0; i < 5; i = i + 1)
{out.println("value of i is " + i); } A loop body can also contain an empty statement for (i = 0; i < 1000; i = i+1);
35
The Basics of Object-Oriented Programming
SSD1 – Introduction to Information Systems The Basics of Object-Oriented Programming Lecture 5
36
Classes Object-oriented programming is based on the notion of classes.
A class is a reference type which is used as a template for creating objects. So the object is an instance of the class. A person might be a class. And each of you is an instance of this class or an object of this class.
37
Classes A class usually contains attributes and methods. An attribute is a characteristic of the class. A method is an action which is performed with the instance of that class. int i; // variable of int type Car y; // variable of Car type (or object of the Car class)
38
Object Orientated Programs
Let’s take an example of corporate personnel to illustrate OOPs. - Problem: Write a project proposal - Personnel Involved: Vice President (VP) and Project Manager (PM) We need two objects to model this task
39
Steps to Modelling Task
Object needed: One for Vice President and another for Project Manager The class that these objects would belong to depends on behavior of these objects. Hence consider attributes of the two objects: Both objects have different tasks and responsibilities although both employees Project Manager can be asked to prepare proposals, Vice President prepares financial data One Vice President heads several teams, while a manager heads only one team
40
Steps to Modelling Task
Implies the two objects need two different classes Details of tasks is as follows: The VP requests PM to write a proposal. In response, PM starts the proposal and realizes financial data is needed from the VP. PM asks VP for financial data. VP sends financial data to PM in response to this message. PM completes the proposal with the financial data and other necessary information.
41
Attributes Attributes for the Vice President object:
Name Department Attributes for the Project Manager object: Team
42
Messages Message(s) from the Vice President object that the Project Manager object must respond to: Write project proposal Message(s) from the Project Manager object that the Vice President object must respond to: Provide financial data
43
Steps to Modelling Task
Class representation according to attributes and functions discussed is illustrated in the diagram below.
44
Catfish Class. Attributes
... private int row; private int column; private String imageFileName; Identifiers of the attributes Access to the attributes Types of the attributes
45
Types of Access Private. A class element with the private level can be accessed only within a class Public. A class element with the public level can be accessed by objects of other types (from outside of the program) Protected. A class element with the protected level can be accessed by objects of child classes.
46
Catfish Class. Methods getRow getColumn get methods getImage
swimRightIfPossible swimLeftIfPossible swimUpIfPossible swimDownIfPossible get methods
47
get Methods These methods are used to access the class attributes
For each attribute a get method must be defined
48
The type of value returned by the method
get Methods public int getRow() { return row; } For instance, the getRow method allows another object to find out the row in the grid corresponding to the location of the Catfish object The type of value returned by the method
49
Methods The second set of methods attempts to modify a Catfish object to swim in a particular direction swimRightIfPossible swimLeftIfPossible swimUpIfPossible swimDownIfPossible
50
Method returns no value
Methods public void swimRightIfPossible() { // If the cell to the right is within bounds, move right. if (column + 1 <= 10) { column = column + 1; } imageFileName = "/Catfish-right.gif"; Method returns no value
51
The Diagram of the Catfish Class
52
Writing Java Source Code
Java Compiler (javac.exe) is a program that translates a high-level language (Java) program into byte code (the result – the file with .class extension) Java Virtual Machine (JVM) interprets the byte code into the machine code and executes this code as native code on the client machine. The JVM is the core of the Java platform.
53
Writing Java Source Code
Java Runtime Environment (JRE) contains JVM, class libraries, and other supporting files. JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.
54
Writing Java Source Code
Java Developer Kit (JDK) is a software bundle which contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), the documentation generator (javadoc.exe), etc…
55
Writing Java Source Code
56
Writing Java Source Code
If you want to write your own programs in Java and to compile them you need JDK. For running java programs, JRE is sufficient. In order to compile a Java program you need to write the following command in the command line javac Catfish.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.