CS 145 Lecture 6 – Scanner and String
Agenda
- what ?s
- homework
- quiz in second half of lab
- hackathon (extra credit participation)
- close out computer as calculator
- computer as chef
- Scanner
- MADLIB
- how tall am I metrically?
- program this 1
- String
- methods
- program this 2
Program This 1
- Digital cameras are advertised in terms of how many megapixels are in the images they capture. You’re curious about the dimensions of its pictures—int terms of pixels. Write a little program to accept a number of megapixels as input, and print out its images’ width x height.
Program This 2
- 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.
- 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.
- You have a String of the form “Lastname, Firstname”. Create a new String of the form “Firstname Lastname”.
- You have an HTML hexadecimal color code of the form “#RRGGBB”. How much green is in the color?
- 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
Detects and collects what’s next
Rejects whitespace specks