Lecture
Lecture
Introduction to Programming
CRICOS 03171A
Focus for this week
• If more than one class is in a source code file, only one of them may be public.
• The public class and the filename of the source code file must match.
ex: A class named Simple must be in a file named Simple.java
• Each Java class can be separated into parts.
2-4
Practice activity
Download and install IDE using the instructions
Code and Run the above example program
2-6
Console Output
• The previous example uses the line:
System.out.println("Programming is great fun!");
• This line uses the System class from the standard Java library.
• The System class contains methods and objects that perform system level tasks.
• The out object, a member of the System class, contains the methods print and
println.
2-7
Console Output
• The print statement works very similarly to the
println statement.
• However, the print statement does not put a
newline character at the end of the output.
• The lines:
System.out.print("These lines will be");
System.out.print("printed on");
System.out.println("the same line.");
Will output:
These lines will beprinted onthe same line.
Notice the odd spacing? Why are some words written
together?
2-8
\t tab Causes the cursor to skip over to the next tab stop
\b backspace Causes the cursor to back up, or move left, one position
Practice Question
• Write the code that would result in the following
output:
These are our top seller:
Computer games
Coffee
Asprin
My name is “Qasir”
• Programmers determine the number and type of variables a program will need.
The + Operator
• The + operator can be used in two
ways.
– as a concatenation operator
– as an addition operator
• If either side of the + operator is a
string, the result will be a string.
System.out.println("Hello " + "World");
System.out.println("The value is: " + 5);
System.out.println("The value is: " + value);
System.out.println("The value is: " + ‘/n’ + 5);
2-12
Identifiers
• Identifiers are programmer-defined names for:
– classes
– variables
– methods
• Identifiers may not be any of the Java reserved keywords.
2-14
Identifiers
• Identifiers must follow certain rules:
– An identifier may only contain:
letters a–z or A–Z,
the digits 0–9,
underscores (_), or
the dollar sign ($)
Variable Names
• Variable names should be descriptive.
• Descriptive names allow the code to be more readable; therefore, the code is more
maintainable.
• http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html
2-16
Practice Question
• Why does below code give an error
– float number;
– number = 23.5; // Error!
• The value of a boolean variable may only be copied into a boolean variable.
Unicode
• Internally, characters are stored as numbers.
• This number to Text conversion is done using
Unicode
• The Unicode character set can consist of
65536 (216) individual characters.
• ASCII was 256
2-22
Unicode
The binary numbers
A represent these
decimal values.
B
00 65 00 66
0000000001000001 0000000001000011
https://unicodetable.archive.thomasorlita.com/
2-23
Practice Question
• What is the error in below code
public static void main(String [] args)
{
int month, days;
Arithmetic Operators
• Java has five (5) arithmetic operators.
Operator Precedence
• Mathematical expressions can be very complex.
1 2
4
2-27
Commenting Code
• Java provides three methods for commenting code.
Comment
Style Description
Single line comment. Anything after the // on the line will be
//
ignored by the compiler.
Block comment. Everything beginning with /* and ending with
/* … */ the first */ will be ignored by the compiler. This comment type
cannot be nested.
Javadoc comment. This is a special version of the previous block
comment that allows comments to be documented by the javadoc
/** … */ utility program. Everything beginning with the /** and ending
with the first */ will be ignored by the compiler. This comment
type cannot be nested.
2-29
Indentation
• Programs should use proper indentation.
• Each block of code should be indented a few spaces from
its surrounding block.
• Two to four spaces are sufficient.
• Tab characters should be avoided.
– Tabs can vary in size between applications and devices.
– Most programming text editors allow the user to replace the tab
with spaces.
2-30
import java.util.Scanner;
2-31
Practice Questions
List the variables, literals and what would be the screen output?
Public class BigLittle {
Public static void main (String[] args)
{ int little;
int big;
little = 2;
big = 2000;
System.out.println (“The little number is ” + little);
System.out.println (“The big number is ” + big);
}
}
- Which of the following are illegal names for identifiers and why?
- x
- 99bottles
- July 97
- R&D