[go: up one dir, main page]

0% found this document useful (0 votes)
182 views14 pages

Practical File CJP Harshil

This document is the lab manual for the subject Core Java Programming (CE0421) at Indus University. It contains 4 practical sessions with objectives and sample code to achieve each objective. The practical sessions cover topics like printing output, checking prime numbers, finding the greatest among 3 numbers, printing Fibonacci series, finding average of numbers in an array, replacing and sorting substrings. Each code example includes the enrollment number IU2141230170 and expected output.
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)
182 views14 pages

Practical File CJP Harshil

This document is the lab manual for the subject Core Java Programming (CE0421) at Indus University. It contains 4 practical sessions with objectives and sample code to achieve each objective. The practical sessions cover topics like printing output, checking prime numbers, finding the greatest among 3 numbers, printing Fibonacci series, finding average of numbers in an array, replacing and sorting substrings. Each code example includes the enrollment number IU2141230170 and expected output.
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/ 14

Core Java Programming IU2141230170

INDUS UNIVERSITY
Indus Institute of Technology and Engineering

Lab manual of
Core Java Programming
(CE0421)

Computer Science & Engineering 1


Core Java Programming IU2141230170

Indus Institute of Technology and Engineering


Branch: Computer Science & Engineering
Semester: IV
Subject: Core Java Programming (CE0421)

INDEX
Sr. Aim Page Date Sign
No No.

1 1.1 Write a program to display “Welcome to Java World”. 3

1.2 Write a program to find whether the number is prime or not. 4

1.3 Write a program to find a greater number among given 5


three numbers using
a) ternary operator
b) nested if.

1.4 Write a program to print the Fibonacci series. 7

2 2.1 Write a program to find the average of n numbers stored in an 8


Array.

3 3.1 WAP to replace substring with other substring in the given 9


string.

3.2 WAP that to sort given strings into alphabetical order. 10

3.3 Create a String Buffer with some default string. Append any 11
string to ith position of original string and display the
modified string. Also display the reverse of modified string.

4 4.1.1 WAP that declares a class named Person. It should have 12


instance variables to record name, age and salary. Use new
operator to create a Person object. Set and display its instance
variables.

4.1.2 Add a constructor to the Person class developed above. 13

Computer Science & Engineering 2


Core Java Programming IU2141230170
Practical-1
1.1 Write a program to display “Welcome to Java World”.

File Name: demo.java


class demo
{
public static void main(String[] args)
{
System.out.println("Enrollment No: IU2141230170");
System.out.println("Hello, World!");
}
}

Output:

Computer Science & Engineering 3


Core Java Programming IU2141230170
Practical-1
1.2 Write a program to find whether the number is prime or not.

File Name: prac1_2.java


import java.util.*;
public class prac1_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");

int a, cnt = 0;
System.out.print("Enter the Number for which you want to check prime or not: ");
a = sc.nextInt();

for (int i = 2; i < a / 2; ++i) {


if (a % i == 0) {
cnt++;
}
}

if (cnt == 0) {
System.out.println(a + " is a prime!");
} else {
System.out.println(a + " is not a prime!");
}
}
}
Output:

Computer Science & Engineering 4


Core Java Programming IU2141230170

Practical-1
1.3 Write a program to find a greater number among given three numbers using
a) ternary operator
b) nested if.

File Name: prac1_3.java

a)
public class prac1_3 {
public static void main(String[] args)
{

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");

int a = 40, b = 50, c = 25, d;


d = ((a > b && a > c) ? a : ((b > a && b > c) ? b : c));

System.out.println(d + " is greater");


}
}
Output:

Computer Science & Engineering 5


Core Java Programming IU2141230170
b)
File Name: prac1_3_.java

public class prac1_3_ {


public static void main(String[] args) {
int a = 10, b = 20, c = 30;

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");

if (a > b) {
if (a > b)
System.out.println(a + " is greater");
else if (a < c)
System.out.println(c + " is greater");

} else if (b > a) {
if (b > c)
System.out.println(b + " is greater");
else if (c > b)
System.out.println(c + " is greater");
}
}
}
Output:

Computer Science & Engineering 6


Core Java Programming IU2141230170
Practical-1
1.4 Write a program to print the Fibonacci series.

File Name: prac1_4.java

import java.util.Scanner;

public class prac1_4 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num, a = 0, b = 1, c;

System.out.println("Enrollment No: IU2141230170");

System.out.println("---------------------------");

System.out.print("Enter how many elements you want to print:");

num = sc.nextInt();

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

System.out.print(a + " ");

c = a + b;

a = b;

b = c;

} } }
Output:

Computer Science & Engineering 7


Core Java Programming IU2141230170
Practical-2
2.1 Write a program to find the average of n numbers stored in an Array.

File Name: prac2.java


import java.util.Scanner;

public class prac2 {


public static void main(String[] args) {
int size;
float sum = 0;

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of ARRAY: ");
size = sc.nextInt();
int arr[] = new int[size];

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


System.out.print("Enter element " + i + " : ");

arr[i] = sc.nextInt();
sum += arr[i];
}
System.out.println("AVERAGE = " + (sum / size));
} }

Output:

Computer Science & Engineering 8


Core Java Programming IU2141230170

Practical-3
3.1 WAP to replace substring with other substring in the given string.

File Name: prac3_1.java


import java.util.*;

public class prac3_1 {


public static void main(String[] args) {
System.out.println("Enrollment No: IU2141230170");
System.out.println("---------------------------");

Scanner sc = new Scanner(System.in);


System.out.print("Enter The String: ");
String a, b, c;
a = sc.nextLine();
System.out.print("Enter substring you want to replace : ");
b = sc.nextLine();
System.out.print("Enter substring you want to enter : ");
c = sc.nextLine();

a = a.replace(b, c);

System.out.println("new string is: " + a);


}
}

Output:

Computer Science & Engineering 9


Core Java Programming IU2141230170
Practical-3
3.2 WAP that to sort given strings into alphabetical order.

File Name: prac3_2.java


import java.util.Arrays;
import java.util.Scanner;
public class prac3_2 {
public static void main(String[] args) {
System.out.println("Enrollment No: IU2141230170");
System.out.println("---------------------------");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String line = sc.nextLine();
char[] arr = line.toCharArray();
char temp;

for (int i = 0; i < arr.length - 1; ++i) {


for (int j = 0; j < arr.length - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; }
}}
System.out.print("Sorted String is: ");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]);
} }

Output:

Computer Science & Engineering 10


Core Java Programming IU2141230170

Practical-3

3.3 Create a String Buffer with some default string. Append any string to ith position of original
string and display the modified string. Also display the reverse of modified string.

File Name: prac3_3.java


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

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");
StringBuffer s = new StringBuffer("Harshil ");

System.out.println("current string is: " + s);


System.out.println("String after inserting new sub string : " + s.insert(7, " Patel"));
System.out.println("Reversed String : " + s.reverse());
}
}

Output:

Computer Science & Engineering 11


Core Java Programming IU2141230170
Practical-4
4.1 WAP that declares a class named Person. It should have instance variables to record name,
age and salary. Use new operator to create a Person object. Set and display its instance variables.
File Name: prac4_1.java
import java.util.Scanner;

class person {
int age, salary;
String name;
}

public class prac4_1 {


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

System.out.println("Enrollment No: IU2141230170");


System.out.println("---------------------------");
System.out.print("Enter name: ");
p.name = s.nextLine();
System.out.print("Enter age: ");
p.age = s.nextInt();
System.out.print("Enter salary: ");
p.salary = s.nextInt();
System.out.println("\n\nPerson name:" + p.name);
System.out.println("Person age:" + p.age);
System.out.println("Person salary:" + p.salary);
} }

Output:

Computer Science & Engineering 12


Core Java Programming IU2141230170

Practical-4
4.1.2 Add a constructor to the Person class developed above.
File Name: prac4_1_.java
import java.util.Scanner;
class person {
Scanner s = new Scanner(System.in);
int age, salary;
String name;
person() {
System.out.print("Enter name: ");
name = s.nextLine();
System.out.print("Enter age: ");
age = s.nextInt();
System.out.print("Enter salary: ");
salary = s.nextInt();
}
}

public class prac4_1_ {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enrollment No: IU2141230170");
System.out.println("---------------------------");
person p = new person();

System.out.println("\n\nPerson name:" + p.name);


System.out.println("Person age:" + p.age);
System.out.println("Person salary:" + p.salary);
}
}

Computer Science & Engineering 13


Core Java Programming IU2141230170

Output:

Computer Science & Engineering 14

You might also like