Bsc Java Practical Programs (1)
Bsc Java Practical Programs (1)
Program 1: Write a Java program that prompts the user for an integer and then prints out all
the prime numbers up to that Integer?
Algorithm
class prime
{
static void prime_N(int N)
{
// Declaring the variables
int x, y, flg;
// Printing display message
System.out.println(
"All the Prime numbers within 1 and " + N
+ " are:");
for (x = 1; x <= N; x++)
{
if (x == 1 || x == 0)
continue;
flg = 1;
OUTPUT:
Algorithm:
import java.util.Scanner;
// Matrix multiplication is possible only if columns of first matrix equals rows of second
matrix
if (n1 != m2) {
System.out.println("Matrix multiplication is not possible. Number of columns of first
matrix must equal number of rows of second matrix.");
return;
}
// Declare matrices
int[][] matrix1 = new int[m1][n1];
int[][] matrix2 = new int[m2][n2];
int[][] result = new int[m1][n2];
// Matrix multiplication
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
result[i][j] = 0;
for (int k = 0; k < n1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
scanner.close();
}
}
OUTPUT:
Algorithm
import java.util.Scanner;
if (line.isEmpty()) {
break; // End the input when a blank line is encountered
}
inputText.append(line).append("\n");
}
// Count the number of words (split by spaces and other word delimiters)
String[] words = text.split("\\s+");
wordCount = words.length;
OUTPUT :
Algorithm:
Step 1. Create a Random object to generate random numbers.
Step 2. Initialize an integer array `k` of size 5.
Step 3. For i = 0 to 4:
- Generate a random integer between 0 and 9.
- Store the generated number in the `k[i]` array.
Step 4. For i = 0 to 4:
For j = 0 to 4:
- If k[i] > k[j], then swap k[i] and k[j] using a temporary variable `t`.
Step 5. Print the sorted array `k` (ascending order).
Program:
import java.io.*;
import java.util.Random;
public class Rand1
{
public static void main(String s[])
{
Random r=new Random ();
int k[ ]=new int[5];
int i,j,t;
for(i=0;i<5;i++)
{
k[i]=r.nextInt(10);
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(k[i]>k[j])
{
t=k[i];
k[i]=k[j];
k[j]=t;
}
}
System.out.println("Random numbers in ascending order");
for(i=0;i<5;i++)
{
System.out.println(k[i]);
}
}
}
OUTPUT:
0
2
5
6
9
(The o/p will vary everytime when we run the prg)
5. Write a program to do String Manipulation using Character Array and perform
the following string operations:
a. String length
b. Finding a character at a particular position
c. Concatenating two strings
Algorithm:
Step 1:
Input Strings:
The program first takes two strings as input from the user using a Scanner object.
It converts the strings into character arrays using toCharArray().
Step 2:
String Length:
The length of the strings is calculated using the length property of the character array.
Step 3:
Character at a Particular Position:
The program prompts the user to enter a position (index) and then checks if the position is
valid.
If the position is valid, it retrieves the character at that position from the character array.
Step 4:
Concatenate Strings:
The two input strings are concatenated using the + operator and displayed.
import java.util.Scanner;
OUTPUT:
a. Single inheritance
Algorithm:
Program:
class Employee
{
float salary=34534*12;
}
public class Executive extends Employee
{
float bonus=3000*6;
public static void main(String args[])
{
Executive obj=new Executive();
System.out.println("Total salary credited: "+obj.salary);
System.out.println("Bonus of six months: "+obj.bonus);
}
}
Output:
Algorithm:
Program:
class Student
{
int reg_no;
void getNo(int no)
{
reg_no=no;
}
void putNo()
{
System.out.println("registration number= "+reg_no);
}
}
//intermediate sub class
class Marks extends Student
{
float marks;
void getMarks(float m)
{
marks=m;
}
void putMarks()
{
System.out.println("marks= "+marks);
}
}
//derived class
class Sports extends Marks
{
float score;
void getScore(float scr)
{
score=scr;
}
void putScore()
{
System.out.println("score= "+score);
}
}
public class MultilevelInheritanceExample
{
public static void main(String args[])
{
Sports ob=new Sports();
ob.getNo(0987);
ob.putNo();
ob.getMarks(78);
ob.putMarks();
ob.getScore(68.7);
ob.putScore();
}
OUTPUT:
a. Method Overloading
Algorithm:
Program:
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
OUTPUT:
No parameters
a: 10
a and b: 10 20
Result of ob.test(123.25): 123.25
b. Method Overriding
Algorithm:
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
// display i and j
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
B(int a, int b, int c)
{
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
OUTPUT:
k: 3
8. Write a program to create a package
Algorithm:
Program:
package MyPack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n;
bal = b;
}
void show()
{
if(bal<0)
System.out.print("NIL");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
OUTPUT:
K.J.Fielding $123.23
Will Tell $157.02
Tom Jackson NIL
9. Write a program to implement Inter thread communication
Algorithm:
Program:
class Customer
{
int amount=10000;
synchronized void withdraw(int amount)
{
System.out.println(“going to withdraw...”);
if(this.amount<amount)
{
System.out.println(“Less balance; waiting for deposit...”);
try
{
wait();
}
catch(Exception e)
{
}
}
this.amount-=amount;
System.out.println(“withdraw completed...”);
}
synchronized void deposit(int amount)
{
System.out.println(“going to deposit...”);
this.amount+=amount;
System.out.println(“deposit completed... “);
notify();
}
}
public class Test
{
public static void main(String args[])
{
final Customer c=new Customer();
new Thread()
{
public void run()
{
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run()
{
c.deposit(10000);
}
}.start();
}
}
}
OUTPUT:
going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed