Week-2 Java
Week-2 Java
In-lab Task:
1. Given an integer n, return an array ans of lengthn+ 1 such that for each i (0<=i=n), ans[i]
is the number of 1's in the binary representation ofi.
Link:bit.ly/CTOOD-W2P
Class Diagram:
Program:
cos Soluim
pulblic int) CountBis(in n)
int a a enew intn+
int i
fli-o, ie=n; i+t)
ht ki;
int C0
whilelk o)
it 1.9 -)
Ctt
alij-c;
retuan o 33
2. Consider the below class diagram and develop program
P1
Demo
tmain(args:string:void
Program:
OHlity
packoge pa:
pulolic clas Uility
puldic static long. firdReverse (long n) Another ackage
int revo;
patkage p (DEM
while(n sb)
irnpoi jova.uil, *,
int r
(nt)n/- 1d; irnpoit pa. DisplayAl;
rev public clos's DemD
=(revt1o) +T; pulolie stahe void main(Shingt) ay)
nen/tD Scanner Se =neuo Seanner(ysem-in)
int n Se.next1nt);
retn sev; 3 DisplayAl. displayta indaomeNDs();
checka Displaynll. eiplayArmstrengNos ();
Se. close );
Pulblic elass checker
public otatic boolean ishnnstrong Cint r)
35
ORIENI
OBJECT
FOR
2 1 S C 1 2 0 3 - c O M P U T A T I O N A
THINKING
L
You are climbing a staircase. It takes n steps to reach the top. Each time you
can either climb 1
3.
or 2 steps. In how many distinct ways can you climb to the top?
LINK: bit.lv/CTOQD-W2PO2
clas Soution
it(n<o)
rebun 0
Pln- o)
Yerun I;
int val -c[n),
val! o)
retwn val;
val climlbStairs(n-)+clinbStairsln-a),
c[n]-val
retwn Val;
Outputi
na ))1, 2
1, 2-3
2,1
37 L
21SC1203-cOMP
methods isArmstrong)
with 2 static Armstrong numbers up to
. Write a class Amstrong
displayArmstrong)
to displays all . Aa
is Amstrong ornot and
main mcthod from
the same class.
them using ()
Class Diagram:
Program:
public
class Armehrana
public ptaic boolkan ssrmstrong
tnt umo, Y, temp;
temp n displaurtrmshrongb);
while (nl =o
Ten10,
Surn: Sum +r*r¥V;
Cutput
nen/10
ifuntemp) 153
tetun true; 340
ebe retusn faky? 331
pulle ataie void
int n;
dispayrhrmshenglint
h(ne1; n4=x; n1 t)
i (isArmsthrongtn) tru
Sophnn);
public static void
min(Strina ag)
nt X 500
38
21SC1203-cOMPUTATIONALTHINKING FOR OBJECT ORIENTED DESIGN
2. Write a class Palindrome with 3 static methods findReversc) which reverses a given number
n", isPalindrome) to check whether a given number ,n" is palindrome or not and
displayPalindromeNos) to displays all palindrome numbers up to ,n". Access them using main
0 of PalindromeDemo Class which is in the same package.
Class Diagram:
nenD
retan reV,
Public Ataic boolan ilalindrome
if hndReverse (n) (irt o)i
Yetun true;
= =
n)
clse retuon
fake; ? 39
THINKING FOR
OBJEL
21SC1203-COMPUTATiONAL
Skill Session
in hacker rank
1. Solve the following problems using java
LINK:bit.ly/CTOOD-W2SKILLI
Class Diagram:
Program:
innpo jaa.io.¥ publit void
impdt java-ufil r;
yearasest )
pubic clous Prson
private int 0ge pullic tatic void
moain(String] aM
Sconner se
public Persn Gint neo
Scanen( Sestom in)
Hioi tialge c0) initialAge) int T se
.nextTnt );
oge : 0 f Lint io, i<T, itt)
int
S.opin("qe is not valid, setting age St
nextInt();
age to o); } erson Pp=
elue
Personlaqd
PrarmTote s pyar
new
p.aml0d , Passes),
age =iniialAge; }z
public void am1old s.o-pln;
String an
ilage 13) Se.closeC,
ans "You are
young":f
ele if oqe> =13 22 oqe <=18)
an You are a
teen ager",t
else ans
You 0su old'
Sp-pln("an)
FOR OBJECT ORIENTED DESIGN
21SC1203-cOMPUTATIONAL THINKING
LINK:bit.ly/CTOOD-W2SKILL2
11]
Class Diagram:
Program:
class Coluion
pullic List e Liste Inteqer> generate (int oumRows)S
List înteger > row = new Arayliske >( ),
List ist<Integer >> es= neuo Araylist < >l
YoLO.dd );
t Sur= previos.qeti) +previou
Yes. add(oo)
ow odld Su) gerl+:
IPnumRowsr =l)
etuon ies rOLO addl);
Touo newArray List >); Yes.add (ou)
Youo.add C0;
row.addD; r t n res,
Te.odd (ros)
P (PumRows =2) =
Teruan Tes;
foh lint i , ic
Touw new
numlows, it+)S
AYraylist < >l);
LictTnteqer> previous Tesget (i-D; =
traince.
i-th ands
for the
number ai
such that
x divides ai,
and1dev
trainces. He
assigneda
anintegerx (122),
Hossam is very tir
is very
tired no
5.
Hossam has
n
if
successful there is successful pair of
trainees.
called
trainees is
to knowif
there is a
wants
Hossam
L i n k :bit.ly/CTOOD-WEEK2SKILLS
Class Diagram:
Program:
public clas Hoam
Publie Atalic boluan hindac (rt n, irk n)
int num: (nikna)?ni:n2; a) Se.rextnt();
if (nm)
Tetuon truue, 43lijea.length: jt t)g
f (ke1, rea. lmqi, kt t
a (ot i-ai <numair)
iP(n.i:o na1.i=-o) if(tindfacoCi), alt)--huo
retwn true; 3 f1:
retuan falie } S.o-plnyE s"),
public staic void irain(Shingt) an) break;
Stanner st =new Scanner (Systemio)
int tc,
tc se.nextintl); it( =o)
int ni,i,tro, So-pln( NO0)
Palizl, icek, it)i
hSc.nxtint);
int af] -heuo Int[n);
48(j2o;j<n:)jtt 44