0% found this document useful (0 votes)
1 views21 pages

Module - 1 Programs

The document provides a comprehensive overview of Java programming concepts, including basic syntax, control statements, data types, typecasting, arrays, and operators. It features multiple code examples demonstrating the use of classes, methods, and various programming constructs. The content is structured into sections, each focusing on a specific topic related to Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views21 pages

Module - 1 Programs

The document provides a comprehensive overview of Java programming concepts, including basic syntax, control statements, data types, typecasting, arrays, and operators. It features multiple code examples demonstrating the use of classes, methods, and various programming constructs. The content is structured into sections, each focusing on a specific topic related to Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Introduction
1.
public class HelloWorld {
public static void main(String[] args)
{
[Link]("Hello World!!!");
}
}
2.
public class ControlStatementsDemo {
public static void main(String[] args) {
int number = 5;

// if statement
if (number % 2 == 0) {
[Link](number + " is Even");
} else {
[Link](number + " is Odd");
}

// for loop
[Link]("Numbers from 1 to 5:");
for (int i = 1; i <= 5; i++) {
[Link](i);
}
}
}
2. Basics
1.
public class StringLiteralsDemo {
public static void main(String[] args) {
// Simple string literals
String greeting = "Hello World";
String name = "Java";

// String with escape sequences


String twoLines = "First Line\nSecond Line";
String inQuotes = "\"This is inside quotes\"";
String withTab = "Hello\tWorld";

// Uni-code in string
String unicodeString = "Smiley: \u263A";

// Concatenated string literals


String combined = "Java " + "Programming";

// Display results
[Link]("Greeting: " + greeting);
[Link]("Name: " + name);
[Link]("Two lines:\n" + twoLines);
[Link]("Quoted string: " + inQuotes);
[Link]("With tab: " + withTab);
[Link]("Unicode string: " + unicodeString);
[Link]("Combined string: " + combined);
}
}

2.
public class PrimitiveDataTypesDemo {
public static void main(String[] args) {
// Integer types
byte b = 100; // 8-bit
short s = 10000; // 16-bit
int i = 100000; // 32-bit
long l = 10000000000L; // 64-bit, needs 'L'

// Floating-point types
float f = 3.14f; // 32-bit, needs 'f'
double d = 3.14159265359; // 64-bit

// Character type
char c = 'A'; // 16-bit Unicode

// Boolean type
boolean flag = true; // true or false

// Display all primitive types


[Link]("byte value: " + b);
[Link]("short value: " + s);
[Link]("int value: " + i);
[Link]("long value: " + l);
[Link]("float value: " + f);
[Link]("double value: " + d);
[Link]("char value: " + c);
[Link]("boolean value: " + flag);
}
}

3.
public class IntegerLiteralsDemo {
public static void main(String[] args) {
// Integer literals in different forms
int decimal = 42; // Decimal literal
int octal = 052; // Octal literal (leading 0, value = 42 in decimal)
int hex = 0x2A; // Hexadecimal literal (0x prefix, value = 42 in decimal)
int binary = 0b101010; // Binary literal (0b prefix, value = 42 in decimal)

// Using underscores for readability


int bigNumber = 1_000_000; // 1,000,000 with underscore

// Display results
[Link]("Decimal literal: " + decimal);
[Link]("Octal literal: " + octal);
[Link]("Hexadecimal literal: " + hex);
[Link]("Binary literal: " + binary);
[Link]("Readable literal with underscores: " + bigNumber);
}
}

4.
public class FloatingPointLiteralsDemo {
public static void main(String[] args) {
// Standard notation
double pi = 3.14159; // double by default
float fraction = 0.75f; // float literal (append f or F)

// Scientific notation
double avogadro = 6.022E23; // 6.022 × 10^23
double smallNumber = 3.14e-4; // 0.000314

// Explicit double (optional)


double gravity = 9.81d; // appending d or D (redundant)

// Hexadecimal floating-point literal


double hexValue = 0x1.2p3; // = (1.125 × 2^3) = 9.0

// Using underscores for readability


double bigNumber = 9_423_497_862.0;
double withFraction = 1_234.56_78;

// Display results
[Link]("Pi (double): " + pi);
[Link]("Fraction (float): " + fraction);
[Link]("Avogadro's number: " + avogadro);
[Link]("Small number: " + smallNumber);
[Link]("Gravity: " + gravity);
[Link]("Hexadecimal floating literal: " + hexValue);
[Link]("Big number with underscores: " + bigNumber);
[Link]("Fraction with underscores: " + withFraction);
}
}

5.
public class CharacterLiteralsDemo {
public static void main(String[] args) {
// Direct character literals
char letterA = 'A';
char digit = '5';
char symbol = '@';

// Escape sequences
char singleQuote = '\'';
char newLine = '\n';
char tab = '\t';

// Uni-code representation
char unicodeA = '\u0041'; // 'A'
char unicodeSmile = '\u263A'; // ☺

// Octal representation
char octalA = '\101'; // 'A' (octal 101 = decimal 65)

// Display results
[Link]("Direct characters: " + letterA + ", " + digit + ", " + symbol);
[Link]("Single quote: " + singleQuote);
[Link]("New line after this line:" + newLine + "This is on a new line");
[Link]("Tab between words: Hello" + tab + "World");
[Link]("Unicode A: " + unicodeA);
[Link]("Unicode Smile: " + unicodeSmile);
[Link]("Octal A: " + octalA);
}
}

6.
public class BooleanLiteralsDemo {
public static void main(String[] args) {
// Boolean literals: only true or false
boolean flag = true;
boolean status = false;

// Using boolean values in conditions


if (flag) {
[Link]("Flag is true!");
}

if (!status) {
[Link]("Status is false!");
}

// Directly printing Boolean literals


[Link]("Boolean literal true: " + true);
[Link]("Boolean literal false: " + false);
}
}
3. Typecast
1.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
[Link]("\nConversion of int to byte.");
b = (byte) i;
[Link]("i and b " + i + " " + b);
[Link]("\nConversion of double to int.");
i = (int) d;
[Link]("d and i " + d + " " + i);
[Link]("\nConversion of double to byte.");
b = (byte) d;
[Link]("d and b " + d + " " + b);
}
}

2.
public class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
[Link]((f * b)+ " + " + (i / c) + " - " + (d * s));
[Link]("result = " + result);
}

3.
public class VarDemo {
public static void main(String[] args) {
// Local variable type inference examples
var num = 100; // inferred as int
var pi = 3.14159; // inferred as double
var name = "Java"; // inferred as String
var flag = true; // inferred as boolean
var arr = new int[5]; // inferred as int[]

[Link]("Number: " + num);


[Link]("Pi Value: " + pi);
[Link]("Name: " + name);
[Link]("Flag: " + flag);
[Link]("Array length: " + [Link]);
}
}

4. Arrays
1.
public class Testarray {
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation

a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

//traversing array
for(int i=0;i<[Link];i++) //length is the of array
[Link](a[i]);
}
}

2.
public class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
[Link](twoD[i][j] + " ");
[Link]();
}
}

3.
public class ThreeDAarray {
public static void main(String[] args) {
// Create a 3D array of size 2x3x2
int[][][] arr = new int[2][3][2];
// Assign values
int value = 1;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i][j].length; k++) {
arr[i][j][k] = value++;
}
}
}

// Display values
for (int i = 0; i < [Link]; i++) {
[Link]("Block " + (i + 1) + ":");
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i][j].length; k++) {
[Link](arr[i][j][k] + " ");
}
[Link]();
}
[Link]("----");
}
}
}
5. Operators
1.
public class ArithmeticOperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 5;

// Basic Arithmetic
[Link]("a + b = " + (a + b)); // Addition
[Link]("a - b = " + (a - b)); // Subtraction
[Link]("a * b = " + (a * b)); // Multiplication
[Link]("a / b = " + (a / b)); // Division
[Link]("a % b = " + (a % b)); // Modulus

// Increment and Decrement


int x = 5;
[Link]("x = " + x);
[Link]("x++ = " + (x++)); // Post-increment
[Link]("After x++, x = " + x);
[Link]("++x = " + (++x)); // Pre-increment
[Link]("x-- = " + (x--)); // Post-decrement
[Link]("After x--, x = " + x);
[Link]("--x = " + (--x)); // Pre-decrement

// Assignment Operators
int y = 20;
[Link]("\nInitial y = " + y);
y += 5;
[Link]("y += 5 → " + y); // Addition assignment
y -= 3;
[Link]("y -= 3 → " + y); // Subtraction assignment
y *= 2;
[Link]("y *= 2 → " + y); // Multiplication assignment
y /= 4;
[Link]("y /= 4 → " + y); // Division assignment
y %= 3;
[Link]("y %= 3 → " + y); // Modulus assignment
}
}

2.
public class BitwiseOperatorsDemo {
public static void main(String[] args) {
int a = 6; // 0110 in binary
int b = 3; // 0011 in binary

[Link]("a = " + a + " (" + [Link](a) + ")");


[Link]("b = " + b + " (" + [Link](b) + ")\n");

// Bitwise unary NOT


[Link]("~a = " + (~a) + " (" + [Link](~a) + ")");

// Bitwise AND, OR, XOR


[Link]("a & b = " + (a & b) + " (" + [Link](a & b) + ")");
[Link]("a | b = " + (a | b) + " (" + [Link](a | b) + ")");
[Link]("a ^ b = " + (a ^ b) + " (" + [Link](a ^ b) + ")");

// Shift operators
[Link]("a >> 1 = " + (a >> 1) + " (" + [Link](a >> 1) + ")");
[Link]("a >>> 1 = " + (a >>> 1) + " (" + [Link](a >>> 1) +
")");
[Link]("a << 1 = " + (a << 1) + " (" + [Link](a << 1) + ")");
// Assignment with bitwise operators
int x = 7; // 0111
[Link]("\nInitial x = " + x + " (" + [Link](x) + ")");
x &= 3; // AND assignment
[Link]("x &= 3 → " + x + " (" + [Link](x) + ")");
x |= 4; // OR assignment
[Link]("x |= 4 → " + x + " (" + [Link](x) + ")");
x ^= 2; // XOR assignment
[Link]("x ^= 2 → " + x + " (" + [Link](x) + ")");
x >>= 1; // Shift right assignment
[Link]("x >>= 1 → " + x + " (" + [Link](x) + ")");
x >>>= 1; // Shift right zero fill assignment
[Link]("x >>>= 1 → " + x + " (" + [Link](x) + ")");
x <<= 2; // Shift left assignment
[Link]("x <<= 2 → " + x + " (" + [Link](x) + ")");
}
}

3.
public class LogicalOperatorsDemo {
public static void main(String[] args)
{
boolean a = true;
boolean b = false;

[Link]("a = " + a + ", b = " + b);

// Basic logical operators


[Link]("a & b = " + (a & b)); // Logical AND
[Link]("a | b = " + (a | b)); // Logical OR
[Link]("a ^ b = " + (a ^ b)); // Logical XOR (exclusive OR)
[Link]("a || b = " + (a || b)); // Short-circuit OR
[Link]("a && b = " + (a && b)); // Short-circuit AND
[Link]("!a = " + (!a)); // Logical unary NOT

// Assignment operators
boolean x = true;//y = false;
x &= b; // AND assignment
[Link]("After x &= b, x = " + x);

x = true;
x |= b; // OR assignment
[Link]("After x |= b, x = " + x);

x = true;
x ^= b; // XOR assignment
[Link]("After x ^= b, x = " + x);

// Equality operators
[Link]("a == b : " + (a == b)); // Equal to
[Link]("a != b : " + (a != b)); // Not equal to

// Ternary operator
String result = a ? "a is true" : "a is false";
[Link]("Ternary (a ? ... ) = " + result);
}
}

4.
public class RelationalOperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 20;
[Link]("a = " + a + ", b = " + b + "\n");

// Relational operators
[Link]("a == b : " + (a == b)); // Equal to
[Link]("a != b : " + (a != b)); // Not equal to
[Link]("a > b : " + (a > b)); // Greater than
[Link]("a < b : " + (a < b)); // Less than
[Link]("a >= b : " + (a >= b)); // Greater than or equal to
[Link]("a <= b : " + (a <= b)); // Less than or equal to
}
}

6. Selection Statements

1.
public class SelectionStatementsDemo {

public static void main(String[] args) {

int number = 15;

// 1. if
[Link]("---- if Example ----");
if (number > 0) {
[Link]("Number is positive");
}

// 2. if-else
[Link]("\n---- if-else Example ----");
if (number % 2 == 0) {
[Link]("Number is even");
} else {
[Link]("Number is odd");
}

// 3. if-else-if ladder
[Link]("\n---- if-else-if Ladder Example ----");
if (number < 0) {
[Link]("Number is negative");
} else if (number == 0) {
[Link]("Number is zero");
} else if (number < 10) {
[Link]("Number is a small positive");
} else {
[Link]("Number is a large positive");
}

// 4. nested if
[Link]("\n---- nested if Example ----");
int age = 20;
if (age >= 18) {
if (age < 60) {
[Link]("You are an adult but not a senior citizen");
} else {
[Link]("You are a senior citizen");
}
} else {
[Link]("You are a minor");
}

// 5. switch
[Link]("\n---- switch Example ----");
int day = 4;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid day");
}
}
}

2.
public class NestedSwitchDemo {
public static void main(String[] args) {
int branch = 1; // 1 - CSE, 2 - ECE
int year = 2; // Year of study

switch (branch) {
case 1: // CSE
[Link]("Branch: CSE");
switch (year) {
case 1:
[Link]("Subjects: Programming in C, Mathematics");
break;
case 2:
[Link]("Subjects: Data Structures, OOP in Java");
break;
case 3:
[Link]("Subjects: Database Systems, Operating Systems");
break;
case 4:
[Link]("Subjects: Machine Learning, Cloud Computing");
break;
default:
[Link]("Invalid year for CSE");
}
break;
case 2: // ECE
[Link]("Branch: ECE");
switch (year) {
case 1:
[Link]("Subjects: Basic Electronics, Mathematics");
break;
case 2:
[Link]("Subjects: Digital Systems, Signals and Systems");
break;
case 3:
[Link]("Subjects: Microprocessors, Communication Systems");
break;
case 4:
[Link]("Subjects: VLSI Design, Embedded Systems");
break;
default:
[Link]("Invalid year for ECE");
}
break;

default:
[Link]("Invalid branch");
}
}
}
7. Iteration Statements
1.
public class IterationDemo {
public static void main(String[] args) {

// 1. for loop
[Link]("---- for loop ----");
for (int i = 1; i <= 5; i++) {
[Link]("i = " + i);
}

// 2. while loop
[Link]("\n---- while loop ----");
int j = 1;
while (j <= 5) {
[Link]("j = " + j);
j++;
}

// 3. do-while loop
[Link]("\n---- do-while loop ----");
int k = 1;
do {
[Link]("k = " + k);
k++;
} while (k <= 5);

// 4. Enhanced for loop (1D array)


[Link]("\n---- enhanced for loop (1D array) ----");
int[] numbers = {10, 20, 30, 40, 50};
for (int n : numbers) {
[Link]("n = " + n);
}

// 5. Enhanced for loop (2D array)


[Link]("\n---- enhanced for loop (2D array) ----");
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

for (int[] row : matrix) {


for (int value : row) {
[Link](value + " ");
}
[Link]();
}

// 6. Nested for loops


[Link]("\n---- nested for loops (Multiplication Table) ----");
for (int a = 1; a <= 5; a++) {
for (int b = 1; b <= 5; b++) {
[Link]((a * b) + "\t");
}
[Link]();
}
}
}

8. Jump Statement
1.
public class JumpStatementsDemo {
public static void main(String[] args) {

// 1. break statement
[Link]("---- break example ----");
for (int i = 1; i <= 10; i++) {
if (i == 5) {
[Link]("Breaking the loop at i = " + i);
break; // exits the loop
}
[Link]("i = " + i);
}

// 2. continue statement
[Link]("\n---- continue example ----");
for (int j = 1; j <= 5; j++) {
if (j == 3) {
[Link]("Skipping value j = " + j);
continue; // skips current iteration
}
[Link]("j = " + j);
}

// 3. return statement
[Link]("\n---- return example ----");
for (int k = 1; k <= 5; k++) {
if (k == 4) {
[Link]("Returning from main at k = " + k);
return; // exits the method (program ends here)
}
[Link]("k = " + k);
}

// This line will never be executed


// because 'return' exits the program.
[Link]("End of program");
}
}

You might also like