0% found this document useful (0 votes)
4 views9 pages

Week-2 Java

The document outlines a series of programming tasks related to computational thinking and object-oriented design, including methods for counting binary ones, checking Armstrong numbers, and determining distinct ways to climb stairs. It also includes class diagrams and sample code for implementing these tasks in Java. Additionally, it provides links to further resources and skill sessions for practice.

Uploaded by

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

Week-2 Java

The document outlines a series of programming tasks related to computational thinking and object-oriented design, including methods for counting binary ones, checking Armstrong numbers, and determining distinct ways to climb stairs. It also includes class diagrams and sample code for implementing these tasks in Java. Additionally, it provides links to further resources and skill sessions for practice.

Uploaded by

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

21SC1203-cOMPUTATIONALTHINKING FOR OBJECT ORIENTED DESIGN

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

Utility Checker DisplayAl

+findReversein:long):long isAmstrong(n:int):boolean displayPalindromeNOs(n:long):void


tisPalindrometn:long):boolean +displayArmstrong(n.int):void

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

int digik (int)Mah.loq1oln)+};


Int sum 0, nuM=N;
while(n! o){
int ren/. 10;
Su Sum +(Int)Math.pow (,diqis);
han/o;
I f(rum = Sum)
tetn rue;
else retun aue; ?
pubic static bookan
isalindoome (long n)5
iPUlhy.Findeversetn)e
Yetuan true;
=n)
else Tetuan toase,
DisplayAll
public clos DisplayAlN
public atatic veid
dispaylalindsome ND:(Jong
System. ou t.print In Plindone Numbers! ;
P Cint i l; i<en; it++) S
t Cchecker. islalind some i)==
rue)
Systetr. ou t.printlp li +
public stoatic void
System.out-print In("InArmstrong Num bers cisplaymstrongNOs int
faint it; iz=n; "),
i-+t)
iFCCheeke in Amstrong u)=c
Systern. outprintin (i+" tru)
21SC1203-cOMPUTATIONALTHINKING FOR OBJECT ORIENTED DESIGN

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

intt c=neuo int{46


public int elimbstairs[int r

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

ost-lab Task to check whether a given


umber
.

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:

Program public shaic void displas


public clas palindbomeDern o
pulic stahe void man (StingC ) og){ Palindromelint )
int X f(int n =1;n<ax; n+ )

Palindrome r dispayPalinchome (); ? iPCisPalindrome (n)- =true)


clos palindrome S-0.pln(n)D
public Ataic int findReverslint n)
int rev s0,
while(n! 0) 3
ranT-1dD;
rev Yev¥ 1D+Y;

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

Pascal's triangle In Pascal's triangle,


return the first numRows of
2. Given an integer numRows, numbers directly below it as shown:
each number is the sum of the two

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; =

Too oolol (1)


(nt j-o;,i<
previous.sizel) -1j+) 41
21SCLz
A pairofthe i-th ang
i.u

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

for your help!


asks you

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

You might also like