0 ratings0% found this document useful (0 votes) 9 views18 pagesFinal ProgCodes
java questions and answers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
tea net uno tat mds asttfinogescee tana pins ean of eV.
Telos ene ate ise,
Hn ide anomie sn fy a these Haste ers eg hen
Fenians:
+ ou mstonner yur ce apap
+ urcodemst tees eiaetaspssle
+ Alumter at tobe sto corsa.
ser eat eg, 10 23, 408)
7
2] seater san = ry Searer(stenin);
3) dita; jfariate the else
| int mms ety wriate
5
a] a
7 | sysenavprrta rer a unter (@ to auit)")5
| msc); ting te iru fon erty i inside aril an
4
ae] may
u res
x]:
Ba] i naan
* n=
s| 3
wl y
| ase,
System pitta fever nbs ic “my
1 [ineite a JAVA metho dey tht eas users ful name an stores it as one sting. The mathe shel then perform te fling
+ coments our amet a uppereas eters and pts it out othe screen
+ converts you nat al fowerase tas and prints ou tothe screen,
+ insta lngth ofthe name and pints it out to the seven
Hi: You nay assume the Scanner cassis doady imported for you.
Forevamele:
Test pat Resa
eer) A ne Re ete ll
Te lag of yr
“Answer: (penalty opie: 0, 10,20, 3,40)
usie static void Seantty() (
11 wre your coe Far the meth here
Scatnar scan ~ ne Scaner( System)
String nane = scen.nextLin();
Systen.out.prinen("Plzase enter your fullnae: *);
systenout printn( "Your nae in upercase Is: “sranetotppercese()}:
systonout printnYour nae In lovercase 1s: "onan tolowerase()}:
nt Length = nae. length);
‘ystenout printin("The length of your nane is: “slangth);
¥Hit: You may assure the Seanner cassis areadymporedfer you Make sures cere an use a constant fr soragthe vue
20.
Forecample:
mat
Answer: (erat regine:0,1, 2, 30,40)
_Jpblie stati veld passuordchecker()
Fine Strine poesvord ~ "CORICT";
Seamer scan = new Seamer( Syste}
systen out gelatin "Please enter passiard
String utePasmieré-scon.ext()s
if (4serDasvordeqale("ORECT))
‘
Systan.o
riat("You have geised access to the syste");
Systen.cetprintla("our pasuord is Sncoreet!");ire mathed findFrstint{] vector nt value) tna tums the inx of te st occurence of valuein vactarar the 1 atherise.
eguiemants
* You must comment your code appropriately
+ Your code mst be as efficent as possible
+ AllnumberReras need tobe sets constant
Forexamy
Test Fes
Sytem egitim C18, 28054038, 30), 2
re a pinion C8308, | +
syste. arrest 08,209,508, 3, 62)3
‘Answer: (penalty regime: 0,10, 20,30, 40)
/Returns the index to the Finst occurrence of value in vector
1 Feturns -1 if there is no such ocurrence
int 2-05
nt occurrence = © 5
boolean exit - Flee j
ubSle(lodt Mt Avector Length)
(value — vector[i)
F occurrence = 15
exit tev 5
else i1(fLoccurrence 2 88 i == vector.length-1}{
Foceumence = lj
eturm occurrence;‘tes method daysToSeconds that reads rom user the nun of day in). The method shoud then comer the numberof dys to seconds end
isla the esl oth usr, Make ur user inputs vay postive, Jar consis of A400 eaconds,
Requirements:
+ Yous comment your code appropritly
+ Your code must bea ficient s possible
+ Alnumber tras reed tobe set as constant
Note: You can assume that Scanner lass is ead inorted fr you,
Forexample:
eat opt osu
erent) Ete name
syria); © Sterne
ula tat
Answer: (pent regime: 0,10, 20,30, 40%)
public static void daysTeSecands() (
i Write your coe foc the nethad here
Systen.et.println( "Enter ruber of days: “Ys
Scamer scan = nee Searer (Syste. in);
nt mun = scannevtIe()s
(oun)
Ssystenovtprintlnoun + "days emsists of * + sec +" second!"
}
ese {
systen.out.printin( "Invalid input");
}ite method (occurences tht is the numberof ocuences of th infeger specie ste frst parameterin the ary of integers that is
specified asthe second parameter,
Forexample:
Tet
et) mes = 2845; i we
Syste at. rntinie enrs careers ts i "aera. gates)
ng) ames = (453405 tm,
Syseua rine crs “ocorecs bers tes i “eras. trieate)
nk) = AAA, iat
car cect)
raps. striae)
Answer pera ee: 0,10, 20,30, 40%)
s[public static int occurences(int value, int[] vector) {
{start
Jf sie loop
vhile(lerit 88 devector length)
sf(value = vector[i] ){
Foccurnence = 1s
return occurrences
1 |iferd
est
ces 1 ti a 23 8,51
cs 2 1 5,3, 61
tere Sto 41,4, 411 Gay
1 “Systenoct print ‘ncareect int
Y
for (int Set;deniie iHfor low
© system sriet1 9s
| Fe Git Fi Fife
| astenott ont 95
oo
| Systenct pintin( 5
+
oe (lot Eesboas—AN¢ 140° toe
1 stent print 1 "5
foe tine SoLidtea
Sytner” Ye
2
Systanoat-platin(-+eres mata id age Sart tar tops nce cepaanae acre tz ogee schon maT,
segue:
+ Yumstcomenyar app
+ Yarsesomattesrotson sone
Note: Mote ae te parameter tars proves postive number
Fereanole:
rawr rat eg, 3403)
SpE sac vets eacageasts(t FSSE
ce fo aid ost
eas cay
‘steed. pitlncrect ep);
eum
sts dinwelty
fe (eS 8c
sora joa ca 0
Sater eit)
:
Ii grant tes ne 2 ee
syst. sltine 5
fe littse trae a) soy
systevad. ain"
>
patent. pit" 5
soe sae ioe ease
for (it estes = bm AIL
Visas se
fe Gat = 85
ww) ase
a systen.ot.grintn("Pleae enter a valid year of Bsth);
n| }ite JAVA method ina that reas nthe users est name and lastname and stores s oe String, The method sha then perform the
folowing
+ Pint outers othe screen st characte of test ame ang win the fest cera ofthe ast ran).
«tract user family name and print tthe screen.
Hint You may assume the Scanner cassis areal imported or you
Forexample:
Test inp | Resut
‘niin | at ze ree ere yur eae
‘Answer: (penalty regime: 0,10, 20,30, 40%)
s]oublic static void initials() {
I site your code far the athod hare
Scanner scan ~ new Scemer(Systen.in);
String fullname;
Systen.out.println(*Please enter your fulloa
fullnane = scan.nextLine();
10| char Firstinitiel
ui | char lastinitial - oanes[nanes length - 1]-char(0);
2
13 | Sjstenout.printla("Your initials are: "+ firstinitial+ '." slastinitial );
14] Syston.out.println("Your fanily nane is: *snanes[naneslength -1});
5
16)/)]Witea meted insert that ales 2 parameters:
1. ainteer aay sorted in ascending der
2 ineger wae
‘Themathd shuld then eta he inex if he vas ound in he ara. ot, return the index were the vabie shoul be insertd in the aay. For
‘ample, forthe parameter aay (23457 and va 6, your method shoud rtum index 42s 8 wou be inserted after the valu 5 and befor the
value 7in the ary i, atthe thine
Note ay such uestos shouldbe soed with theleast runtime. Hower to mae things xsi, edo not onside his Moreover, youd
teed ose the vate inthe ray jst return the inex where th value would elnsred
Fer example
Test Rest
an) OHS = 58915 fi
it ner = febartoonte, $j
staat git;
inf)ames -snameialy [7
int er «fuses, 2);
site rites
bs :
fit be Seeberteioete,
staan ruininn;
od 1
stm Scere, 2)
teat istinnn
‘Answer: (penalty regime 01,2, 30, 40%)
Tp[public static int Finnsereider(int[] numbers, int value] { [/netiod
2] int a8
ube nrumbars. Length)
(oubers[npvalua( //3F
return 95 Hearn
[ss
4
5
6 }
8
9 | return runbers. Lengthy Hete
«+ Yourst commen your cade sppropdtely
«Your code mus be as efcent as possible
+ A umber Heras need to be st as constant
Fer example:
Test feu
2 our cote For the method
rf sjstenot.rintla("Tnvali
putSas SS]
er
oe
Gale Soa eae
esha: ation
seat —
cepa, hae
sete ran
sess ie
spa bey
cei a
ase (eat ie 0,1, 20 3,408)
pie static seit) attr] res, Sng a) Jett
bone faa - Sle
vee (res egy e
f
4 (nd = false one eplsignreaecmesné))
4
08-20
nse] = ah
Syste. itn + "as delete");
)
4 (om
4
inst) = neste +15
?
nde
ve i css Legth = 2)
}
1 aed =tn2)
{
raves length 1 = lls
systno rit pe 9"
soca
She (bie aes angi raeng
{
Sister. ita);
ates
»?
cue
‘
‘stodpinti|rne +" as wc fae);
fete re
HERES RE SRG ROR RY SEER GEER ES een eweWe a mathos cal caleulateciele tat ead the rads of a cee ana pints ts area (A= WR? and its cicumfaence (2 mR) Dsoa both resu
vith 3 decina paces,
Hin: Yournay assume the Scanner clessisaeady imported fr you.
Foreample:
Test Input Rest
Coumacirar)|1s [ree ae oe as of me draw:
Ine erence te arden 5
he piso te cue Ta
‘Answer: (penalty regime: , 10, 20, 30, 40°)
Ts public static void caleuateCirele() {
2 1 Create a Scanner object to resd input
3 Scannar scanner = nay Scannar(Systex in)
4
5 J Prope the usar to enter the radius of the cincle
6 sjstenut.println("Please enter the radius of the circle
1 ube radius = scamer.nextDouble();
3
9 J} Cleulate the anea and circunferece
® ube circumference = 2 * Wath.PL * radius;
a ube ares = lsth.PL © radius © radius;
2
B J rin the results vith 3 decimal places
u Sjstenut.printf("he circaference of the cinle is: 8.3\n", circaference);
B systenout.printf("The area of the circle is: 4.3fn", area);
6
a 11 Close the Scanner object
scanner. close)‘camer scan = ree searmer(Systen.{n);
Systenut print "Enter size:
rt size - scan.nertit(s
(size > 0) {
for (inti ats Lestze ; te){ //for
for (ant $45 fe Ay He
systenout.print(™")s
+
systen.ot.prbnea(">"
+
for (ln A ostze; bots 1-)f /Hforite metodHongestShing hl lesa prone an ayo sings andes the best sting nthe ra.
Hin: Reena ta txLengn eu he eng ofthe sing tat
Tat esat
Siri] rs «(Pig oe’ nae gst sig is ne
gra. secs“ gH);
Stig] es « Pre) get rig ea
‘esr: “pegs;
ag rai ea | ue rigs cage
steal iesttnis: "zesreers
Ane: (era aime, 10,20, 30,40)
[pple state Seng Lngsttring String] sera
2] serig lest = amafe
3s) for(et 1a; aay. for Lop
4 ‘if(longest.Jength() < array[i].length()){ //if statenent
5 longest = arcay[i];
6) 3
7] >
| ceturn longest;
9 fivient
You might also like
JavaAssignment (1,2,3,4,5,6,7,9)
JavaAssignment (1,2,3,4,5,6,7,9)
84 pages