Control Structures Solutions

From CompSciWiki
Jump to: navigation, search

Main_Page > Control Structures


Introduction

These are the solutions for the review questions for control structures.





Overview

If you did not answer any of these questions successfully make sure to go back and re-read the material. If you have trouble understanding the material talk to your prof or a TA

Template loop detected: Template loop detected:

Review Questions

1. The three logical operators are AND, OR, and NOT. They allow boolean statements to operate on each to yield results which would normally be obtained by multiple if statements. Overall, they reduce code and complexity while maintaining readability.


2. Use the equals() method because 99% of the time you will want to compare strings by value, not by reference.


3. The answers are:

  1. true
  2. true
  3. false


4. The truth table for the expression (X || Y) && Z is:

X    Y    Z  |  (X || Y)  | (X || Y) && Z
-------------|------------|---------------
0    0    0  |      0     |       0
0    0    1  |      0     |       0
0    1    0  |      1     |       0
0    1    1  |      1     |       1
1    0    0  |      1     |       0
1    0    1  |      1     |       1
1    1    0  |      1     |       0
1    1    1  |      1     |       1

Exercises

1. Nesting the first two if statements can be combined into one using a logical AND. The !!!!true is redundant and can be eliminated. The last two if statements can be combined into one using a logical OR. Lastly, all the statements can be combined using if-else's.

if (x == 1 && y == 1)
{
  System.out.println("X and Y are 1!");
}
else if (x == 2)
{
  System.out.println("X is 2!");
}
else if (x < 1 || x > 2)
{
  System.out.println("X is under 1 or over 2!");
}


2. The result of the program is: The result is false. The left hand side is originally false, but a NOT changes it to true. This makes the statement true between the left and right hand side since an OR only requires one side to be true. However, another NOT changes the result to false, which causes the else part of the if-else to be executed.


3.

import javax.swing.JOptionPane;

public class NumberTester
{
    public static void main(String[] args)
    {
        int num;
        num = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number: "));

        if(num%2 == 0)          //test to see if the number is even
        {
            System.out.println("The number " + num + " is even.");
        }

        else
        {
            System.out.println("The number " + num + " is odd.");
        }


        if(num > 0)
        {
            System.out.println("The number " + num + " is positive.");
        }

        else if(num < 0)
        {
	    System.out.println("The number " + num + " is negative.");
	}	

        else if (num == 0)
        {
            System.out.println("The number " + num + " is equal to zero.");
        }
	

        //print that the program finished successfully and exit	
        System.out.println("*** Application Terminated Normally ***");
        System.exit(0)
    }
}


4.

import javax.swing.*;
public class Question4
{
    public static void main(String[] args)
    {
        String user_input = JOptionPane.showInputDialog(null,"Enter a word or phrase");
        char first_letter;
        
        first_letter = user_input.toLower().charAt(0);        //convert the string to all lower-case, and get the first character

        //check to see if the first letter is a vowel or not
        //print the proper message to the screen
        if(first_letter=='a' || first_letter=='e' || first_letter=='i' || first_letter=='o' || first_letter=='u')  //ignore "sometimes y"
        {
            System.out.println("The first letter is a vowel");
        }//if

        else
        {
            System.out.println("The first letter is a consonant");
        }//else

        //print that the program is finished and exit
        System.out.println("*** Application Terminated Normally ***");
        System.exit(0);
    }//main
}//Question4

Template loop detected: Template loop detected: Template loop detected: