CS 145 Lecture 4 – String problems and our first method
Agenda
- extracting names: “LASTNAME, FIRSTNAME MIDDLENAME …”
- lock the web: does a URL end in .edu?
- separate a comma-separated list
- turn “1,234,567” into 1234567
- abstraction: hiding details
- abstracting SVG
Code
NameFixerUpper.java
package preexam1;
public class NameFixerUpper {
public static void main(String[] args) {
String name = "STEINMEYER, JOEL EDWARD";
// names in bad order!
// names in uppercase!
// bah!
int commaSpot = name.indexOf(',');
System.out.println(commaSpot);
String lastName = name.substring(0, commaSpot);
System.out.println(lastName);
String rest = name.substring(commaSpot + 2);
System.out.println(rest);
int spaceSpot = rest.indexOf(' ');
String firstName = rest.substring(0, spaceSpot);
System.out.println(firstName);
String fixeredUpName = firstName + " " + lastName;
System.out.println(fixeredUpName);
}
}
WebFilter.java
package preexam1;
public class WebFilter {
public static void main(String[] args) {
String url = "http://fooboo.woo.gov/page.edu.index.html";
// System.out.println(url.replace('/', '?'));
// Gives false positives.
// boolean isSafe = url.contains(".edu");
// System.out.println(isSafe);
String httpless = url.substring(url.indexOf(':') + 3);
System.out.println(httpless);
int slashSpot = httpless.indexOf('/');
String host = httpless.substring(0, slashSpot);
boolean isSafe = host.endsWith(".edu");
System.out.println(host + " is safe? " + isSafe);
}
}
CircleMaker.java
package preexam1;
import java.util.Scanner;
public class CircleMaker {
public static void main(String[] args) {
// Get some circle data from the user.
Scanner in = new Scanner(System.in);
System.out.println("Where? ");
int x = in.nextInt();
int y = in.nextInt();
System.out.println("What color? ");
String color = in.next();
System.out.println("How big? ");
int radius = in.nextInt();
// Use that data to make them their awesome circle.
System.out.println("<html>");
System.out.println("<body>");
System.out.println("<h1>My first SVG</h1>");
System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
System.out.println("<circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius + "\" stroke=\"black\"");
System.out.println("stroke-width=\"2\" fill=\"" + color + "\" />");
System.out.println("</svg>");
System.out.println("</body>");
System.out.println("</html>");
}
}
TODO
- Read section 1.4.
Haiku
Sweet automation.
Doesn’t mean more vacation.
We just do more stuff.
Doesn’t mean more vacation.
We just do more stuff.