Solution

advertisement
Tutorial 5 - Answers
1.Identify which of the following are invalid and which are reserved words in Java.
int
74ElmStreet
L$&%#
boolean
Boolean
_number
INT
public
Private
Joe
j1
k*2
Valid
‡
Reserved
‡
INVALID - variable names can't
begin with a digit
INVALID - variable names can't
include special characters (%#&)
‡
‡
‡
‡
‡
‡
‡
‡
‡
‡
INVALID - variable names can't
include special characters (*)
2.Let p, q, r be declared to be int. Write a single Java statements which do the following:
for you:
Part a) is done
a) if p and q are divisible by r, print out p/r and q/r
Answer
if ((p % r == 0) && (q % r == 0) )
System.out.println( p/r + " " + q/r);
b) if p is less than q and q is less then r, print out p
if ((p < q) && (q < r))
System.out.println (p);
c) if p is odd, or: if q is odd and r is even, then print p and r
if ((p%2 != 0) || ((q % 2 != 0) && (r % 2 == 0)))
System.out.println(p + " " + r);
d) if p is equal to q and neither r = p nor r = q holds, print r
if ((p==q) && !((r==p) || (r==q)))
System.out.println(r);
OR
if ((p == q) && ((r!=p) || (r!=p))
System.out.println(r);
3.For the following exercises, indicate the output that will be produced. Assume the following declarations
are made just before each exercise. That is, assume these initializations are in effect at the beginning of
each problem.
final int MIN=10, MAX=20;
int num = 15;
Output
for (int i=1; i<10; i++)
System.out.println(i);
1
2
3
4
5
6
7
8
9
for (int i=10; i>10; i--)
System.out.println(i);
No output (i=10 and 10 is not greater than 10)
for (int i=1; i<10; i=i*2)
System.out.println(i);
1
2
4
8
4.Write a program that asks for a integer number n (make sure the number entered is greater than
one). The program should then read in n numbers, compute the sum of those numbers and print
the sum. Write this program using a while loop, a do-while loop and a for loop.
For example: Enter a Number: 3 (user enters 3)
(Then ask the user to enter in 3 numbers)
Number: 3
Number: 4
Number: 1
The output would be (3+4+1)=8
Use a While Loop
/* Tutorial 5 - csci1100-1201
This program reads in a int "n" from the screen for "n" times requests the user
to enter
in "n" other ints. It prints the sum of these inputted integers using a
WHILE LOOP
*/
import java.io.*;
public class SumWhile
{
public static void main (String args[]) throws IOException
{
String readIn; //read in text from user
int count=0, sum=0, num;
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
//get the the total number of ints to be entered by the user
System.out.print("Enter a total number: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
//check that the number entered is greater than 1
while (count<=1)
{
System.out.println("Incorrect value - it must be greater than one");
System.out.print("Try again: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
} //end while
//Read in "count" number of ints to be summed together
int i=0; //keep track of times through the loop
while (i<count)
{ //while i is less than the number count do...
System.out.print("Enter a number to add: ");
readIn = input.readLine();
num = new Integer(readIn).intValue();
sum = sum + num; //add all the inputted numbers together
i++; //increment i
}//end while
System.out.println ("The total sum of all numbers is: " + sum);
}//end main
}//end class
Use a Do-While Loop
/* Tutorial 5 - csci1100-1201
This program reads in a int "n" from the screen for "n" times requests the user to enter
in "n" other ints. It prints the sum of these inputted integers using a
DO-WHILE LOOP
*/
import java.io.*;
public class SumDoWhile
{
public static void main (String args[]) throws IOException
{
String readIn; //read in text from user
int count=0, sum=0, num;
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
//get the the total number of ints to be entered by the user
System.out.print("Enter a total number: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
//check that the number entered is greater than 1
while (count<=1)
{
System.out.println("Incorrect value - it must be greater than one");
System.out.print("Try again: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
} //end while
//Read in "count" number of ints to be summed together
int i=0; //keep track of times through the loop
do
{ //do at least once
System.out.print("Enter a number to add: ");
readIn = input.readLine();
num = new Integer(readIn).intValue();
sum = sum + num; //add all the inputted numbers together
i++; //increment i
} while (i<count);// while i is less than the number count do...
System.out.println ("The total sum of all numbers is: " + sum);
}//end main
}//end class
Use a For Loop
/* Tutorial 5 - csci1100-1201
This program reads in a int "n" from the screen for "n" times requests the user
to enter
in "n" other ints. It prints the sum of these inputted integers using a
FOR LOOP
*/
import java.io.*;
public class SumFor
{
public static void main (String args[]) throws IOException
{
String readIn; //read in text from user
int count=0, sum=0, num;
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
//get the the total number of ints to be entered by the user
System.out.print("Enter a total number: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
//check that the number entered is greater than 1
while (count<=1)
{
System.out.println("Incorrect value - it must be greater than one");
System.out.print("Try again: ");
readIn = input.readLine();
count = new Integer(readIn).intValue();
} //end while
//Read in "count" number of ints to be summed together
for (int i=0; i<count; i++)
{
System.out.print("Enter a number to add: ");
readIn = input.readLine();
num = new Integer(readIn).intValue();
sum = sum + num; //add all the inputted numbers together
} //end for
System.out.println ("The total sum of all numbers is: " + sum);
}//end main
}//end class
Download