teaching machines

CS 145 Lecture 6 – Scanner and String

September 15, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

Program This 1

Program This 2

  1. You have a dinosaur species name stored in a String. Assign to a variable isCeratopsian a value indicating whether or not the species is in the Ceratopsian family.
  2. You have a credit card number stored in a String. Create a new String that masks out all but the last four digits with asterisks.
  3. You have a String of the form “Lastname, Firstname”. Create a new String of the form “Firstname Lastname”.
  4. You have an HTML hexadecimal color code of the form “#RRGGBB”. How much green is in the color?
  5. You have a sentence stored in a String. What is its last character?

Code

MadLib.java

package lecture0915;

import java.util.Scanner;

public class MadLib {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Verb: ");
    String verb2 = in.nextLine();
    System.out.print("Noun: ");
    String noun2 = in.nextLine();
    System.out.print("Noun: ");
    String noun3 = in.nextLine();
    System.out.print("Verb: ");
    String verb1 = in.nextLine();
    System.out.print("Pronoun: ");
    String pronoun1 = in.nextLine();
    System.out.print("Noun: ");
    String noun4 = in.nextLine();
    String text = "I am a monther of 3 seeking to " + verb2 + ". I have currently lost my " + noun2 + " and am seeking to be able to stay home with my " + noun3 + " and " + verb1 + " other parents out to keep their jobs. " + pronoun1 + " am located right in the middle of Eau Claire and Mondovi. I live on a big 2acre yard with a " + noun4 + ", and plenty of room to run, ride bikes and explore. My children are 10, 4, and 10 months. I have babysat since age 12 and have experience with all ages and a lot of disabilities such as ADHD, autism, bipolar, etc. We go outside as much as possible and have a set schedule for meals, television, learning and relax time. I have a complete nursery as well as toddler toys and older kid activities. All meals would be provided for kids old enough for tackle food. My rates are $4/hr for 25 hrs or less. Or $115 flat rate for 26 to 42 hours. If u have multiple children rates decrease and we can discuss this to fit. If interested in more info or to meet with me and see my home. Please contact me.";
    System.out.println(text);
  }
}

HeightMetricator.java

package lecture0915;

import java.util.Scanner;

public class HeightMetricator {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter feet, foobag: ");
    int heightFeet = in.nextInt();
    System.out.print("Please enter inches, foobag: ");
    int heightInches = in.nextInt();

    // heightInches = heightInches + heightFeet * 12;
    heightInches += heightFeet * 12;

    int heightCm = (int) (heightInches * 2.54);
    System.out.println(heightCm);
  }
}

HighStrung.java

package lecture0915;

public class HighStrung {
  public static void main(String[] args) {
    String species = "diplodocus";
    boolean isCeratopsian = species.endsWith("ceratops");
    System.out.println(isCeratopsian);
    
    String ccn = "1234234567890044";
    System.out.println("************" + ccn.substring(12));
  }
}

Haiku

Scanner dissects text
Detects and collects what’s next
Rejects whitespace specks