PROGRAMMING WITH JAVA
Preliminaries
By
Samuel K. Opoku
Computer Science Department
Kumasi Technical University
Video Link
[Link]
.mp4
Resources:
• [Link]
[Link]
• [Link]
1
GOALS OF THE COURSE
• General information about Java
• Java Basics (J2SE)
• Control Structures in Java
• Object Oriented Programming in Java
• Working with GUI, Database and files
• Creating Reports in Java with Jasper Report 3
APPROACH AND MATERIALS
- Textbook: Slide notes will be provided after each day
- Quizzes, Assignments and Lab Activities (or minor
projects): Quiz dates unannounced
- Mid Semester exams (30 marks): 3 parts. Different set of
questions for each student
Part 1: Structured Programming - 0 or 10 marks ;
Part 2: GUI Programming – 0 or 10 marks;
Part 3: Database and File Programming – 0 or 10 marks
2
END OF SEMESTER EXAMINATION
(ANSWER ALL QUESTIONS) – 2.5 HRS
Section A (MCQ’S):
Section B (Explanation and short questions):
Section C (Error Fixing):
Section D (Programming questions
1. Structured Programming
2. Graphic User Interface
3. Object Oriented programming
(Implementation of inheritance, polymorphism, database and file)
6
3
Introduction to Java
10
CONTENTS
• Compare and contrast Java and
other programming languages
• What Java is, what Java can do
and what it cannot do
• Getting started with Java 11
4
OVERVIEW OF PROGRAMMING LANGUAGES(1/4)
• A program is a sequence of instructions that tells the
computer what to do.
• Execution is the carrying out of the program instructions
• Programming languages are languages designed to write
program instructions
• The most primitive type of programming language is a
Machine language (that is binary codes). A machine language
program (called object code) is a program to which a computer
can respond directly
12
OVERVIEW OF PROGRAMMING LANGUAGES(2/4)
• Machine languages are unique to their CPU and thus different
computers use different machine language. Thus machine
language understand by Intel’s Pentium Processor is quite
different from the machine language understand by IBM’s
PowerPC® Processor
• Machine language programming is very tedious and error
prone. Slightly less tedious is Assembly language which is a
symbolic language for coding machine language instructions
• For even very simple task, Assembly language programs can
be quite long and complicated
13
5
OVERVIEW OF PROGRAMMING LANGUAGES(3/4)
• High-level programming languages are languages at a higher
level than Assembly language. These languages permit people
to write programs in a way that is more natural.
• In high-level language, the detail knowledge of the machine
being programmed is not required. It uses a vocabulary and
structure that suites the type of problem being solved.
Example: The programming language FORTRAN is used to
solve scientific and engineering problems.
• Commands in a high-level program are not executed directly
by a computer. It is translated first by a specialized program
called a translator.
14
OVERVIEW OF PROGRAMMING LANGUAGES(4/4)
• A translator accepts a program written in a language (called
source code) and translates it to an equivalent program in
another language (called target program which is the object
code)
• Most translators convert a high-level language program to a
machine language program. For high-level languages, a
translator is normally called a compiler.
• A particular type of translator of interest to Java programmers
is an interpreter. An interpreter is a translator that both
translates and executes the source program
15
6
JAVA AND ITS CHARACTERISTICS
• Java is a programming language in the tradition of C and C++
• Characteristics of Java includes:
– Platform independence: can run on any machine
– Automatic Memory management
– Exception handling
– Type Checking
– Java is multithreaded and distributed
• Java language itself is very simple. Java comes with a library of
classes that provide commonly used utility functions that most Java
programs can’t do without
16
WEAKNESSES OF JAVA
• Does not handle true decimal data. Example
• Windows computers with Internet Explorer will have problems running
Java applications. These problems are easily solved by going to the Sun
Web site and downloading the latest version of the Java Runtime
Environment
• The API is overdesigned. In some cases, it seems as if the Java designers
go out of their way to make things that should be simple hard to use. For
example, the API class that defines a multi-line text input area doesn’t
have a scroll bar. Instead, a separate class defines a panel that has a scroll
bar.
17
7
COMPONENTS OF JAVA TECHNOLOGY
18
USING JAVA TOOLS (1/2)
19
8
USING JAVA TOOLS (2/2)
20
ILLUSTRATION
2
7.
Add Semi-colon (;) to
the end of the list in
1 the VARIABLE VALUE
TEXTBOX and then
paste the path you
copied.
Click OK three times to
exit all windows
5. Click on 6. You can also choice
Edit CLASSPATH if it exists 21
9
HOW TO WRITE AND RUN JAVA PROGRAMS
TOOLS Procedure for Running a Java
a. Notepad (better use Notepad ++)
Program
a. IDE’s like
[Link]
[Link] (that is what we’ll use)
STRUCTURE
All Java programs are written in Java Class.
public class Class_name {
-- creation of other methods or subclasses
public static void main (String[] args){
--- place major instruction
}
-- creation of other methods or subclasses
22
}
23
10
OUR FIRST JAVA PROGRAM
24
PARTS OF A JAVA PROGRAM (1/2)
public: A keyword of the Java
public class Hello { language that indicates that the
element that follows should be made
available to other Java elements
public static void main (String[] args){ class: A Java keyword that indicates
that the element being defined is a
[Link] (“Hello World”); class. All Java programs are made
} up of one or more classes.
} The class name follows that is Hello
void: In Java, a method must have a return value. A class is a set of code that defines the
main: A name for this method. Java requires that this behaviour of the objects created and
method be named main. Besides, you can also create used by the program
additional methods with whatever names you want.
(String[] args): It is called a parameter list, and it is The opening brace, {, marks the
used to pass data to a method. Java requires that the beginning of the body of the Class (ie
main method must receive a single parameter that is Hello), Method (ie main) or a block
an array of String objects. By convention, this (eg. if statement, for loop). An
parameter is named args. opening brace is always ended with a
Java language requires that you specify static when closing brace, }.
you declare the main method 25
11
PARTS OF A JAVA PROGRAM (2/2)
What about [Link](“Hello World”); ?
We want to
We want to Method member Indicates end of
select an
select an of out use to statement
individual class
individual class display to the
member of the
member of out output stream
System class
System . out . println ( “Hello World” ) ;
println() and print()
println(): cursor moves
Class System Member out of to next line after
Literal
is defined in System is an output printing output
character
the standard stream object string that is
package associated with the print(): cursor remains
the parameter
[Link] console window on the same line after
to println()
running application printing output
26
KEYWORDS
A keyword is a word that has special meaning defined by Java programming
language
true, false, and null — are not technically considered to be keywords.
Instead, they are called literals. Still, they are reserved for use by the Java
language in much the same way that keywords are used
const and goto — are reserved by Java but do not do anything. Both are
carryovers from the C++ programming language 27
12
28
LAB ACTIVITY 1
Note: Call me to award marks when you complete each step
[Link] your computer to be Java friendly by installing and
setting the appropriate parameters of the current version of JDK
2. Test your system using the “Hello” program on slide 19 (Write in
a text file with the filename [Link], compile and run)
3. Modify the “[Link]” program you just created so that you
produce this out put using asterisk (“*” ) only.
Expected output:
**
*** ***
** **
29
**
13
ACTIVITY
Write a program you just created so that you produce the output
below using asterisk (“*” ) only.
Expected output:
**
*** ***
** **
**
30
31
14
Assignment
Create a file called [Link] to produce this output
* *
* * * *
* * * *
* * *
* o o *
* -- *
* * * * *
*
* * *
*
*
* * *
th
Submit to VClass latest by 6 February, 2023 at 11:59pm 32
HIT ANY KEY TO END DAY 1
33
15