Sie sind auf Seite 1von 5

New Era University College of Engineering and Technology Department of Computer Science

CS 132L Introduction to Programming (Laboratory)

ASSIGNMENT Control Structures Looping Statement

Name: ESPERANZA, JEREMIAS C. Date Submitted: Sept. 7, 2011

Rating:

MR JEREMIAS C. ESPERANZA Instructor

DIRECTION: 1. 2. 3. 4. 5.

All solutions should be HANDWRITTEN. Use extra SHORT BOND PAPERS (ONLY) if necessary. Incomplete assignment or solutions receives 0 mark AUTOMATICALLY. Write legibly; dirty assignment receives 30 marks less. Staple your work.

I. PROGRAM SIMULATION: Determine the output. (80 marks)


public class Exercise_1 { public static void main (String[] args) { int n=0, remainder = 0; n=1; while (n<=5) { remainder = n%2; if (remainder == 0) { System.out.println( n +" is an even number" ); } else { System.out.println( n + " is an odd number"); } n++; } } } Accumulator/Counter:

______________________________________________________ Output:

public class Exercise_2 { public static void main(String[] args) { int _ctr=0; do { System.out.print ("*"); _ctr++; } while ( _ctr<5); } }

Accumulator/Counter:

______________________________________________________ Output:

Page 2 of 5

public class Exercise_3 { public static void main(String[] args) { int year=0; double $my_investment=1000.0, interest=0.0, $money=0.0; double total_money=0.0; // Printing of the Headers System.out.print("Year\t\t"); System.out.print("Amount\t\t"); System.out.print("Interest\t"); System.out.print("Year-end Money"); // Place the cursor to the nextline System.out.println(); // Let the game begin for (year=1; year<=5; ++year) { interest = $my_investment * 0.1; $money = $my_investment+ interest; total_money += $money; // Printing of Details System.out.print(year+"\t\t"); System.out.print($my_investment+"\t\t"); System.out.print(interest+"\t\t"+$money); // Cursor to proceed on the nextline System.out.println(); $my_investment=$money; } //Skip two lines System.out.print("\n\n"); System.out.println("\t\t\t\tTotal\t\t"+total_money); } }

Accumulator/Counter:

____________________________________________________________ Output:

public class Exercise_4 { public static void main(String[] args) { int x,y; x=1; while (x<=3) { y=3; while (y>0) { System.out.print("$"); --y; } System.out.println(); x++; } } }

Accumulator/Counter:

_____________________________________________________________ Output:

Page 3 of 5

public class Exercise_5 { public static void main(String[] args) { int a, c; for( a=5; a>=1; a-=1) { if (a<5) { c=4; while (c>=a) { System.out.print(" "); c=c-1; } } System.out.println("&"); } } } public class Exercise_6 { public static void main(String[] args) { int theNumber=23100, factor=1; System.out.print("Factors of "+theNumber+" are "); do { if (factor!=1) { if (theNumber%factor==0) { theNumber=theNumber/factor; System.out.print(factor+" "); } if (theNumber%factor!=0) { factor++; } if (factor>theNumber) factor=0; } else { factor++; } } while (factor!=0); } }

Accumulator/Counter:

__________________________________________________________ Output:

Accumulator/Counter:

__________________________________________________________ Output:

Page 4 of 5

public class Exercise_7 { public static void main (String args[]) { int x,y,z; for (x=11; x>=1; x-=1) { if ((x % 2)!=0) { y=11; while( y>x ) { System.out.print (" "); // one space is // inside the double quotes y-=2; } z=1; do { System.out.print("@"); ++z; } while (z<=x); System.out.println(); } } } } public class Exercise_8 { public static void main (String args[]) { int testNum, counter; String message=""; boolean divisible_Ako = false; testNum=10; while (testNum>=1) { counter=1; while (counter<=10) { divisible_Ako = (testNum%counter == 0); switch(counter) { case 1: message = "one"; break; case 2: message = "two"; break; case 3: message = "three"; break; case 4: message = "four"; break; case 5: message = "five"; break; case 6: message = "six"; break; case 7: message = "seven"; break; case 8: message = "eight"; break; case 9: message = "nine"; break; case 10: message = "ten"; break; default: if (counter == testNum) message ="itself"; break; }

Accumulator/Counter:

_______________________________________________ Output:

Accumulator/Counter:

____________________________________ Output:

if (divisible_Ako) { System.out.println(testNum+" is divisible by " + message); } counter++; } testNum--; } } }

Page 5 of 5

Das könnte Ihnen auch gefallen