BASIC JAVA PROGRAMMING
BASIC JAVA PROGRAMMING
The kind of values that can be represented and manipulated in a programming language are
known as _________.
types
variables
symbols
constants
I – DESIGN : Basic Java Programming
S1P1-WELCOME MESSAGE
Welcome Message
"Pine Tree" is a recently launched startup Event Management company. The company gained
a good reputation within a short span because of its highly reliable service delivery.
Nikhil, the founder of this company wished to take the company’s services to the next step
and decided to design an Event Management System that would let its Customers plan and
host events seamlessly via an online platform. As a part of this requirement, Nikhil wanted to
write a piece of code for his company’s Amphi Event Management System that will welcome
all the Customers who are using it. Help Nikhil on the task.
Output Format:
Output should display "Welcome to Amphi Event Management System".
Refer sample output for formatting specifications.
Sample Output:
Welcome to Amphi Event Management System
import java.io.*;
class Main
{
public static void main(String args[])
{
System.out.print("Welcome to Amphi Event Management System");
}
}
Nikhil, the founder of “Pine Tree” company wished to design an Event Management System
that would let its Customers plan and host events seamlessly via an online platform.
As a part of this requirement, Nikhil wanted to write a piece of code for his company’s
Amphi Event Management System that will display customized welcome messages by taking
Customers’ name as input. Help Nikhil on the task.
Input Format:
First line of the input is a string that corresponds to a Customer’s name. Assume that the
maximum length of the string is 50.
Output Format:
Output should display the welcome message along with the Customer’s name.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
Nikhil, the founder of “Pine Tree” company wished to design an Event Management System
that would let its Customers plan and host events seamlessly via an online platform.
As a part of this requirement, Nikhil wanted to write a piece of code for his company’s
Amphi Event Management System that will display customized welcome messages by taking
Customers’ name as input. Help Nikhil on the task.
Input Format:
First line of the input is a string that corresponds to a Customer’s name. Assume that the
maximum length of the string is 50.
Output Format:
Output should display the welcome message along with the Customer’s name.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
S1P3-NUMBER OF EVENTS
Number of events
"Pine Tree" Company has signed up a big time Event Management deal from the Rotary
Youth Club for a Trade Fair organized at Codissia Complex, wherein all startup companies in
the Software industry are demonstrating their latest products and services and meet with
industry partners and Customers.
Amphi Event Management System has to be modified to write a piece of code that will get
the input of the number of events to be hosted for the Fair at Codissia from its users and
display the same. Help the company to accomplish the requirement.
Input Format:
First line of the input is an integer that corresponds to the number of events to be hosted at
Codissia.
Output Format:
Output should display the number of events to be hosted at Codissia.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
S1P4-EVENT DETAILS
Event Details
Be it a last minute get together, a birthday party or corporate events, the "Pine Tree" Event
Management Company helps you plan and execute it better and faster. Nikhil, the founder of
the company wanted the Amphi Event Management System to get and display the event
details from his Customers for every new order of the Company.
Write a program that will get the input of the event details like name of the event, type of the
event, number of people expected, a string value (Y/N) telling whether the event is going to
be a paid entry and the projected expenses (in lakhs) for the event. The program should then
display the input values as a formatted output.
Input Format:
First input is a string that corresponds to the name of the event. Assume the maximum length
of the string as 50.
Second input is a string that corresponds to the type of the event. Assume the maximum
length of the string as 50.
Third input is an integer that corresponds to the number of people expected for the event.
Fourth input is a character that corresponds to Y/N telling whether the event is going to be a
paid entry or not.
Fifth input is a double value that corresponds to the projected expenses (in lakhs) for the
event.
Output Format:
Output should display the event details as given in the sample output.
All double values need to be displayed correct to 1 decimal place
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
The program should get the branding expenses, travel expenses, food expenses and logistics
expenses as input from the user and calculate the total expenses for an event and the
percentage rate of each of these expenses.
Input Format:
First input is a int value that corresponds to the branding expenses.
Second input is a int value that corresponds to the travel expenses.
Third input is a int value that corresponds to the food expenses.
Fourth input is a int value that corresponds to the logistics expenses.
Output Format:
First line of the output should display the int value that corresponds to the total expenses for
the Event.
Next four lines should display the percentage rate of each of the expenses.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
System.out.println("Enter branding expenses");
int be = ip.nextInt();
System.out.println("Enter travel expenses");
int te = ip.nextInt();
System.out.println("Enter food expenses");
int fe = ip.nextInt();
System.out.println("Enter logistics expenses");
int le = ip.nextInt();
int tot = be + te + fe + le;
double bp = Math.round((100 * be) / tot);
double tp = Math.round((100 * te) / tot);
double fp = Math.round((100 * fe) / tot);
double lp = Math.round((100 * le) / tot);
java.text.DecimalFormat ft = new java.text.DecimalFormat("#.00");
System.out.println("Total expenses : Rs."+ft.format(tot));
System.out.println("Branding expenses percentage : "+ft.format(bp)+"%");
System.out.println("Travel expenses percentage : "+ft.format(tp)+"%");
System.out.println("Food expenses percentage : "+ft.format(fp)+"%");
System.out.println("Logistics expenses percentage : "+ft.format(lp)+"%");
}
}
S1P6-TRADE FAIR
Trade Fair
Trade Fairs are important for companies to present their products and to get in touch with its
customers and business parties. One such grandeur Trade Fair Event was organized by the
Confederation of National Large Scale Industry.
Number of people who attended the event on the first day was x. But as days progressed, the
event gained good response and the number of people who attended the event on the second
day was twice the number of people who attended on the first day. Unfortunately due to
heavy rains on the third day, the number of people who attended the event was exactly half
the number of people who attended on the first day.
Given the total number of people who have attended the event in the first 3 days, find the
number of people who have attended the event on day 1, day 2 and day 3.
Input Format:
First line of the input is an integer value that corresponds to the total number of people.
Output Format:
First line of the output should display the number of attendees on day 1.
Second line of the output should display the number of attendees on day 2.
Third line of the output should display the number of attendees on day 3.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
HelpIndia, a famous NGO has been selective in identifying events to raise funds for charity.
Suzanne is a volunteer from the NGO who was selling tickets to the public for the charity
event. She sold 'X' more adult tickets than children tickets and she sold twice as many senior
tickets as children tickets. Assume that an adult ticket costs $5, children ticket costs $2 and
senior ticket costs $3.
Suzanne made 'Y' dollars from ticket sales. Find the number of adult tickets, children tickets,
and senior tickets sold.
Input Format:
The first input is an integer value X that corresponds to the number of adult tickets more than
children tickets.
The second input is an integer value Y that corresponds to the money in dollars made by
Suzanne from ticket sales.
Output Format:
The first line of the output should display the number of children tickets sold.
The second line of the output should display the number of adult tickets sold.
The third line of the output should display the number of senior tickets sold.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
Help the kids by writing a program to find the area of the largest possible square that can be
formed, given the side of a square tile (in cms) and the number of square tiles available.
Input Format:
First line of the input is an integer that corresponds to the side of a square tile (in cms).
Second line of the input is an integer that corresponds to the number of square tiles available.
Output Format:
Output should display the area of the largest possible square that can be formed (in square
cms) with the available tiles.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
Sample Input and Output :
Enter the side in cm of a square tile
5
Enter the number of square tiles available
8
Area of the largest possible square is 100sqcm
import java.io.*;
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
System.out.println("Enter the side in cm of a square tile");
int side = ip.nextInt();
System.out.println("Enter the number of square tiles available");
int no = ip.nextInt();
long t = Math.round(Math.sqrt(no));
if(t*t > no)
t = t-1;
long area = t*t*side*side;
System.out.println("Area of the largest possible square is "+area+"sqcm");
}
}
S1P9-WISCONSIN STATE FAIR
Wisconsin State Fair
Wisconsin State Fair is one of the largest midsummer celebrations in the Midwest Allis,
showcasing the agriculture skills and prowess of the state. The Event organizers hired few
part-time employees to work at the fair and the agreed salary paid to them are as given below:
Justin is a part-time employee working at the fair. Number of hours Justin has worked in the
weekdays is 10 more than the number of hours he had worked during weekends. If the total
salary paid to him in this month is known, write a program to estimate the number of hours
he had worked during weekdays and the number of hours he had worked during weekends.
Input Format:
First line of the input is a double value that corresponds to the total salary paid to Justin.
Output Format:
First line of the output should display the number of hours Justin has worked during the
weekdays.
Second line of the output should display the number of hours Justin has worked during the
weekends.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
The Magic Castle, the home of the Academy of Magical Arts at California has organized the
great ‘WonderWorks Magic Show’. 3 renowned magicians were invited to mystify and thrill
the crowd with their world’s spectacular magic tricks. At the end of each of the 3 magicians’
shows, the audience were requested to give their feedback in a scale of 1 to 10. Number of
people who watched each show and the average feedback rating of each show is known.
Write a program to find the average feedback rating of the WonderWorks Magic show.
Input Format:
First line of the input is an integer value that corresponds to the number of people who
watched show 1.
Second line of the input is a float value that corresponds to the average rating of show 1.
Third line of the input is an integer value that corresponds to the number of people who
watched show 2.
Fourth line of the input is a float value that corresponds to the average rating of show 2.
Fifth line of the input is an integer value that corresponds to the number of people who
watched show 3.
Sixth line of the input is a float value that corresponds to the average rating of show 3.
Output Format:
Output should display the overall average rating for the show. Display the rating correct to 2
decimal places.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
System.out.println("Enter the number of people who watched show 1");
int c1 = ip.nextInt();
System.out.println("Enter the average rating for show 1");
float r1 = ip.nextFloat();
System.out.println("Enter the number of people who watched show 2");
int c2 = ip.nextInt();
System.out.println("Enter the average rating for show 2");
float r2 = ip.nextFloat();
System.out.println("Enter the number of people who watched show 3");
int c3 = ip.nextInt();
System.out.println("Enter the average rating for show 3");
float r3 = ip.nextFloat();
double fr = ((c1 * r1)+(c2 * r2)+(c3 * r3)) / (c1 + c2 + c3);
java.text.DecimalFormat ft = new java.text.DecimalFormat("#.00");
System.out.printf("The overall average rating for the show is "+ft.format(fr));
}
}
S1P11-BIRTHDAY CHALLENGE
Birthday Challenge
Louis was celebrating his 10th Birthday and his parents wished to make his birthday more
special by throwing a surprise bash, inviting all their friends, relatives and neighbors. The
little mathematics geek Louis has another surprise waiting for him when he had to cut his
favorite Choco vanilla cake. The cake is a rectangular cake and it consists of m×n (1≤m,
n≤1000) squares. His friends now called him out for a challenge.
The challenge is that, Louis has to break the cake up into 1×1 pieces (individual squares) and
find what is the minimum number of times that he breaks the choco vanilla cake, or pieces
therefore, in order to achieve this?
Note that he cannot stack pieces of the cake and break them, because the choco vanilla cake
is thick. As an example, a 2×2 cake requires 3 breaks. First he can break it in half, then break
each of the halves in half. He cannot break it in half, stack the two 1×2 pieces, and then use
only one more break to achieve his goal.
Input Format:
First line of the input consists of an integer, the dimensions m of the choco vanilla cake.
Second line of the input consists of an integer, the dimensions n of the choco vanilla cake.
Output Format:
Output the minimum number of times that he breaks the choco vanilla cake
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
System.out.println("Enter m");
int m = ip.nextInt();
System.out.println("Enter n");
int n = ip.nextInt();
//(m-1)+((n-1)*m)
"Planet Kids Entertainment Fair" is back to delight kids and parents. The Fair will have non-
stop entertainment with an extravaganza of games, exciting rides, sports, art & crafts, role-
plays, inspiring competitions, prizes & gifts, and yummy food.
Few lucky attendees at the Fair will be given a pack of candies as a lucky gift and the show
coordinator has assigned you the task for choosing the number of attendees who will receive
the pack of candies. There are 'N' candies available and you need to decide how many
candies to place in each pack. Each pack must contain the same number of candies. You
should choose an integer A between 1 and N, inclusive, and place exactly A candies into each
pack. You should make as many packs as possible but since you enjoy eating candies very
much, you eat the remaining candies.
Write a program that will calculate the pack size(A) so that you can eat as many candies as
possible. If multiple pack size will result in the same number of leftover candies, then print
the largest pack size.
Input Format:
The first and only line of input contains an integer N.
Output Format:
Output a single line that gives the pack size that will maximize the number of leftover
candies.
Refer sample input and output for formatting specifications.
Sample Input 1:
2
Sample Output 1:
2
Explanation:
There will be no leftover candies regardless of the size of the pack you choose. So you
choose the largest possible pack size 2.
Sample Input 2:
5
Sample Output 2:
3
Explanation:
There will be 2 leftover candies, if you choose 3 as the pack size.
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
int n = ip.nextInt();
if(n>2)
System.out.println((n/2)+1);
else if(n == 2)
System.out.println(n);
}
}
Pranav, an enthusiastic kid visited the "Fun Fair 2017" along with his family. His father
wanted him to purchase entry tickets from the counter for his family members. Being a little
kid, he is just learning to understand about units of money. Pranav has paid some amount of
money for the tickets but he wants your help to give him back the change of Rs. N using
minimum number of rupee notes.
Consider a currency system in which there are notes of seven denominations, namely, Rs. 1,
Rs. 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100. If the change given to Pranav Rs. N is input, write a
program to compute smallest number of notes that will combine to give Rs. N.
Note:
Refer to problem specifications.
Input Format:
First line of the input is an integer N, the change to be given to Pranav.
Output Format:
Output should display the the smallest number of notes that will combine to give N.
Refer sample input and output for formatting specifications.
Sample Input 1:
1200
Sample Output1:
12
Sample Input 2:
242
Sample Output2:
7
Problem Requirements:
C
Keyword Min Count Max Count
do 0 0
while .... 0 0
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
int n = ip.nextInt();
int n100 = 0, n50 = 0, n10 = 0, n5 = 0, n2 = 0, n1 = 0;
n100 = n / 100;
n = n % 100;
if (n >= 50) {
n50 = 1;
n = n - 50;
}
n10 = n / 10;
n = n % 10;
if (n >= 5) {
n5 = 1;
n = n - 5;
}
n2 = n / 2;
n1 = n - (n2 * 2);
int tot = n100 + n50 + n10 + n5 + n2 + n1;
System.out.println(tot);
}
}
S1P14-FOOD FESTIVAL AT HILLTOWN
Food Festival at HillTown
HillTown Inn is planning to organize a Food Festival bringing together at one place, a wide
variety of cuisines from across the world on account of Christmas. The Hotel Management
has rented out a square hall of an indoor Auditorium for this extravaganza. The side of the
square hall is y inches in which a large square table is placed for the display of the most
popular and celebrated food items. The side of the square table is x inches, such that x<y.
The Management wanted to fill the remaining floor area with a decorative carpet. To get this
done, they needed to know the floor area to be filled with the carpet. Write a program to help
the Management find the area of the region located outside the square table, but inside the
square hall.
Input Format:
First line of the input is an integer y, the side of the square hall.
Second line of the input is an integer x, the side of the square table placed for display.
Output Format:
Output should display the area of the floor that is to be decorated with the carpet.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
Of the total audience who had come for the show, 1/3 were boys, 3/6 were girls and the rest
of them were adults. If there were 'x' more girls than adults, how many people were there in
total? Help the School authorities to find the total people who visited their show.
Input Format:
First line of the input is an integer 'x', which corresponds to the count of girls more than
adults.
Output Format:
Output the total number of people who had visited the talent show.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and rest corresponds to output.]
Input Format:
The first input is an integer which corresponds to A. The second input is an integer which
corresponds to B. The third input is a float which corresponds to gain %.
Output Format:
Refer sample input and output for formatting specifications.
The float values are displayed correct to 2 decimal places.
Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner ip = new Scanner(System.in);
System.out.println("Price of old scooter:");
int a = ip.nextInt();
System.out.println("Repair amount:");
int b = ip.nextInt();
System.out.println("Selling price:");
int c = ip.nextInt();
//Gain % = (100*( Selling Price - ( Cost Price + Repair Cost ))) / (Cost Price + Repair
Cost ) )
//((c-a-b)*1.0/(a+b))*100 )
double g = (100*(c-a-b)*1.0)/(a+b);
java.text.DecimalFormat ft = new java.text.DecimalFormat("#.00");
System.out.println("Gain percentage is "+ft.format(g));
}
}
CHANGE POSITION
Change Position
The room that Patrick and Johnny were staying was very big. They felt lazy to walk inside
the room from the bed's location to another location. So they wanted to change the position of
the bed. The shape of the room is a Square. They decided to place the bed at the centre of the
room so that their walking distance would be minimised. Can you please help them in placing
the bed at the centre?
Given the coordinates of the left bottom vertex of the square room and the length of the side,
you need to write a program to determine the coordinates of the centre of the room.
[Assumption --- Length of the side is always even]
Input Format:
Input consists of 3 integers. The first integer corresponds to the x-coordinate of the left
bottom vertex. The second integer corresponds to the y-coordinate of the left bottom vertex.
The third integer corresponds to the length of the square.
Output Format:
Refer Sample Input and Output for exact formatting specifications.