SlideShare a Scribd company logo
1 of 47
Download to read offline
Dr. Martin Chapman
programming@kcl.ac.uk
martinchapman.co.uk/teaching
Programming Practice and Applications (4CCS1PPA)
Topic 1: Getting Started
Q: Name a piece of software that takes a file in one format as input, and outputs a file in a different format. 1
Thursday 29th September
Before We Start
2
SYMBOLS TO LOOK OUT FOR
3
This content is particularly important, and will be
tested in either the coursework, class test or
exam.
🤓
This is not my area of expertise, but may still be
interesting or relevant to another module. This
will not be tested.
🤐
This is for interest only, and will not be tested in
either the coursework, class test or exam.🤔
There is content in this slide that we will revisit
at a later date.😴
SYMBOLS TO LOOK OUT FOR
4
Slides with no symbol are all of equal
importance, and may or may not be tested in the
coursework, class test, or the exam.
This slide features animations, which were
presented in the lecture, and may not appear
well in print.
🙈
🖥
You should be implementing all the code in these
slides in your lab sessions (line-by-line), but this
symbol tells you that this code is particularly
important, or alerts you to the presence of
particular tasks, which need to be completed.
To understand a definition of programming, and some open
areas of debate around the subject.
To write and run one of the simplest Java programs.
GETTING STARTED: OBJECTIVES
5
Some Starter Questions…
6
What Is Programming?
7
Formally?
• "Writing a sequence of instructions that are designed to
solve a particular problem in a formal, constructed
language."
• "Writing stuff in a made up language on your computer so
that other stuff happens on your computer."
A PROPOSED DEFINITION
8
Most people know informally.
This topic is often debated.
Certainly mostly scientific.
• A set language; the logical resolution of problems.
But definite artistic elements.
• Visual code style; creative solutions.
Because of the artistic element of programming, the code I show you, and, to
some extent, the topics I share with you, will always be influenced by my
style.
• Others, perhaps even those of you with programming experience, may
prefer to do things in different ways.
• The fundamentals are style-independent.
PROGRAMMING: AN ART OR A SCIENCE?
9
Why Is Programming Important?
10
ENOUGH SAID
11
When Can You Call Yourself `A Programmer’?
12
When you know the syntax (key words) and semantics (the
meaning of these words) in every programming language?
• I don’t think so.
• This is the `how’ of programming.
• This can be obtained simply through memory.
WHEN CAN YOU CALL YOURSELF `A PROGRAMMER’? (1)
13
When you develop a sense for how to solve problems by
breaking them down; you're able to abstract the world; and
you know which approaches to apply in different
circumstances
• I think so.
• This is the `what’ of programming; even the pros use
web searches for the `how’ element of programming,
but the `what’ of programming cannot be obtained
from Google.
• This can only be obtained by practising.
WHEN CAN YOU CALL YOURSELF `A PROGRAMMER’? (2)
14
This emphasises the difference between learning a programming
language (e.g. learning Java) and learning to program.
In PPA, we will focus on Java, but it is important to remember
that this is just a vehicle for teaching you how to program.
• (I’ll say it again) Practise (programming), don’t just
memorise (Java syntax).
• This will help you develop the fundamental skills
associated with being a programmer.
• A lengthy process that may stretch beyond PPA.
• Pedagogy necessitates toy examples. It may not be until
you meet real programming tasks that skills develop.
PROGRAMMING VS. WRITING USING A PROGRAMMING LANGUAGE
15
Diving In…
16
How Do You Program (In Java)?
17
Most programs start their lives as simple text files, which
contain our source code.
In Java, these files have the extension .java (which you typically
alter manually from .txt after saving your text in a text editor).
START WITH A TEXT FILE
18
We then need to transform this simple text file (and the
source code it contains) into a program.
To do this, we need a specialist piece of software called a
compiler.
Like other software, the compiler can be downloaded from
the Internet (see KEATS).
Like most pieces of software, a compiler takes a file as input
(our text file), and produces a different type of file as output
(a program).
UNDERSTANDING THE ROLE OF THE COMPILER (1)
19
UNDERSTANDING THE ROLE OF THE COMPILER (2)
20
A music converter
A word processor
The Java compiler (javac)
The compiler produces our program, a .class file, which is
then read by the Java virtual machine, another piece of
software (which can also be downloaded, see KEATS)
designed to run Java programs and, typically, produce
output that can be viewed.
UNDERSTANDING THE ROLE OF THE JAVA VIRTUAL MACHINE (1)
21
UNDERSTANDING THE ROLE OF THE JAVA VIRTUAL MACHINE (2)
22
The Java virtual machine (java)
A music player
A document reader
🤔
THE FULL PROCESS
23
This compiled language (Java Bytecode) is close to
assembly (4CCS1CS1).
When we use software such as a video converter to convert
files, or we use a word processor, we input our source files
(.mp3 file, .docx file) into the software using the GUI.
• We might right-click on the .docx file and select `open
with Microsoft Word’.
When we use software such as the Java compiler and the Java
virtual machine to compile and run programs, we input our
source files using a terminal (a command line interface).
• It is possible to run compiled (Java) programs from the
GUI, but it requires a more complex compilation
configuration.
USING THE COMPILER AND THE JAVA VIRTUAL MACHINE
24
THE TERMINAL (1)
25
The terminal is a text representation of a location in the file system.
You issue commands in order to move through the file system.
🖥
Experiment with the terminal, and different commands, in
your laboratory session. The commands you need will vary
between operating system.
• How do you change directory?
• How do you list what is in the current directory?
• How do you find out your current location in the file
system?
THE TERMINAL (2)
26Topic1-1
The equivalent of right-clicking on an application and selecting
`open with’, on the terminal, is to write the name of the application
you wish to run, followed by the name of the file you wish to input.
• This assumes that the application you wish to run is installed
(i.e. exists) somewhere on your computer.
• The terminal does not necessarily know by default where the
application you may wish to run is stored.
• So, if you wish to use the Java compiler and the Java virtual
machine on your own machine you may have to tell the
terminal where to find these two applications by updating
your terminal configuration after you have downloaded and
installed both piece of software (details on KEATS).
THE TERMINAL (3)
27
THE TERMINAL (4)
28
The name of the Java compiler software is javac.
The name of the Java virtual machine software is just java.
Note that we omit the .class extension when we are
inputting a compiled Java program.
There should now be a clear process for compiling and
running a Java program.
1. Write the program in a text file.
2. Change the extension of the program to .java.
3. In a terminal, navigate to the directory containing
your .java file.
4. (Assuming the Java compiler and the Java virtual machine
are installed and available to the terminal) Run the compiler
with the .java file as input to produce a .class file.
5. Run the virtual machine with the .class file as input.
SUMMARY: FROM TEXT TO PROGRAM (1)
29
SUMMARY: FROM TEXT TO PROGRAM (2)
30
On KEATS you will find links to download (and then install) the Java
Development Kit, which includes the Java compiler and the Java virtual
machine.
• Programming languages, like Java, come in versions, each with
slightly different keywords and conventions. We may briefly touch
on some of the latest Java features in this course (Java 1.8), so make
sure you download the latest version. But in general we won’t teach
to a particular version.
Follow the instructions given on KEATS to configure your terminal in order
to find the javac and java software (if needed).
• Confirm that the terminal can find this software by entering both
commands into the terminal.
In our laboratories, everything is already installed for you.
REMINDER: KEATS
31
Let’s Try It Out…
32
A SIMPLE JAVA PROGRAM
33
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Must be inside a file called HelloWorld.java
🙈
A SIMPLE JAVA PROGRAM: CODE BLOCKS (1)
34
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
This code has a clear structure. It is organised into nested blocks, as
indicated by the curly braces, new lines and indentation.
Must be inside a file called HelloWorld.java
🙈
A SIMPLE JAVA PROGRAM: CODE BLOCKS (2)
35
public class HelloWorld { public static
void main(String[] args)
{ System.out.println("Hello World"); } }
New lines and indentation are optional, but greatly increase readability.
Must be inside a file called HelloWorld.java
😴
A SIMPLE JAVA PROGRAM: THE MAIN METHOD
36
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Must be inside a file called HelloWorld.java
A Java class requires something called a main method in order to be run by
the Java virtual machine.
• Any code we run has to be either in the main method or be called by it.
Because the main method contains several reasonably advanced pieces of
syntax, I am going to have to ask you to use this code as boilerplate.
• To use a piece of code without altering it (including its case), or really
understanding its operation.
• This is considered an pedagogical sin, but is unavoidable.
If this does not work for you, consider changing your learning path
• Reminder: see the `Support’ section on KEATS.
There will be several points in the course at which I will (necessarily) ask you
to accept boilerplate code, but we will always come back to it.
ASIDE: BOILERPLATE CODE
37
🙈
A SIMPLE JAVA PROGRAM: CLASSES AND CLASS NAMES (1)
38
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Some more boilerplate, for now.
Any code we write has to be
placed inside the block associated
with something called a class.
The text following the class
keyword is the name of the
class and a title that
summarises the functionality
of the code inside.
Must be inside a file called HelloWorld.java
Each file can only contain one public class.
The name of this class must match the name of the file in
which the class resides.
Therefore, at this stage, we will consider classes to be
roughly equivalent to files.
• One file for each class.
• Later on in the course, we’ll see that this isn’t strictly
true.
A SIMPLE JAVA PROGRAM: CLASSES AND CLASS NAMES (2)
39
Not all the words in a Java program have a special meaning. Some
are selected by us.
NAMING RULES AND CONVENTIONS IN JAVA (1)
40
But there are rules for human selected text (intuitive)
• Cannot begin with a number (because we wouldn’t be able to
identify values).
• Cannot contain spaces.
• Cannot contain symbols other than $ and _.
• Cannot be reserved Java keywords (such as class).
public class IsThisLectureReallyStillGoingOn {
camelCase notation is typical, but not enforced
• Class names capitalise the first letter (HelloWorld)
Making intelligible and presentable class name choices is part of the
artistic element of coding.
NAMING RULES AND CONVENTIONS IN JAVA (2)
41
Using the same class name twice will result in a name conflict.
• But any name selected will differ from the same name written
in a different case.
public class isTHISLEctur3reallystill_goingon {
All inside a file called HelloWorld.java
A SIMPLE JAVA PROGRAM: SEMI-COLONS
42
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
😴
All inside a file called HelloWorld.java
A SIMPLE JAVA PROGRAM: PRINTING OUTPUT
43
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
TRYING IT OUT: PROGRAM OUTPUT
44
The Java virtual machine (java)
Text output from a program is printed in the terminal, after the
program has been passed to the Java virtual machine.
Please avoid using IDEs for the first few weeks of term (e.g.
Eclipse, Netbeans, IntelliJ etc.) or at least disable features
like code completion and code structuring.
• Using an IDE at this stage will be like learning to ride a
bike with stabilisers, and then never taking them off
(you won’t learn to ride properly).
You can start thinking about and exploring the potential
uses of version control.
A NOTE ON SOFTWARE
45
You actually now have all the tools you need to write and run Java programs.
How can you practise going forward?
• Attend labs, completing the lab slides and coursework tasks.
• Watch the KEATS `Challenge Forum’ for additional challenges and
problems
• Text books often contain hundreds of problems; specialist coding
challenge books exist; or search online.
• But try not to jump ahead, or approach things in different ways, unless
you find yourself struggling.
• Make something up
• If you want to write a program that changes the lights behind your
television to match the colour of the content being played, do it.
HOW TO PRACTISE GOING FORWARD
46
Dr. Martin Chapman
programming@kcl.ac.uk
martinchapman.co.uk/teaching
Programming Practice and Applications (4CCS1PPA)
Topic 1: Getting Started
These slides will be available on KEATS, but will be subject to
ongoing amendments. Therefore, please always download a new
version of these slides when approaching an assessed piece of work,
or when preparing for a written assessment. 47
Thursday 29th September

More Related Content

What's hot

Chapter 2.3
Chapter 2.3Chapter 2.3
Chapter 2.3
sotlsoc
 
Chapter 5( programming) answer
Chapter 5( programming) answerChapter 5( programming) answer
Chapter 5( programming) answer
smkengkilili2011
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
mark-asoi
 
Fundamentals of programming final santos
Fundamentals of programming final santosFundamentals of programming final santos
Fundamentals of programming final santos
Abie Santos
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
Akshay Nagpurkar
 

What's hot (19)

Chapter 2.3
Chapter 2.3Chapter 2.3
Chapter 2.3
 
Chapter 5( programming) answer
Chapter 5( programming) answerChapter 5( programming) answer
Chapter 5( programming) answer
 
Java programming language
Java programming languageJava programming language
Java programming language
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
 
Programming language
Programming languageProgramming language
Programming language
 
Advanced Java Topics
Advanced Java TopicsAdvanced Java Topics
Advanced Java Topics
 
Grade 8: Introduction To Java
Grade 8: Introduction To JavaGrade 8: Introduction To Java
Grade 8: Introduction To Java
 
Fundamentals of programming final santos
Fundamentals of programming final santosFundamentals of programming final santos
Fundamentals of programming final santos
 
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
How a Compiler Works ?
How a Compiler Works ?How a Compiler Works ?
How a Compiler Works ?
 
What is programming what are its benefits
What is programming  what are its benefits What is programming  what are its benefits
What is programming what are its benefits
 
Java notes
Java notesJava notes
Java notes
 
Introduction to programming by MUFIX Commnity
Introduction to programming by MUFIX CommnityIntroduction to programming by MUFIX Commnity
Introduction to programming by MUFIX Commnity
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
 
Interpreters & Debuggers
Interpreters  &  DebuggersInterpreters  &  Debuggers
Interpreters & Debuggers
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
VMPaper
VMPaperVMPaper
VMPaper
 
Programming Fundamentals lecture 2
Programming Fundamentals lecture 2Programming Fundamentals lecture 2
Programming Fundamentals lecture 2
 

Similar to Programming in Java: Getting Started

Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
Rich Helton
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
(Ebook pdf) java programming language basics
(Ebook pdf)   java programming language basics(Ebook pdf)   java programming language basics
(Ebook pdf) java programming language basics
Raffaella D'angelo
 

Similar to Programming in Java: Getting Started (20)

Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
 
Java programming language basics
Java programming language basicsJava programming language basics
Java programming language basics
 
Java
JavaJava
Java
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 
J introtojava1-pdf
J introtojava1-pdfJ introtojava1-pdf
J introtojava1-pdf
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
(Ebook pdf) java programming language basics
(Ebook pdf)   java programming language basics(Ebook pdf)   java programming language basics
(Ebook pdf) java programming language basics
 
Java presentation
Java presentationJava presentation
Java presentation
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
java 1 new.pdf
java 1 new.pdfjava 1 new.pdf
java 1 new.pdf
 

More from Martin Chapman

Using AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patientsUsing AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patients
Martin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
Martin Chapman
 

More from Martin Chapman (20)

Principles of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learningPrinciples of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learning
 
Principles of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systemsPrinciples of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systems
 
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
 
Technical Validation through Automated Testing
Technical Validation through Automated TestingTechnical Validation through Automated Testing
Technical Validation through Automated Testing
 
Scalable architectures for phenotype libraries
Scalable architectures for phenotype librariesScalable architectures for phenotype libraries
Scalable architectures for phenotype libraries
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Using AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patientsUsing AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patients
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Principles of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical softwarePrinciples of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical software
 
Principles of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical softwarePrinciples of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical software
 
Principles of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile healthPrinciples of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile health
 
Principles of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcarePrinciples of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcare
 
Principles of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systemsPrinciples of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systems
 
Principles of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledgePrinciples of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledge
 
Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...
 
Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...
 
Principles of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systemsPrinciples of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systems
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Using Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research SoftwareUsing Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research Software
 
Using CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotypingUsing CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotyping
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 

Programming in Java: Getting Started

  • 1. Dr. Martin Chapman programming@kcl.ac.uk martinchapman.co.uk/teaching Programming Practice and Applications (4CCS1PPA) Topic 1: Getting Started Q: Name a piece of software that takes a file in one format as input, and outputs a file in a different format. 1 Thursday 29th September
  • 3. SYMBOLS TO LOOK OUT FOR 3 This content is particularly important, and will be tested in either the coursework, class test or exam. 🤓 This is not my area of expertise, but may still be interesting or relevant to another module. This will not be tested. 🤐 This is for interest only, and will not be tested in either the coursework, class test or exam.🤔 There is content in this slide that we will revisit at a later date.😴
  • 4. SYMBOLS TO LOOK OUT FOR 4 Slides with no symbol are all of equal importance, and may or may not be tested in the coursework, class test, or the exam. This slide features animations, which were presented in the lecture, and may not appear well in print. 🙈 🖥 You should be implementing all the code in these slides in your lab sessions (line-by-line), but this symbol tells you that this code is particularly important, or alerts you to the presence of particular tasks, which need to be completed.
  • 5. To understand a definition of programming, and some open areas of debate around the subject. To write and run one of the simplest Java programs. GETTING STARTED: OBJECTIVES 5
  • 8. Formally? • "Writing a sequence of instructions that are designed to solve a particular problem in a formal, constructed language." • "Writing stuff in a made up language on your computer so that other stuff happens on your computer." A PROPOSED DEFINITION 8 Most people know informally.
  • 9. This topic is often debated. Certainly mostly scientific. • A set language; the logical resolution of problems. But definite artistic elements. • Visual code style; creative solutions. Because of the artistic element of programming, the code I show you, and, to some extent, the topics I share with you, will always be influenced by my style. • Others, perhaps even those of you with programming experience, may prefer to do things in different ways. • The fundamentals are style-independent. PROGRAMMING: AN ART OR A SCIENCE? 9
  • 10. Why Is Programming Important? 10
  • 12. When Can You Call Yourself `A Programmer’? 12
  • 13. When you know the syntax (key words) and semantics (the meaning of these words) in every programming language? • I don’t think so. • This is the `how’ of programming. • This can be obtained simply through memory. WHEN CAN YOU CALL YOURSELF `A PROGRAMMER’? (1) 13
  • 14. When you develop a sense for how to solve problems by breaking them down; you're able to abstract the world; and you know which approaches to apply in different circumstances • I think so. • This is the `what’ of programming; even the pros use web searches for the `how’ element of programming, but the `what’ of programming cannot be obtained from Google. • This can only be obtained by practising. WHEN CAN YOU CALL YOURSELF `A PROGRAMMER’? (2) 14
  • 15. This emphasises the difference between learning a programming language (e.g. learning Java) and learning to program. In PPA, we will focus on Java, but it is important to remember that this is just a vehicle for teaching you how to program. • (I’ll say it again) Practise (programming), don’t just memorise (Java syntax). • This will help you develop the fundamental skills associated with being a programmer. • A lengthy process that may stretch beyond PPA. • Pedagogy necessitates toy examples. It may not be until you meet real programming tasks that skills develop. PROGRAMMING VS. WRITING USING A PROGRAMMING LANGUAGE 15
  • 17. How Do You Program (In Java)? 17
  • 18. Most programs start their lives as simple text files, which contain our source code. In Java, these files have the extension .java (which you typically alter manually from .txt after saving your text in a text editor). START WITH A TEXT FILE 18
  • 19. We then need to transform this simple text file (and the source code it contains) into a program. To do this, we need a specialist piece of software called a compiler. Like other software, the compiler can be downloaded from the Internet (see KEATS). Like most pieces of software, a compiler takes a file as input (our text file), and produces a different type of file as output (a program). UNDERSTANDING THE ROLE OF THE COMPILER (1) 19
  • 20. UNDERSTANDING THE ROLE OF THE COMPILER (2) 20 A music converter A word processor The Java compiler (javac)
  • 21. The compiler produces our program, a .class file, which is then read by the Java virtual machine, another piece of software (which can also be downloaded, see KEATS) designed to run Java programs and, typically, produce output that can be viewed. UNDERSTANDING THE ROLE OF THE JAVA VIRTUAL MACHINE (1) 21
  • 22. UNDERSTANDING THE ROLE OF THE JAVA VIRTUAL MACHINE (2) 22 The Java virtual machine (java) A music player A document reader
  • 23. 🤔 THE FULL PROCESS 23 This compiled language (Java Bytecode) is close to assembly (4CCS1CS1).
  • 24. When we use software such as a video converter to convert files, or we use a word processor, we input our source files (.mp3 file, .docx file) into the software using the GUI. • We might right-click on the .docx file and select `open with Microsoft Word’. When we use software such as the Java compiler and the Java virtual machine to compile and run programs, we input our source files using a terminal (a command line interface). • It is possible to run compiled (Java) programs from the GUI, but it requires a more complex compilation configuration. USING THE COMPILER AND THE JAVA VIRTUAL MACHINE 24
  • 25. THE TERMINAL (1) 25 The terminal is a text representation of a location in the file system. You issue commands in order to move through the file system.
  • 26. 🖥 Experiment with the terminal, and different commands, in your laboratory session. The commands you need will vary between operating system. • How do you change directory? • How do you list what is in the current directory? • How do you find out your current location in the file system? THE TERMINAL (2) 26Topic1-1
  • 27. The equivalent of right-clicking on an application and selecting `open with’, on the terminal, is to write the name of the application you wish to run, followed by the name of the file you wish to input. • This assumes that the application you wish to run is installed (i.e. exists) somewhere on your computer. • The terminal does not necessarily know by default where the application you may wish to run is stored. • So, if you wish to use the Java compiler and the Java virtual machine on your own machine you may have to tell the terminal where to find these two applications by updating your terminal configuration after you have downloaded and installed both piece of software (details on KEATS). THE TERMINAL (3) 27
  • 28. THE TERMINAL (4) 28 The name of the Java compiler software is javac. The name of the Java virtual machine software is just java. Note that we omit the .class extension when we are inputting a compiled Java program.
  • 29. There should now be a clear process for compiling and running a Java program. 1. Write the program in a text file. 2. Change the extension of the program to .java. 3. In a terminal, navigate to the directory containing your .java file. 4. (Assuming the Java compiler and the Java virtual machine are installed and available to the terminal) Run the compiler with the .java file as input to produce a .class file. 5. Run the virtual machine with the .class file as input. SUMMARY: FROM TEXT TO PROGRAM (1) 29
  • 30. SUMMARY: FROM TEXT TO PROGRAM (2) 30
  • 31. On KEATS you will find links to download (and then install) the Java Development Kit, which includes the Java compiler and the Java virtual machine. • Programming languages, like Java, come in versions, each with slightly different keywords and conventions. We may briefly touch on some of the latest Java features in this course (Java 1.8), so make sure you download the latest version. But in general we won’t teach to a particular version. Follow the instructions given on KEATS to configure your terminal in order to find the javac and java software (if needed). • Confirm that the terminal can find this software by entering both commands into the terminal. In our laboratories, everything is already installed for you. REMINDER: KEATS 31
  • 32. Let’s Try It Out… 32
  • 33. A SIMPLE JAVA PROGRAM 33 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Must be inside a file called HelloWorld.java
  • 34. 🙈 A SIMPLE JAVA PROGRAM: CODE BLOCKS (1) 34 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } This code has a clear structure. It is organised into nested blocks, as indicated by the curly braces, new lines and indentation. Must be inside a file called HelloWorld.java
  • 35. 🙈 A SIMPLE JAVA PROGRAM: CODE BLOCKS (2) 35 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } New lines and indentation are optional, but greatly increase readability. Must be inside a file called HelloWorld.java
  • 36. 😴 A SIMPLE JAVA PROGRAM: THE MAIN METHOD 36 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Must be inside a file called HelloWorld.java
  • 37. A Java class requires something called a main method in order to be run by the Java virtual machine. • Any code we run has to be either in the main method or be called by it. Because the main method contains several reasonably advanced pieces of syntax, I am going to have to ask you to use this code as boilerplate. • To use a piece of code without altering it (including its case), or really understanding its operation. • This is considered an pedagogical sin, but is unavoidable. If this does not work for you, consider changing your learning path • Reminder: see the `Support’ section on KEATS. There will be several points in the course at which I will (necessarily) ask you to accept boilerplate code, but we will always come back to it. ASIDE: BOILERPLATE CODE 37
  • 38. 🙈 A SIMPLE JAVA PROGRAM: CLASSES AND CLASS NAMES (1) 38 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Some more boilerplate, for now. Any code we write has to be placed inside the block associated with something called a class. The text following the class keyword is the name of the class and a title that summarises the functionality of the code inside. Must be inside a file called HelloWorld.java
  • 39. Each file can only contain one public class. The name of this class must match the name of the file in which the class resides. Therefore, at this stage, we will consider classes to be roughly equivalent to files. • One file for each class. • Later on in the course, we’ll see that this isn’t strictly true. A SIMPLE JAVA PROGRAM: CLASSES AND CLASS NAMES (2) 39
  • 40. Not all the words in a Java program have a special meaning. Some are selected by us. NAMING RULES AND CONVENTIONS IN JAVA (1) 40 But there are rules for human selected text (intuitive) • Cannot begin with a number (because we wouldn’t be able to identify values). • Cannot contain spaces. • Cannot contain symbols other than $ and _. • Cannot be reserved Java keywords (such as class). public class IsThisLectureReallyStillGoingOn {
  • 41. camelCase notation is typical, but not enforced • Class names capitalise the first letter (HelloWorld) Making intelligible and presentable class name choices is part of the artistic element of coding. NAMING RULES AND CONVENTIONS IN JAVA (2) 41 Using the same class name twice will result in a name conflict. • But any name selected will differ from the same name written in a different case. public class isTHISLEctur3reallystill_goingon {
  • 42. All inside a file called HelloWorld.java A SIMPLE JAVA PROGRAM: SEMI-COLONS 42 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
  • 43. 😴 All inside a file called HelloWorld.java A SIMPLE JAVA PROGRAM: PRINTING OUTPUT 43 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
  • 44. TRYING IT OUT: PROGRAM OUTPUT 44 The Java virtual machine (java) Text output from a program is printed in the terminal, after the program has been passed to the Java virtual machine.
  • 45. Please avoid using IDEs for the first few weeks of term (e.g. Eclipse, Netbeans, IntelliJ etc.) or at least disable features like code completion and code structuring. • Using an IDE at this stage will be like learning to ride a bike with stabilisers, and then never taking them off (you won’t learn to ride properly). You can start thinking about and exploring the potential uses of version control. A NOTE ON SOFTWARE 45
  • 46. You actually now have all the tools you need to write and run Java programs. How can you practise going forward? • Attend labs, completing the lab slides and coursework tasks. • Watch the KEATS `Challenge Forum’ for additional challenges and problems • Text books often contain hundreds of problems; specialist coding challenge books exist; or search online. • But try not to jump ahead, or approach things in different ways, unless you find yourself struggling. • Make something up • If you want to write a program that changes the lights behind your television to match the colour of the content being played, do it. HOW TO PRACTISE GOING FORWARD 46
  • 47. Dr. Martin Chapman programming@kcl.ac.uk martinchapman.co.uk/teaching Programming Practice and Applications (4CCS1PPA) Topic 1: Getting Started These slides will be available on KEATS, but will be subject to ongoing amendments. Therefore, please always download a new version of these slides when approaching an assessed piece of work, or when preparing for a written assessment. 47 Thursday 29th September