0% found this document useful (0 votes)
128 views28 pages

Designing & Developing: Object Oriented Computer Programs

This document outlines the topics to be covered in a course on designing and developing object oriented computer programs in Java. The 10 topics include GUI and IDE, the genesis and structure of Java, object orientation, graphics classes, user interfaces, design methodology, exception handling, creating classes, data structures, and text files. It provides learning objectives and content for the first topic, which introduces GUI, IDE, concepts like classes and objects, and shows a simple Java program example. It also covers Java fundamentals like programming languages, data types, variables, storage, and the basic program structure in Java.

Uploaded by

Lawrence Saliba
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views28 pages

Designing & Developing: Object Oriented Computer Programs

This document outlines the topics to be covered in a course on designing and developing object oriented computer programs in Java. The 10 topics include GUI and IDE, the genesis and structure of Java, object orientation, graphics classes, user interfaces, design methodology, exception handling, creating classes, data structures, and text files. It provides learning objectives and content for the first topic, which introduces GUI, IDE, concepts like classes and objects, and shows a simple Java program example. It also covers Java fundamentals like programming languages, data types, variables, storage, and the basic program structure in Java.

Uploaded by

Lawrence Saliba
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DESIGNING & DEVELOPING

OBJECT ORIENTED COMPUTER PROGRAMS


Mr. John Debono MSc SE, BSc IS (Hons)

Module Outline

Topic 1 GUI & IDE Topic 2 Genesis & Structure Of Java Topic 3 Introduction To Object Orientation Topic 4 Graphics Class Topic 5 More Complex User Interfaces

Topic 6 Approaches To Design Methodology


Topic 7 Exception Handling Topic 8 Creating Classes Topic 9 Data Structures Topic 10 Text Files

Material

Suggested Books
For Dummies (4th Edition) Head First Java
Java

Slides Worksheets Past Papers

Contact

John Debono MSc SE, BSc IS (Hons)


[email protected]

GUI & IDE


Topic 1

Learning Objectives

Understand the importance of well designed Graphical User Interfaces (GUI) Understand the importance of Integrated Development Environments (IDE) Use the Eclipse IDE Use the Java facility swing to create a simple GUI Use the String Class

Be aware of certain concepts that form part of a simple Java program including

Class Object Method

The Main Method


The Import Statement Swing Components

Computer Programs

A computer program is not unlike:


The assembly instructions for building a model car from a kit A recipe for cooking a meal A knitting pattern

Those who devise such instruction lists may be thought of as programmers.

Fundamentals Of Computer Programs

Lets consider the assembly instructions for a model car kit more closely. There are three fundamental types of instructions:
Step 1 Attach Part B To Part A S Step 1 Attach Part C To Part B

Step 2 For Each Of The Four Wheels


Fit A Tyre Place Wheel On Axle

Step 3 If Bumper Is White Then Attach To Front Step 3 If Bumper Is Red Then Attach To Back

Fundamentals Of Computer Programs

In Step 1 we are asked to follow instructions in a Sequence, one after the other. In general the order of the sequence is important. In Step 2 we are asked to do the same sequence of operations four times. This is an example of looping known as Iteration. In Step 3 we are asked to carry out a test, the result of which affects what we do next. This is an example of Selection.

Programming Languages

To communicate a program to a computer, the program has to be written in a language that the computer understands. For Java this means that the computer being used must have installed what is known as a Java Virtual Machine. The JVM is a program that has the function of checking the syntax and grammar of Java statements in the context of a Java program and then communicating the program to the host computer in a code that the computer can execute.

Programming Languages

Technologically we are now at a time when users expect to communicate with a computer via a Graphical User Interface (GUI) by clicking a mouse on screen objects and simple use of a keyboard. Programs have to be written that can achieve this type of interface for the users, thus modern programming languages must make this possible. Java is now such a language

Java

A computer program is written in a computer programming language such as Java. Like a natural language, Java has a character set, a vocabulary and a grammar. Thus Java needs a special type of word processing software to help with the input of a Java program into a computer. Such software is known as a context sensitive editor and forms part of the Integrated Development Environment (IDE) that is often the front end of modern programming systems.

Data Types

Java uses a variety of Data Types which are used as placeholders for various information. These placeholders are also known as variables. For instance

A whole number is known as an Integer data type. A number with fractional type is known as a Double data type. A group of characters is known as a String data type.

Using Constants & Variables

A Variable can be
Constant
A constant variable

cannot be changed after the program

is compiled.
Variable
A

variable can change numerous times within an application after compile time.

Variables Example

Java is a strongly typed language because when a variable is declared then its type must also be given.

//Integer Data Type int amount = 50; //Double Data Type double bankAccountBalance = 190.87; //String Data Type String nameAndSurname = John Debono; //Constant Integer Data Type final int vatRate = 0.18;

Primitive Data Types

In Java the data type Integer is known as a primitive data type while as String is known as a reference data type or a complex data type. A primitive type is defined by the Java language itself. A reference type is defined by a Class that is a part of a massive library of classes that form a wider definition of Java.

Primitive Data Types

For a primitive type, the memory associated with the declared variable actually contains the value of the variable. For a reference type, the memory associated with the declared variable only contains the address where the current manifestation of the object can be found.

Primitive Data Types Example


//Boolean Data Type boolean isMarried = false; //Character Data Type char initial = J; //Byte Data Type byte amount = -12; //Short Data Type short amount = 1235; //Integer Data Type int amount = 50000; //Long Data Type long amount = 579123323412; //Double Data Type double bankAccountBalance = 190.87; //Float Data Type float bankAccountBalance = -1987212.67;

Variable Comparison

The distinction between primitive and reference types matters.

//Integer Data Type int amount = 50; if(amount == 50) { //Logic }

//String Data Type String name = John;


if(name.equals(John)) { //Logic }

Variable Comparison

To explain this remember that a String object is created by a class; thus the actual String value is NOT stored in the memory location associated with the variable surname. This memory just holds the address of the memory where the String value can be found. The method equals will use this address to retrieve the String value of surname and compare it with the literal String John

Storage

Technically the storage for data in a computer is a large number of identical boxes called bytes. Programmers using programming languages such as Java prefer to think of the storage as boxes of different sizes and of different types. The programmer has to set up the boxes they need before they write the program. This set up process is known as Declaring Variables.

Storage Example
//Storing Variables int firstVar, secondVar, resultVar;

This allocates three boxes which can be addressed by the names firstVar, secondVar and resultVar. Each box has access to storage for just a whole number. Each box only has the facilities to perform whole number arithmetic.

//Changing Variables firstVar = 123; secondVar = 456; resultVar = firstVar + secondVar;

Other Storage Example


//Storing Variables int potatoesPrice, amountInKilos, potatoesTotalPrice;

This allocates three boxes which can be addressed by the names potatoesPrice, amountInKilos and potatoesTotalPrice. Each box has access to the storage for a number with fractional parts.

//Changing Variables potatoesPrice = 11.21; amountInKilos = 3.4; potatoesTotalPrice = potatoesPrice * amountInKilos;

String Operations
//Storing Variables String phrase = ; String firstNineLetters = ;

The following statements allocate two boxes which can be referenced by the names phrase and firstNineLetters.

Note that initializing each string ensures that the memory pointers to the storage for the strings are set up.
Each box has a size to hold a group of characters. Each box only has the facilities to perform operations on characters.

//Changing Variables phrase = First Program; firstNineLetters = phrase.substring(0, 9);

Program Structure

The distinctions between attribute, method and event are very important to emphasise. Java program code is structured in blocks. In general, blocks are delimited by a pair of curly brackets.

All Java programs must have a main() method.


When a Java program is run, main() is the first to be executed. Hence it is common for the main() method to merely instantiate an object via a new call to a class.

Example
public class FirstJavaProject { //Constructor public FirstJavaProject() { this.setSize(500,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("At last some action!"); this.setVisible(true); } //Main Method public static void main(String[] args) { new FirstJavaProject(); } }

Difficulties With Eclipse IDE


Every word in Java is case sensitive (annoyingly!) Many errors may be flagged up by Eclipse. Many of the error messages are difficult to understand. Many of the errors may be due to:

Not obeying the case sensitivity; Missing a semicolon at the end of a statement; Misplacing a curly bracket.

Dont worry about warning messages that are flagged in orange Code completion is an important feature in an IDE. This can be used to discover methods.

Additional Material TheNewBoston.Org

1 - Installing the JDK 2 - Running a Java Program 3 - Downloading Eclipse 4 - Hello YouTube 5 - Variables

You might also like