Changing Directories

master
Hammy 4 years ago
parent 0a3dfd2ca5
commit d395f02d18

@ -1,46 +0,0 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
public class AnagramGenerator {
static ArrayList<Character> getStringList(String userWord) {
/* Return a character list of the string input by the User */
// Define fixed length array of the same length as the word
ArrayList<Character> charList = new ArrayList<>();
// Add each character to the list of characters
for (int i = 0; i < userWord.length(); i++)
charList.add(userWord.charAt(i));
return charList;
}
static String generateAnagram(ArrayList<Character> charList) {
/* Returning an Anagram of the word input by the User */
StringBuilder stringBuilder = new StringBuilder();
while (!charList.isEmpty()) {
int randomIndex = new Random().nextInt(charList.size());
stringBuilder.append(charList.get(randomIndex));
charList.remove(charList.get(randomIndex));
}
return stringBuilder.toString();
}
public static void main(String[] args) {
/* Let the user enter in a word and generate an Anagram from it*/
Scanner scan = new Scanner(System.in);
System.out.println("Please enter in a word that you would like to see an Anagram of");
String userWord = scan.next();
ArrayList<Character> charList = getStringList(userWord);
String finalAnagram = generateAnagram(charList);
System.out.println("Your Anagram: " + finalAnagram);
scan.close();
}
}

@ -1,7 +0,0 @@
public interface Animal {
String animalName();
String animalSound();
double animalPrice();
}

@ -1,18 +0,0 @@
public class Cat implements Animal {
@Override
public String animalName() {
return "Cat";
}
@Override
public String animalSound() {
return "Meoooww";
}
@Override
public double animalPrice() {
return 29.99;
}
}

@ -1,18 +0,0 @@
public class Cow implements Animal {
@Override
public String animalName() {
return "Cow";
}
@Override
public String animalSound() {
return "Moooo";
}
@Override
public double animalPrice() {
return 10.99;
}
}

@ -1,18 +0,0 @@
public class Dog implements Animal {
@Override
public String animalName() {
return "Dog";
}
@Override
public String animalSound() {
return "Woof Woof";
}
@Override
public double animalPrice() {
return 15.99;
}
}

@ -1,32 +0,0 @@
public class Helper {
private final Animal[] animals;
private double averagePrice;
public Helper(Animal[] animals) {
this.animals = animals;
}
public void displayInformation() {
int total = 0;
double sum = 0.0;
for (Animal animal : animals) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n\nName: " + animal.animalName());
stringBuilder.append("\nSound: " + animal.animalSound());
stringBuilder.append("\nPrice: £" + animal.animalPrice());
System.out.println(stringBuilder.toString());
total++;
sum += animal.animalPrice();
}
averagePrice = sum / total;
}
public void displayAveragePrice() {
System.out.printf("\nThe Average Price For All The Animals Are: £%.2f", averagePrice);
}
}

@ -1,18 +0,0 @@
public class Horse implements Animal {
@Override
public String animalName() {
return "Horse";
}
@Override
public String animalSound() {
return "Neighh";
}
@Override
public double animalPrice() {
return 59.99;
}
}

@ -1,17 +0,0 @@
public class Main {
public static void main(String[] args) {
displayAnimals();
}
private static void displayAnimals() {
Helper helper = new Helper(new Animal[] {
new Cow(),
new Cat(),
new Dog(),
new Horse()
});
helper.displayInformation();
helper.displayAveragePrice();
}
}
Loading…
Cancel
Save