ICSE Computer Project
ICSE Computer Project
ICSE Computer Project
Project
Programs:
1)Palindrome Strings
2)Kaprekar Numbers
3)Line in Lowercase
4)Word Anagrams
5)Name
6)Happy Number
7)String Permutations
8)Frequency of a Letter
9)Circular and Co-prime
10)Time Period
11)Square-free Number
12)Processing a Number
13)Duplicate elements in an array.
14)Amicable Numbers.
15)Customizing an Array.
16)Areas of Geometrical Figures
17)Student Subjective Makrs
18)Student Ranking Record
19)Separation of Digits
20)Patterns
Introduction
Source Code:
import java.util.*;
public class Hotel_Management
{ Scanner sc= new Scanner(System.in);
int days, room;
boolean Type[];
String date, name, gen, occp, add, pur, rname, email;
int adults, child, no;
double bill;
boolean breakfast;
//all the various variables are initialized.
public static void main()
{
Hotel_Management ob=new Hotel_Management();
ob.input();
ob.roomBook();
ob.registration();
ob.billCalc();
}
{
//This is simply done to make the WELCOME page more flashy, purely done because
of the ample amount of time I seem to have.
System.out.print(" ");
System.out.println(" # # ####### ####### ####### # ######
####### ###### ##### ####### # ###### ");
System.out.print(" "); System.out.println(" # # # # # # # #
## # # # # # # # # # ");
System.out.print(" "); System.out.println(" # # # # # # # #
## # # # # # # # #");
System.out.print(" "); System.out.println(" ####### # # # #####
# ###### ##### # # ##### # # # ######");
System.out.print(" "); System.out.println(" # # # # # # # #
# # # # # # ####### # # ");
System.out.print(" "); System.out.println(" # # # # # # # #
# # # # # # # # # # # ");
System.out.print(" "); System.out.println(" # # ####### #
####### ####### # # ####### ###### ##### # # # #
");
System.out.println();
//prints a design with HOTEL RED STAR written.
System.out.println(" ^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^ ");
System.out.println(" MARUNOUCHI TRUST TOWER MAIN, 1 Chome-8-3
Marunouchi, Chiyoda City, Tokyo 100-8283, Japan ");
System.out.println();
System.out.println("Dear customer,");
System.out.println("A warm welcome to our online booking site,for the greatest hotel
in Tokyo,");
System.out.println("built in the year 1967, dedicated to the rich cultural history of our
country ");
System.out.println("At Hotel Red Star, we bring to you absolut ely modern living in
utmost comfort, ");
System.out.println("amidst the fragrance of our glorious heritage.");
System.out.println();
System.out.println("Be you a native weekend tourist, or be you an international tourist
coming ");
System.out.println("to witness the wonders of Japan, be assured that we will take up
the ");
System.out.println("responsibity of making your stay a memorable one.");
System.out.println();
System.out.println("Do you want to book a room in our grand hotel?");
System.out.println("Please reply in Yes or No: ");
if((sc.nextLine()).equalsIgnoreCase("Yes"))
{
System.out.println();
System.out.println("Please state your date of visit, and the number of days you
intend to stay");
System.out.print("Date: ");
date=sc.nextLine();
System.out.print("Number of days: ");
days=sc.nextInt();
}
else
{
System.out.println("Thank you for visiting the website of Hotel Red Star, Tokyo!");
System.exit(0);
}
}
public void roomBook()
{
boolean King[]=new boolean[5];
boolean Queen[]= new boolean[5];
boolean AC[]= new boolean[15];
boolean Non[]= new boolean[15];
King=randomFiller(King);
Queen=randomFiller(Queen);
AC=randomFiller(AC);
Non=randomFiller(Non);
System.out.println("Please select the type of room you prefer. ");
System.out.println("1. King Suite");
System.out.println("2. Queen Suite");
System.out.println("3. AC Deluxe");
System.out.println("4. Non-AC Deluxe");
Hotel_Management object=new Hotel_Management() ;
System.out.println("Do you wish to obtain a description of the rooms?( Answer with a
Yes or a No)");
sc.nextLine();
String s=sc.nextLine();
//program statements do a good job at replacing comments, to make the user
understand the function of each part.
if(s.equalsIgnoreCase("yes"))
{
System.out.print("Which one do you wish to obtain?");
System.out.print("The number corresponding to the room whose description you
want to obtain: ");
int option=sc.nextInt();
switch(option)
{
case 1:
if(object.king()==true)
{
Type= King;
rname="King Suite";
}
else
{
roomBook();//reruns the method.
}
break;
case 2:
if(object.queen()==true)
{
Type= Queen;
rname="Queen Suite";
}
else
{
roomBook();
}
break;
case 3:
if(object.ac()==true)
{
Type= AC;
rname="AC Deluxe";
}
else
{
roomBook();
}
break;
case 4:
if(object.non()==true)
{
Type= Non;
rname="Non-AC Deluxe";
}
else
{
roomBook();
}
break;
default:
System.out.println("INPUT IS INVALID!!! PLEASE RE-ENTER YOUR CHOICE. ");
roomBook();
}
}
else if(s.equalsIgnoreCase("no"))
{
System.out.print("Enter your choice of the preferred room: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
Type= King;
rname="King Suite";
break;
case 2:
Type= Queen;
rname="Queen Suite";
break;
case 3:
Type= AC;
rname="AC Deluxe";
break;
case 4:
Type= Non;
rname="Non-AC Deluxe ";
break;
default:System.out.println("Input is invalid! Plese re-enter your choice. ");
roomBook();
}
}
else
{
System.out.println("Your input is not valid, please try again.");
roomBook();
}
System.out.println("Single bed or double bed? ");
sc.nextLine();
String bed=sc.nextLine();
System.out.println("Checking for room availibilty.........");
for(long i=0; i<1599999999; i++); //A loop to imitate searching .
System.out.println("DONE!");
int cn;
System.out.println("Rooms available= 5");
for(int i=0; i<Type.length; i++)
{
if(bed.equalsIgnoreCase("single")||bed.equalsIgnoreCase("single bed"))
{
if(Type[i]==true&&i<=(Type.length/2))
{
System.out.println("Room "+(i+1));
room=i;
}
}
else if(bed.equalsIgnoreCase("double")||bed.equalsIgnoreCase("double bed"))
{
if(Type[i]==true&&i>(Type.length/2))
{
System.out.println("Room "+(i+1));
room=i;
}
}
}
System.out.println("We have assigned room "+(room+1)+" for you ");
System.out.println("Let us now proceed to the registration");
}
void registration()
{
System.out.println();
System.out.println(" _________________________________________
__________________________________________________________
____________ _______________________");
System.out.println(" REGISTRATION "); System.out.println(" ''''''''''''''");
System.out.println(" _________________________________________
__________________________________________________________
____________ _______________________");
System.out.println(" ");
System.out.print("Name: ");
name=sc.nextLine();
System.out.print("Gender: ");
gen=sc.nextLine();
System.out.print("Residential Address: ");
add=sc.nextLine();
System.out.print("Occupation: ");
occp=sc.nextLine();
System.out.print("Purpose of visit: ");
pur=sc.nextLine();
System.out.print("Registered Email: ");
email=sc.nextLine();
System.out.print("Number of rooms booked: ");
no=sc.nextInt();
System.out.print("Total number of adults: ");
adults=sc.nextInt();
System.out.print("Total number of children(below 18): ");
child=sc.nextInt();
System.out.println("Do you wish to have complimentary breakfast (yes/no)? ");
//increases the cost, 'complimentary' was a lie.
sc.nextLine();
String bk=sc.nextLine();
System.out.println("Thank You for registering with Hotel Red Star,Tokyo! ");
breakfast=bk.equalsIgnoreCase("yes");
}
void display(int n)
{
System.out.println();
System.out.println(" __ ");
System.out.println(" |__) * | |");
System.out.println(" |__) | |__ |__");
System.out.println();
System.out.println(" HOTEL RED STAR ");
System.out.println(" ^^^^^^^^^^ ^^^^^^ ");
System.out.println();
System.out.println(" Room type: "+ rname);
System.out.println(" Room number= "+room);
System.out.println(" Customer Name: "+name);
System.out.println(" Total number of people= "+(adults+child));
System.out.println(" Date of check-in: "+date);
System.out.println(" Days stayed= "+days);
System.out.println();
System.out.println(" Gross amount= "+n);
double servtax=(5.0/100)*n;
double gst=(14.5/100)*(servtax+n);
bill=servtax+gst+n;
System.out.println(" Taxes= "+gst);
System.out.println(" Net amount= "+bill);
System.out.println();
System.out.print(" Please enter custom mode of payment: ");
String mode=sc.nextLine();
System.out.println(" Your booking has been confirmed!");
System.out.println(" An email regarding the details and payment credit
will be sent shortly to your registered email ID.");
System.out.println(" Thank You for choosing Hotel Red Star!");
}
// Will contain the descriptions of the rooms.
//When the user inputs his choice, a description will be available, which will be
accessible through this class.
public boolean king()
{
//the various descriptions of the rooms are given in this method.
boolean a=false;
System.out.println("KING SUITE");
System.out.println("");
System.out.println("The King Suite is for the very best of our guests. With world class
");
System.out.println("features, and excellent room décor, this suite is nothing short of ");
System.out.println("perfection, which is desired by many. The room is spacy, and the ");
System.out.println("cosiness of it makes one feel at home, albeit a more comfortable
and ");
System.out.println("more exquisite one. We assure you, that if this shall be your choice,
you ");
System.out.println("shall regret the day of your departure.");
System.out.println(""); System.out.println("Features");
System.out.println("^^^^^^^^^");
System.out.println("");
System.out.println("> 350x200 ft. room, large, spacy and comfortable");
System.out.println("> A gigantic Double-bed, with Spektron-technology custom made
");
System.out.println(" mattress, for a good nights sleep.");
System.out.println("> Air-conditioned, with two split A/Cs.");
System.out.println("> A 42 inch LG Smart TV");
System.out.println("> Two large sofas, a bedside table, and a coffee table");
System.out.println("> A long balcony, with a stunning view of the Tokyo Tower, as well
as the ");
System.out.println(" rest of Tokyo");
System.out.println("> A mini-bar, with an assortment of the finest elixirs of Japan");
System.out.println("> A huge bathroom, fit for a king.");
System.out.println("");
System.out.println("The cost for this room (exclusive of taxes ) is = Rs. 8000/day");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Do you want to choose this type?");
System.out.println("Enter 'Yes' to book, or 'No' to go back to the menu: ");
if(sc.nextLine().equalsIgnoreCase("yes"))
a=true;
System.out.println("");
return a;
}
Source Code:
import java.util.*;
class Palindrome
{
public boolean isPalindrome(int n)//this is to check if a number is palindrome
{
int rev=0;
int copy=n;
while(copy!=0)//reverses the number
{
rev=(rev+copy%10)*10;
copy/=10;
}
rev/=10;
if(rev==n)//checks if the reversed number is equal to the original.
return true;
return false;
}
Source Code:
import java.util.*;
class Kaprekar
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: " );
int n=sc.nextInt();
int sq=n*n; //takes the square of said number.
double copy=n;
int sum=0;
double count=0.0;
while(n>0)
{
n/=10;
count++;
}
double k= Math.pow(10.0,count);//done to divide the square to parts , with the second
part having the same number of digits as the original number.
int s=(int)k;
while(sq>0)//loop to calculate the sum of the two parts of the number.
{
sum=sum+(sq%s);
sq/=k;
}
VDT
First Case:
Second Case:
Q3) Write a program to input a line using uppercase letters, and
convert the letters into lowercase, except for the first letter of
each word.
Source Code:
import java.util.*;
class Lowercase_Line
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a line in uppercase letters: ");
String str=sc.nextLine(); //this is the original string
String k="";
char c=0;
int a=0;
int p=0;
char temp=0;
System.out.println();
for(int i=0;i<str.length();i++)//this is a loop to go through all the characters of the
string.
{
//the line that is entered should be in uppercase, and should only contain letters in
the alphabet with a full stop or a comma.
c=str.charAt(i);
if(c==',') //detects a comma in the string.
{
//Not adding a space after the comma, as one normally should, disrupts the
functionality.
k=k+',';
continue;
}
if(c=='.')
{
k=k+'.';
continue;
}
a=(int)c+32;
temp=(char)a;
if(c==' ') //when it detects a space, a space is added, and the first letter is not
converted to lowercase.
{
p=i+1;
k=k+' ';
continue;
}
else if(p==i) //this lets the first letter of each word remain uppercase.
{
k=k+c;
continue;
}
k=k+temp; //this is the usual function where the letters get converted to lowercase.
}
System.out.print("The processed line is: "+k);//the processed string is printed.
}
}
VDT
Name Datatype Description
c char This variable goes through all characters of a string.
a int This variable stores the integer value of each character in the string, in lowercase.
p int This variable helps in skipping the first letter of each word when processing the line.
This variable stores the lowercase value of each letter in the line, and adds it after
temp char processing.
i int Loop control variable for loop which passes the value of each character of the string.
Source Code:
import java.util.*;
class Anagram
{
public boolean isAnagram(String s,String str)
{
char c[]=new char[s.length()]; //array to hold characters of the first string.
char ch[]=new char[str.length()]; //array to hold characters of second string.
int i=0; //serves as a loop control variable several times.
int count=0;
if(s.length()==str.length()) //words cannot be anagrams of each other unless they have
the same number of characters.
{
for(i=0;i<s.length();i++) //extracting characters of the first word to an array.
{
c[i]=s.charAt(i);
}
for(i=0;i<str.length();i++) //extracting characters of the second word to an array.
{
ch[i]=str.charAt(i);
}
for(i=0;i<s.length();i++)
{
for(int j=0;j<ch.length;j++)
{
if(c[i]==ch[ j])
{
ch[ j]='+'; //this is done firstly to ensure that repeating elements do not count
again.
//the main reason is to check if all the elements of ch[] match the ones of c[].
//if all the elements of ch are +, then the words are anagrams of each other.
break;
}
}
}
for(i=0;i<str.length();i++)
{
if(ch[i]!='+') //checks all the elements of the array.
{
count++;
break;
}
}
if(count==0)
{
return true;
}
return false;
}
else
return false;
}
First Case:
Second Case:
Q5) Write a program to input a name which is three words long,
and the output being the last name followed by the first and
middle name.
Source Code:
import java.util.*;
class Name
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a name: ");
String s=sc.nextLine();
char c=s.charAt(0);
int count=0;
System.out.print("The name now is: ");
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c==' ')//detects whenever a space is inputted.
{
count++;
}
//it should be noted that multiple spaces must not be used.
if(count>=2)//after 2 spaces are detected, the program prints the last name.
{
System.out.print(c);
}
}
count=0;//the value of count is reset.
System.out.print(" ");
for(int i=0;i<s.length();i++)//this loop simply prints the first and middle name.
{
c=s.charAt(i);
if(c==' ')
{
count++;
}
if(count<2)//after 2 spaces are detected, the program terminates.c
{
System.out.print(c);
}
}
}
}
VDT
Name Datatype Description
c char This variable goes through all the characters of the string.
count int This variable is to count the number of times a space has been detected in the string.
i int Loop control variable.
s String The string contains the name which is 3 words long.
Example:
13 is a happy number because,
12 +32=10
12 +0=1.
Source Code:
import java.util.*;
public class HappyNumber
{
public int happy(int n)
{
int r=0,s=0;
while(n>0)//loop which keeps adding the sum of the squares of the digits of the
number.
{
r=n%10;
s=s+(r*r);
n=n/10;
}
return s;
}
VDT
First Case:
Second Case:
Q7) Write a program to input a string and find all the possible
permutations of the string.
Source Code:
import java.util.*;
class String_Permutations
{
public static void Permutation(String s,String a)
{
if (s.length()==0)//this detects when string is empty
{
System.out.println(a);
return; //this is to escape out of recursive method
}
for (int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
String k=s.substring(0,i)+s.substring(i+1); //this calculates the possible permutations
System.out.println("k is "+k);
Permutation(k,a+ch); //recursive method call
}
}
Source Code:
import java.util.*;
class String_Frequency
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a line: ");
String s=sc.nextLine();
System.out.print("Enter the letter which has to be checked for frequency: ");
char c=sc.next().charAt(0);
int count=0;
int c1=0;
char c2=0;
char chrtr=0;
if((int)c>97)//if the letter is in lowercase.
{
c1=(int)c-32;
c2=(char)c1; //uppercase of the letter
}
else //if the letter is in uppercase.
{
c1=(int)c+32;
c2=(char)c1; //lowercase of the letter
}
for(int i=0;i<s.length();i++)
{
chrtr=s.charAt(i);
if((chrtr==c)||(chrtr==c2))
{
count++; //counter variable to check for the frequency of required letter..
}
}
System.out.println("The frequency of letter is "+count+".");
}
}
VDT
Name Datatype Description
Variable to store value of character which is going to be checked for
c char frequency.
count int Variable storing the frequency of the given letter in the string.
c1 int Stores the typecasted value of the character c , but as an integer.
c2 char Stores the value of the character, but in the opposite case.
This variable is used to compare each character of the String s, to the given
chrtr char letter.
i int Loop control variable used to go through the characters of String s.
Java Terminal Window
First Case:
Second Case:
Q9) Write a menu driven program , giving the user an option to
check if two numbers are co-prime, or to check if a number is a
circular prime, i.e. All permutations of that number are primes.
Source Code:
import java.util.*;
class Primes
{
public boolean isPrime(int n)//checks if the number is prime.
{
if (n<=1)
return false;
if (n<=3)
return true;
if (n%2==0||n%3==0)//the two smallest cases, if the number is even then it is not
prime.
//and if it's divisible by 3, it is not.
return false;
for (int i=5;i*i<=n;i=i+6)
{
//value of i increases by 6, as the number not divisible by 6.
//due to the previous if conditions, the loop is shortened to check for much lesser
numbers.
if (n%i==0||n%(i+2)==0)
return false;
}
return true;
}
}
}
}
VDT
First Case:
Second Case:
Q10) Write a program, using overloaded constructors, to enter
two sets of time consisting of hours , minutes and seconds, in both
, and add the two time periods and print it.
Source Code:
import java.util.*;
class Time_Period
{
public static int hours,min,sec,hours1,min1,sec1;
Time_Period(int h,int m,int s,int k )//constructor to initialize the first set of number of
hours, minutes and seconds.
{
hours=h;
min=m;
sec=s;
}
public static void calculator(int hours1,int min1,int sec1)//constructors to add the values
of the variables, and increase the value of hours or minutes after minutes or seconds,
reaches a number more than 60, respectively.
{
if(min1>=60)
{
hours1=hours1+(min1/60);
min1=min1%60;
}
if(sec1>=60)
{
min1=min1+(sec1/60);
sec1=sec1%60;
}
//time is printed in hh:mm:ss format.
System.out.println("Total Time- "+hours1+" : "+min1+" : "+sec1);//prints the time after
adding the two sets of time.
}
}
VDT
Source Code:
class SquareFree
int c=0;
// Loop runs through all the odd natural numbers starting from 3 till the
square root of the number as the square of the square root of the number is
the number itself.
{
if (n%i==0) //If n can be divided by i twice, then n is not a square
free number. {
c++;
//If n passes all the above tests and the value of c remains unchanged, then the
number n is a square free number.
if (c==0)
free number.");
}
VDT
n int This variable stores the value which is to be checked to be square free.
c int This variable increases in value when the number is found to be divisible by a
square.
i int Loop control variable to check if the number is divisible by any odd
number,twice.
Output:
Source Code:
import java.util.*;
class Processing
double p=n/i; //This creates the other part of the number, containing 'k'
int part1=0;
int part2=0;
int fa=(int) f; //Typecasting is used to initialize the integer value of f into variable fa.
System.out.println("The numbers after the first step are: "+sa+" and
int r2=fa%10;
part2=(part2*10 + r2);
fa=fa /10;
merged correctly. }
no=no/10; //To remove the last digit in the loop(which is an extra 0).
sa = sa /10;
m=m/10;
"+rev+".");
prime=rev%10; //this will isolate the last digit of the program to check if it
{
System.out.print(prime+","); //the prime digits are printed here.
rev=rev/10;
copy=copy/10;
valid or not. {
obj.execution(n,k,count);
Input:
Enter a number of minimum 6-digits ,and maximum of 9-digits:
1236789
Output:
VDT
f double This variable holds the value of the remainder of the original number divided by 'i',
hence the second part.
p double This variable holds the value of the quotient of the original number divided by 'i',
hence the first part.
no double Variable holds the value after merging the two parts of the number as described in
step 3.
m double Variable holds an exponent value of 10 which helps in merging the digits of the two parts
after step 2.
part1 int This variable contains the reversed value of the first part after step 1.
part2 int This variable contains the reversed value of the first part after step 2.
prime int This variable holds the value of each digit in the number after step 4, enabling the
check for prime position digits.
k int User inputted variable for the second number between 1-5 .
copy int Copies the value of the variable 'n',so that the value of n does not change when
counting digits in the first number.
Source Code:
import java.util.*;
class Duplicate
for(int i=0;i<8;i++) //A loop is run going through the eight elements
of the array. {
for(int j=0;j<8;j++) //This loop is for checking the array for the
duplicate element. {
if(j==i) //This ensures that when the loop skips checking the same
element as the duplicate is another element with the same value in the array.
continue;
else if(a[i]==a[ j]) //This checks the corresponding element for the
duplicate element. {
}
}
if(obj.arr()!=0)
else
VDT
Source Code:
import java.util.*;
class Amicable
divisors of a number. {
int result = 0;
result+=i;
result+=(i+n/i);
}
// Adding 1 and n to result as above loop, because by the above loop,1 is not
considered a proper divisor.
return (result+1);
if (divSum(x)!=y)
int x=sc.nextInt();
int y=sc.nextInt();
are amicable.");
Input:
Please enter two numbers which are to be checked for properties of
amicable numbers:
5564
5020
Output:
Yes,the numbers are amicable.
VDT
Name Datatype Description
n int This variable holds the value of either x and y in the program to
calculate the sum of their factors, respectively.
result int This variable holds the value of the sum of the factors of the
parameter which is passed.
i int Loop control variable to check for the factors of the number.
Source Code:
import java.util.*;
class Customize_Array
an array. {
for(int i=0;i<10;i++)
array. {
break; //ends the loop if an empty element is found and replaced with the
user inputted number.
else if(i!=9)
continue;// prevents the loop to reach the else statement until the last
element of the array is checked to be empty.
{
System.out.println("There is no empty element in the array,please
remove an element before adding one.");
}
array. {
array. {
if(a[i]==p)
array."); break;
else if(i!=9) //prevents the loop from reaching the else statement until
the last element of the array is checked to be the user inputted element.
continue;
else //statement is printed if the user inputted element is not found in the
array. {
if(i<9)
System.out.print(a[i]+", ");
else if(i==9) //prints a full stop after the array has been printed.
System.out.println(a[i]+".");
void end(int [] a) //method to end the program after displaying the final
elements in the array.
the array. {
if(i<9)
System.out.print(a[i]+", ");
else if(i==9)
System.out.println(a[i]+".");
System.out.println();
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int n=0;
a[i]=sc.nextInt();
for(int i=0;i<10;i++)
if(i<9)
System.out.print(a[i]+", ");
else if(i==9)
System.out.println(a[i]+".");
System.out.println();
System.out.println("1 : Insert a number into any empty element of the array .");
System.out.println("4 : Print the final elements of the array end the program. ");
//group of statements to familiarize the user with the various uses of the
program. System.out.println();
while(true) // takes input from the user in a loop, ending the program
when the corresponding input is received.
System.out.println();
int c=sc.nextInt();
value. break;
value. break;
break;
case 4: //calls the method to end the program.
VDT
Name Datatype Description
a[] int This array stores all the initial elements that the user inputs and gets
customized accordingly.
c int This variable takes a user inputted value that it has to replace an empty
element of the array with.
i int Loop control variable for various loops for the array throughout the program.
p int This variable takes a user inputted value which has to be removed from
the array once, if present.
n int This variable helps the user operate the program and choose what process
is to be conducted.
Source Code:
import java.util.*;
class Areas
side=a;
radius=b;
side3=d;
"+areaC);
double s=(1.0/2.0)*(side+side2+side3);
areaT=Math.sqrt(s*(s-side)*(s-side2)*(s-side3)) ;
//Heron's formula is used to calculate the area. Note that if a triangle cannot be
formed by the inputted sides, the area would be ‘NaN’, as the formula only works if
the geometry of a triangle is acknowledged.
}
public void aHexagon() //method to calculate the area of a
regular hexagon. {
areaRH=((3*Math.sqrt(3))/2)*Math.pow(side,2);
System.out.println("The area of the hexagon is: "+areaRH);
public static void main() //main method to take the user input and call the
respective methods. {
System.out.println();
System.out.println();
while(true) // loop to take input, loop stops when the corresponding 'terminate
program 'option is chosen.
System.out.println();
switch(n) //switch case to call the respective methods and take the
respective inputs. {
case 1:
{
double b=sc.nextDouble();
ob.aCircle();
break;
}
case 2:
obje.aTriangle();
break;
case 3:
double b=sc.nextDouble();
ob.v3DSphere();
break;
case 4:
{
obj.aHexagon();
break;
}
case 5:
default:
break;
}
Input:
1: Area of a circle with the radius as inputted by the
user. 2: Area of a triangle with the sides as inputted
by the user. 3: Area of a 3D Sphere with the radius as
inputted by the user. 4: Area of a hexagon with the
side as inputted by the user. 5: Exit the program.
Output:
The area of the triangle is: 20.396078054371138
VDT
Name Datatype Description
side int This variable stores the value of the first side of the triangle.
side2 int This variable stores the value of the second side of the triangle.
side3 int This variable stores the value of the third side of the triangle.
radius double This variable stores the value of the radius of the circle or sphere.
areaC double This variable stores the value of area of the circle
areaS double This variable stores the value of the three dimensional sphere.
areaT double This variable stores the value of the area triangle.
areaRH double This variable stores the value of the area of the regular hexagon .
b int Parameter for user inputted value of radius of the circle or sphere.
a int Parameter for user inputted value of the respective side of a triangle
or a regular hexagon.
d int Parameter for user inputted value of the respective side of a triangle.
n int Variable to operate the switch case, hence taking the user's choice.
Source Code:
import java.util.*;
class Average_Marks
a=a1;
b=b1;
c=c1;
d=d1;
e=e1;
public void Con1() //method to calculate the average of the five marks.
avg=(a+b+c+d+e)/5;
}
public static void main()
}
Input:
Enter the marks of the student in the first subject: 56
Enter the marks of the student in the second subject: 89
Enter the marks of the student in the third subject: 75
Enter the marks of the student in the fourth subject: 96
Enter the marks of the student in the fifth subject: 100
Output:
The average marks of the student is: 83
VDT
Name Datatype Description
a int Variable for storing marks of the first subject.
b int Variable for storing marks of the second subject.
c int Variable for storing marks of the third subject.
d int Variable for storing marks of the fourth subject.
e int Variable for storing marks of the fifth subject.
avg int Variable to store the average marks of the student.
a1 int Parameter for user inputted marks in the first subject.
b1 int Parameter for user inputted marks in the second subject.
c1 int Parameter for user inputted marks in the third subject.
d1 int Parameter for user inputted marks in the fourth subject.
e1 int Parameter for user inputted marks in the fifth subject.
ob Average_Marks Object of class Average_Marks.
Q18) WAP to input the names and ranks of ten students,
and then display the name corresponding to the rank the
user wants to obtain. No two students can have the same
rank.
Source Code:
import java.util.*;
class BinarySearch
public static void main() //function to call various methods and print
System.out.println();
int n=sc.nextInt();
//the value of r represents the index the rank was found in,hence the
corresponding name can be found from the other
else
}
public void input(String name[],int rank[]) //method to take the user input
for the name and corresponding rank.
System.out.println(" Student:"+(i+1));
name[i]=sc.nextLine();
rank[i]=sc.nextInt();
System.out.println();
public static int search(int rank[],int n ) //method to search for the name
corresponding to the rank to be searched.
while(low<=high)
int mid=low+(high-low)/2;
if(rank[mid]==n)
return mid;
if (rank[mid]<n)
low=mid+1;
else
high=mid-1;
return 0; //If the required rank is not found in the given array then
this returns 0. }
Input:
Student:1
Please enter the name of the student: Evan
Please enter the rank of the respective student: 2
Student:2
Please enter the name of the student: Guru
Please enter the rank of the respective student: 4
Student:3
Please enter the name of the student: Georgio
Please enter the rank of the respective student: 7
Student:4
Please enter the name of the student: Simon
Please enter the rank of the respective student: 8
Student:5
Please enter the name of the student: Krish
Please enter the rank of the respective student: 1
Student:6
Please enter the name of the student: Aryan
Please enter the rank of the respective student: 3
Student:7
Please enter the name of the student: Sasha
Please enter the rank of the respective student: 5
Student:8
Please enter the name of the student:
Anant Please enter the rank of the
respective student: 10
Student:9
Please enter the name of the student:
Ojas Please enter the rank of the
respective student: 6
Student:10
Please enter the name of the student:
Anubhav Please enter the rank of the
respective student: 9
Output:
rank[] int This array stores the ranks of the corresponding students.
name int This array stores the names of the corresponding students.
r int This array stores the return value of the method 'search'.
low int Variable used in binary search loop to set the lower limit.
high int Variable used in binary search loop to set the upper limit.
mid int Variable used in binary search loop to store the value of the middle
index between the two limits.
n int Variable that has user inputted value for specific rank whose
corresponding name has to be found.
Source Code:
import java.util.*;
class Digit_Seperation
//i is initialized to be 1 less than the number of digits to ensure that the
array elements correspond with the digits.
//First digit in the number would be the lower bound,the last digit would
be the upper bound.
a[i]=n%10;
n/=10;
System.out.println(a[i]+".");
continue;
}
System.out.print(a[i]+",");
}
int i,j, small,temp,pos;
int h=count;
int f=0;
small=a[i];
pos=i;
for(j=i+1;j<h;j++)
if(a[ j]>small)
small=a[ j];
pos=j;
temp=a[i];
a[i]=small;
a[pos]=temp;
if(i==count-1)
f=(a[i]+f);
break;
f=(a[i]+f)*10;
}
System.out.println("The final number is: "+f);
int n=sc.nextInt();
int count=0;
copy/=10;
count++;
}
Input:
Enter the number: 589612
Output:
The array containing the digits of the number is:
5,8,9,6,1,2. The final number is: 986521
VDT
i) # # # # # # #
######
#####
####
###
##
#
ii) 9
898
78987
6789876
567898765
45678987654
3456789876543
234567898765432
12345678987654321
Source Code:
import java.util.*;
class Patterns
System.out.println();
int copy2=rows;
for(int j=0;j<copy2-copy;j++)
for(int i=0;i<=copy;i++)
triangle pattern. }
copy=copy-1;
System.out.println();
System.out.print(" ");
System.out.print(j+" ");
numbers in a row. {
System.out.print(j+" ");
System.out.println();
int n=0;
Patterns obj=new Patterns();
System.out.println();
System.out.print("Input: ");
n=sc.nextInt();
int rows=sc.nextInt();
System.out.println();
if(rows>0)
else
break;
case 2:
int rows=sc.nextInt();
System.out.println();
else
{
break;
}
First Case:
Enter 1 to print the first
pattern. Enter 2 to print
the second pattern.
Input: 1
Enter the number of rows: 6
Output:
The first pattern is being printed:
#######
######
#####
####
###
##
#
The pattern has been printed.
Second Case:
Enter 1 to print the first pattern.
Enter 2 to print the second pattern.
Input: 2
Enter the number of rows, not more than 9: 9
Output:
The second pattern is being printed:
9
898
78987
6789876
567898765
45678987654
3456789876543
234567898765432
12345678987654321
n int Variable to operate the switch case, taking a user inputted value.
rows int Variable to store the number of rows of the pattern to print.
copy int Variable to copy the number of rows in the method to print the first pattern.
copy2 int This is the second variable to copy the number of rows in the method to
print the first pattern.
k int Loop control variable in the process to print the first pattern.
j int Loop control variable in the loop to print the spaces in the first pattern.
i int Loop control variable in the loop to print the hashtags in the first pattern.
z int Loop control variable which stores the value of the number of rows in
the second pattern.
obj Pattern
Object of the class Pattern.
----------------------X----------------------
Acknowledgement
Thank You