OOP Lab1-1
OOP Lab1-1
Lab 1
04/11/2024
JDK (java development kit)
How java compiles ?
Hello Java
First, create file “App.java”
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
2- Save the file then open the cmd in the same path of the file
3- write in the cmd ------> javac App.java //to compile
4- then write -------> java App //to run
Blocks
// Assign a radius
radius = 20;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Naming Conventions
• Choose meaningful and descriptive names.
• Variables and method names:
• Use lowercase. If the name consists of several words,
concatenate all in one, use lowercase for the first
word, and capitalize the first letter of each subsequent
word in the name. For example, the variables
radius and area, and the method
computeArea.
Naming Conventions, cont.
• 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
Numerical Data Types (Primitive Data Types)
Positive range:
4.9E-324 to 1.7976931348623157E+308
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
|| or logical disjunction
ComputeAreaWithConsoleInput Run
ComputeAverage Run
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
String is actually a predefined class in the Java library just like the
System class and Scanner class. The String type is not a primitive
type.
Simple Methods for String
Objects
Method Description
inside main
if(ss1 == ss2) //always false, we always use equals() when comparing strings
{
System.out.println("done");
}
Obtaining Substrings
Method Description
substring(beginIndex) Returns this string’s substring that begins with the character at the specified
beginIndex and extends to the end of the string, as shown in Figure 4.2.
substring(beginIndex, Returns this string’s substring that begins at the specified beginIndex and
endIndex) extends to the character at index endIndex – 1, as shown in Figure 9.6.
Note that the character at endIndex is not part of the substring.
Creating Arrays
arrayRefVar = new datatype[arraySize];