[Go to site: main page, start]

0% found this document useful (0 votes)
9 views7 pages

JavaLab New

The document contains three Java programs demonstrating various programming concepts. The first program showcases control statements including if-else, switch, while, do-while, and for loops. The second program implements an array of objects for managing student data, while the third program illustrates string manipulation and functions in Java.
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)
9 views7 pages

JavaLab New

The document contains three Java programs demonstrating various programming concepts. The first program showcases control statements including if-else, switch, while, do-while, and for loops. The second program implements an array of objects for managing student data, while the third program illustrates string manipulation and functions in Java.
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. Write a Java Program to demonstrate control statements.

import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);

// if-else
int n;
[Link]("Enter any number:");
n=[Link]();
if(n%2==0)
[Link]("Given number is even Number");
else
[Link]("Given number is Odd number");

//Switch statement
[Link]("Arithematic operations Using swith case");
[Link]("Enter first number: ");
int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

[Link]("Choose an operation:");
[Link]("1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");

[Link]("Enter your choice: ");


int ch = [Link]();
switch (ch) {
case 1:
[Link]("Addition = " + (a + b));
break;

case 2:
[Link]("Subtraction = " + (a - b));
break;

case 3:
[Link]("Multiplication = " + (a * b));
break;

case 4:
if (b != 0) {
[Link]("Division = " + (a / b));
} else {
[Link]("Cannot divide by zero");
}
break;

default:
[Link]("Invalid choice");
}

// While loop
int i = 2;
[Link]("Even numbers series are using while loop:");
while (i <= 10)
{
[Link](i + " ");
i = i + 2;
}
[Link]();
//do-while loop
int j = 1;
[Link]("Odd numbers series are using do-while:");
do
{
[Link](j + " ");
j = j + 2;
}while (j <= 10);

[Link]();

//for loop
[Link]("Printing number series using for loop:");
for(int k=10;k<=15;k++)
{
[Link](k+" ");
}
[Link]();
// break and continue
[Link]("Break staement demo");
for(int p=10;p<=15;p++)
{
if(p==12)
continue;

if(p==14)
break;

[Link](p+" ");
}

}
}
2. Write a Java Program to implement array of objects.
import [Link];

class Student {
int id;
String name;

void read() {
Scanner sc = new Scanner([Link]);
[Link]("Enter student id: ");
id = [Link]();

[Link]("Enter student name: ");


name = [Link]();
}

void display() {
[Link]("ID: " + id + ", Name: " + name);
}
}

public class Main {


public static void main(String[] args) {

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


int n = [Link]();

Student[] s = new Student[n]; // array of objects

// create and read objects


for (int i = 0; i < n; i++) {
s[i] = new Student(); // object creation
s[i].read();
}
[Link]("\nStudent Details:");
// display objects
for (int i = 0; i < n; i++) {
s[i].display();
}

[Link]();
}
}

3. Write a Java Program to demonstrate String and String function.


import [Link];
public class Main {
public static void main(String[] args)
{
String str = "apple";
str = [Link]();
// it will return new string with all upper cases
[Link](str);

String name = "Hari";


name = [Link]();
// it will return new string with all lower cases
[Link](name);
// length() method returns number of characters in the string
int strLength = [Link]();
[Link]("Length of string is:");
[Link](strLength);

//Using substring we can get part of the string based on indexes


[Link]("Printing string from specific index:" );
String s1 = "Java is fun";
String s2 = [Link](8);
[Link](s2);

//indexOf is used to get an index of a certain string or char

String s3 = "apples";
int index = [Link]("e");
[Link]("Index of given character is:");
[Link](index);

//replace(oldChar, newChar)
String names = "Hari-Prasad";
[Link]("Given String is:");
[Link](names);
String s4 = [Link]("-", " ");
[Link]("After replacing:");
[Link](s4);
}}

You might also like