0% found this document useful (0 votes)
8 views

proj comp (1) (1)

Uploaded by

zahraaaishh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

proj comp (1) (1)

Uploaded by

zahraaaishh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

STRING HANDLING

Write a program to extract a portion of a character string and print the extracted string. Assume
that m characters are extracted, starting with the nth character.

Variable name Data Type Description


s String String input by the user
len Int Store length of the string
n int Input the start index
m Int Input the number of characters
extract
substr String Store the substring of the string
Write a program, which will get text string and count all occurrences of a particular word.

Variable name Data Type Description


Str String Sentence input by the user

ipWord String Word input by the user

Count Int Store number of words

len Int Store length of the string


CLASSES, OBJECTS, AND
CONSTRUCTORS
Define a class named movieMagic with the following description:

Instance Variables/Data Members:


int year - to store the year of release of a movie
String title - to store the title of the movie.
float rating - to store the popularity rating of the movie.
(minimum rating = 0.0 and maximum rating = 5.0)
Member Methods:
i. movieMagic() - Default constructor to initialise numeric data members to 0 and String data
member to "".
ii. void accept() - To input and store year, title and rating.
iii. void display() - To display the title of a movie and a message based on the rating as per the
table given below.
Variable name Data Type Description
Year int to store the year of release of a
movie
Title String to store the title of the movie.

rating Float to store the popularity rating of the


movie.
Write a program in Java using a method Discount( ), to calculate a single discount or a
successive discount. Use overload methods Discount(int), Discount(int,int) and
Discount(int,int,int) to calculate single discount and successive discount respectively. Calculate
and display the amount to be paid by the customer after getting discounts on the printed price of
an article.

Sample Input:

Printed price: ₹12000

Successive discounts = 10%, 8%

= ₹(12000 - 1200)

= ₹(10800 - 864)

Amount to be paid = ₹9936

Variable name Data type Description


price Int Price input by the user
d Int Store the discount
priceAfterDisc1, double To store price after discount
priceAfterDisc2 calculation
An electronics shop has announced a special discount on the purchase of Laptops as given
below:

Category Discount on Laptop

Up to ₹25,000 5.0%

₹25,001 - ₹50,000 7.5%

₹50,001 - ₹1,00,000 10.0%

More than ₹1,00,000 15.0%

Define a class Laptop described as follows:

Data members/instance variables:

name

price

dis

amt

Member Methods:

A parameterised constructor to initialize the data members

To accept the details (name of the customer and the price)

To compute the discount

To display the name, discount and amount to be paid after discount.

Write a main method to create an object of the class and call the member methods.
Variable name Data type Description
name String To input the name
of the purchaser
price;p int To input/store the price of
the purchased item
respectively.
dis double To calculate the discount
amt double To calculate the total bill
amount
USER-DEFINED FUNCTIONS
Write a program using method name Glcm(int,int) to find the Lowest Common Multiple (LCM) of
two numbers by GCD (Greatest Common Divisor) of the numbers. GCD of two integers is
calculated by continued division method. Divide the larger number by the smaller, the remainder
then divides the previous divisor. The process is repeated till the remainder is zero. The divisor
then results in the GCD.

Variable name Data type Description


a Int First number input by the user
b Int Second number input by the user
x Int Copy of the first number
y Int Copy of the second number
t Int Store the second number
Lcm int Store the lcm of the two numbers
Write a program in Java to accept a word. Pass it to a function magic(String str). The function
checks the string for the presence of consecutive letters. If two letters are consecutive at any
position then the function prints "It is a magic string", otherwise it prints "It is not a magic string".

Sample Input: computer

Sample Output: It is not a magic string

Sample Input: HEEL

Sample Output: It is a magic string

Variable name Data type Description


str String Word input by the user
isMagicStr boolean Store true or false
i Int Loop variable
temp String Store the uppercase of word str
Write a class with the name Area using function overloading that computes the area of a
parallelogram, a rhombus and a trapezium.

Formula:

Area of a parallelogram (pg) = base * ht

Area of a rhombus (rh) = (1/2) * d1 * d2

(where, d1 and d2 are the diagonals)

Area of a trapezium (tr) = (1/2) * ( a + b) * h

(where a and b are the parallel sides, h is the perpendicular distance between the parallel sides)
Variable name Data type Description
Base double Input the base of the
parallelogram
ht double Input the height of the
parallelogram
d1 double Input the first diagonal of the
rhombus
d2 double Input the second diagonal of the
rhombus
a double First side of the trapezium
b double Second side of the trapezium
h double Input the height of the trapezium
ARRAY PROGRAMS
Write a program to accept the year of graduation from school as an integer value from the user.
Using the binary search technique on the sorted array of integers given below, output the
message "Record exists" if the value input is located in the array. If not, output the message
"Record does not exist".

Variable name Data type Description


n Int array Store the year of graduation
year Int Year input by the user
l Int First index of array
h Int Last index of array
idex Int Store the index of the array
m Int Store the mid index of array
Write a program to perform binary search on a list of integers given below, to search for an
element input by the user. If it is found display the element along with its position, otherwise
display the message "Search element not found".

5, 7, 9, 11, 15, 20, 30, 45, 89, 97

Variable name Data type Description


arr Int array Store the elements of the array
n Int To input the number to be
searched by the user
l Int First index of array
h Int Last index of array
idex Int Store the index of the array
m Int Store the mid index of array
WAP to input, find and print the largest number in an array.

Variable name Data type Description


ar int array Store 10 numbers input by the
user
l int Store the largest number
i int Loop variable
WAP to calculate the sum of the diagonal elements of an array with 16 digits.

Variable name Data type Description


A Int array Two dimensional array to store
16 numbers
i int Loop variable
j int Loop variable
sum int To store some of diagonal
elements
Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.
Variable name Data type Description
a String array String array to store 40
strings input by the user
i int Loop variable
n int Store the length of the
array
idex int Store the index of the
array
t String String to swap values

MENU DRIVEN PROGRAMS


Variable name Data type Description
word String Word input by the user
choice int Choice input by the user
len int Store length of the word
i int Loop variable
j int Loop variable
Variable name Data type Description
choice int Character input by the user

l int Length input by the user

b int Breath input by the user

area double Store the different area

a int Side of triangle input by the user

r int Radius input by the user


Write a program to display the following patterns as per the user's choice.

Pattern 1
I
C
S
E

Pattern 2
I
C
S
E
Variable name Data type Description
ch Int Choice input by the user
str String Word input by the user
len int Store the length of the word
i int Loop variable
j int Loop variable
Write a program to input a number and check and print whether it is a Pronic number or not.
(Pronic number is a number which is the product of two consecutive integers.)
Example: 12 = 3 x 4
20 = 4 x 5
42 = 6 x 7

Variable name Data type Description


num int Number input by the user

isPronic boolean Store true or false

i int Loop variable


Variable name Data type Description
num Long Number input by the user

sum Long Store The sum of digit of number

n Long Store copy of number

d int Store the digit of number


Variable name Data type Description
n int Number input by the user
sum int Copy of the number n
r int Store the digit of number
sum int Store sum of digits of number
mul int Store product of the digits of the
number
Employees at Arkenstone Consulting earn the basic hourly wage of Rs.500. In addition to this,
they also receive a commission on the sales they generate while tending the counter. The
commission given to them is calculated according to the following table:

Write a program in Java that inputs the number of hours worked and the total sales. Compute
the wages of the employees.

Variable name Data type Description


hrs int Store number of hours input by
the user
sale int Store the total sales input by the
user
wage double Store the wage
c double Store the rate of commission
comm double Store the commission

You might also like