Difference between revisions of "CSC212 Lab 2 2014"
(→If statements) |
(→Problem 4: If statements) |
||
(17 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:53, 9 September 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 10:53, 9 September 2014 (EDT) | ||
---- | ---- | ||
+ | __TOC__ | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <bluebox>This lab is to be done in class, or outside class if you haven't finished it in class. It is concurrent with the lecture presenting the basic Java concepts (except classes and objects). | ||
+ | <br /> | ||
+ | You are to submit the solution program for the problem asking you to create a function that will print a string inside a box of stars. | ||
+ | </bluebox> | ||
+ | <br /> | ||
+ | <br /> | ||
= Problem 1: playing with ints and doubles= | = Problem 1: playing with ints and doubles= | ||
<br /> | <br /> | ||
− | * Write a | + | * 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 | + | * Modify your program so that it creates 3 doubles and displays their products. |
<br /> | <br /> | ||
+ | |||
=Problem 2: Strings= | =Problem 2: Strings= | ||
<br /> | <br /> | ||
Line 40: | Line 50: | ||
... | ... | ||
+ | * Write a Java program where the function '''printBox()''' seen before, actually puts a box around the string it prints. In order to get the length of the string received, the function can do this: | ||
<br /> | <br /> | ||
− | = If statements= | + | <source lang="java"> |
+ | |||
+ | int n = name.length(); // name is the string | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | :Also, if you use '''System.out.print()''' instead of '''System.out.println()''', you can put many strings/chars on the same line without going to the next line... | ||
+ | : Make your program create a string containing "Smith College" and have the function print it as shown below. Note that the function prints a line of *-characters that is 4 chars longer than the number of chars in the string: | ||
+ | |||
+ | ***************** | ||
+ | * Smith College * | ||
+ | ***************** | ||
+ | |||
+ | <br /> | ||
+ | <tanbox>Submit your solution program to Moodle (see last section of this lab). | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | |||
+ | =Problem 4: If statements= | ||
<br /> | <br /> | ||
* 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: | * 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: | ||
Line 55: | Line 84: | ||
</source> | </source> | ||
+ | : Hints: The first 2 numbers of the output are 0 and 105... | ||
+ | * Write a java program that contains a function called ''min3( int a, int b, int c )'' that returns the smallest of 3 integers. Test it on 3 different variables. | ||
+ | * Modify the same Java program, and add a new function called ''min3( double a, double b, double c )'' that returns the smallest of 3 doubles. Verify that the java compiler does not complain about two functions/methods with the same name. As long as their list of parameters are different, Java is fine with it. This is what ''overloading'' is about. | ||
+ | <br /> | ||
+ | =Problem 5: While loops= | ||
+ | <br /> | ||
+ | * Redo the questions of Problem 3 above, but using while loops only this time. | ||
+ | <br /> | ||
+ | =Submission to Moodle= | ||
+ | <br /> | ||
+ | * Submit the Java program that prints a boxed string to Moodle. | ||
+ | * Make sure you call the class for this program '''Lab2''', and save it a file called '''Lab2.java'''. | ||
+ | * Make sure your program uses the string "Smith College", and passes it to the function '''boxPrint()'''. The function should print this: | ||
+ | <br /> | ||
+ | |||
+ | ***************** | ||
+ | * Smith College * | ||
+ | ***************** | ||
+ | |||
+ | <br /> | ||
+ | * You can follow the directions for submitting this program for automatic grading on Moodle [[Submitting Java Programs to Moodle| here]]. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
− | |||
<br /> | <br /> | ||
− | + | [[Category:CSC212]][[Category:Java]] |
Latest revision as of 14:34, 10 September 2014
--D. Thiebaut (talk) 10:53, 9 September 2014 (EDT)
Contents
This lab is to be done in class, or outside class if you haven't finished it in class. It is concurrent with the lecture presenting the basic Java concepts (except classes and objects).
You are to submit the solution program for the problem asking you to create a function that will print a string inside a box of stars.
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 displays 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 ...
- Write a Java program where the function printBox() seen before, actually puts a box around the string it prints. In order to get the length of the string received, the function can do this:
int n = name.length(); // name is the string
- Also, if you use System.out.print() instead of System.out.println(), you can put many strings/chars on the same line without going to the next line...
- Make your program create a string containing "Smith College" and have the function print it as shown below. Note that the function prints a line of *-characters that is 4 chars longer than the number of chars in the string:
***************** * Smith College * *****************
Submit your solution program to Moodle (see last section of this lab).
Problem 4: 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 }
- Hints: The first 2 numbers of the output are 0 and 105...
- Write a java program that contains a function called min3( int a, int b, int c ) that returns the smallest of 3 integers. Test it on 3 different variables.
- Modify the same Java program, and add a new function called min3( double a, double b, double c ) that returns the smallest of 3 doubles. Verify that the java compiler does not complain about two functions/methods with the same name. As long as their list of parameters are different, Java is fine with it. This is what overloading is about.
Problem 5: While loops
- Redo the questions of Problem 3 above, but using while loops only this time.
Submission to Moodle
- Submit the Java program that prints a boxed string to Moodle.
- Make sure you call the class for this program Lab2, and save it a file called Lab2.java.
- Make sure your program uses the string "Smith College", and passes it to the function boxPrint(). The function should print this:
***************** * Smith College * *****************
- You can follow the directions for submitting this program for automatic grading on Moodle here.