Professional Documents
Culture Documents
Programming (OOP)
CHAPTER TWO-II
Fundamental Programming
Structures in Java
package fahrenheittocelsius;
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
// Convert Fahrenheit to Celsius
double celsius = 5.0/9* (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is " +
celsius + " in Celsius");
}
} 10/02/22 by: A.F 6
Comments in java
Comments are ignored by the compiler but are useful to other
programmers.
Java programming language supports three kinds of comments:
1. Single line comment // comment text
The compiler ignores everything from // to the end of the line.
Logical operators
&& for the logical “and” operator and
|| for the logical “or” operator.
Relational operators
• == (equal),
• != (not equal)
• & (“and”)
• | (“or”)
• ^ (“xor”)
• Reference Variables
• Are used to store the address of an object
• Created from Classes
• E.g. Scanner input;
10/02/22 by: A.F 13
Conditional Statements
if Conditions
if condition
If…else condition
if …else if…else condition
statement2 {
} bonus = 100;
}
• Schematically:
• E.g.
Scanner s=new Scanner(System.in);
DataInputStream str=new DataInputStream
(System.in);
\b backspace
\t tab
\n newline
\" double quote
\' single quote
\\ backslash
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result