Class Solved Progz
Class Solved Progz
Problem 1 :
1irom the
Write a
program O denne
a method void largest( int x, int y ) and print the
B gest
rom X and y.
Darameters/arguments
Solution:
"
and "+ y+ is
Sy'stem.out.printinThe largest from "+ x+
Cancen
Problem 2
int y, int z ) to decide and return the
Write program to define
a
a method static int largest( int x,
and zZ.
parameters/arguments x, y
largest from the three integer
Solution:
largest from three numbers
7 The program that finds the
public class Question_2
int z )function definition with arguments x, y andz
public static int largest( int x, int y,
IIvariable declaration
int large;
from x and y, and store in variable L
/finding below the largest
Iif (x>y) then variable L' stores * otherwise y
int L = ( x> y ) ? x : y
Inow finding below the largest irom prev1ous and z, and store in variable large
reurned: Get
hty
nt B6
Ok Cance Ooe
Problem 3
Write a program to define a method/function int ASCIL code( char ch), to return the ascii value ot
the parameter/argument ch. Define another method/function void result , to invoke the metho
ASCILcode( ) by passing the relevant value to the parameter/argument and print the AsC code.
Solution:
This program prints the ASCII code
public class Question3
public int ASCII codel char ch ) I/function or method definition with parameter 'ch'
Options
The ASCII code of G is =
71
olsd lbeit
Problem 4:
Write a program to create class
Measurement with a method/function
a
height containing height of a person in inches as the
) void ConvertHe fed
and inches and print the neght along parameter/argument, Convert the height 1
with
nches), Also, define a main method/function to
the converted
height in feet and inches (where 1 e
lass type and by callinginvoking the method voldassign
the height of a person. Create suitable o
ConvertHelght(.) print the result.
22
Classea and Method for Funeto
ogram converts height in inches into feet and inches, input using sCan
This
b l i c class Measuremement
nblic void ConvertHeight int height )/function definition with height as paraee
int feet = height/ 12; convert height into feet and store in variable 1eet
int inches = height % 12; "inches
VConvert height into inches and store in variable
lInow printing the results
Problem 5
Write a program to create a method/function void show( char st ), to print the original character in
e parameter 'st and next two characters. For example:ifst M then output will be M,'A'N, X otherwise
'a'
will be g h, i. It is assumed that the characters
are between or x.
-B g then the output
>olution:
characters and their conversions
nis program uses
ystem.out.println("Next character+Bt);
I inding the second next character arter previous st + 1. The rules
B+ 1 Itor finding varlous characters as same as mentloned above that use
/st++ o r s t l instead of stmst+1
next character 8t);
/tunetivem.out.println("Second
hd
Method (or Funetions) 128
Output:The following is a computer generated output after calling function sSh
(1) If parameter value st E' then output is: (ii) If parameter value st = th
then outpu
Bluek Teminal Window- d aBluel: Terminal Window-d
Opbons Ophons
original character =
j
original character E
Next character =k
Next character =F
Second next character
=1
Second next character =G
Problem 6:
Write a program to define a method/function void Swap_Numbers( int x, int y ), to swapinter
the integer parameters x and y, and print the interchanged numbers (TOr example: it x=12 and
after swapping'interchange x=9 and y=9 t
y=12).
Also define a method/function void Result( ), to assign two integers and print them as oin.
integers before swap/interchange. Create a suitable object/instance of the class and by callinginat
method Swap_numbers(..), swap and print the integers after interchange. /invok
Solution:
W This program performs swapping of numbers
class SwapNumbers
void Swap_Numbers( int x, I/method definition with parameters
int y) x,y
Output:
The numbers before interchange or swap are = 14 8
The numbers after interchange or swap are = 8 14
of square
aaot rogram calculates
area
WpThis
ublic class Square
of is = 243.35999999999999
The area square
Problem 8 :
Wnte a program to input the length and breadth in whole numbers. Print area of rectangle.
bolution:
This program calculates the area of rectangle
is + b r e d
stem.out.println("The breadth of rectangle
n . o u t . p r i n t l n ( " T h e area of rectangle is = + area )
)
ondofofthe
Wlend
the function rectangleArea(
class
laases nd
a
Methods (or Functions) 125
rectangle_Area()
18 invoked
method
when
following is the
output l0
function len
=
first argument
Assume that the value of
OUTPUT
is= 16
The length of rectangle
is 14
The breadth of rectangle 224.0
area of rectangle is =
The
Wnte a program to define a method/function long sumDigits() along with a long integeralsoararetun
Problem 9
number and alsn
o
tuwo the last digits the
the last and and s u m 18 52).
number and to print be s, 48
digit output will
accept four is : 3648, then
a
to (for example: if
the number four digt long integer number and
theirsum
void accept()
to initialize a Sum or the last and la
another function
with
Also define the results along
methodfunction sumDigits( ), print
the
invoking
digits
Solution:
public class Question_l5. num
f u n c t i o n definition
with tong parame
or
num) llmethod
public long sumDigits( long
variables
Wdeclaration of long integer type
long last_digit, last_two_digits; last digit of a
remainder (divide by 10) to get the
% 10; /finding the last two dig
last_digit = num
(divide by 100) to get
num "e 100; I/find remainder
last two_digits =
IPrinting results
System.out.printin( OUTPUT ";
+num
System.out.printin(" The number 18
System.out.println("The last digit =+last_digit
last digit =
last two_digits);
Sy'stem.out printint The ) ; l/returning sum
return (( last_digit
+
last two_digits)
Wlend of the function sumDigits(
or function
definition
void accept) Ila n e w method
Problem 10: n l sn
ctio
tloat avg
ldeclaration of fractional variable to store the average
)/3;
Vg=(mi + mz +m3 lcalculate of average marks
tem.out.println OUTPUT "); Printing results
System.out.printinRoll number : +1
Problem 111:
Wri a
program to define a
mistry, Biology and Computer Applications.
method/function void Scores( )toinput marks in English, Maths,
the marks obtained in all the subjects, total
Print
marke in each subject 1s 100).
(assume that the maximum marks
Percentage
Hint: eE a t the subject names are mentioned, so for each suoject tare a ariable of your choice.
Solution:
public class
Student into
the following function definition contains parameters to input marks in 'subjects
void Sco o a t eng, float mat, float phy, tloat che, tloat bio, float cta )
Parameter
input 81.0
window Harks in
English
88.5
Marks in Maths 79.5
Marks in phyBics
Marks an cheamis ErY
Problem 12:
Solution:
class Product Info
) I/method definition with formal argumens
public void CashMemol int pe, int qty, double pr
p
vold Cash Memo(int pc, Rnt qy, doubile pr)
OUTPUT
Product code 22113
product 106
prodct L.e 22113 nt pc QuantitY of
nt q y
Onit price of product 572. 75
Total cOst 60711.5
discount
arter
O Cancel ount 54336.792499999996
is=
7607.150949999999
paid
Oa
amount
to be
Problem 13:
Write a program to create a class Larger and overload method m a x i m u m ) as TOIOWS
nl and
int maximum( int nl, int n2) return the largest integer from the arguments
) to the suitable
mathematical method.
n2, using and
return the larger character
from the arguments xl,
(ii) char maximum( char xl, char x2 ) to
x2.
relevant
Also define a main) method to invoke the above overloaded methods/functions by pasSing
and print the results.
parameters/argument
Solution:
class the the method/function overloading concept
program uses
Larger in this class
note, that there is NO data members
definition
int maximum int nl, int n2 /1st overloaded function
Opuos
25 is =
24
The greater from 15 and
The greater character irom W and E is = W
friendly.
be defined with modifiers/access specifiers as, private or publie
The methods/functions of class can
t i o n a )
puD new o oDerator creates anobject of the specified class and returns a reference to
(2) Instan
he
contains tWO Wpes ot methods or functions as: (1) Class Methods and
he
class
Methods, methods/functions
A l l t h e
nctions defined using the
m e t h o d s / f u n c t i o
or class
o n d arguments
131
Claaaes and Methods (or Functions)