0% found this document useful (0 votes)
5 views21 pages

CJ Lecture 4

The document outlines the typical programming cycle in Java, starting from coding to debugging, emphasizing the importance of syntax, keywords, and identifiers. It explains the use of comments, escape sequences, and variable declarations, along with naming rules and styles for Java identifiers. Additionally, it covers expressions and statements, providing examples and assignments related to Java programming concepts.

Uploaded by

roseyyabdulaahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

CJ Lecture 4

The document outlines the typical programming cycle in Java, starting from coding to debugging, emphasizing the importance of syntax, keywords, and identifiers. It explains the use of comments, escape sequences, and variable declarations, along with naming rules and styles for Java identifiers. Additionally, it covers expressions and statements, providing examples and assignments related to Java programming concepts.

Uploaded by

roseyyabdulaahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Typical Programming Cycle

ENG.GOBANIMO 1
Cont..
It normally starts with the coding step, in which the source code is written in any
text editors or integrated programming environment.
In Java, source code files are usually saved with .java extension.
Once the Java source code is saved, it is compiled using a java compiler, such as
javac.exe, to obtain the resulting Java bytecode, which is also called a Java class.
The actual Java class file is created with .class extension.
Then, the program is executed by running the command ‘java.exe’ on the class
file.
If the result appears as expected, the cycle terminates.
Otherwise, the source code has to be edited, then compiled and executed again.
The process of fixing the source code in order to obtain the right result is called
‘debugging’.
ENG.GOBANIMO 2
Syntax, Keywords, and Identifiers
Writing program in Java, or other computer programming languages, is like
writing in any human spoken language but a lot more formal.
When constructing a sentence in a human language, we need to follow its
grammar.
Programming languages also have syntax or rules of the language and they
have to be followed strictly.
Keywords are words that are reserved for particular purposes.
They have special meanings and cannot be changed or used for other
purposes.
For example, from the class definition header in the last example, public class
Myprogram , the word public and class are keywords.
Thus, both words cannot be used in Java programs for naming classes,
methods, or variables.

ENG.GOBANIMO 3
Examples of keywords

ENG.GOBANIMO 4
Cont..
Identifiers are names that programmers give to classes, methods, and
variables.
There are certain rules and styles for Java naming, which we will
discussed in details.
For the previous example, public class Myprogram, the word
Myprogram is a Java identifier selected as the name of the class.
Note that Java is a case-sensitive language.
Uppercase and lowercase letters are not considered the same.
Java keywords are only consisted of lowercase letters.

ENG.GOBANIMO 5
Comments
It is a good practice to always add meaningful comments into the source code.
There are two ways of commenting in Java source code.
The first way is called single line commenting.
It is done by using double slashes // as shown previously.
Any texts between and including // and the end of the line are ignored by the
compiler.
The second way is called block commenting.
In contrary to the first way, this can make comment span more than one line.
Any texts that are enclosed by a pair of the symbol /* and */ are ignored by the
compiler, no matter how many lines there are.

ENG.GOBANIMO 6
Example
// An example of how to add comments
public class MyCommentedProgram
{ // program starting point
public static void main(String [] args)
{ System.out.print("Hello World!");
//This part of code is commented out.
/* System.out.print("Hello again.");
System.out.println(); */
}//end of main()

ENG.GOBANIMO 7
Example2
Write a java program allowing to read data from the keyboard
import java.lang.*;
import java.util.*;
public class Readkeyboard
{ public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
System.out.println(x);
}
}

ENG.GOBANIMO 8
Example3
Write a java program allowing to read data from the keyboard
import java.lang.*;
import java.util.*;
public class Readkeyboard
{ public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
float x=sc.nextFloat();
System.out.println(x);
}
}

ENG.GOBANIMO 9
Example4
import java.lang.*;
import java.util.*;
public class Readkeyboard
{public static void main(String arg[])
{ Scanner sc=new Scanner(System.in);
int x,y;
System.out.println("Enter 2 numbers:");
x=sc.nextInt();
y=sc.nextInt();
int z=x+y;
System.out.println("sum is"+z);
}
}
ENG.GOBANIMO 10
Escape Sequences
An escape sequence is a special character sequence that represents another character.
Each of these special character sequences starts with a backslash, which is followed by
another character.
Escape sequences are used to represent characters that cannot be used directly in a
string (double quoted message).
Escape sequence Represented character Escape sequence Represented character

\b Backspace \\ Backslash

\n Newline \" Double quote

\t Tab \' Single quote

\r Carriage return

ENG.GOBANIMO 11
Variable
Variables are symbolic names of memory locations.
They are used for storing values used in programs.
In many programming languages including Java, before a variable can be
used, it has to be declared so that its name is known and proper space in
memory is allocated.
int x;
double y;
String myText;

ENG.GOBANIMO 12
Cont..
On the first line, a variable x is created for storing an int (integer) value.
On the second line, a variable y is created for storing a double (double
precision floating point) value.
On the last line, a variable myText is created for storing a reference to
String (a Java standard class representing double quoted text) object.
The name x, y, and myText are Java identifiers.
int and double are Java keywords.
String is the name of a Java class.

ENG.GOBANIMO 13
Cont..
Variables are normally used with the assignment operator (=)
Which assign the value on the right to the variable on the left as can be
observed in below:
x=5;
y=6.5;
myText=“java programming”
int x;
int z=x;

ENG.GOBANIMO 14
Cont..
On the first three lines, values are assigned to the three variables according to their
types.
On the last two lines, a variable z is declared and then assigned the value of the variable
x.
Declaration and value assignment (initialization) could be done in the same statement as
can be observed in below table.
int i = 1;
double f = 0.0;
string mySecondText =
"Java is fun.";

ENG.GOBANIMO 15
Naming Rules and Styles
There are certain rules for the naming of Java identifiers.
Valid Java identifier must be consistent with the following rules.
➢An identifier cannot be a Java reserve word.
➢An identifier must begin with an alphabetic letter, underscore (_), or a
dollar sign ($).
➢If there are any characters subsequent to the first one, those characters
must be alphabetic letters, digits, underscores (_), or dollar signs ($).
➢Whitespace cannot be used in a valid identifier.
➢An identifier must not be longer than 65,535 characters.

ENG.GOBANIMO 16
Cont..
Also, there are certain styles that programmers widely used in naming
variables, classes and methods in Java.
Here are some of them.
➢Use lowercase letter for the first character of variables’ and methods’
names.
➢Use uppercase letter for the first character of class names.
➢Use meaningful names.
➢Compound words or short phrases are fine, but use uppercase letter for
the first character of the words subsequent to the first.
➢use underscore to separate words.
ENG.GOBANIMO 17
Cont..
➢Use uppercase letter for all characters in a constant.
➢Use underscore to separate words.
➢Apart from the mentioned cases, always use lowercase letter.
➢Use verbs for methods’ names
Here are some examples for good Java identifiers.
➢Variables: height, speed, filename, tempInCelcius, incomingMsg, textToShow.
➢Constant: SOUND_SPEED, KM_PER_MILE, BLOCK_SIZE.
➢Class names: Account, DictionaryItem, FileUtility, Article.
➢Method names: locate, sortItem, findMinValue, checkForError.

ENG.GOBANIMO 18
Statements and Expressions
An expression is a value, a variable, a method, or one of their combinations that can be
evaluated to a value.

3.857
a + b – 10
8 >= x
p || q
"go"
Math.sqrt(2)
squareRootTwo = Math.sqrt(2)

ENG.GOBANIMO 19
Cont..
A statement is any complete sentence that causes some action to occur.
A valid Java statement must end with a semicolon.
Each line of the below is a Java statement.
int a;
int y=10;
double d1,d2,d3;
k=a+b-10;
boolean p=(a>=b);

ENG.GOBANIMO 20
Assignment
1) Explain why java is a platform independent?
2) Explain java program skeleton with example?
3) Define class with example?
4) Define polymerization?
5) Write a java program adding three numbers by applying read keyboard?
6) Write a java program adding four numbers by applying read keyboard?
7) Explain java package with examples?

ENG.GOBANIMO 21

You might also like