Textbook

1. Introduction 

2. Data and Operators

3. Using Objects

4. Writing Classes

Homework One is due Friday. 

https://www.cs.indiana.edu/classes/c212-dgerman/spr2015/hwOne.html

Write a program that prompts the user for some input. 

The user will enter a string of three characters. 

The program returns true or false depending on whether their string was a winner or not. 

The rules are as follows: 

  (a) if the string starts with a consonant (not vowel) 
      it is a winner if the second character is a vowel

  (b) if the string starts with a vowel it is a winner 
      unless the second character is also a vowel 
      except when the third is also a vowel 

http://www.cs.indiana.edu/classes/c211/gru.jpg

  baa  winner 
  bbc  loser
  abb  winner 
  aab  loser 
  aaa  winner 

This problem combines the two in Homework One 

Questions:

1. How do we read the user input? 

2. How do we extract the individual letters/characters?

3. How do we determine if a character is a vowel or not? 

4. How do we calculate the boolean that represents the answer? 

Answers:

1. Easy, do it like in Lab One (with Scanner). 

2. See the following experiment:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String line = "abc"
> line
"abc"
> line.substring(0, 1)
"a"
> line.substring(1, 2)
"b"
> line.substring(2, 3)
"c"
> line.substring(2)
"c"


3. I will give you 

String letter = "u"; 

   How can I determine if letter is a vowel or not? 

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#contains%28java.lang.CharSequence%29

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String letter = "a"
> letter
"a"
> letter.length() > 2
false
> "aeiouAEIOU".length()
10
> "aeiouAEIOU".contains(letter)
true


4. 

The rules are as follows:

  (a) if the string starts with a consonant (not vowel)
      it is a winner if the second character is a vowel

  (b) if the string starts with a vowel it is a winner
      unless the second character is also a vowel
      except when the third is also a vowel


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 1 + 1
2
> 1 < 1
false
> 1 <= 1
true
> boolean a = true
> boolean b = false
> a
true
> b
false
> a && b
false
> a || b
true
> ! a // TeachScheme!
false
> a && !a == false
true


Now we know we need to produce some sort of expression that uses &&, || and ! to calculate the answer. 


The rules are as follows:

  (a) if the string starts with a consonant (not vowel)
      it is a winner if the second character is a vowel

  (b) if the string starts with a vowel it is a winner
      unless the second character is also a vowel
      except when the third is also a vowel


Here's the program: 

import java.util.Scanner; 

class Chen {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);  
    System.out.print("Enter input: "); 
    String line = in.nextLine(); 
    
    String a = line.substring(0, 1); 
    String b = line.substring(1, 2); 
    String c = line.substring(2, 3); 
    
    boolean caseOne = ! "aeiouAEIOU".contains(a) && "aeiouAEIOU".contains(b) ;

    boolean caseTwo = "aeiouAEIOU".contains(a) && ! "aeiouAEIOU".contains(b) ; // jonesdac shiding
    boolean caseThree = "aeiouAEIOU".contains(a) &&
                        "aeiouAEIOU".contains(b) &&
                        "aeiouAEIOU".contains(c); // kitanok
    
    boolean answer = caseOne || caseTwo || caseThree; 
    
    System.out.println( answer ); 
    in.close();
  }
}