Download Exercise 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ECCE/Exercises 2
Software Development
Loops and Arithmetics
Part A
1. Analyze the structure of following Java program “Comparison.java”.
2. Run the program and predict the outcome in your logbook.
// Comparison.java
// Compare integers using if statements, relational operators
// and equality operators.
import java.util.Scanner; // program uses class Scanner
public class Comparison
{
// main method begins execution of Java application
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
int number1; // first number to compare
int number2; // second number to compare
System.out.print( "Enter first integer: " ); // prompt
number1 = input.nextInt(); // read first number from user
System.out.print( "Enter second integer: " ); // prompt
number2 = input.nextInt(); // read second number from user
if ( number1 == number2 )
System.out.printf( "%d == %d\n", number1, number2 );
if ( number1 != number2 )
System.out.printf( "%d != %d\n", number1, number2 );
if ( number1 < number2 )
System.out.printf( "%d < %d\n", number1, number2 );
if ( number1 > number2 )
System.out.printf( "%d > %d\n", number1, number2 );
if ( number1 <= number2 )
System.out.printf( "%d <= %d\n", number1, number2 );
if ( number1 >= number2 )
System.out.printf( "%d >= %d\n", number1, number2 );
} // end method main
} // end class Comparison
Part B
3. Analyze the structure of following Java program “Sum1.java”.
4. Run the program and predict the outcome in your logbook.
Updated 30/09/05
1
ECCE/Exercises 2
Software Development
___________________________________________________________
class Sum1
public
int
sum
num
for
{
static void main(String[] args) {
i, num, sum;
= 0;
= Integer.parseInt( args[0] );
(i=1;i<=num;i++){
sum += i;
}
System.out.println("The sum is " + sum);
}
}
__________________________________________________________
5. Create and compile the above program.
6. Type “java Sum1 20” to run the program and compare with your predicted
results.
7. Calculate the sum of 1,2,3,….100, then use the Sum1.java program to check
your result.
Part C
1. Analyze the structure of following Java program “Display1.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
class Display1 {
public static void main(String[] args) {
String star = "*";
int i,j, count;
count = 5;
do {
System.out.println("==========");
count -= 1;
} while (count >0);
for (i=0;i<3;i++){
for (j=0;j<5;j++){
System.out.print(star);
}
System.out.println("=====");
}
}
}
__________________________________________________________
3. Create and compile the above program.
4. Type “java Display1” to run the program and compare with your predicted
results.
5. Modify the program so that it produces an upside-down triangle shape with the
“*” sign.
Part D
1. Analyze the structure of following Java program “Sum2.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
class Sum2 {
public static void main(String[] args) {
int i, num, sum;
Updated 30/09/05
2
ECCE/Exercises 2
Software Development
i=0;
sum = 0;
while (i<args.length){
num = Integer.parseInt( args[i] );
sum += num;
i++;
}
System.out.println("The sum is " + sum);
}
}
__________________________________________________________
3. Create and compile the program.
4. Type “java Sum2 3 5 9 11” to run the program and compare with your
predicted results, write down your comments.
Part E
1. Analyze the structure of following Java program “Max1.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
class Max1 {
public static void main(String[] args) {
int i, num, temp;
temp = Integer.parseInt( args[0] );
for (i=1;i<args.length;i++){
num = Integer.parseInt( args[i] );
if (temp < num) {
temp = num;
}
}
System.out.println(temp);
}
}
3.
4.
5.
6.
7.
8.
__________________________________________________________
Create and compile the above program.
Type “java Max1 2 34 16 28 9 11” to run the program and compare with
your predicted results.
What would happen if you change the line “if (temp < num)” to “if
(temp > num)”?
Modify the program so that it can find and display both the maximum and
minimum values of a series of integers.
If you just type “java Max1” to run the program, what would happen?
Modify the program so that if you don’t type in any command line parameters,
it will produce an error message and exit, if you do, it will find out the
maximum value of the integer series.
Questions:
1.
2.
3.
4.
5.
What is the life cycle of writing a software program?
What are the primitive types of variables?
What are the rules for naming a variables in Java?
Write down the syntax for if….else, switch, and other conditional statements.
Write an application that displays a box, an oval, an arrow and a diamond
using asterisk (*) [see Part C].
Updated 30/09/05
3