You are on page 1of 11

NAME : Yatinder Singh Rawat

ROLL NO. - 07211402020

JAVA
PROGRAMMING

EXTERNAL PRACTICAL
BPIBS

NAME: YATINDER SINGH


RAWAT
ROLL NO. – 07211402020
DATE - 11/06/2022
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Program -1

Aim: Write a program to find the number of arguments provided at


command line.
Code:
import java.util.Scanner;
class Practical_4
{
public static void main(String args[])
{
System.out.println("Number of arguments provided: " +
args.length);
}
}

Output:
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Program -2

Aim: Write a program to find sum of elements of a one


dimensional array entered dynamically by the user.
Code:
import java.util.Scanner;
class Practical_7
{
public static void main(String args[])
{
array obj = new array();
obj.getArray();
obj.getValues();
obj.showValues();
obj.add();
}
}
class array
{
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

private int arr[];


Scanner input = new Scanner(System.in);
public void getArray()
{
System.out.println("Enter the number of elements you
want in array: ");
int N = input.nextInt();
arr = new int[N];
}
public void getValues()
{
for(int i = 0; i<arr.length;i++)
{
System.out.print("Value for " + (i+1) + " element: ");
arr[i] = input.nextInt();
}
}
public void showValues()
{
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();

}
public void add()
{
int addition = 0;
for(int i = 0;i<arr.length;i++)
{
addition += arr[i];
}
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

System.out.println("Addition = "+addition);

}
}

Output:
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Program-3

Aim: Write a program “DivideByZero” that takes two


numbers a and b as input, computes a/b, and invokes
Arithmetic Exception to generate a message when the
denominator is zero.
Code:

import java.util.Scanner;
public class ques
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a,b;
int result;
System.out.println("Enter value of a");
a=sc.nextInt();
System.out.println("Enter value of b");
b=sc.nextInt();
try{
result=a/b;
System.out.println("Result ="+result);
}
catch(ArithmeticException e){
e.printStackTrace();
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

// System.out.println(e);
System.out.println("Denominator is zero ");
}
sc.close();
}
}

Output:
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Program-4

Aim: A string is defined as String Vowels=”AEIOU”. Write


a program to
a) Display the characters with the blank space after
each character.
b) Display the String in Lower case.
c) Display the string in reverse order.
Code:
import java.lang.String;
import java.lang.StringBuffer;

class AEIOU {
String str = new String("AEIOU");

StringBuffer str2 = new StringBuffer(str);

void replace() {
System.out.print("Blank SpaceAfter Every Character :");
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i));

System.out.printf(" ");
}
}
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

void LowerCase() {

System.out.println("LowerCase :" + str.toLowerCase());


}

void reverse() {
System.out.println("Reverse :" + str2.reverse());
}
}
public class Ques_20 {
public static void main(String[] args) {

AEIOU obj = new AEIOU();


obj.replace();
System.out.println();
obj.LowerCase();
obj.reverse();

}
}
Output:

Program-5
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Aim: Write a program to add two times using class and


objects. Initlalize the two objects using default and
parameterised constructor.
Code:
class time{
int hour,minute,second;
time(){}
time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void showtime()
{
System.out.println("Hour: "+hour +" " + "Minute: "+minute+ " " +"Second:
"+second);
}
void addtime(time obj1, time obj2){
second=obj1.second+obj2.second;
minute=second/60;
second=second%60;
minute=minute+obj1.minute+obj2.minute;
hour=minute/60;
minute=minute%60;
hour=+hour+obj1.hour+obj2.hour;
}
}
public class Ques_12 {
public static void main(String[] args) {
time obj1=new time(10,53,45);
obj1.showtime();
time obj2=new time(28,63,56);
obj2.showtime();
time obj3=new time();
System.out.println("After Addition: ");
obj3.addtime(obj1,obj2);
obj3.showtime();
}
}
NAME : Yatinder Singh Rawat
ROLL NO. - 07211402020

Output:

You might also like