import java.util.Scanner;
import java.util.InputMismatchException;
// This program was made by Erdogan Akbiyik
/* Project 4 Guess Program - Guess is a program to predict a number.
The computer randomly sets a number between 1-100 and asks users to estimate this number.
*/
public class Guess {
public static int high_or_low(int numberToGuess, int newGuess, int min, int max)
{
return newGuess - numberToGuess;
}
public static void main(String[] args) {
Random random = new Random();
Scanner key = new Scanner(System.in);
int numberOfTries = 0;
int numberOfValidTries = 0;
int min = 1;
int max = 100;
int numberToGuess=random.nextInt(100) + 1; //Since random.nextInt(100) returns 0 to 99 we add 1 to find 1 to 100
System.out.println("Welcome to 'Guess The Number Game'"); //Welcome messages
System.out.println("We pick a number between 1 and 100, can you guess it?");
while (true) {
try {
numberOfTries++; //Increment number of tries in all cases
System.out.print("Enter " + min +" to " + max + ": ");
int newGuess = key.nextInt();
if(newGuess < min || newGuess > max) { // If the entered value is out of range throw exception
throw new Exception("Value is outside of range, Try again!");
}
numberOfValidTries++; // If value is in valid range and valid format increment valid tries
int diff = high_or_low(numberToGuess, newGuess, min, max);
if(diff < 0) // If guess is lower than the picked number
{
System.out.println("Good guess but your value is too low, Try again!");
min = newGuess;
}
else if(diff > 0) // If guess is higher than the picked number
{
System.out.println("Good guess but your value is too high, Try again!");
max = newGuess;
}
else // If guess is correct
{
System.out.println("That is the correct number! It took you "+numberOfValidTries+" valid guesses, "+numberOfTries+" in total.");
break; // we found the answer, so break the while loop and show the user
}
}
catch (InputMismatchException e) {
System.out.println("Inputs must be numerical, Try again!");
key.next(); // bad input is removed from buffer
}
catch (Exception e) {
System.out.println(e.getMessage()); // Writes the exception message to screen
}
}
System.out.println("Thanks for using my program!"); //Thank you and good bye message
}
}
import java.util.Scanner;
// This program was made by Erdogan Akbiyik
/* Project 4 Hangman Program - Hangman program is a program that follows the correct and inaccuracy of the letters of the word requested from the users.
*/
public class Hangman {
private static String[] words = {"terminator", "love", "banana", "computer", "java", "cow", "rain", "water" }; // The words to be guessed from users are as follows.
private static String word = words[(int) (Math.random() * words.length)];
private static String asterisk = new String(new char[word.length()]).replace("\0", "*");
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (count < 7 && asterisk.contains("*")) {
System.out.println("Guess any letter in the word");
System.out.println(asterisk);
String guess = sc.next();
hang(guess);
}
sc.close();
}
public static void hang(String guess) {
String newasterisk = "";
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess.charAt(0)) {
newasterisk += guess.charAt(0);
} else if (asterisk.charAt(i) != '*') {
newasterisk += word.charAt(i);
} else {
newasterisk += "*";
}
}
if (asterisk.equals(newasterisk)) {
count++;
hangmanImage();
} else {
asterisk = newasterisk;
}
if (asterisk.equals(word)) {
System.out.println("Correct! You win! The word was " + word);
System.out.println("Thanks for using my program!");
}
}
// Visual graphics with false answers consist of view. In this way, users do not intend to see how close they are.
public static void hangmanImage() {
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2) {
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7) {
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
System.out.println("Thanks for using my program!");
}
}
}
import java.util.Random; //import random class
import java.util.*;
// This program was made by Erdogan Akbiyik
/* Project 4 MaxArray Program - This program is made to generate random numbers according to the indexes created according to the values in a given number of digits.
*/
public class MaxArray
{
static void labeledBubbleSort(int[] labels, int[] val) {
int n = labels.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(val[labels[j-1]] < val[labels[j]]){ // compare values array
//swap in labels array not in values array so order the labels
temp = labels[j-1];
labels[j-1] = labels[j];
labels[j] = temp;
}
}
}
}
public static void main(String[] arr)
{
int array[] = new int[1000]; // Random numbers array
int histogram[] = new int[50]; // Number counts array
int labels[] = new int[50]; // Number labels array, which later will be used in ordering
Random rand = new Random(1); //create random object and sets seed to one//create random object and sets seed to one
// Initializing array with random numbers
for(int i = 0; i < array.length; i++)
{
array[i] = rand.nextInt(50) + 1; // returns a single random integer between 1 and 50
}
// Initializing histogram and labels arrays
for(int i = 0; i < histogram.length; i++)
{
histogram[i] = 0;
labels[i] = i;
}
// we create the histogram (counts) of the array
for(int i = 0; i < array.length; i++)
{
histogram[array[i]-1]++;
}
// use a modified version of bubble sort algorithm to order the array
// classic bubble sort algorithm orders the values of the array
// in this modification algorithm there is a label and a value array
// label array contains the label values 0-49
// values array (histogram) contains the counts of the numbers in the random array
// labeledBubble sort will order the labels according to values array
labeledBubbleSort(labels, histogram);
System.out.println("Welcome to random array counter and reporter");
System.out.println("1000 numbers between 1-50 are randomly created. ");
System.out.println("Here is the most frequently picked 10 random numbers:");
//Printing most occuring 10 numbers
for(int i=0; i < 10; i++)
{
System.out.println("Number: " + (labels[i]+1) + ", Count: " + histogram[labels[i]]);
}
System.out.println("Thanks for using my program!");
}
}
No comments:
Post a Comment