Assign Java Titli
Assign Java Titli
Submitted by:
Titiksha Shukla
1
Table of Contents
3. Print given number in words. (E.g. 123 = “One thousand Two hundred 5-6
Three”)
(i)
*
**
***
****
*****
(ii)
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
6. Program to print following patterns of Numbers. 9
1
121
12321
1234321
123454321
1
23
456
7 8 9 10
11 12 13 14 15
2
8. WAP to find second largest number in an array. 10
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;
OUTPUT:
INPUT:
import java.util.*;
class ComputeElectricityBill
{
public static void main(String args[])
{
long 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;
OUTPUT:
Question-3 Print given number in words. (E.g. 123 = “One thousand Two hundred Three”).
INPUT:
import java.util.Scanner;
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);
}
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:
INPUT:
import java.util.Scanner;
public class palindrone {
public static void main(String[] args) {
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;
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:
1
23
456
7 8 9 10
11 12 13 14 15
INPUT:
import java.util.Scanner;
9
{
for(j=1;j <= i; j++)
{
System.out.print(" "+k++);
}
System.out.println();
}
}
}
OUTPUT:
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:
INPUT:
import java.util.Arrays;
int n = arr.length;
int j = 0;
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
10
}
temp[j++] = arr[n-1];
return temp;
Arrays.sort(arr);
if(result[i] != 0){
System.out.print(result[i]+" ");
OUTPUT:
INPUT:
11
}
}
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:
INPUT:
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:
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 {
public Account() {
this.accountNumber=AccountNumber++;
this.balance = 0;
13
}
OUTPUT:
Question-14 WAP to design a class Shape (Implement run time polymorphism) using abstract methods
& classes.
INPUT:
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));
}
}
OUTPUT:
16