0% found this document useful (0 votes)
108 views

Introduction To Programs and Java: Lund Amateur Coder's Club

The document introduces an introductory course on Java programming held at the Lund Amateur Coder's Club, including details about meeting times and resources, an overview of what programming and Java are, and examples of simple Java code for mathematical functions and displaying text messages to learn basic programming concepts.

Uploaded by

Christine Lehane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Introduction To Programs and Java: Lund Amateur Coder's Club

The document introduces an introductory course on Java programming held at the Lund Amateur Coder's Club, including details about meeting times and resources, an overview of what programming and Java are, and examples of simple Java code for mathematical functions and displaying text messages to learn basic programming concepts.

Uploaded by

Christine Lehane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture 1

Introduction to Programs and


Java
Lund Amateur Coders Club
Introduction to Programs and Java

Book: Introduction to JAVA Programming 8th Edition by Daniel Liang

Purpose of course: To learn the basics of JAVA programming

Meetings: Every Sunday 13:00 at Lund Public Library

Questions:
https://www.facebook.com/groups/533416536863723/?ref=aymt_h
omepage_panel
Introduction to Programs and Java What is
programming?

Programming is the process of feeding commands for the computer


to execute.
These commands can have varying goals and complexity, ranging
from simple mathematical operations (addition, multiplication..) to
large libraries of information about, for example, a car rentals
selection of cars and information about each car (model, license
number, whether it is rented out or not, the last 10 people who have
rented a certain car etcetera).
There is a myriad of different programming languages, but Java is said
to be a good starting point if one is new to programming.
Introduction to Programs and Java What is
programming?
Programming is a good training in logical thinking and process
planning, the mind-set taught can be applied outside of
programming.
The world is moving more and more towards digitalization of tasks
this is where a foundation in programming comes in handy.
Introduction to Programs and Java What is
Java?
Java was developed by James Gosling at Sun Microsystems.
Originally called Oak, it was designed in 1991 for use in embedded
chips in consumer electronic appliances.
In 1995, renamed Java, it was redesigned for developing Internet
applications (apps for short).
Since then Java has become a big language used in a variety of
circumstances (for example communication and control of the Mars
rover).
For the history of Java, see here
Introduction to Programs and Java What is
Java?
Java is a high-level language meaning its instructions are close to
natural language (English), e.g., grossPay = basePay + overTimePay

Natural language is too ambiguous and can have multiple meanings,


thus mathematical notations are used to circumvent ambiguity in
natural language.

JAVA has two main components:


API Application Program Interface (library of commands)
JVM Java virtual machine (interprets Java code in machine language e.g.,
binary code 0s and 1s)

Java can be run on multiple programs inc Mac, Linux, Windows etc.
Introduction to Programs and Java What is
Object Oriented Programming?
Object oriented programming is a programming method that can
contain a varying number of objects that interact with each other
An object can be a list of books in the city library, each book listed
can in turn be another object with certain attributes
These attributes are descriptive properties such as author, publishing
date, number of pages, location within the library building, whether
it is lent out or available..
In order to see if the new Harry Potter book is available, you will ask
the library object to go through its list of books and return all new
Harry Potter books that are available.
In this process the library object will ask the book objects that are
relevant (new HP-book) whether they are free or not, to which the
books will return yes or no.
Introduction to Programs and Java What is
Object Oriented Programming?
If a book object returns a yes, then the library object will put the
book object in a list of available books.

This logical operation (just an example) will only add our


Examplebook to the list of available Harry Potter books if the
Bookavailable-function return the logical value true, otherwise it will
not do anything.
Introduction to Programs and Java Different
types of variables
When programming you assign values to different variables. Some of
these variables are integers, Booleans, double, char, string..
Integers can only have whole-number values, so for example 1,5,-20,
100000000 etc.
Booleans are logical variables that only take the values true or
false
Doubles can also contain decimals, so 15.3, -18.78475 etc. Doubles
can also be whole numbers (13.0).
Chars (short for characters) contain character values for example a,
z, 5 (please note that a character with the value 5 is just the
character 5, not the mathematical value 5.
Strings contain strings of text (a series of chars), for example hello
world or R2-D2.
Note that these are only a few of the variables available.
Introduction to Java
Programming in Java can be done with the help of a program called
Eclipse. Download link here (we will be using this one)
Or NetBeans. Download link here (both are IDEs integrated
development environments)
You can also do Java programming online with this site
Introduction to Java
Programming is a sequence of commands.
Each command must begin with at least one class, here the class is
Welcome1 class names must begin with an upper case letter
Note: Java is case sensitive!
Line two defines the main method. The statement
System.out.println (to print text) is contained in the method main.
Every Java statement ends in a semi-colon (statement terminator)
Open and close commands with a curly bracket { }
Introduction to Java
To keep track of your code, it is good to label each of your
commands. You can do this with forward slash:

Java does not consider // a command. It allows you to describe the


action so that you can remember what each command does/means
when you review your code at another time.
When the compiler sees //, it ignores all text after // on the same
line.
This is similar to other programming packages such as R and Digram.
Introduction to Java
Note that Java has certain reserved words that can only be used one
way.
For example, when the compiler sees the word class, the program
understands that the word after class is the name for the class. Other
reserved words in this program are public, static, and void.
Introduction to Java, mathematical functions

To begin with we will learn how to do mathematical functions in Java.


Again, in this example, the class is ComputeExpression this allows
you to remember that the command will involve calculations
(however, the class name is not very relevant).
Again, the main method is main as you want to print the answer
from the calculations.
Introduction to Java, mathematical functions

You may have noticed that to close your command you need two
curvy brackets (opening & closing braces). This is because you
typically have functions within functions (different command blocks).
For example:

An opening brace must be matched by a closing brace. Whenever


you type an opening brace, immediately type a closing brace to
prevent the missing-brace error. Most Java IDEs automatically insert
the closing brace for each opening brace.
Introduction to Java, mathematical functions

Here we assign values to integers a and b, and assign the value a+b to
integer c. Then we return (print) the value of c. The result will be
printed in the window below.

Errors will show up as red Xs on the left side of your coding screen.
Remember to check that your syntax is correct (using upper or lower
case correctly, open and close braces, semi-colons)
Introduction to Java, Assignment 1

Play around with mathematical functions in Java, for example try to


write a program that calculates the area of a rectangle with sides 5
and 7 (so, something that calculates 5*7).
Try to write code that will give the difference between a rectangle
with sides 5&7 and a rectangle with the sides 4&8. (so 5*7-4*8).
Try to solve for how big a percentage of a circle with radius 4 is
covered by a square with sides 3. (so 3*3/(4*4*3.14) * 100)
Introduction to Java, Assignment 2
Produce text in a message dialog box:
import javax.swing.JOptionPane; -> name class, state method,
describe action using //, create dialog text.
Javas predefined classes are grouped into packages. JOptionPane is
in the javax.swing package.
Command import tells Java where to locate package.
Introduction to Java, Assignment 2
1. Create a dialog message that says I love programming
2. Write a program that prints I love JAVA 5 times in the text
output.
3. Create star patterns following instructions here

You might also like