You are on page 1of 5

Computer Programming

5.1) What happens in the MinOfThree program if two or more of the values are equal? If exactly
two of the values are equal, does it matter whether the equal values are lower or higher than the
third?

The program still prints the lowest value. Because only less than comparisons are made, the
comparison of two equal values produces a false result. If two values are equal, and lower than
the third value, then one of the two lower but equal values is printed. If all three values are equal,
then this value is printed. Which “version” of the equal value is irrelevant. The correct result is
determined in either case. If the two equal values are lower than the third, then one of the two
lower but equal values is printed. If the two equal values are higher than the third, then the third
value is printed.

5.2) What is wrong with the following code fragment? Rewrite it so that it produces correct
output.

The else clause is associated with the line that is immediately preceding “if” rather than the first
if. The program will produce the correct output if it is rewritten as:

if (total == MAX)
{
if (total < sum)
System.out.println (“total == MAX and is < sum.”);
}
else
System.out.println (“total is not equal to MAX”);

5.3) What is wrong with the following code fragments? Will this code compile if it is part of an
otherwise valid program? Explain.

The assignment operator (=) is used erroneously in place of the equality operator (==). Hence, it
will not compile in an otherwise valid program.

5.4) The output produced is:


One is printed!
Two is printed!
Three is printed!

(The second println statement is improperly indented.)


5.5) The output is:

One is printed!

Two is printed!

Three is printed!

5.6) The strings in lexicographic order:


""
"******"
"12345"
"6789"
";+<?"
"?-?-?-?"
"Ethel"
"HEFFALUMP"
"Lucy"
"^^^^^^^^^^"
"book"
"bookkeeper"
"fred"
"hephalump"
"ricky"
"{ ( [ ] ) }"

5.7) The output is:


true

5.8) The output is:


8
16
32
64
128

5.9) On the line “count = 50;”, there is no “int” before the “count”, thus the type of variable is
not declared.
5.10)
int count = 1;
while (count >= 0)
{
System.out.println(count);
count++;
}

5.11)
public static void main(String[] args)
{
int sum = 0, value, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
value = scan.nextInt();
while (value != 0)
{
Count++;
sum += value;
System.out.println("The sum so far is " + sum);
System.out.print("Enter an integer (0 to quit): "); value = scan.nextInt();
}

5.12) System.out.println (“Enter the first integer.”);


{
firstNo = scan.nextInt();
System.out.println(“Enter the second integer.”);
secondNo = scan.nextInt();
if (count == 0)
System.out.println("Not valid!!!");
else
{
remainder= (firstNo / secondNo);
}

System.out.println(“ ‘+ firstNo +’ divided by ‘ + secondNo + ’ is ‘ + remainder + ‘. ”);


5.13)
Public class largerSUM;
{
if (size >= MAX)
{
size = 0;
if (numBooks < stackCount + inventoryCount)
reorder = true;
Else
Reorder = false;
}
}

5.14)
public boolean complexEquation(int one, int two, int three)
{
if(one != two && (one * two) == three)
return true;
return false;
}

5.15)
Public boolean isAlpha (char c){
If (Character.isLetter(c))
Return true;
Return false;
}

5.16)
Public floatEquals (float f1, float f2, float f3){
If (Math.abs(f1 / f2 - 1) < f3)
Return true;
Return false;
}

5.17)
Public isIsosceles (int i1, int i2, int i3){
if(i1 == i2 && i1 != i3)
Return true;
if(i1 == i3 && i1 != i2)
Return true;
if(i2 == i3 && i2 != i1)
Return true;
Return false;
}

5.18)
If the radio buttons in the QuoteOptions program were not organized into a ButtonGroup, then
selecting one button won’t turn off other buttons and selecting an already selected radio button
will turn off that radio button.

You might also like