0% found this document useful (0 votes)
8 views12 pages

Java File Updated

The document contains Java programming assignments submitted by Kanishk Anand, focusing on various concepts such as data types, classes and objects, string manipulation, and matrix operations. Each program includes code snippets demonstrating the respective concepts, along with expected outputs. The assignments aim to illustrate practical applications of Java programming in a lab setting.

Uploaded by

Ak
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)
8 views12 pages

Java File Updated

The document contains Java programming assignments submitted by Kanishk Anand, focusing on various concepts such as data types, classes and objects, string manipulation, and matrix operations. Each program includes code snippets demonstrating the respective concepts, along with expected outputs. The assignments aim to illustrate practical applications of Java programming in a lab setting.

Uploaded by

Ak
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

Java Programming

File

Name · KANISHK ANAND


[Link]- 10324210136
Section- G
Semestor-4th
Subject code-23CS2116
Subject name-Java Programming Lab

Submitted by: Kanishk Anand


Submitted to: Dr. Ruchi
Program1

Program: WAP in Java illustrating various data types in Java.


Code:
public class data
{
public static void main(String[] args)
{
// Integer data types
byte byteValue = 10;
short shortValue = 200;
int intValue = 5000;
long longValue = 9876543210L;

// Floating point data types


float floatValue = 12.5f;
double doubleValue = 123.456;

// Character data type


char charValue = 'J';

// Boolean data type


boolean boolValue = true;

// Non-primitive data type


String stringValue = "Welcome to Java";

// Displaying values
[Link]("----- Java Data Types Example -----");
[Link]("Byte Value : " + byteValue);
[Link]("Short Value : " + shortValue);
[Link]("Int Value : " + intValue);
[Link]("Long Value : " + longValue);

[Link]("Float Value : " + floatValue);


[Link]("Double Value : " + doubleValue);

[Link]("Char Value : " + charValue);


[Link]("Boolean Value : " + boolValue);

[Link]("String Value : " + stringValue);


}
}

Output:
Program2

Program: WAP in Java illustrating class, objects and methods.


Code:
// Define a class
class Student {

// Data members (variables)


int rollNo;
String name;

// Method to assign values


void setData(int r, String n) {
rollNo = r;
name = n;
}

// Method to display values


void display() {
[Link]("Roll Number: " + rollNo);
[Link]("Name: " + name);
}
}

// Main class
public class Main {
public static void main(String[] args) {

// Creating objects of Student class


Student s1 = new Student();
Student s2 = new Student();

// Calling methods using objects


[Link](101, "Devansh");
[Link](102, "Rahul");

[Link]("Student 1 Details:");
[Link]();

[Link]("\nStudent 2 Details:");
[Link]();
}
}

Output:
Program3

Program: WAP in Java to manipulate strings.


Code:
import [Link];

public class StringManipulation13 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String s = [Link]();

[Link]("1. Length: " + [Link]());

[Link]("2. Uppercase: " + [Link]());

[Link]("3. Lowercase: " + [Link]());

[Link]("4. Trimmed String: " + [Link]());

[Link]("Enter index: ");


int index = [Link]();
[Link]("5. Character at index: " + [Link](index));
[Link]();

[Link]("Enter string to concatenate: ");


String s2 = [Link]();
[Link]("6. Concatenated String: " + [Link](s2));

[Link]("Enter string to compare: ");


String s3 = [Link]();
[Link]("7. Equals: " + [Link](s3));

[Link]("8. Equals Ignore Case: " + [Link](s3));

[Link]("Enter start index: ");


int start = [Link]();
[Link]("Enter end index: ");
int end = [Link]();
[Link]("9. Substring: " + [Link](start, end));
[Link]();

[Link]("Enter old character: ");


char oldChar = [Link]().charAt(0);
[Link]("Enter new character: ");
char newChar = [Link]().charAt(0);
[Link]("10. Replaced String: " + [Link](oldChar, newChar));
[Link]();

[Link]("Enter word to check: ");


String word = [Link]();
[Link]("11. Contains word: " + [Link](word));

[Link]("Enter prefix: ");


String pre = [Link]();
[Link]("12. Starts with prefix: " + [Link](pre));
String rev = new StringBuilder(s).reverse().toString();
[Link]("13. Reversed String: " + rev);

[Link]();
}
}

Output:
Program4

Program: WAP in Java for addition and multiplication of Matrices.


Code:
import [Link];

public class MatrixOperations {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Input rows and columns


[Link]("Enter number of rows: ");
int r = [Link]();

[Link]("Enter number of columns: ");


int c = [Link]();

int A[][] = new int[r][c];


int B[][] = new int[r][c];
int sum[][] = new int[r][c];
int mul[][] = new int[r][c];

// Input Matrix A
[Link]("Enter elements of Matrix A:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
A[i][j] = [Link]();
}
}

// Input Matrix B
[Link]("Enter elements of Matrix B:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
B[i][j] = [Link]();
}
}

// Matrix Addition
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}

[Link]("\nMatrix Addition Result:");

for (int i = 0; i < r; i++) {


for (int j = 0; j < c; j++) {
[Link](sum[i][j] + " ");
}
[Link]();
}

// Matrix Multiplication
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
mul[i][j] = 0;
for (int k = 0; k < c; k++) {
mul[i][j] = mul[i][j] + A[i][k] * B[k][j];
}
}
}

[Link]("\nMatrix Multiplication Result:");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](mul[i][j] + " ");
}
[Link]();
}

[Link]();
}
}

Output:

You might also like