Showing posts with label language. Show all posts
Showing posts with label language. Show all posts

Tuesday, 7 August 2007

Java Language Fundamentals (Part 10)

Self Test Questions

· What are the 3 types of comments in Java?

· What is the standard naming convention for variables? Methods? Classes? Constants?

· What are the legal identifiers?

· How big is a:
Char
Byte
Short
Int
Long
Float
Double

· What does 36.789F mean?

· Write a simple class which models an aircraft.

Monday, 6 August 2007

Java Language Fundamentals (Part 9)

Objects and Classes - cont

Now, you can create a new type by basing it on your class. The variables within the class and the final constant are called members and can be referenced by a variable which has been declared on the new type.
For example:

TheCar myCar;

Using this declaration, you can reference the variables by using dot (.) notation. Again, using the car example:

myCar.topSpeed = 127;
myCar.lowSpeed = 25;

The myCar variable which is based on the TheCar class is declared not on the data itself, but rather a reference to the data. You can (if you like) think of the variable myCar as a pointer to the class TheCar. This is because the declaration of myCar doesn’t actually allocate memory space for that object.

Before you can actually use this reference variable (myCar) you have to instantiate it using the keyword new. This will allocate space for the three integers used to form TheCar. This reference (myCar) now becomes an object.

To finish the example:
class TheCar{
int topSpeed;
int lowSpeed;
final int MAX_SPEED = 200;
}
public class OurCar{
public static void main(String args[]){
TheCar myCar;
myCar = new TheCar();
myCar.topSpeed = 127;
myCar.lowSpeed = 25;
System.out.println("top speed = " + myCar.topSpeed);
System.out.println("low speed = " + myCar.lowSpeed);
System.out.println("max speed = " + myCar.MAX_SPEED);
}
}

Exercise 2 – 1 using a class variable
· Use the above material to create an application called BigPet which will print 5 different attributes (features) of the concept of a pet (number of legs, tail, weight, name, colour) Use a class called AllPets to hold the member variables. Then use println to send the string “My pets name is Binky and he has 8 legs. He is black and weighs 0.89 kg”

Sunday, 5 August 2007

Java Language Fundamentals (Part 8)

Objects and Classes

Although we won’t launch into Object-Oriented theory now, we’ll quickly cover the basic idea of classes and objects.

A class is like a blueprint or template. An Object is a tangible item, something you could touch or see. The common example is a house. You have a blueprint of a house, but you don’t live in the blueprint. You have to construct a house object to live in. A Method is like a function or procedure. You use methods to do things with an object.

For example, if you wanted to model the concept of a car, you might like to (amongst others) declare these variables to hold some important data about cars in general. So you might declare:
int topSpeed;
int lowSpeed;
final int MAX_SPEED = 200;

Then if you wanted to make 2 cars, you would have to type:

int topSpeed1, topSpeed2;
int lowSpeed1, lowSpeed2;
final int MAX_SPEED = 200;

As you can see, this would be very impractical for lot’s of cars! A better idea is to create a class.

For example:
class TheCar{int topSpeed;int lowSpeed;final int MAX_SPEED = 200;}

Saturday, 4 August 2007

Java Language Fundamentals (Part 7)

Language Fundamentals


Data Types - cont

Integrals & floats

The following table gives the length of integrals and floats






























Name or TypeLength
byte8 bits
short16 bits
int32 bits
long64 bits
float32 bits
double64 bits

Note! Floating
point literals are double unless explicitly
declared as type float


The following is an example of a various declarations


public class
ExampleDeclarations{


public static
void main(String args[]){


int
topSpeed = 150;


float
gear1Change = 25.09F;


double
gear2Change = 48.98;


boolean
goFast = true;


char
myCharacter = ‘R’;


String
errorMessage = “You made a boo boo!”;


}


}



Friday, 3 August 2007

Java Language Fundamentals (Part 6)

Single characters are represented using a char type. A char literal must be enclosed by single quotes (‘ ‘) whilst a String type (which is not a primitive class but is used to represent a collection of characters) us double quotes to delimit (“ “). A sequence of character data is called a string and is implemented in the Java environment by the String class (a member of the java.lang package). You will need to use character strings in most of your programs, if only to output error messages. Strings You have already been making extensive use of string constants for output. Every time you used the println() method, you used a string constant as the argument. A string constant is a sequence of characters between double quotes:

String myString = “This is a string!”;

Some characters can’t be entered explicitly from the keyboard to be included in a string constant. For example you can’t include a double quote because it is used to indicate where a string constant begins and ends. You can’t also use a new line character by pressing Enter key, as this will move the cursor to a new line. For this reason you must use an escape sequence to specify those characters. Shown below is a list of escape characters that you can use to define control characters:

\’ Single quote \t Tab
\” Double quote \n New line
\r Carriage return \\ Backslash
\u???? Replace Unicode with Hexadecimal


If for example, you want to print the following string on the screen,

“He’s brilliant in Java”
He can just about do anything.
then you can write it as follows:

“\”He\’s brilliant in Java\”\n\tHe can just about do anything.”

Thursday, 2 August 2007

Java Language Fundamentals (Part 5)

Java Language Fundamentals

Data Types - continued

The range of values stored by each type is always the same, regardless of what kind of computer you’re using. Therefore your program will execute in the same way on computers that may be quite different. In contrast the format and size of primitive data types of other languages may depend on the platform on which a program is running.

Declaring a variable is exactly same as it is in C. For example:

int value; long bigValue;

float value; double bigValue;

char letter; boolean check;

You can also initialize them at the same time you declare them. For example:
int value = 10; long bigValue = 99999999L;
float value = 1.28E12F; double bigValue = 1.56E-35;
char letter = ‘a’; boolean check = true;

Note: All integer values of type long must have an L appended to them to differentiate them from other integer constants. Same with the float variables, they must have F or a D appended to them.

Booleans
Booleans have two states – true and false. In other languages, a boolean can also have the value of 0 or 1 to signify true or false, but in Java, the only valid values are the literals “true” and “false”. For example:
// declares the variable truth as a boolean and assigns a value
boolean truth = true;

Wednesday, 1 August 2007

Java Language Fundamentals (Part 4)

Java Language Fundamentals (Part 4)


Data Types


All variables in the Java language must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. For example, the declaration


int value

declares that

value
is an integer (
int
).

There are two major categories of data types in the Java language: primitive and reference. The following table lists, by keyword, all of the primitive data types supported by Java:


















































Data Type



Description



Integral type




byte



Values from –128 to +127



short



Values from –32768 to +32767



int



Values from –21474883648 to +2147483647



long



Values from –9223372036854775808 to
+9223372036854775807



Floating type




float



Values from –3.4E38 to +3.4E38



double



Values from –1.7E308 to +1.7E308



textual




char


logical



Stores single character (16-bit Unicode character)



boolean



Can have one of two values (true or false)


Saturday, 28 July 2007

Java Language Fundamentals (Part 3)

Java Language Fundamentals (Part 3)


Java Keywords


The following is a list of keywords in the Java language











































































abstract



do



implements



private



throw



boolean



double



import



protected



throws



break



else



instanceof



public



transient



byte



extends



int



return



true



case



false



interface



short



try



catch



final



long



static



void



char



finally



native



super



volatile



class



float



new



switch



while



continue



for



null



synchronized




default



if



package



this



Friday, 27 July 2007

Java Language Fundamentals (Part 2)

Java Language Fundamentals (Part 2)


Identifiers


Identifiers are the blocks of a programming language - that is, they are the entities (values and data) that act or are acted upon. An identifier can be any length, but it must start with a letter, an underscore or a dollar sign. The rest of the identifier can include any characters except for those that are used as operators, e.g. +, -, * and others. There is one more restriction, you can’t use keywords in Java as a variable name. Valid identifiers are:



  • Identifier

  • UserName

  • User_Name

  • _Sys_name

  • $changeName


Java uses 16 bit Unicode rather than 8 bit ASCII text, so a letter has a considerably wider definition than just a to z or A to Z.


As mentioned before, an identifier can’t be a keyword, but it can contain a keyword as part of it’s name. For example:

importText()
is valid even though import is a keyword.

Tuesday, 24 July 2007

Java Language Fundamentals (Part 1)

Java Language Fundamentals (Part 1)



Comments and Code Conventions


There are 3 types of comments allowed in the Java language:



     // comment on one line

     /* comment on one or more lines */

     /** comment which can be used in the javadoc tool to produce HTML documents */

Always document your code!!!!! I’ll say it again, ALWAYS DOCUMENT YOUR CODE!!! Well documented code is easy for other developers to modify at a later date. In fact you could be that developer, who has to change the undocumented code that you produced a year earlier and can’t remember what all of those 1000 lines of code does.

Do not put obvious comments in. if you have one line of code in a class that prints a string, don’t write “this class prints a string”. On the other hand, if you have a complex class that contains 150 lines of code, comment liberally.

Whitespace (tabs, newlines, etc.) in Java is ignored. A line of code is terminated with a semicolon(;) even if that line happens to span several lines on your screen. A block of code is a collection of statements bounded by opening and closing braces. For example:


     {
topSpeed = 120;
lowSpeed = 35;
}

You can increase readability by using meaningful class, variable, and method names. Avoid using names like “d1” – rather use something like “firstDate”. The Java convention for identifiers are:

Class identifiers – a noun with the first letter of each word capitalized – for example: class MyCat

Variables – a noun with the first letter of the first word lowercase and each subsequent word capitalized – for example: int topSpeed;

Methods – a verb with the same naming convention as variables – for example: public void DropCat()

Interfaces – capitalized like class names

Constants (finals) – all letters capitalized. For example:

Final int MAX_SPEED = 200;

Also, the use of indentation is invaluable. Consider the following code snippet:

while (counter < 0){
for (loopCounter = 0; loopCounter < 11; loopCounter++){
System.out.println(“this is difficult to read”);
}
System.out.println(“think how this looks with another 100 lines!”);
}

or this code snippet:

While (counter < 0){


For (loopCounter = 0; loopCounter < 11; loopCounter++){

System.out.println(“this is better”);
}

System.out.println(“this shows the inner loop better”);
}


Monday, 23 July 2007

Java Language Fundamentals (intro)

Language Fundamentals



Nuts and Bolts

This section covers the basic components of the Java Language. It will include variables, keywords, primitive types and class types.


Section Objectives


  • Use comments and learn the Java coding convention
  • Recognize the difference between valid and invalid identifiers
  • Recognize Keywords
  • List the eight primitive types
  • Define literal values for numeric and text types
  • Be able to discuss the terms class, object, member variable, and reference variable
  • Create a simple class
  • Declare variables of type class
  • Use the new keyword to construct an object
  • Describe default initialization
  • Access member variables using dot notation
  • Understand the consequences of assigning variables of class type