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 with others

What is a programming language?

Any computer game or program such as word processor that you 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 u 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 you download it

What is an applet?

An applet is simply a program that is typed in JAVA, that someone can use or play with over the Internet. An applet can be a game you make an online calculator or anything else created in Java that can be used on the Internet.

When u 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 PC

Create a folder to hold you must follow this step:

  1. Type in and save your Java program (in the c:)
  2. Compiling the applet
  3. Creating a web page to display the applet
  4. Viewing your project

Basic Applet frame work

Import Lines makes built in Java words available for your program.

Program heading line  this is where you name your program.

Variable Section lets you declare any specific Java things you will be using.

Methods these are the instruction for the computer.

What do the CURLY BRANCHES DO?

Indicate the beginning and end of the program section.

  1. Import Section: 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.

  1. Program Heading Section: WHAT DOES THE PROGRAM HEADING LINE DO?

All of your Java programs must contain this line. The only thing you can change is the name of the program. What this line does is create a new applet from the built in pre-made Java applet template (that is what extends does in the program heading line).

  1. Variable Section: WHAT DO VARIABLES DO?

In our sample program, we want to draw to the applet. We need to tell the computer to reserve some space for all our drawing information. So, we name our drawing area screen, and we tell the computer that screen holds graphics information.

  1. Methods Section

Methods are sections of a program that perform certain instructions. As a programmer, you can make your own methods and even give them your won name. The word public just means that the method is accessible from any art of the program.

COMMENTS

Comments are there for your reference. You can write whatever you want in them and they will be ignored during the compile process. If your comment is going to be larger than one line enclose it between the following set of symbols:

/*Put comment here */

DO NOT PUT SEMI-COLONS AFTER A COMMENT IS FINISHED!

Paint Method Explained

To make an applet visible on web browser, most often you will use paint method to do your drawing.

Public void paint(Graphics screen) {

Drawing instructions:

}

“(Graphics screen)”, these words are called parameters. These are any variables the method uses.

Rules or naming in Java

There are certain restrictions to naming variables or applications:

  • You may not have any spaces in the name (underscores are allowed)
  • Names cannot 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 (called reserved words.)
  • Do not name any of variables with the same name as each other
  • Do not name any of your variables with the same name as your program

What does the word CLASS mean in Java

In the program heading line, it always begins with public class. The meaning is:

1)A program that a user made. A blueprint for an applet. This is why you use the word class in your program heading line. It tells the computer that you are creating your own version of an applet, and then you provide all the instructions for your applet in the method section.

2) A reserved word that identifies a pre-made Java structure. For example, Graphics is a pre-made Java class, that means that Graphics has many of its own pre-made methods with may be used. This is why you can type in scree.drawString (“Hi”,50,50);

We know that screen is a Graphics class, which has many pre-built methods, on eof which is drawString. To call up a method of a pre-built class, you use the general structure:

Variable_name.method_name(Parameters);

For example in the above program we used the line:

Screen.drawRect(30,30,50,50);

Exercises:

  1. comment
  2. method
  3. whaterver is written after slashes
  4. semi colon
  5. paint
  6. curly brackets

Unit 4 – Graphics

Steps / Examples
Declare a Graphics variable in the variable section / Graphics screen;
Declare a paint method in the method section, make sure you are sending the Graphics variable to the paint method as a parameter / Public void paint(Graphics screen) {
Instructions here;
}
Inside the paint method: use your variable name in combination with all the Graphics methods to create your drawing. / Public void paint(Graphics screen) {
Screen.drawRect(0,0,10,10);
}

Java Coordinate System : Introduction

Each point corresponds to a pixel unit. This is a term used to define distance on a display.

0,0 starts here

and increases in

the x and y axis

X 

Y 

Basic Methods of the Graphics Class: Creating Lines, Rectangles, Ovals, Arcs

Lines:

General Structure:

Screen.drawLine(starting x pixel, starting y pixel, ending x pixel, ending y pixel);

Example:

Screen.drawLine(10,30,100,50)

This would draw a line from coordinate 10,30 to coordinate 100,50

Rectangles:

General Structure:

Screen.drawRect(upper left x pixel, upper left y pixel, width, height)

Screen.drawRoundRect(x,y, width, height, corner width, corner height);

Example:

Screen.drawRect(50,20,91,120);

Screen.drawRoundRect(100,20,90,120,10,5);

Ovals:

Ovals are drawing inside the boundary defined by a rectangle. The coordinates are written in the exact same way as the rectangle above.

Example:

Screen.drawOval(50,20,90,120);

Arcs:

An arc is a segment of an oval. If you want to draw a part of a curve an arc will do that for you.

General Structure:

Screen.drawArc(x,y,width,height, start_angle,degrees from start angle to draw);

The first 4 numbers define the oval that you will be drawing a section of. These numbers are exactly as defined in the oval section. The last 2 numbers tell which section of the oval you want to actually draw.

90 degrees

90 degrees

Example:

screen.drawArc(50,150,90,120,0,180);

This would draw an arc that starts started at 0 degrees and went 180 degrees (counter-clockwise) from that point. It would draw the top half of the oval.

The order in which you draw you shapes matters. For example, if you want a solid blue rectangle with yellow ovals on it, you must draw the blue rectangle first, then draw the yellow ovals.

Using The Fill Method

Another way to draw shapes is to have them solid. You can have an oval filled by this method.

Example:

Screen.fillRect(10,10,70,100)

Screen.fillOval(30,50,100,10)

Screen.fillArc(50,50,100,10,0,90)

Using Colors

There are some basic colors included in Java, and you can also make you own colors.

General Structure:

Screen.setColor(Color.______);

Put the name of the color in the blank area.

Creating your own color

The general structure for creating your own colors in Java requires the following at the beginning of your paint method for each color you want to make.

Form:

Color ______= new Color(number for red, number for green, number for blue);

Put the name of your new color here.

Example: To create red

Color my_red=new Color(255,0,0) ;

To implement the color change use the following:

Screen.setColor(my_red);

NOTICE: this line is slightly different form the setColor used for basic pre-made Java colors.

Premade colors: screen.setColor(Color.red); //this has the word Color inside the braces

Your own colors: screen.setColor(me_red); //only the color name in the braces.

Creating Polygons

Steps /

Examples

Declare a Polygon variable in the variable section: Initialize it in the init method / Polygon mypoly;
Mypoly=new Polygon();
In the paint method, add points to the polygon / Mypoly.addPoint(10,10);
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 polygon for you.) / Screen.drawPolygon(mypoly);
Or
Screen.fillPolygon(mypoly);

DrawString and Changing Fonts

Example:

Screen.drawString(“hello, welcome to the end of unit 4!”, 100,100);

The Java code(“comment that you will see”, X-position of message, Y-position of Message);

Changing Fonts

Example:

Screen.setFont( new Font (“Courier”, Font.BOLD,20));

Screen.setFont( new Font (“put the name of the you want like to try here”, Font.Style(this has 3 types: PLAIN, BOLD, ITALIC),SIZE(the size of your font)));

Calling your own methods and calling them from the paint

Create your own method:

General Structure:

Al of the bold faces items are the ones which you will fill in. the rest must be typed as shown. Methods shouldn’t be longer than 1 screen length.

Public void name(Graphics screen) {

Instructions;

Instructions;

Instructions;

}

To call up a method from another part of the program

You need to send the Graphics variable to your new method because it needs it to draw on. You must write it inside the round braces when you call it up, which sends the Graphics variable to the method when it is called.

Example:

Public void house(Graphics screen) { --The house Method. We indicated that

Screen.drawRect(100,100,50,50); the house method is expecting Graphics

Screen.drawLine(100,100,125,50); variable by putting the words Graphics

Screen.drawLine(125,50,150,100); screen in the round braces.

} //ends the house method

public void paint(Graphics screen) {

house(screen); --to actually show the house on the screen

} //ends paint we must call up or activate the house

method from inside the paint method.

“house(screen);” - we are sending the Graphics variable screen to the house method because it is expecting it. It needs the screen to draw on.

Advantages of using methods

Imagine drawing a scene of a house with a garage with a car in it. If you had to type all your code inside the paint method, it would become huge! Also, if there was a problem, you would have to hunt through all the code to try to isolate which part drew what.

Here is what the paint method of an organized program might look like:

Public void paint(Graphics screen) {

Houseframe(screen);

Windows(screen);

Chimney(screen);

Garage(screen);

Carbody(screen);

Carwheels(screen);

} //ends paint method

Unit Exercise

  1. a) methods

b) screen

c) width, height

d) start angle, degrees from start angle to draw

e) Polygons

VARIABLES

What is a Variable?

To make programs useful, quite often we let the user ender in values for something. For example, if a program is meant to figure out the area of a rectangle, it would be useful to let the user enter in values for the length and width of the rectangle they with to find the area of. Variables in Java enable programs to store values in the computer’s memory. Java can then access these values by calling up the location where the value is stored. It is just like using a formula in math. A formula always has variables in it. This enables a person to put whatever value they need into the formula to find a result.

In summary a variable is a programming structure that lets a program store values of different types of data. Data can be whole numbers, decimal numbers, letters, words, symbols, or Java built in classes.

Declaring variables, and the assignment statement

To use a variable in a Java program:

  1. determine what type of data will be stored (these are pre-made Java types)
  2. declare the variables in the variable declaration section
  3. give a value to the variable immediately, or do it later in the program

Determining Data Type

An example: a program may be created which prints a country. Since a country name is a value (a word), we know we should declare a variable to store the country’s name. The type of data to be stored will be a String. The name of the variable will be country.

Declaring the variable

The declaration would look like:

String country ;

What is happening is the computer is reserving a space in its memory for a String value. The program must remember where in memory the value is stored, so the variable name is the “label” used to identify the memory location.

In this diagram:

<Here the space reserved in memory called country now

has the value Canada stored in it.

You can change the value in country many different times in a program. In that case all you are doing is replacing the value inside the memory box.

Note: you can only add data, not numbers.

Examples for declaring variables (in the variable declaration Section)

String my_name ;

Int salary

String (data type) my_name(your variable name)

Examples for Assigning Values to Variables (in any method)

My_name=”Bob” ;

Salary=1000 ;

Master Questions 5.2

1.a) not right. The $ sign

b) not right. The My_name thing has to go after String.

c) not right. SEMI COLON foo!

d) right sexy!

2.a) not right brother. The colon is annoying.

b) right.

c) not right hommie. The colon is really annoying.

Java Variable Types

There are several basic data type built into Java. Determine what type of data you will be storing then declare and assign a value to the variable.

Data Type / Description / Examples
Int / A whole number, values from –2 billion to 2 bilion / 10,-10
Long / A whole number values from –9E18 to 9E18 / 3,000,000,000,000
Double / A decimal number from
–1.7E308 to 1.7E308 / 102.998E48
Char / A single character from the keyboard. Must be enclosed in single quotes ‘r’ / ‘r’ ‘&’ ‘*’
String / A combination of character. Must be enclosed in quotes “ word “ / “Henry” “Aryan”
Boolean / A variable whose value can either be true of false / The valueis either trye or false

To print out the value of a variable in the drawString method, you put an empty pair of quotation marks, then the + sign, then the variable name as the first parameter.

General structure for printing variable values

Screen.drawString(“”+variable_name, 50,50);

You could also print more than one variable on the same line.

Screen.drawString(“”+variable1+variable2+variable3+”the end”, 50,50) ;

Mastery Questions 5.3
  1. a) & - “char”

b) “Kate” – String

c) 12.44 – double

d) –12.9983 – double

e) 12321 – long

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.

Example:

Final double tax=1.14 ;

This declares tax to e 1.14 throughout the whole program. So if you want to change the tax rate, you would simply change the value in the constant declaration.

Java Math Functions

Often computer programs are required to do mathematical operations. There are only 5 basic math functions. The basic orders of operations apply here.

Mastery Questions 5.4

  1. Helps you so that you don’t have to enter the value again and again, and again. It stays once and for all.

Operator
/
Operation
/
Example
+
/
Adds numbers
/ 2+3 or num1+num2
-
/
Subtracts numbers
/ 2-3 or num1-num2
*
/
Multiplies numbers
/ 2*3 or num1*num2
/
/
Divides numbers
/ 2/3 or num1/num2
%
/
Gives integer remainder
/ 3%2 would equal 1

Math operation can be used with numbers or with variables that contain number values. Also, you can put a math operation on the right side of an assignment statement and the result of that math operation will be stored to the variable on the left side of the assignment statement.

Example: