Skip to content

Instantly share code, notes, and snippets.

@dzt
Last active October 16, 2017 01:17
Show Gist options
  • Save dzt/1431a2499d57b103cc1b762cac6cbc2d to your computer and use it in GitHub Desktop.
Save dzt/1431a2499d57b103cc1b762cac6cbc2d to your computer and use it in GitHub Desktop.
Chp5 Ex1, 2
import java.util.HashMap;
public class Card {
private String input;
private HashMap<Integer, String> numberDictionary = new HashMap<Integer, String>();
private HashMap<String, String> cardDictionary = new HashMap<String, String>();
public Card(String input) {
this.input = input;
assignValues();
}
public String getDescription() {
if (input == null) {
return "No Input was assigned.";
}
String cardOutput, numberOutput;
if (input.length() == 2) {
cardOutput = cardDictionary.get(input.substring(1));
numberOutput = numberDictionary.get(Integer.parseInt(input.substring(0, 1)));
} else if (input.length() == 3) {
cardOutput = cardDictionary.get(input.substring(2));
numberOutput = numberDictionary.get(Integer.parseInt(input.substring(0, 2)));
} else {
return "Invalid Input, please try again.";
}
if (cardOutput == null || numberOutput == null) {
return "Invalid Input, please try again.";
}
return numberOutput + " of " + cardOutput;
}
private void assignValues() {
cardDictionary.put("A", "Ace");
cardDictionary.put("J", "Jack");
cardDictionary.put("Q", "Queen");
cardDictionary.put("K", "King");
cardDictionary.put("D", "Diamonds");
cardDictionary.put("H", "Hearts");
cardDictionary.put("S", "Spades");
cardDictionary.put("C", "Clubs");
numberDictionary.put(1, "One");
numberDictionary.put(2, "Two");
numberDictionary.put(3, "Three");
numberDictionary.put(4, "Four");
numberDictionary.put(5, "Five");
numberDictionary.put(6, "Six");
numberDictionary.put(7, "Seven");
numberDictionary.put(8, "Eight");
numberDictionary.put(9, "Nine");
numberDictionary.put(10, "Ten");
}
}
public class QuadraticEquation {
private double a, b, c;
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getSolution1() {
if (Double.isNaN((-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a))) {
return 0.0;
}
return (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
}
public double getSolution2() {
if (Double.isNaN((-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a))) {
return 0.0;
}
return (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
}
boolean hasSolutions() {
if (Double.isNaN(getSolution1()) && Double.isNaN(getSolution2())) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment