Difference between revisions of "CSC212 Lab 2 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- = Problem 1: playing with ints and doubles= <br /> * Write a java program that contains 3 ints and displays the sum of the 3 ints * Modify your program so that it ...")
 
(If statements)
Line 58: Line 58:
  
 
<br />
 
<br />
=Problem 4:
+
=Problem 4: While loops=
 +
<br />
 +
* Redo the problems of

Revision as of 09:54, 9 September 2014

--D. Thiebaut (talk) 10:53, 9 September 2014 (EDT)


Problem 1: playing with ints and doubles


  • Write a java program that contains 3 ints and displays the sum of the 3 ints
  • Modify your program so that it creates 3 doubles and display their products


Problem 2: Strings


  • Write a java program with a function called printBox(). The function receives a string as an argument, and displays the string between 2 lines of stars. For example:


String name = "Smith College";

printBox( name );


will print something like this:
*********************************
Smith College
*********************************

Note that the length of the lines of stars is fixed.


Problem 3: Loops


  • Write a Java program that displays all the integers from -10 to +10, one per line.
  • Write a Java program that displays all the positive multiples of 3 less than 30, starting with 3.
  • Write a Java program that displays all the positive multiples of 3 less than 30, starting with 0.
  • Write a Java program that displays all the negative multiples of 5 greater than -40.
  • Write a Java program that displays all the powers of 2 less than a million, starting with 2.
  • Write a Java program that prints the first 10 positive multiples of 3 and of 5, two per line, as shown below:
 3 5
 6 10
 9 15
 ...


If statements


  • Write a Java program that prints only the multiples of 3, 5 and 7 that are less than 500. To find if a variable n is divisible by 3, for example, you can do this:


if ( n % 3 == 0 ) {
     // n is a multiple of 3
}
else {
     // n is not a multiple of 3
}



Problem 4: While loops


  • Redo the problems of