0% found this document useful (0 votes)
27 views37 pages

SEO Best Practices for Document Optimization

This document provides an overview of Java basics including: 1. What is Java? It introduces Java as a programming language and discusses its syntax. 2. The differences between Java applets and applications. Applets are programs embedded in web pages while applications are standalone programs. 3. Recommended introductory books on Java programming.
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 PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views37 pages

SEO Best Practices for Document Optimization

This document provides an overview of Java basics including: 1. What is Java? It introduces Java as a programming language and discusses its syntax. 2. The differences between Java applets and applications. Applets are programs embedded in web pages while applications are standalone programs. 3. Recommended introductory books on Java programming.
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 PPT, PDF, TXT or read online on Scribd

| 

Ô 

    
     
  

Overview of Java basics

What is Java?
Java syntax basics

1
ecommended books:
0 "Java Programming: from Problem Analysis to
Program Design", Malik & Nair, ½
ourse Technology ISBN 1997
0 "ore Java ½: Fundamentals" Horstmann and ornell,
The Sun Microsystems Press Java Series, ½½
0 or any other introductory Java text
0 applications, not applets
0 text output, not graphical

½
Applets vs applications
0 Java applets
0 a program embedded into a web page

0 download and run on user's browser (or applet viewer)

0 internet programming

0 Java applications
0 standalone programs ƛ Java is a fullyfledged programming
language
0 many Java class libraries for

0 GUI, graphics, networking


0 data structures
0 database connectivity
0 we will be writing applications in this module


First, a bit of backgroundƦ
0 1st Generation ± machine language
0 (raw machine code ± lots of binary 0100101010001000111)

0 2nd Generation ± assembly language


0 (mnemonic representation ± short series of chars to represent binary)

0 3rd Generation ± structured programming


0 (e.g. Pascal, C, C++, Java)

0 4th Generation ± application specific


0 (SQL, Mathematica, RPG II, PostScript)

0 5th Generation ± combining artificial intelligence


0 (best not mentioned«)

0 The aim is to have a programming language that is as close as possible to


natural speech ± a bit like in µStar Trek¶.


The Software risis
0 The ³software crisis´, recognized c.1969. - threatened
the progress of the computer industry.
0 People-time was and still is relatively expensive,
machine-time is now very, very cheap.
0 Programming was and still is very time intensive.
0 Products need support - this is probably more than ever
0 Software is complex; imagine creating a car with no
drawings, specifications or planning.
0 Amazingly, this is how a lot of the software in the past
was created (some of it still is)!
0 This situation had to end«

ù
Towards Structured Programming
0 As computer programs become larger and more complex, more errors are
introduced.
0 It has been estimated that there are 15 bugs in every 1000 lines of
commercial code.
0 Windows 2000 had 40 million lines of code!
0 Most bugs are caused by poor memory management.
0 Clearly there is a need for a structured programming language that helps in
reducing the number of errors and speeds development for programming
teams.
0 C
C++
Java
0 2unctional Programming
bject rientation
l  



 

    
   


 



What is ObjectOriented code?
0 Procedural coding is just a list of instructions.
0 bject-oriented code has a lot of similarity with code from
other procedural languages.
0 Basically, a lot of the µwords¶ are the same but the way the
words are put together (the structure) is different.
0 bject-oriented coding does also contain lists of instructions
but these lists are bound to specific objects.
0 What an object actually represents is up to the programmer.

7
Simple Example
0 A car is an object (in the real world) and in our program we can
make a software object to represent it.
0 2undamentally, an object contains data and methods which can act
on that data.
0 The data we might want to have in our µsoftware¶ car could be
things like: body colour, engine size, current speed, whether it has
electric windows etc. Basically it¶s up to you.
0 The methods we might want our µcar¶ to have could be things like:
accelerate, brake, respray body, open passenger window etc. Again,
it¶s up to you.
0 Whatever you need your software car to model from the real world
object must be present in your code.
0 The  syntax enables you to do this intuitively.


What technology does it replace?

0 Procedural programming (C, Visual Basic, 2ortran etc.)


0 In procedural programming, functions were the most
important part of the software.
0 Where and how the data was stored was secondary (at best).
0 Procedural code is process-oriented,  code is data-
oriented.
0 In procedural programming, if your code is in error then it is
relatively easy to fix but incorrect data may be ÷ ÷ to
fix!

9
Java (why use it?)
u  
  

0 WRA - Write nce, Run Anywhere (portable).


0 Security (can run ³untrusted´ code safely).
0 Robust memory management (opaque references, automatic garbage
collection)
0 Network-centric programming.
0 Multi-threaded (multiple simultaneous tasks).
0 Dynamic & extensible.
0 Classes stored in separate files
0 Loaded only when needed
0 Can dynamically extend itself to expand its functionality (even over a
network and the Internet!)

1
How does it help?
0 Large projects can be broken down into modules more easily.
0 Aids understanding.
0 Groupwork is easier.
0 Less chance of data corruption.
0 Aids reusability/extensibility.
0 Maintaining code is far easier.
0 Hides implementation details (just need to know what
methods to call but no need to understand how the methods
work to use them).

11
Programming vs Puzzle Solving
0 With any programming language, there are concepts and some
terminology to learn.
0 It takes time for these things to sink into your brain.
0 Ô       

0 Like driving a car, you cannot learn in a day and it takes practice with a
lot of trial and error.
0 This is normal. You can only learn by experience.
0 Programming is very similar to solving puzzles.
0 Solving any complex puzzle (Su Doku, Rubik¶s Cube, Mah-jong etc.)
requires you to think and trying out ideas until you finally (hopefully)
solve the puzzle.
0 The only difference is that with programming you have to translate your
ideas into code before you can test your solution.


Applets
0 embed into web page using applet tag
0 <APPLET src = "myprogramclass" width = "1"
height = "1"> < /APPLET>
0 source code extends Java Applet class
0 has skeleton user interface
0 programmer can add code to be executed when
applet is initialised and painted
0 programmer can easily add GUI components
such as buttons, labels, textfields, scrollbarsƦ
and respond to their events

1
Example
applet 
ManySpikes
0 set up label
and textfield in
init() method
0 draw the spikes
in the paint()
method
Java applications
0 standalone programs
0 not GUI by default
0 text input and output to a console
0 can add user interface components
0 execution always starts at a main() method
0 analogous to init() method of applet


A simple Java application
÷ 
 corejavaonsole;
÷ Hello {
÷÷ ÷ main(String args[])
{
String name; // this declares the variable Ơnameơ
name = onsolereadString ("What is your name? ");
Systemoutprintln("Hello, " + name);
}
} /* end of program */
1
The Hello class and its method
0 name of class is same as name of file (which has
java extension)
0 body of class surrounded by { }
0 this class has one method called main
0 all Java applications must have a main method in one
of the classes
0 execution starts here
0 body of method within { }
0 all other statements end with semicolon š

17
Java keywords
0 keywords appear in  
0 reserved by Java for predefined purpose
0 donƞt use them for your own variable, attribute or
method names!
0 ÷
0 visibility could be 
÷
0 ÷
0 the main method belongs to the Hello class, and
not an instance (object) of the class
0  ÷
0 method does not return a value
1
omments
0 important for documentation!!!!
0 ignored by compiler
// single line (or part of line)

/* multiple line comments go here


everything between the marks
is ignored */

0 useful to Ɲcomment outƞ suspect code or make notes

/**
* These are used by the javadoc utility to create HTML
* documentation files automatically.
*/

19
ariables and data types
0 name is a variable of type String
0 we have to declare variables before we use
them
0 unlike , variables can be declared anywhere
within block
0 use meaningful names numberOfBricks
0 start with lower case
0 capitalise first letter of subsequent words

½
Data types
0 ÷  byte integer (whole number)
0 range ½17 to +½17

0 ?   byte floating point number


0 decimal points, numbers outside range of ÷

0   byte floating point number


0 1ù decimal digits (float has 7) so bigger precision

and range
0 
½ byte letter
0 |
÷ string of letters
0   true or false (not 1 or )

½1
onsole input
0 data input is difficult in Java
0 methods for input in the library class
corejavaonsole
0 we have to ÷ 
 corejavaonsole to use it
0 each book has its own methods for input!
0 Malik and Nair "Java programming" book uses raw Java
which is difficult to read

½½
onsole methods
0 general form:
my ariable = onsolereadType("Put a prompt
here");
0 onsolereadString("prompt")

0 onsolereadInt("prompt")

0 onsolereadDouble("prompt")

0 onsolereadWord("prompt")

0 gives error message if wrong type is input


0 Handy!

½
System output
0 Java provides print methods in the class
Systemout (donƞt need to import)
0 println(name);
0 prints out what is stored in name, then goes to a
new line
0 print(name);
0 prints out what is stored in name, but does not
start a new line
0 print("My name is " + name);
0 put text in quotes
0 use + to print more than one item
½
Methods in Java
0 methods break down large problems into
smaller ones
0 your program may call the same method
many times
0 saves writing and maintaining same code
0 methods take parameters
0 information needed to do their job
0 methods can return a value
0 must specify type of value returned

½ù
Example method
signature

÷÷÷addNums(÷num1, ÷num½)
{
÷answer = num1 + num½;


answer;
}

body
½
Method signature
visibility [static] returnType methodName(parameterList)
0 visibility:
0 ÷
0 accessible to other objects and classes
0 

0 accessible to classes which inherit from this one
0 
÷
0 ÷ keyword:
0 use when method belongs to class as whole
0 not object of the class
½7
Method signature
visibility [static] returnType methodName(parameterList)
0 return type:
0 specifies type of information returned
0 can be a simple type
0 ÷, ? , , 
, String,  
0 or a class
0 if nothing returned, use keyword  ÷
0 method name:
0 use meaningful name which describes what method does!

½
Method signature
visibility [static] returnType methodName(parameterList)
0 parameter list:
0 information needed by method
0 pairs of type name
0 examples:
addNums(÷ num1, ÷ num½)
drawPerson(  isBald, String name, ÷numEarrings)
0 use empty brackets if method has no parameters
printHeadings()

½9
Method body
signature

÷÷÷addNums(÷num1, ÷num½)
{
÷answer = num1 + num½;


answer;
}

body

Method body
0 use curly brackets to enclose method body
0 all your code goes in here
0 write it so the method does what you intended
0 last line should return a value of appropriate type
0 must match type in method header
0 nothing is executed after

 statement
0 if method returns  ÷  can omit

 statement
0 method will automatically return at closing

1
alling a method
0 methods will not run unless called from elsewhere
0 a statement in main() method could call another

method
0 this method could call a third method 

0 class methods are called with the form:


lassNamemethodName(parameters);
0 omit lassName if called in same class

0 method name and parameters must match the


method signature
0 if the method returns a value, it can be stored in a
variable or passed to another method

alling methods
÷÷ ÷ main(String args[])
{
÷ input;
input = onsolereadInt("Number? ");
Systemoutprint("Your number plus  is ");
Systemoutprintln(addNums(input, ));
}


alling methods
0 the previous example uses four methods
0 from class onsole:
÷÷÷ readInt(String prompt)
0 store returned integer in variable input

0 from Systemout:
÷ ÷ print(String s)
÷ ÷ println(÷ x)
0 no returned value (void)


alling methods
0 from class JavaTest:
÷÷÷addNums(÷num1, ÷num½)
0 pass returned integer to println() method

0 gets printed to screen

0 could also store it in another variable

÷answer = addNums(input, );


Summary
0 reviewed Java basics
0 applications vs applets
0 class declaration, main method
0 Java keywords
0 comments
0 data types, variables
0 input and output
0 writing and using methods
0 Tutorial
0 write a calculator application which takes input
from the user, calls methods and output answers

eading
0 Malik and Nair
0 hapter 1 ( eview)

0 An overview of omputers and Programming

Languages
0 hapter ½

0 Basic Elements of Java

0 skip sections on Arithmetic Operators,

Expressions, Type onversion, The class String


0 remember the book does not use onsole

methods for input


0 hapter 7

0 section on "UserDefined Methods" only

7

You might also like