0% found this document useful (0 votes)
24 views15 pages

Lecture 2

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

Lecture 2

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

Lecture 2

Introduction to Programming
Introduction

• Brief introduction to Java and its structure.


• Understanding the Java program structure.
• The focus of the lecture: Basic syntax, writing a simple
program, and handling input/output.
Java Program Structure

• class declaration: All java


code resides within public static void main(String[]
classes args){
• main method: Entry point // Code goes here
for any Java program
• Statements: Executable }
code.
• Comments: // Single-line
comments and /* Multi-line
comments */.
Basic Syntax elements

• Data Types :
• Primitive Data Types: int,double,boolean,char
• Non-Primitive Data Types: String, arrays, classes.

– Basic Operators:
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
Example Code

• int number = 10;


• double price = 9.99;
• boolean isAvailable = true;
• char grade = 'A';
Int vs long vs byte vs short

• int: 4 bytes, stores whole numbers from -2^31 to 2^31-1.


• long: 8 bytes, stores large whole numbers from -2^63 to
2^63-1.
• byte: 1 byte, stores small whole numbers from -128 to
127.
• short: 2 bytes, Stores whole numbers from -32,768 to
32,767
Example code

• int age = 25;


• long population = 7800000000L;
• byte level = 120;
• short random = 56;
Float vs Double

• In Java, both float and double are used to represent real


numbers (numbers with decimal points), but there are key
differences between them in terms of precision, size, and
usage.
Float vs Double

• Float: Double:
• Size: 4 bytes (32 bits) • Size: 8 bytes (64 bits)
• Precision: Up to 7 decimal • Precision: Up to 15-16
digits decimal digits
• Default value: 0.0f • Default value: 0.0d
• Example: • Example:
float price = 9.99f; double pi =
3.141592653589793;
All primitive data types
Input in Java

• import java.util.Scanner // import the Scanner class


• Scanner scn = new Scanner(System.in); // Create a
object
• Example of reading input:
System.out.println(“Enter your name: “);
String name = scn.nextLine();

System.out.println(“Enter your age: “);


int age = scn.nextInt();
Input in Java

• nextLine(): For reading a string type


• nextInt(): For reading a int type
• nextDouble(): For reading a double type
• nextFloat(): For a reading a float type
Output in Java

• Printing without a newline:


System.out.print(“Hello”);
System.out.print(“Freshmen);
• Printing with a newline:
System.out.println(“Hello Freshmen”);
• Formatting Output:
Using printf for formatted output:
System.out.printf("Name: %s, Age: %d", name, age);
Source Code
import java.util.Scanner;

public class BasicJavaProgram {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input
System.out.print("Enter your name: ");
String name = scanner.nextLine();

System.out.print("Enter your age: ");


int age = scanner.nextInt();

// Output
System.out.println("Hello, " + name + "!");
System.out.printf("You are %d years old.\n", age);
}
}
Math functions
• Math.abs(x): Returns the absolute value of x (for both integers and floating-point numbers)
• Math.max(x, y) and Math.min(x, y): Return the maximum or minimum of two numbers.
• Math.pow(base, exponent): Returns the value of base raised to the power of exponent.
• Math.sqrt(x): Returns the square root of x
• Math.cbrt(x): Returns the cube root of x
• Math.ceil(x) and Math.floor(x): ceil rounds up to the nearest whole number, while floor
rounds down
• Math.round(x): Rounds x to the nearest integer
• Math.sin(angle):Returns the sine of an angle (in radians).
• Math.cos(angle): Returns the cosine of an angle (in radians).
• Math.PI: Value of π (3.141592653589793).
• Math.E: Value of Euler's number (e ≈ 2.718281828459045).

You might also like