Advance Computer Programming: by Hasnat Ali
Advance Computer Programming: by Hasnat Ali
By
Hasnat Ali
JAVA
Elementary Programming
Contents
// Create a Scanner
Scanner input = new Scanner(System.in);
import javax.swing.JOptionPane;
// Obtain input
String val = JOptionPane.showInputDialog(null, “Enter Value”);
// calculating square
int square = intValue * intValue;
JOptionPane.showMessageDialog(null, output);
}
}
Two ways to invoke the Method
There are several ways to use the showInputDialog method. For the time
being, you only need to know two ways to invoke it.
JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE);
where x is a string for the prompting message, and y is a string for the
title of the input dialog box.
JOptionPane.showInputDialog(x);
To convert a string into an int value, you can use the static
parseInt method in the Integer class as follows:
Others to use
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
GUI Displaying Text
import javax.swing.JOptionPane;
Syntax:
final datatype CONSTANTNAME = VALUE;
Example:
final double PI = 3.14159;
+ Addition 34 + 1 35
% Remainder 20 % 3 2
Integer Division
5 / 2 yields an integer 2.
int i = 34;
long x = 1000000;
double d = 5.0;
Integer Literals
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53
Shortcut Assignment Operators
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
range increases
String s = "Chapter" + 2;
Declaring Array:
double nums[]; OR double[] nums;
Initializing/Instantiating Array:
double[] nums = { 2.0, 4.0, 6.0};
OR
double[] nums = new double[3];
nums[0] = 2.0;
nums[1] = 4.0;
nums[2] = 6.0;
OR
double[] nums = new double[] { 2.0, 4.0, 6.0};
Arrays in Java
Declaring Array:
String str[]; OR String[] str;
Initializing/Instantiating Array:
String[] names = { “John", “Jacob", “Joseph" };
OR
String[] names = new String[3];
names[0] = "John";
names[1] = "Jacob";
names[2] = “Joseph";
OR
String[] names = new String[] {“John", "Jacob", “Joseph" };
Multi-Dimensional Arrays in Java
Declaring Array:
int[ ][ ] aryNumbers = new int[2][3];
Initializing/Instantiating Array:
aryNumbers[0][0] = 10;
aryNumbers[0][1] = 12;
aryNumbers[0][2] = 43;
aryNumbers[1][0] = 11;
aryNumbers[1][1] = 22;
aryNumbers[1][2] = 1;
Multi-Dimensional Arrays in Java
Declaring Array:
int[ ][ ] aryNumbers = new int[6][5];
Initializing/Instantiating Array:
aryNumbers[0][0] = 10;
aryNumbers[0][1] = 12;
aryNumbers[0][2] = 43;
aryNumbers[0][3] = 11;
aryNumbers[0][4] = 22;
aryNumbers[1][3] = 1;
aryNumbers[1][4] = 33;
Multi-Dimensional Arrays in Java
Declaring Array:
double[][][] numbers = new double[1][2][4];
Initializing/Instantiating Array:
numbers[0][0][0] = 12.44;
numbers[0][0][1] = 525.38;
numbers[0][0][2] = -6.28;
numbers[0][0][3] = 2448.32;
numbers[0][1][0] = 632.04;
numbers[0][1][1] = -378.05;
numbers[0][1][2] = 48.14;
numbers[0][1][3] = 634.18;
Programming Style and Documentation
Appropriate Comments
Naming Conventions
Block Styles
Appropriate Comments
Class names:
– Capitalize the first letter of each word in the name.
For example, the class name ComputeArea.
Constants:
– Capitalize all letters in constants, and use underscores
to connect words. For example, the constant PI and
MAX_VALUE
Proper Indentation and Spacing
Indentation
– Indent two spaces
Spacing
– Use blank line to separate segments of the code
Block Styles
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}