0% found this document useful (0 votes)
31 views24 pages

OOP Lab1-1

Uploaded by

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

OOP Lab1-1

Uploaded by

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

OOP

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

A pair of braces in a program forms a block that groups


components of a program.

public class Test {


Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}
animation
Trace a Program Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
radius 20
double radius;
double area; area 1256.636

// Assign a radius
radius = 20;

// Compute area print a message to the


area = radius * radius * 3.14159;
console

// 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)

Name Range Storage Size

byte –27 to 27 – 1 (-128 to 127) 8-bit signed

short –215 to 215 – 1 (-32768 to 32767) 16-bit signed

int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed

long –263 to 263 – 1 64-bit signed


(i.e., -9223372036854775808 to 9223372036854775807)

float Negative range: 32-bit IEEE 754


-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324

Positive range:
4.9E-324 to 1.7976931348623157E+308

• please not that boolean Datatype is true/false; Not 0/1


Integer Division
+, -, *, /, and %

5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)


Relational Operators
Java Mathematics Name Example Result
Operator Symbol (radius is 5)

< < less than radius < 0 false


<= ≤ less than or equal to radius <= 0 false
> > greater than radius > 0 true
>= ≥ greater than or equal to radius >= 0 true
== = equal to radius == 0 false
!= ≠ not equal to radius != 0 true
Logical Operators
Operator Name Description

! not logical negation

&& and logical conjunction

|| or logical disjunction

^ exclusive or logical exclusion


Reading Input from the Console

1. Create a Scanner object


Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a
double value. For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();

ComputeAreaWithConsoleInput Run

ComputeAverage Run
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();

Method Description

nextByte() reads an integer of the byte type.


nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
The String Type
The char type only represents one character. To represent a string
of characters, use the data type called String. For example,

String message = "Welcome to Java"; (immutable)

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

length() Returns the number of characters in this string.


charAt(index) Returns the character at the specified index from this string.
concat(s1) Returns a new string that concatenates this string with string s1.
toUpperCase() Returns a new string with all letters in uppercase.
toLowerCase() Returns a new string with all letters in lowercase.
trim() Returns a new string with whitespace characters trimmed on both sides.
Getting String Length
String message = "Welcome to Java";
System.out.println("The length of " + message + " is "
+ message.length());
Getting Characters from a
String

String message = "Welcome to Java";


System.out.println("The first character in message is "
+ message.charAt(0));
Reading a Character from
the Console
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
String s = input.nextLine();
char ch = s.charAt(0);
System.out.println("The character entered is " + ch);
Comparing Strings
Method Description

equals(s1) Returns true if this string is equal to string s1.


equalsIgnoreCase(s1) Returns true if this string is equal to string s1; it is case insensitive.
compareTo(s1) Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether
this string is greater than, equal to, or less than s1.
compareToIgnoreCase(s1) Same as compareTo except that the comparison is case insensitive.
startsWith(prefix) Returns true if this string starts with the specified prefix.
endsWith(suffix) Returns true if this string ends with the specified suffix.
Comparing two strings

inside main

String ss1 = new String("Hello");


String ss2 = new String("Hello");

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];

new Keyword is used to create object


Example: inside HEAP memory
myList = new double[10];

myList[0] references the first element in the array.


myList[9] references the last element in the array.
Declaring and Creating
in One Step
• datatype[] arrayRefVar = new
datatype[arraySize];
double[] myList = new double[10];

• datatype arrayRefVar[] = new


datatype[arraySize];
double myList[] = new double[10];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy