Tuesday, March 14, 2017

MovieListerApp

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
package murach.movies;
import murach.movies.MovieDB;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.TreeSet;
import murach.movies.Movie;
/*
 * This application stores a list of 100 movies and displays them by category.
The user can enter any of the following categories to display the films in the list that match the category:
animated, drama, horror, musical, scifi
After each list is displayed, the user is asked whether to continue. If the user enters Y or y,
the program asks for another category. Otherwise, the program ends.
 */

public class MovieListerApp {
/*
* Author: Piano Hagens
* 03/13/2017
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("Welcome to the Movie List Application.");
        System.out.println("There are 100 movies on the list.\n");


        Scanner sc = new Scanner(System.in);

        String choice = "y";

        ArrayList<Movie> myListOfMovies = createArrayList();

//
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.println("Here are categories: animated, drama, horror, musical, comedy, and scifi");
            System.out.println();

            String personMovieCategory = Validator.getRequiredString(sc, "What category are you interested in? ");

            Collection<Movie> matchedMovies = new TreeSet<>();
            for(Movie movie : myListOfMovies) {
                if (personMovieCategory.equalsIgnoreCase(movie.getCategory()))
                {
                    matchedMovies.add(movie);
                }
            }
            for (Movie movie : matchedMovies)
            {
                System.out.println(movie.getTitle());
            }

            choice = Validator.getRequiredString(sc, "\nContinue? (y/n): ");
            System.out.println();
        }
    }
//createArrayList()
    public static ArrayList<Movie> createArrayList()
    {
        ArrayList<Movie> m;
        ArrayList<Movie> creatingArrayList = new ArrayList<>();
     
        for (int i = 0; i <= 100; i++ )
        {
       
         m = MovieDB.getAllMovies();

            creatingArrayList.addAll(m);
        }
        return creatingArrayList;

    }

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package murach.movies;

import java.util.Scanner;

public class Validator
{

    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (!isValid)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Sorry! No this category has found.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getIntWithinRange(Scanner sc, String prompt,
                                        int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (!isValid)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                        "Error! Number must be greater than " + min);
            else if (i > max)
                System.out.println(
                        "Error! Number must be less or equal to " + max);
            else
                isValid = true;
        }
        return i;
    }

    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0;
        boolean isValid = false;
        while (!isValid)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Sorry! No this category has found. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    }

    public static String getRequiredString(Scanner sc, String prompt)
    {
        String s = "";
        boolean isValid = false;
        while (!isValid)
        {
            System.out.print(prompt);
            s = sc.nextLine();
            if (s == null || s.equals(""))
            {
                System.out.println("Sorry! No this category has found. Try again.");
            }
            else
            {
                isValid = true;
            }
        }
        return s;
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package murach.movies;
import java.util.ArrayList;

public class MovieDB {
/*
* Use the MovieDB class that’s provided to you to get the ArrayList objects. 
* To do that, you’ll need to finish the code for the getMovies methods
*/
private static ArrayList<Movie> allMovies = new ArrayList<>(100);
    
    public static ArrayList<Movie> getAllMovies() {
        allMovies.add(new Movie("Citizen Kane", "drama"));
        allMovies.add(new Movie("Casablanca", "drama"));
        allMovies.add(new Movie("The Godfather", "drama"));
        allMovies.add(new Movie("Gone With The Wind", "drama"));
        allMovies.add(new Movie("Lawrence Of Arabia", "drama"));
        allMovies.add(new Movie("The Wizard Of Oz", "musical"));
        allMovies.add(new Movie("The Graduate", "drama"));
        allMovies.add(new Movie("On The Waterfront", "drama"));
        allMovies.add(new Movie("Schindler's List", "drama"));
        allMovies.add(new Movie("Singin' In The Rain", "musical"));
        allMovies.add(new Movie("It's A Wonderful Life", "drama"));
        allMovies.add(new Movie("Sunset Boulevard", "drama"));
        allMovies.add(new Movie("The Bridge On The River Kwai", "drama"));
        allMovies.add(new Movie("Some Like It Hot", "drama"));
        allMovies.add(new Movie("Star Wars", "scifi"));
        allMovies.add(new Movie("All About Eve", "drama"));
        allMovies.add(new Movie("The African Queen", "drama"));
        allMovies.add(new Movie("Psycho", "horror"));
        allMovies.add(new Movie("Chinatown", "drama"));
        allMovies.add(new Movie("One Flew Over The Cuckoo's Nest", "drama"));
        allMovies.add(new Movie("The Grapes Of Wrath", "drama"));
        allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
        allMovies.add(new Movie("The Maltese Falcon", "drama"));
        allMovies.add(new Movie("Raging Bull", "drama"));
        allMovies.add(new Movie("E.T. The extra-terrestrial", "scifi"));
        allMovies.add(new Movie("Dr. Strangelove", "drama"));
        allMovies.add(new Movie("Bonnie And Clyde", "drama"));
        allMovies.add(new Movie("Apocalypse Now", "drama"));
        allMovies.add(new Movie("Mr. Smith Goes to Washington", "drama"));
        allMovies.add(new Movie("The Treasure Of The Sierra Madre", "drama"));
        allMovies.add(new Movie("Annie Hall", "comedy"));
        allMovies.add(new Movie("The Godfather Part II", "drama"));
        allMovies.add(new Movie("High Noon", "drama"));
        allMovies.add(new Movie("To Kill A Mockingbird", "drama"));
        allMovies.add(new Movie("It Happened One Night", "drama"));
        allMovies.add(new Movie("Midnight Cowboy", "drama"));
        allMovies.add(new Movie("The Best Years Of Our Lives", "drama"));
        allMovies.add(new Movie("Double Indemnity", "drama"));
        allMovies.add(new Movie("Doctor Zhivago", "drama"));
        allMovies.add(new Movie("North By Northwest", "drama"));
        allMovies.add(new Movie("West Side Story", "musical"));
        allMovies.add(new Movie("Rear Window", "drama"));
        allMovies.add(new Movie("King Kong", "horror"));
        allMovies.add(new Movie("The Birth Of A Nation", "drama"));
        allMovies.add(new Movie("A Streetcar Named Desire", "drama"));
        allMovies.add(new Movie("A Clockwork Orange", "scifi"));
        allMovies.add(new Movie("Taxi Driver", "drama"));
        allMovies.add(new Movie("Jaws", "horror"));
        allMovies.add(new Movie("Snow White And The Seven Dwarfs", "animated"));
        allMovies.add(new Movie("Butch Cassidy And The Sundance Kid", "drama"));
        allMovies.add(new Movie("The Philadelphia Story", "drama"));
        allMovies.add(new Movie("From Here To Eternity", "drama"));
        allMovies.add(new Movie("Amadeus", "drama"));
        allMovies.add(new Movie("All Quiet On The Western Front", "drama"));
        allMovies.add(new Movie("The Sound Of Music", "musical"));
        allMovies.add(new Movie("M*A*S*H", "comedy"));
        allMovies.add(new Movie("The Third Man", "drama"));
        allMovies.add(new Movie("Fantasia", "animated"));
        allMovies.add(new Movie("Rebel Without A Cause", "drama"));
        allMovies.add(new Movie("Raiders Of The Lost Ark", "drama"));
        allMovies.add(new Movie("Vertigo", "drama"));
        allMovies.add(new Movie("Tootsie", "comedy"));
        allMovies.add(new Movie("Stagecoach", "drama"));
        allMovies.add(new Movie("Close Encounters Of The Third Kind", "scifi"));
        allMovies.add(new Movie("The Silence Of The Lambs", "horror"));
        allMovies.add(new Movie("Network", "drama"));
        allMovies.add(new Movie("The Manchurian Candidate", "drama"));
        allMovies.add(new Movie("An American In Paris", "drama"));
        allMovies.add(new Movie("Shane", "drama"));
        allMovies.add(new Movie("The French Connection", "drama"));
        allMovies.add(new Movie("Forrest Gump", "drama"));
        allMovies.add(new Movie("Ben-Hur", "drama"));
        allMovies.add(new Movie("Wuthering Heights", "drama"));
        allMovies.add(new Movie("The Gold Rush", "drama"));
        allMovies.add(new Movie("Dances With Wolves", "drama"));
        allMovies.add(new Movie("City Lights", "drama"));
        allMovies.add(new Movie("American Graffiti", "drama"));
        allMovies.add(new Movie("Rocky", "drama"));
        allMovies.add(new Movie("The Deer Hunter", "drama"));
        allMovies.add(new Movie("The Wild Bunch", "drama"));
        allMovies.add(new Movie("Modern Times", "drama"));
        allMovies.add(new Movie("Giant", "drama"));
        allMovies.add(new Movie("Platoon", "drama"));
        allMovies.add(new Movie("Fargo", "drama"));
        allMovies.add(new Movie("Duck Soup", "comedy"));
        allMovies.add(new Movie("Mutiny On The Bounty", "drama"));
        allMovies.add(new Movie("Frankenstein", "horror"));
        allMovies.add(new Movie("Easy Rider", "drama"));
        allMovies.add(new Movie("Patton", "drama"));
        allMovies.add(new Movie("The Jazz Singer", "drama"));
        allMovies.add(new Movie("My Fair Lady", "musical"));
        allMovies.add(new Movie("A Place In The Sun", "drama"));
        allMovies.add(new Movie("The Apartment", "drama"));
        allMovies.add(new Movie("Goodfellas", "drama"));
        allMovies.add(new Movie("Pulp Fiction", "drama"));
        allMovies.add(new Movie("The Searchers", "drama"));
        allMovies.add(new Movie("Bringing Up Baby", "drama"));
        allMovies.add(new Movie("Unforgiven", "drama"));
        allMovies.add(new Movie("Guess Who's Coming To Dinner", "drama"));
        allMovies.add(new Movie("Yankee Doodle Dandy", "musical"));
        return allMovies;
    }
    
    public static ArrayList<Movie> getMovies(String category) {
        ArrayList<Movie> movies = new ArrayList<>();
        // TODO: add code to add all movies with the specified category
        return movies;
    }    

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
package murach.movies;
/*
 * Use the Movie class that’s provided to store the title and category for each movie.
 */

public class Movie implements Comparable{
private String title;
   private String category;

   public Movie(String title, String category) {
       this.title = title;
       this.category = category;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getCategory() {
       return category;
   }

   public void setCategory(String category) {
       this.category = category;
   }
   
   @Override
   public int compareTo(Object o)
   {
       Movie m = (Movie) o;
       int titleCompare = this.getTitle().compareTo(m.getTitle());
       if (titleCompare != 0)
           return titleCompare;
       else
           return(this.getTitle().compareToIgnoreCase(m.getTitle()));
   }

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Monday, March 13, 2017

RoshamboApp

package roshambo;

import roshambo.RoshamboApp.Roshambo;

public class Bart extends Player{
/*
* Create classes named Bart and Lisa that inherit the Player class and implement
* the generateRoshambo method. The Bart class should always select rock. The Lisa
* class should randomly select rock, paper, or scissors (a 1 in 3 chance of each).
*
*/

private Roshambo r;

@Override
public Roshambo generateRoshambo()
{//The Bart class should always select rock.
r = Roshambo.ROCK;
return r;
}
@Override
public Roshambo getRoshambo()
{
return r;
}
 

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;
import roshambo.RoshamboApp.Roshambo;
public class Lisa extends Player{
/*
* Create classes named Bart and Lisa that inherit the Player class and implement 
* the generateRoshambo method. The Bart class should always select rock. The Lisa 
* class should randomly select rock, paper, or scissors (a 1 in 3 chance of each).
*/
private Roshambo r;
public Roshambo generateRoshambo(String choice)
{//The Lisa class should randomly select rock, paper, or scissors (a 1 in 3 chance of each).
switch(choice)
{
case "r":
r = Roshambo.ROCK;
break;
case "p":
r = Roshambo.PAPER;
break;
case "s":
r = Roshambo.SCISSORS;
break;
}
return r;
}


@Override
public Roshambo generateRoshambo() {
// TODO Auto-generated method stub
return Roshambo.PAPER;
}
@Override
public Roshambo getRoshambo()
{
return r;
}


  
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;

import roshambo.RoshamboApp.Roshambo;

public abstract class Player {
/*
* Create an abstract class named Player that stores a name and a Roshambo value. 
* This class should include an abstract method named generateRoshambo that allows an
*  inheriting class to generate and return a Roshambo value. 
* It should also include get and set methods for the name and Roshambo value.
*/
//private Roshambo r;
public Player()
{
//r = Roshambo.ROCK;
}
public abstract Roshambo generateRoshambo();
public abstract Roshambo getRoshambo();

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;

import roshambo.RoshamboApp.Roshambo;

public class Player1 extends Player{
/*
* Create a class named Player1 that inherits the Player class and 
* implements the generateRoshambo method (even though it isn’t necessary 
* for this player). This method can return any value you choose.
* */
 
private Roshambo r;
@Override
public Roshambo generateRoshambo()
{
r = Roshambo.values()[(int)(Math.random() * 3)];
return r;
}
@Override
public Roshambo getRoshambo()
{
return r;
}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;

public enum Roshambo {
/*
* Create an enumeration named Roshambo that stores three values: rock, paper, 
* and scissors. This enumeration should 
* include a toString method that can convert the selected value to a string.
*/
ROCK, PAPER, SCISSORS

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;

import java.util.Scanner;

public class Validator {

public static String getAnswer(Scanner sc, String prompt) {
String answer = "";
System.out.println(prompt);
while (true) {
answer = sc.nextLine();
if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n"))
return answer;
else
System.out.println("Error:  Please enter y or n");
}

}
//only take l or b
public static String getTeam(Scanner sc, String prompt) {
String answer = "";
System.out.println(prompt);
while (true) {
answer = sc.nextLine();
if (answer.equalsIgnoreCase("b") || answer.equalsIgnoreCase("l"))
return answer;
else
System.out.println("Error:  Please enter l or b");
}

}
public static String getString(Scanner sc, String prompt) {
System.out.println(prompt);
String s = sc.next();
sc.nextLine();
return s;
}
//only take r, p, or s
public static String getSelection(Scanner sc, String prompt) {
String answer = "";
System.out.println(prompt);
while (true) {
answer = sc.nextLine();
if (answer.equalsIgnoreCase("r") || answer.equalsIgnoreCase("p") || answer.equalsIgnoreCase("s"))
return answer;
else
System.out.println("Error:  Please enter r, p, or s");
}

}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package roshambo;


import java.util.Scanner;

import roshambo.Bart;
import roshambo.Lisa;
import roshambo.Player;
import roshambo.Player1;
import roshambo.Roshambo;

public class RoshamboApp {
/*
* Author: Piano Hagens
* Date: Mar 12th, 2017
* Create a class named RoshamboApp that allows the player to play Bart or Lisa as 
* shown in the console output. Rock should beat scissors, 
* paper should beat rock, and scissors should beat paper.
*/

enum Roshambo
{
ROCK, PAPER, SCISSORS;
public String toString()
{
switch(this)
{
case ROCK:
return "rock";
case PAPER:
return "paper";
case SCISSORS:
return "scissors";
default:
return "";
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);
String name = "";
String player1 = "";
String team = "";
String answer = "";
Player b;
Lisa l = new Lisa();
Roshambo pick;
Roshambo cpuPick;
System.out.println("Welcome to the game of Roshambo");
name = Validator.getString(sc, "Enter your name: ");
team = Validator.getTeam(sc, "Would you like to play Bart or Lisa? (b/l): ");
if(team.equalsIgnoreCase("b"))
{
b = new Bart();
b = (Bart)b;
player1 = "Bart";
}
else if(team.equalsIgnoreCase("l"))
{
b = new Player1();
b = (Player1)b;
player1 = "Lisa";
}
else
{
b = new Bart();
b = (Bart)b;
}
do
{
// get  Validate
answer = Validator.getSelection(sc, "Rock, paper, or scissors? (r/p/s):");
l.generateRoshambo(answer);
b.generateRoshambo();
pick = l.getRoshambo();
cpuPick = b.getRoshambo();
System.out.println(name + ": " + pick.toString());
System.out.println(player1 + ": " + cpuPick.toString());
if(pick == b.getRoshambo())
{
System.out.println("Draw");
}
else if(pick == Roshambo.PAPER)
{
if(cpuPick == Roshambo.ROCK)
{
System.out.println(name + " Wins!");
}
else if(cpuPick == Roshambo.SCISSORS)
{
System.out.println(player1 + " Wins!");
}
}
else if(pick == Roshambo.ROCK)
{
if(cpuPick == Roshambo.SCISSORS)
{
System.out.println(name + " Wins!");
}
else if(cpuPick == Roshambo.PAPER)
{
System.out.println(player1 + " Wins!");
}
}
else if(pick == Roshambo.SCISSORS)
{
if(cpuPick == Roshambo.PAPER)
{
System.out.println(name + " Wins!");
}
else if(cpuPick == Roshambo.ROCK)
{
System.out.println(player1 + " Wins!");
}
}
answer = Validator.getAnswer(sc, "Would you like to play again?");
}
while(answer.equalsIgnoreCase("y"));

}

}



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Tuesday, February 21, 2017

Pig Latin Translate APP

import java.util.Scanner;
/* * Author: Piano Hagens
* Feb 21st, 2017
*/
public class PigLatinTranslateAPP {
//Parse the string into separate words before translating
public static String removeContractions(String words){
if (words.equals("can't")){
words = words.replace("can't", "cannot");
}else if(words.equals("won't")){
words = words.replace("won't", "will not");
}else if(words.equals("don't")){
words = words.replace("don't", "do not");
}else if(words.equals("shouldn't")){
words = words.replace("shouldn't", "should not");
}
return words;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Display a welcome messages
System.out.println("Welcome to the Pig Latin Translator");
       System.out.println();        
       //create a scanner to capture user input
       Scanner sc = new Scanner(System.in);        
       String choice = "y";        
    //Ask the user want to play?   
      System.out.println("Translate a sentence to Pig Latin? (y/n):  ");        
       //create a loop so to designed play the app or not
       while (choice.equalsIgnoreCase("y")){
        choice = sc.nextLine();
        System.out.println();        
        // Get word to translate from the user.
           System.out.print("Please enter a sentence to translate into Pig Latin: ");            
           String line = sc.nextLine();
           String[] words = line.split(" ");            
           line = line.toLowerCase();  // convert to lower case         
        // Determine whether the first character in word is a vowel
           char firstChar = line.charAt(0);  // First index is 0
           String vowels = "aeiouy";            
        // If firstChar is a vowel then indexOf will return at which index
        // it appears in the String vowels, otherwise it returns -1
           int vowelIndex = vowels.indexOf(firstChar);  
           boolean firstIsVowel = (vowelIndex >= 0);             
        // Print out the word in Pig Latin           
           System.out.print("\n\"" + line + "\" in Pig Latin is ");
         if (firstIsVowel) { // \n is newline character// \" is double quote character
               System.out.println("\"" + line + "way\"");
           }
           else { // first character is consonant, assuming it is a letter
               String restOfWord = line.substring(1); // all but first character
               System.out.println("\"" + restOfWord + "-" + firstChar + "ay\"");
           }            
            //see if the user want to play again   
              System.out.println("Translate another line? (y/n):  ");  
            choice = sc.nextLine();
            System.out.println();
       }
       //close scanner and Bye
       sc.close();
       System.out.println("Bye");
}
}
import java.util.Scanner;
/* * Author: Piano Hagens
* Feb 21st, 2017
*/
public class PigLatinTranslateAPP {
//Parse the string into separate words before translating
public static String removeContractions(String words){
if (words.equals("can't")){
words = words.replace("can't", "cannot");
}else if(words.equals("won't")){
words = words.replace("won't", "will not");
}else if(words.equals("don't")){
words = words.replace("don't", "do not");
}else if(words.equals("shouldn't")){
words = words.replace("shouldn't", "should not");
}
return words;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Display a welcome messages
System.out.println("Welcome to the Pig Latin Translator");
       System.out.println();        
       //create a scanner to capture user input
       Scanner sc = new Scanner(System.in);        
       String choice = "y";        
    //Ask the user want to play?   
      System.out.println("Translate a sentence to Pig Latin? (y/n):  ");        
       //create a loop so to designed play the app or not
       while (choice.equalsIgnoreCase("y")){
        choice = sc.nextLine();
        System.out.println();        
        // Get word to translate from the user.
           System.out.print("Please enter a sentence to translate into Pig Latin: ");            
           String line = sc.nextLine();
           String[] words = line.split(" ");            
           line = line.toLowerCase();  // convert to lower case         
        // Determine whether the first character in word is a vowel
           char firstChar = line.charAt(0);  // First index is 0
           String vowels = "aeiouy";            
        // If firstChar is a vowel then indexOf will return at which index
        // it appears in the String vowels, otherwise it returns -1
           int vowelIndex = vowels.indexOf(firstChar);  
           boolean firstIsVowel = (vowelIndex >= 0);             
        // Print out the word in Pig Latin           
           System.out.print("\n\"" + line + "\" in Pig Latin is ");
         if (firstIsVowel) { // \n is newline character// \" is double quote character
               System.out.println("\"" + line + "way\"");
           }
           else { // first character is consonant, assuming it is a letter
               String restOfWord = line.substring(1); // all but first character
               System.out.println("\"" + restOfWord + "-" + firstChar + "ay\"");
           }            
            //see if the user want to play again   
              System.out.println("Translate another line? (y/n):  ");  
            choice = sc.nextLine();
            System.out.println();
       }
       //close scanner and Bye
       sc.close();
       System.out.println("Bye");
}
}