2015-10-30

posted Oct 29, 2015, 8:44 AM by Samuel Konstantinovich   [ updated Oct 29, 2015, 10:22 AM ]
Goal: Go over piglatin, good practices, loopyness.

Reminder: 
1. Halloween
2. Quiz Tomorrow (can't be monday/tues/wed because of trips/election day)
At least one direct copy of a coding bat Array2 problem, potentially more if I modify them or modify + something from Array3.

Sample solution to piglatin:

import java.io.*;
import java.util.*;
public class MakePigLatin{
    public static void main(String[]args){

	String filename = "out.txt";
	//when you type a filename after your
	//command, use that as the file name
	if(args.length > 0){
	    filename = args[0];
	}


	try{
	    Scanner in = new Scanner(new File(filename));
	    //read one line at a time with a scanner
	    while(in.hasNext()){
		//put that line in another scanner
		Scanner line = new Scanner(in.nextLine());

		//read each word in the line
		while(line.hasNext()){
		    //print the new word, and a space. 
		    System.out.print(process(line.next())+" ");
		    
		}
		//put a new line at the end of the line
		System.out.println();
	    }		
	}catch(FileNotFoundException e){
	    System.out.println("File '"+filename+"' not found");
	}
    }
    public static String process(String s){
      	if(s.length()==0){
	    return s;
	}
	if(!Character.isLetterOrDigit(s.charAt(0))){
	    return s.charAt(0)+process(s.substring(1));
	}
	if(!Character.isLetterOrDigit(s.charAt(s.length()-1))){
    return process(s.substring(0,s.length()-1))+s.charAt(s.length()-1);
	}
	return pigIfy(s);
	
    }
    private static boolean isVowel(char s){
	s = (""+s).toLowerCase().charAt(0); 
	return s=='a' || s=='e' || s=='i' || s == 'o' || s=='u' || s=='y';
    }
    public static String pigIfy(String S){
	
	int vow = 0;
	while(vow<S.length() && !isVowel(S.charAt(vow))){
	    vow++;
	}
	if(vow == 0){
	    return S+"-hay";
	}else{
	    return S.substring(vow)+"-"+S.substring(0,vow)+"ay";
	}
    }
}
Comments