Lesson 10 – Logical Operators

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Activity - if

Record your participation in Piazza clicker question Lesson10 Q1

The variables fuelAmount and fuelCapacity hold the actual amount of fuel and the size of the fuel tank of a vehicle. If less than 10 percent is remaining in the tank, a status light should show a red color; otherwise it shows a green color. Write a code segment to simulate this process by printing out either "red" or "green" depending on the amount of fuel in the tank.

Write this on a piece of paper or in a text editor. It will not compile. You can assume the variables are defined and have been given a value is some fashion. You just complete the if statement

  int fuelAmount = ...  
  int fuelCapacity = ...
  if (...)
  {
     ...
  }
  1. I got it
  2. I got it but it was hard
  3. I didn't get it
  4. I didn't try

Activity - Duplicating Code in Branches

Record your participation in Piazza clicker question Lesson10 Q2

This code works but does not follow best practices. What is wrong with this code to adjust the floor number in an elevator without a floor 13?

if (floor > 13)
{
   actualFloor = floor - 1;
   System.out.println("Actual floor: " + actualFloor);
}
else
{
   actualFloor = floor;
   System.out.println("Actual floor: " + actualFloor);
}
  1. You should use else if instend of else
  2. Nothing is wrong
  3. You should do the printing only once, after the given code
  4. You can not have multiple statements in an if or else block

Activity - String Comparison

Record your participation in Piazza clicker question Lesson10 Q3

Supply a condition in this if statement to test whether the user entered an (uppercase) Y. Select all that will work.

System.out.println("Enter Y to quit.");
String input = in.next();
if (. . .)
{
   System.out.println("Goodbye.");
}

Select all that apply

  1. input == "Y"
  2. input.equals("Y")
  3. !input.equals("N");
  4. input.equals("y")

Activity - Testing for Equality

Record your participation in Piazza clicker question Lesson10 Q4

What is the error in this statement? Assume scoreA and scoreB are integers

if (scoreA = scoreB)
{
   System.out.println("Tie");
}
  1. There is no error
  2. The code compiles but but throws an exception at run time
  3. The codition is always true so "Tie" will always print
  4. There is a syntax error. = is for assignment not equaity checking

Activity - Multiple Alternatives

Record your participation in Piazza clicker question Lesson10 Q5

On a piece of paper or in a text file, write an if statement with three branches that sets the int variable sign in the manner explained below. Assume x is an int and has been defined.

Just write the if statement to set sign:

boolean variable type

 

Activity - Logical Operator !

Record your participation in Piazza clicker question Lesson10 Q6

failed  is a boolean variable. We do not know its value. What is the value of !!failed?

  1. true
  2. false
  3. the same as failed
  4. the opposite of failed

Activity - Logical Operator &&

Record your participation in Piazza clicker question Lesson10 Q7

Which statement could be used to determine if both num1 and num2 are positive. Click all that apply

  1. if (num1 > 0 !! num2 > 0)
  2. if (num1 > 0 || num2 > 0)
  3. if (num1 > 0 && num2 > 0)
  4. if (num1 < 0 || num2 < 0)

Activity - Logical Operator || (pipes)

Record your participation in Piazza clicker question Lesson10 Q8

Which statement could be used to determine if either (or both) num1 or num2 is positive. Click all that apply

  1. if (num1 > 0 !! num2 > 0)
  2. if (num1 > 0 || num2 > 0)
  3. if (num1 > 0 && num2 > 0)
  4. if (num1 <= 0 && num2 <= 0)

Activity - Logical operators

Record your participation in Piazza clicker question Lesson10 Q9

Write a program that asks the user for their favorite color and then asks for another color(as Strings). If one is red and one is blue, print "That's PURPLE" Otherwise print "not purple" The words can be upper or lower case. Notice that a color my be two words

  1. I got it
  2. I got it but it was really hard
  3. I didn't get it
  4. I didn't try

Test with these values

color1 color2 output
red blue That's PURPLE
blue red That's PURPLE
BluE RED That's PURPLE
blue green red not purple
white red not purple

 

Here is the skeleton IO project

 

Activity - next vs nextLine

Record your participation in Piazza clicker question Lesson10 Q10

Write a program to ask a user for the city where he/she was born. If the city is San Fransisco, print "You are not far from home." Otherwise, print "Where is that?"

Test with these values:

city output
San Fransisco You are not far from home.
San Antonio Where is that?
New York Where is that?

 

Here is the skeleton IO project

Activity - Input

Record your participation in Piazza clicker question Lesson10 Q11

Run the following test program and supply inputs 2 and three at the prompts. What does it print? Why?

import java.util.Scanner;
public class Test
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      int m = in.nextInt();
      System.out.print("Enter another integer: ");
      int n = in.nextInt();
      System.out.println(m + " " + n);
   }
}

  1. 5
  2. 2 + three
  3. nothing. The code does not compile
  4. nothing. The code throws an exception

 

Activity - Nested if

Record your participation in Piazza clicker question Lesson10 Q12

The Java Ice Cream store sells, what else, ice cream cones. They sell either one scoop or two in either a plain cone or a waffle cone. There is a table of the costs below

  waffle plain
1 scoop 2.50 1.75
2 scoops 2.75 2.25

Let's complete the getCost method of the IcecreamCone class (ice_cream_starter.zip)

 

/**
 * Describes an ice cone
 */
public class IceCreamCone
{

    public static final double ONE_SCOOP_PLAIN_CONE = 1.75;
    public static final double ONE_SCOOP_WAFFLE_CONE = 2.50;  
    public static final double TWO_SCOOP_PLAIN_CONE = 2.25;
    public static final double TWO_SCOOP_WAFFLE_CONE = 2.75;
    public static final int PLAIN_CONE = 1;
    public static final int WAFFLE_CONE = 2;
      
    private int scoops;
    private int type;
    
    public IceCreamCone( int numberOfScoops, int coneType)
    {
        this.scoops = numberOfScoops;
        this.type = coneType;
    }
    
    /**
     * Gets the cost of this IceCreamCone
     * @return the cost of this ice cream cone
     */
    public double getCost()
    {
        return 0;
    }
}