Download Set B - Units 1 to 6

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
What is Java?
Java is the new language for the Internet. It has many advantages that make it
popular for programmers.
 It is cross platform
 It is completely object-oriented

Its programs can be viewed directly through a web page, making everyone an
instant author and software developer
 it is a compact language that makes learning other languages like C++ easier
 it is continually growing, and because of its portability, you can share your
programming code wit others
What is a programming language?
Any computer game or program such as word processor that u may use is created
from a programming language The inner working of a computer is quite complicated
and requires another course just to introduce the basic topics.
 Software: this is that your computer runs, examples like games, processors,
spreadsheets or internet browsers
 Hardware: this is the physical equipment that makes up the computer. All wires,
the disk drives the monitor, the mouse and the keyboard.
 Programming code: these are the special words that a programmer types in to
communicate to the computer hardware, telling it what to do. Software is created
through programming code.
What do you need to run JAVA?
To



have your system to run JAVA, u must have the following:
a JAVA compiler
a text editor
a web browser(to view JAVA program), or a program called an applet viewer that
comes with JAVA program when u download it
What is an Applet?

An applet is simply a program, which is typed in JAVA, that someone can use or
play with over the Internet. An applet can be a game u make; an online
calculator or anything else created in Java that can be used on the Internet.
When you are typing in your Java applet, what you are doing is using special
words and symbols

JAVA Specifics

The language the compilers translate into is called BYTECODE and all web
browsers universally understand this. All web browsers have a JVM (JAVA
virtual machine) which the software that helps the web browser execute the
bytecode.
Outline of Basic steps to run a Java program on a PC
To





create and run an applet you must follow these steps:
step 1-create a folder to hold your Java programs (in the c:)
step 2-type in and save your Java program in a text editor
step 3-compiling How a programming Language Works General Format
step 4- creating a web page to display the applet
step 5- viewing your applet
STEPS IN RUNNING PROGRAM
How a Programming Language works general Format



Type in Code in a Text Editor, you must follow proper rules of language
Compile the program software translate your typed code into a language the
computer understands
Run Program (if compile is successful , this is when the program instructions are
executed
How Java Runs



Your code is typed into an editor
Code is compiled into BYTECODE , BYTECODE is a universal language understood
by all web browsers
If compile is successful, run program (applet) in web or in given software with
JDK
What do the Curly Braces Do



The curly braces just indicate the beginning and ending of a program section
Curly braces come in pairs {}
For every open one you have, you must have a closing one
What does the Import Do




For a Java program to work, and compile, the location of all the special words in
your program must be specified
All of the Java’s special words are located in pre made packages
There are packages for dealing with graphics
Awt stands for abstract window toolkit, it contains all the pre made special words
with proper instructions
What does the Program Line do


All this program should have this line
This line creates a new applet from the built in pre-made Java applet template
Rules for Naming in Java






You may not have any spaces in the name
Names can’t begin with a number
Besides the underscore, do not use any other symbols
Give all your variable and programs meaningful names
You may not use special words already pre-built in the Java language words
Do not name any of your variables with the same name
Unit exercise
1.
a) A COMMENT is created using //.
b) The METHOD section is where the instructions of the program are located.
c) The compiler ignores WHATEVER IS WRITTEN ON THE LINE AFTER THE
SLASHES (//) when compiling your code
d) Every instruction line in Java must end with a SEMI-COLON (;)
e) The PUBLIC VOID method has to have a Graphics parameter.
f) To avoid a common compiling error, make sure your CURLY BRACKETS match
up in pairs.
2.
a) Everything but MAY SALARY is a legal name because you can not have spaces
in the name and as long as there are no spaces in the name the name can
consist of anything.
Introduction to the Graphics Class
STEPS
Declares a graphic variable in the
variable section
Declare a paint method in the method
section, make sure you are sending the
graphics variable to the paint method as
a parameter
Inside the paint method; use your
variable name in combination with all the
Graphics method to create your drawing
Coordinate System
EXAMPLE
Graphics screen;
Public void paint(Graphics screen) {
Instructions here;
}
Public void paint(Graphics screen) {
Screen.drawRect(0,0,10,10);
}


Each coordinate point corresponds to a pixel unit
This is a term used to define distances on a display
Creating Lines, Rectangles, Ovals, Arcs






You just have to know what parameters to send to the method and
it will work
All of the parameters must be whole numbers
If they are larger or smaller than then applet size then that certain
part will not be shown on the display
The graphics variable name should be screen
Each of the commands goes inside the paint method that is
where the drawing is done.
LINES:
 Structure (General) = screen.drawLine(co-ordinate, co-ordinate, co-ordinate,
co-ordinate);
 This would draw a line from one coordinate(the first two co-ordinate two coordinates)
 This general structure translates into: start at this co-ordinate across and this
co-ordinate down, and end at this co-ordinate across and this co-ordinate
down.

RECTANGLES:
 Structure for rectangle (General) = screen.drawRect (co-ordinate, coordinate, co-ordinate, co-ordinate);
 Structure for circle aka round rectangle (General) screen.drawRoundRect (coordinate, co-ordinate, co-ordinate, co-ordinate);

OVALS
 Are drawn inside a boundary defined by a rectangle. The co-ordinate are
written in the exact same way as the rectangle above
 Structure (General) = screen.drawOval (co-ordinate, co-ordinate, co-ordinate,
co-ordinate)

ARCS
 Is a segment of an oval, if you want to draw a part of the curve and arc will
do that for you
 Structure (General) screen.drawArc(co-ordinate, co-ordinate, width coordinate, height co-ordinate, start_angle co-ordinate, degrees from start
angle to drawn co-ordinate)
 The first four numbers define the oval that you will be drawing a section of.
These numbers are exactly as defined in the oval section. The last two
numbers tell which section of the oval you want to actually draw.
Using the Fill Method





Another way to draw shapes is to have them solid ex: an oval will be an oval
filled
To fill in a shape instead of using the drawRect, or drawOval….functions, simply
replace the word draw with the word fill. Leave all the co-ordinates the same.
Ex: screen.fillRect(co-ordinate, co-ordinate, co-ordinate, co-ordinate);
Ex: screen.fillOval(co-ordinate, co-ordinate, co-ordinate, co-ordinate);
Ex: screen.fillArc(co-ordinate, co-ordinate, co-ordinate, co-ordinate, co-ordinate,
co-ordinate)
Using Colors












You must change the current color first, then draw the shapes you want
This will ensure that your shapes are the color you desire
You can change the colors as many times as you want
Structure(General): screen.setColor(Color.________);
The general structure for creating your own colors requires the following at the
beginning of your paint method for each color you want to make.
Structure for new color (General) Color=new Color(number for red, number for
green, number for blue)
In the blank spot you would but the name of your new color
In each of the spots in the round braces, you need to put a whole number
representation of how much of the particular color you want in your new color.
The number can range for 0-225. It is possible to create almost any color from
combinations of red, green and blue.
These colors are called RGB colors – you can find RGB umber combinations on
Internet web sites.
Ex: Color my_red=new Color(number, number, number) = this is one that is
already made
To implement the coor change you use the following line – you only put your
new color name inside the round braces.
Structure (General) screen.setColor(my_red);
Creating Polygons

Polygons are multisided shapes
Steps
Declare a polygon variable in the variable
section: Initialize it in the init method
In the paint method, add in points to the
polygon
When you are finished defining all your
points, use any of the following to
actually draw the polygon on the
screen.(The computer will connect all
your dots and close the polygons for you)
Examples
Polygon mypoly;
mypoly= new Polygon ();
mypoly.addPoint(10,10);
screen.drawPolygon(mypoly);
or
screen.fillPolygon(mypoly);
Drawstring and Changing Fonts

It is possible to change the size and style of writing that shows on the applet
display. You should know what types of Fonts are available on your system, or
else you can just play with the names and see if you enjoy any of the changes in
which you have created.
Creating Your Own Methods and Calling them from the Pain
Method



Sometimes drawings can get very complicated and the paint method can become
very large and cluttered. Therefore at this time you will learn how to write your
own simple methods and use them to help organize your program
Structure (General) all the bold are what you fill in. Anything which isn’t bold you
can leave by itself as is:
public void name(Graphics screen) }
instructions;
instructions;
instructions;
}
Example:
public void house(Graphics Screen)}//this will draw a small house
screen.drawRect(100,100,50,50);
screen.drawLine(100,100,125,50);
screen.drawLine(125,50,150,100);
}//ends house method
public void paint(Graphics screen){
house(screen);
}//ends paint
What is a Variable?


A variable is a ‘programming structure’ that allows a program to store values of
different types of data into variables
Data can be an whole or decimal numbers, they can be numbers, letters, words,
symbols or built in classes
Declaring Variables and the Assignment Statement








To use a variable in Java program:
 Determine they type of data which will be stored (these are pre-made Java
types)
 Declare the variable in the variable declaration section
 Give a value to the variable immediately, or do it later on during the program
Step One – Determining the Data Type
For example: a program can be made to print a country name. Since the name is
a value (a word) you can create a variable for the country’s name. The type of
Data will be stored in a String. The name of the variable will be a country. The
basic Java naming rules.
Step Two – Declaring the Variable
The declaration would look like: Sting country;
The computer is then reserving a space in its memory for a String value. The
program must remember where in the memory the value is located so the
variable name is the “label” used to identify the memory location
Step 3 – Assign Value to the Variable
To assign a value to a variable, you use something called an Assignment
Statement.
The expression on the right side of the assignment operator (=) is the value and
it is stored in the variable written on the left side of the assignment operator.
The assignment operator can be used in any method in the program.
You can change the value many different times in a program. To change the
name you are just replacing the value inside the memory box
Java Variable Types

To print out the value of a variable in the drawString method, you put an empty
pair of quotations marks, then the ‘+’ sign, then the variable name as the first
parameter.
Constants

Constants are similar to variables except they hold a particular value for the
whole program. Their value stays constant the whole program. Constants can be
useful when using values for things such as tax rates or interest rates.
Java Math Functions

To print out the value of a variable in the drawString method, you put an empty
pair of quotations mark, then the + sign, then the variable name as the first
parameter.

Data Type
Description
Example
A whole number, values
10, -10
from –2 billion to 2 billion
long
A whole number values
3,000,000,000,000
from –9E18 to 9E18
double
A decimal number from102.998E48 (E= *10 to
1.7E308 to 1.7E308
the power of …..)
char
A single character from
‘r’ “&” “*”
the keyboard. Must be
enclosed in single quotes
‘r’
String
A combination of
“Henry” Jane”
characters, must be
included in quotes “word”
boolean
A variable whose value
The value is either true or
can either be true or false
false
 Constants are similar to variables except they hold a particular value for the
whole program. Their
int
Constants

Constants are similar to variables except they hold a particular value for the
whole program. Their value stays constant the whole program. Constants can be
useful when using values for things such as tax rates or interest rates.
Java Math Functions

The basic orders of operations taught in Math class apply here
Operator
Operation
Example
+
Adds numbers
2+10=12
Subtracts numbers
8-4=4
*
Multiplies numbers
8*8=64
/
Divides numbers
72/9=8
%
Gives integer remainder
3%2=1
 A math operation can be used with numbers or with variables that contain
number values.
 You put a math operation on the right side of an assignment statement and the
result is that the math operation will be stored to the variable on the left of the
assign statement:
 Examples: salary=15.35*8*4; or without the decimal it could be:
salary=pay_rate*8*4;
Casting

This forces an expression in an assignment statement into a particular data type