Dr. D. Y.
Patil Unitech Society’s
Dr. D.Y. PATIL INSTITUTE OF MANAGEMENT & RESEARCH,
Sant Tukaram Nagar, Pimpri, Pune-411018, Maharashtra, India.
(Approved by All India Council for Technical Education & Recognized by the
Savitribai Phule Pune University)
ACADEMIC YEAR 2022-2023
MCA : I
SEMESTER: I
SUBJECT: JAVA LAB
ASSIGNMENT –I
Submitted by:
Titiksha Shukla
1
Table of Contents
1. Find the sum of the digits of a number 4
2. Calculate Electricity bill (According to units & rate slab ) 4-5
3. Print given number in words. (E.g. 123 = “One thousand Two hundred 5-6
Three”)
4. Write a program to print palindrome number up to N numbers. 6
5. Program to print following patterns of stars. 7-8
(i)
*
**
***
****
*****
(ii)
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
6. Program to print following patterns of Numbers. 9
1
121
12321
1234321
123454321
7. Print Floyds Triangle. 9-10
1
23
456
7 8 9 10
11 12 13 14 15
2
8. WAP to find second largest number in an array. 10
9. WAP to remove duplicate element in an array 10-11
10 WAP to check given matrix is null matrix. 11-12
.
11 WAP to check given matrix is diagonal matrix. 12-13
.
12 WAP to split a comma – separated string 13
.
13 WAP to design a class account using the inheritance and static that show all 13-14
. function of bank (withdrawal, deposit) and generate account no. dynamically.
14 WAP to design a class Shape (Implement run time polymorphism) using abstract 14-15
. methods & classes.
15 WAP to design a String Class that perform string method (Equal, Reverse the 15-16
. string, Change Case, etc).
3
Question-1 Find the sum of the digits of a number.
INPUT:
import java.util.Scanner;
public class SumOfDigits
public static void main(String args[])
{
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
number = sc.nextInt();
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}
OUTPUT:
Question-2 Calculate Electricity bill (According to units & rate slab ).
INPUT:
import java.util.*;
class ComputeElectricityBill
{
public static void main(String args[])
{
long units;
Scanner sc=new Scanner(System.in);
System.out.println("enter number of units");
units=sc.nextLong();
double billpay=0;
if(units<100)
billpay=units*1.20;
else if(units<300)
billpay=100*1.20+(units-100)*2;
else if(units>300)
4
billpay=100*1.20+200 *2+(units-300)*3;
System.out.println("Bill to pay : " + billpay);
}
}
OUTPUT:
Question-3 Print given number in words. (E.g. 123 = “One thousand Two hundred Three”).
INPUT:
import java.util.Scanner;
public class NumberToWords
{
public void pw(int n, String ch)
{
String one[] = { " ", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", "
Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", "Fifteen", " Sixteen", " Seventeen", " Eighteen", "
Nineteen" };
String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "Seventy", " Eighty",
" Ninety" };
if (n > 19)
{
System.out.print(ten[n / 10] + " " + one[n % 10]);
}
else
{
System.out.print(one[n]);
}
if (n > 0)
System.out.print(ch);
}
public static void main(String[] args)
{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer number: ");
n = sc.nextInt();
if (n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWords nw = new NumberToWords();
nw.pw((n / 1000000000), " Hundred");
nw.pw((n / 10000000) % 100, " crore");
nw.pw(((n / 100000) % 100), " lakh");
nw.pw(((n / 1000) % 100), " thousand");
nw.pw(((n / 100) % 10), " hundred");
nw.pw((n % 100), " ");
5
}
}
}
OUTPUT:
Question-4 Write a program to print palindrome number up to N numbers.
INPUT:
import java.util.Scanner;
public class palindrone {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the range = ");
int n1=sc.nextInt();
int n2=sc.nextInt();
for(int i=n1; i<n2;i++)
{
int n=i; int rev=0;
while(n>0)
{
int digit=n%10;
rev= (rev*10)+digit;
n=n/10;
}
if(rev==i)
{
System.out.print(i+ " ");
}
}
}
}
OUTPUT:
6
Question-5 Program to print following patterns of stars.
(i)
*
**
***
****
*****
INPUT:
public class program5i{
public static void main(String args[]){
int n,i,j;
n = 5;
for(i = 0; i<= n; i++)
{
for(j = 0; j<= i; j++)
{
System.out.print("*"+" ");
}
System.out.println(" ");
}
}
}
OUTPUT:
(ii)
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
7
INPUT:
import java.util.Scanner;
public class program5ii
{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j = i; j <= rows; j++)
{
System.out.print("*");
}
for (int k = 1; k <= i*2-2; k++)
{
System.out.print(" ");
}
for (int l = i; l <= rows; l++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
for (int k = i*2-2; k < rows*2-2; k++)
{
System.out.print(" ");
}
for (int l = 1; l <= i; l++)
{
System.out.print("*");
}
System.out.println();
}
}
}
OUTPUT:
8
Question-6 Program to print following patterns of Numbers.
1
121
12321
1234321
123454321
INPUT:
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
int rows = 5;
System.out.println("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
for (int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT:
Question-7 Print Floyds Triangle.
1
23
456
7 8 9 10
11 12 13 14 15
INPUT:
import java.util.Scanner;
public class program7
{
public static void main(String args[]){
int i,j,k = 1;
System.out.println("Printing Floyids Triangle: ");
for(i = 1; i <= 5; i++)
9
{
for(j=1;j <= i; j++)
{
System.out.print(" "+k++);
}
System.out.println();
}
}
}
OUTPUT:
Question-8 WAP to find second largest number in an array.
INPUT:
import java.util.Arrays;
public class LargestNumberSample {
public static void main(String args[]){
int array[] = {10, 20, 25, 63, 96, 57};
int size = array.length;
Arrays.sort(array);
System.out.println("sorted Array ::"+Arrays.toString(array));
int res = array[size-2];
System.out.println("2nd largest element is ::"+res);
}
}
OUTPUT:
Question-9 WAP to remove duplicate element in an array.
INPUT:
import java.util.Arrays;
public class program9 {
public static int[] removeDuplicateElements(int arr[]){
int n = arr.length;
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
10
}
temp[j++] = arr[n-1];
return temp;
public static void main (String[] args) {
int arr[] = {10,70,30,90,20,20,30,40,70,50};
//sort the array
Arrays.sort(arr);
int[] result = removeDuplicateElements(arr);
//printing array elements
for (int i=0; i<result.length; i++) {
if(result[i] != 0){
System.out.print(result[i]+" ");
OUTPUT:
Question-10 WAP to check given matrix is null matrix.
INPUT:
public class program10
{
public static void main(String args[])
{
int[][] a = new int[][] { { 0, 0, 0},{ 0, 0, 0},{ 0, 0, 0} };
boolean setValue = true;
abc: for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
if(a[i][j] != 0)
{
setValue = false;
break abc;
}
11
}
}
System.out.println("The Given Matrix Value:");
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
if(setValue == true)
{
System.out.println("The Given Matrix is a Null Matrix");
}
else
{
System.out.println("The Given Matrix is not a Null Matrix");
}
}
}
OUTPUT:
Question-11 WAP to check given matrix is diagonal matrix.
INPUT:
public class DiagonalMatrix
{
public static void main(String args[])
{
int[][] a = new int[][] { { 1, 0, 0},{ 0, 3, 0},{ 0, 0, 3} };
boolean setValue = true;
abc: for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
if(i == j)
{
if(a[i][j] == 0)
{
setValue = false;
break abc;
}
}
else if(a[i][j] != 0)
{
setValue = false;
break abc;
}
}
}
System.out.println("The Given Matrix Value:");
for(int i = 0;i < a.length;i++)
{
12
for(int j = 0;j < a[i].length;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
if(setValue == true)
{
System.out.println("The Given Matrix is a Diagonal Matrix");
}
else
{
System.out.println("The Given Matrix is not a Diagonal Matrix");
}
}
}
OUTPUT:
Question-12 WAP to split a comma – separated string.
INPUT:
import java.util.Scanner;
public class split {
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
String str;
System.out.println("Enter the String with comma :");
str=sc.nextLine();
String split[] = str.split(",");
for(String n:split) System.out.println(n);
}
}
OUTPUT:
Question-13 WAP to design a class account using the inheritance and static that show all function of
bank (withdrawal, deposit) and generate account no. dynamically.
INPUT:
public class Bank {
static class Account {
private static int AccountNumber = 1;
private int accountNumber=1;
private int balance;
public Account() {
this.accountNumber=AccountNumber++;
this.balance = 0;
13
}
public void deposit(int amt) {
if (amt > 0) { balance=balance+ amt;
}
}
public void withdraw(int amt) {
if (amt > 0 && amt <= balance) {
balance= balance- amt;
}
}
public int getBalance() {
return balance;
}
public int getAccountNumber() {
return accountNumber;
}
}
public static void main(String[] args) {
Account acc1 = new Account();
Account acc2 = new Account();
acc1.deposit(100);
acc2.deposit(100);
System.out.println("Account 1 balance: " + acc1.getBalance());
System.out.println("Account 2 balance: " + acc2.getBalance());
acc1.withdraw(50);
acc2.withdraw(50);
System.out.println("Account 1 balance after withdrawing 50 : " + acc1.getBalance());
System.out.println("Account 2 balance after withdrawing 50: " + acc2.getBalance());
System.out.println("Account 1 number: " + acc1.getAccountNumber());
System.out.println("Account 2 number: " + acc2.getAccountNumber());
OUTPUT:
Question-14 WAP to design a class Shape (Implement run time polymorphism) using abstract methods
& classes.
INPUT:
public class program14
{
abstract class Shape
{
public abstract double getArea();
}
class Circle extends Shape
{
14
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
@Override
public double getArea()
{
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape
{
private double width;
private double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
@Override
public double getArea()
{
return width * height;
}
}
public static void main(String[] args)
{
program14 main = new program14();
Shape circle = main.new Circle(2.0);
Shape rectangle = main.new Rectangle(3.0, 4.0);
System.out.println("Circle area: " + circle.getArea());
System.out.println("Rectangle area: " + rectangle.getArea());
}
}
OUTPUT:
Question-15 WAP to design a String Class that perform string method (Equal, Reverse the string,
Change Case, etc).
INPUT:
public class program15
{
class main
{
public static boolean equalsIgnoreCase(String str1,String str2)
{
return str1.equalsIgnoreCase(str2);
}
public static String reverse(String str)
{
StringBuilder sb= new StringBuilder(str);
return sb.reverse().toString();
}
public static String toUppercase(String str)
{
return str.toUpperCase();
}
public static String toLowercase(String str)
{
15
return str.toLowerCase();
}
}
public static void main(String[] args)
{
String str1 , str2,str3;
str1="Hello";
str2= "World";
str3="java";
System.out.println(main.equalsIgnoreCase(str1, str2));
System.out.println(main.equalsIgnoreCase(str1, str3));
System.out.println(main.reverse(str1+" \n" +str2+ "\n"+str3));
System.out.println(main.toLowercase(str1+" \n" +str2+" \n"+str3));
System.out.println(main.toUppercase(str1+" \n" +str2+" \n"+str3));
}
}
OUTPUT:
16