For my java console application, I need to call a set of functions with user given arguments. My operations O1, O2, .. O5 are defined as an enum as
enum Operations {O1, O2, O3, O4, O5};
I need to read user input args[0] and call function F1, F2,...F5.
For example user is going to give:
>java MyApp O1 5 6
For this I suppose I need to map sting (args[0]) to an enum so that I can use switch select. How to do this?
Enum.valueOf(Class, String).
Example
Operations oper = Enum.valueOf(Operations.class, args[0]);
Will throw an exception if there are no enum values that matches args[0]
I suppose using what chuk lee 's lead you can come up with pritty cool program
here what I did.
public class Main {
public static void main(String[] args) {
if(args.length < NUMBER_OF_OPERATORS){
throw new IllegalArgumentException(INSUFFICIENT_OPERANDS);
}
Operator operator = Enum.valueOf(Operator.class,
args[OPERATION_NAME].toUpperCase());
System.out.println(operator.operate(args[FIRST_OPERAND],
args[SECOND_OPERAND]));
}
private enum Operator {
ADD,SUBSTRACT,MULTIPLY,DIVIDE;
public String operate(String aString, String bString) {
int a = Integer.valueOf(aString);
int b = Integer.valueOf(bString);
int out = 0;
switch(this) {
case ADD : out = a + b; break;
case SUBSTRACT : out = a - b; break;
case MULTIPLY : out = a * b; break;
case DIVIDE : out = a / b; break;
default: throw new UnsupportedOperationException();
}
return String.valueOf(out);
}
}
private static final int NUMBER_OF_OPERATORS = 3;
private static final int OPERATION_NAME = 0;
private static final int FIRST_OPERAND = 1;
private static final int SECOND_OPERAND = 2;
private static final String INSUFFICIENT_OPERANDS =
"Insufficient operads to carry out the operation.";
}
Related
I want the last enum to have a different value in one of the variables:
private enum thing {
thing0(0),
thing1(1),
thing2(2);
int index;
String s;
private thing(int index) {
this.index = index;
s = index == values().length - 1 ? "b" : "a";
}
}
This doesn't work; you can't call values() in the constructor. Is there another way?
In general, don't rely on the declaration order of the enum values. Item 35 in Effective Java 3rd Ed, "Use instance fields instead of ordinals", explains why. (Note that whilst you are using an instance field for s, its value depends on the ordinal.)
If you want a particular value to have a particular property, pass it in as a constructor parameter.
private enum thing {
thing0(0),
thing1(1),
thing2(2, "b");
int index;
String s;
private thing(int index) {
this(index, "a");
}
private thing(int index, String s) {
this.index = index;
this.s = s;
}
}
If you really do want it to be checking for the last value in the enum, an alternative way to do this is with a getter. Initialize a static final field in the enum to be the last value:
// Invokes `values()` twice, but meh, it's only executed once.
private static final thing LAST = values()[values().length-1];
Then check in a getter:
String s() {
return this == LAST ? "b" : "a";
}
There is no need to maintain an index that always matches the ordinal of the enum constant. Further, you can’t rely on the values() array in the constructor as it is supposed to contain the already constructed instances. But to determine, how many constants exist, it is enough to count the associated fields.
public enum Thing {
thing0,
thing1,
thing2;
final String s;
Thing() {
this.s = ordinal() == numConstants() - 1? "b": "a";
}
#Override
public String toString() {
return name() + "(index = " + ordinal() + ", s = " + s + ")";
}
private static int NUM_CONSTANTS;
private static int numConstants() {
int i = NUM_CONSTANTS;
if(i != 0) return i;
for(Field f: Thing.class.getDeclaredFields()) if(f.isEnumConstant()) i++;
NUM_CONSTANTS = i;
return i;
}
}
So System.out.println(EnumSet.allOf(Thing.class)); prints
[thing0(index = 0, s = a), thing1(index = 1, s = a), thing2(index = 2, s = b)]
Note that numConstants() caches the value in NUM_CONSTANTS which is safe as the private method is only invoked within the class initializer. We can’t use a static final variable here, as all custom class initialization will be done after the enum constants have been constructed.
I'm trying to find the index of a char that splits two numbers, the char can either be +, -, / or *. I'm making a simple calculator.
The process would be extremely trivial if i could use the indexofAny method, because i would be able to check for all 4 values in 1 line. Sadly, it's not available in Java.
NOTE: I do not want to use indexOf, since i would have to write 4 lines of nearly identical code.
My main class:
import java.util.Scanner;
public class Main {
static Scanner scanner = new Scanner(System.in);
public static MathUserInput readInput() {
String input = scanner.nextLine();
String[] parts = input.split("\\+|-|/|\\*");
int firstNumber = Integer.parseInt(parts[0]);
int secondNumber = Integer.parseInt(parts[1]);
char operation = input.charAt(1);
return new MathUserInput(firstNumber, secondNumber, operation);
}
public static void main(String[] args) {
System.out.println("This is a calculator.");
System.out.println();
System.out.println("Please provider a number, an operator and a number");
System.out.println();
MathUserInput input = readInput();
char operation = input.getOperation();
switch (operation) {
case '+':
System.out.println(input.getFirstNumber() + input.getSecondNumber());
break;
case '-':
System.out.println(input.getFirstNumber() - input.getSecondNumber());
break;
case '*':
System.out.println(input.getFirstNumber() * input.getSecondNumber());
break;
case '/':
System.out.println(input.getFirstNumber() / input.getSecondNumber());
break;
}
}
}
I'm currently using a switch statement, but i'm hoping that there's a better alternative. Essentially, i'm aiming to only have 1 line that outputs the result in the calculator.
Thank you!
You can use StringUtils#indexOfAny(CharSequence, char...) from Apache Commons Lang which does exactly what you want.
import org.apache.commons.lang3.StringUtils;
public static MathUserInput readInput() {
String input = scanner.nextLine();
int pos = StringUtils.indexOfAny(input, '+', '-', '/', '*');
return new MathUserInput(input.substring(0, pos), input.substring(pos + 1), input.charAt(pos));
}
If you don't want to include a library for this, you can always write your own utility. Have a look at the code of StringUtils to be inspired. They are basically looping over the characters of the input string and in a nested loop over the characters to be found. The index of the first match is then returned.
I think you can use enum for the operations and encapsulate logic in it:
public class Main {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
System.out.println("This is a calculator.");
System.out.println("Please provider a number, an operator and a number");
MathUserInput input = readInput(scan);
System.out.println(input.execute());
}
private static final Pattern PATTERN = Pattern.compile("(?<a>[^+\\-*\\/]+)(?<operation>[+\\-*\\/]+)(?<b>[^+\\-*\\/]+)");
private static MathUserInput readInput(Scanner scan) {
Matcher matcher = PATTERN.matcher(scan.nextLine());
if (!matcher.matches())
throw new RuntimeException("Incorrect expression");
double a = Double.parseDouble(matcher.group("a").trim());
double b = Double.parseDouble(matcher.group("b").trim());
Operation operation = Operation.parseSign(matcher.group("operation").trim().charAt(0));
return new MathUserInput(a, b, operation);
}
private static final class MathUserInput {
private final double a;
private final double b;
private final Operation operation;
public MathUserInput(double a, double b, Operation operation) {
this.a = a;
this.b = b;
this.operation = operation;
}
public double execute() {
return operation.execute(a, b);
}
}
private enum Operation {
SUM('+', Double::sum),
SUBTRACTION('-', (a, b) -> a - b),
MULTIPLY('*', (a, b) -> a * b),
DIVISION('/', (a, b) -> a / b);
private final char sign;
private final BiFunction<Double, Double, Double> func;
Operation(char sign, BiFunction<Double, Double, Double> func) {
this.sign = sign;
this.func = func;
}
public final double execute(double a, double b) {
return func.apply(a, b);
}
public static Operation parseSign(char sign) {
for (Operation operation : values())
if (operation.sign == sign)
return operation;
throw new EnumConstantNotPresentException(Operation.class, Character.toString(sign));
}
}
}
Any help or advice would be greatly appreciated. I'm dooing a simple game which generates ten different, random questions. The questions can be composed from 2, 3 or 4 integers.Something like this: 552 − 4 − 101, 102 / 3 / 3, 589 − 281, 123 + 56 + 2.
The question will be displayed in a textview and then the user can take a guess, entering values into an edittext and then upon clicking a key on a custom keypad, it will check the answer, and then display the next question until it reaches 10 questions. I have a problem with imputing the answer from the code i have. No matter what i do here i cant input the answer to the randomly generated expression.
public enum Operator {
PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/");
private String displayValue;
private Operator(String displayValue) {
this.displayValue = displayValue;
}
public String getDisplayValue() {
return displayValue;
}}
public class Question{
private List<QuestionElement> questionElements;
public Question(int sizeOfQuestionElemets) {
questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets);
}
public void addElement(QuestionElement questionElement) {
questionElements.add(questionElement);
}
public List<QuestionElement> getElements() {
return questionElements;
}
public int size() {
return questionElements.size();
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (QuestionElement questionElement : questionElements) {
sb.append(questionElement);
}
return sb.toString().trim();
}
}
public class QuestionElement {
private int value;
private Operator operator;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
#Override
public String toString() {
return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " ";
}
}
public class RandomQuestions {
static QuestionElement q = new QuestionElement();
private static final int NUMBER_OF_QUESTIONS = 10;
private static final int MIN_QUESTION_ELEMENTS = 2;
private static final int MAX_QUESTION_ELEMENTS = 2;
private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
private static final int MAX_QUESTION_ELEMENT_VALUE = 20;
private final Random randomGenerator = new Random();
public List<Question> getGeneratedRandomQuestions() {
List<Question> randomQuestions = new ArrayList<>(NUMBER_OF_QUESTIONS);
int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity();
Question question = new Question(randomQuestionElementsCapacity);
for (int j = 0; j < randomQuestionElementsCapacity; j++) {
boolean isLastIteration = j + 1 == randomQuestionElementsCapacity;
QuestionElement questionElement = new QuestionElement();
questionElement.setValue(getRandomQuestionElementValue());
questionElement.setOperator(isLastIteration ? null
: Operator.values()[randomGenerator.nextInt(Operator.values().length)]);
question.addElement(questionElement);
}
randomQuestions.add(question);
return randomQuestions;
}
private int getRandomQuestionElementsCapacity() {
return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS);
}
private int getRandomQuestionElementValue() {
return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE);
}
private int getRandomIntegerFromRange(int min, int max) {
return randomGenerator.nextInt(max - min + 1) + min;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
RandomQuestions questionGenerator = new RandomQuestions();
List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions();
for (Question question : randomQuestions) {
System.out.println(""+ question+"=?");
int answer = input.nextInt();
if (answer == q.getValue()) {
System.out.println("CORRECT");
}else{
System.err.println("STILL NOT WORKING");
}
}
}
}
In your main() you are printing question, reading an answer from the user and then comparing the answer to q.getValue(). q is a question element that is not related to question and always has value 0. So the trick is to answer 0 no matter what the question is, then the program will print CORRECT. :-)
I haven’t found anywhere in your code where you are calculating the correct value of the math expression. This would probably be a good first step towards checking whether the user has indeed entered the correct result.
Calculating the correct result is not really trivial if we insist on taking operator precedence into account. 4 + 3 * 2 should be 10 (not 14). I believe that reading about the Shunting-yard algorithm should get you some of the way. It’s an algorithm for parsing a math expression, which is only the first step towards calculating its value, but still a first step.
I suggest that the object-oriented approach will be that the Question object knows how to check an answer. Here is an implementation of the algorithm, simplified to the four operators, but extended to actually do the calculation:
public boolean checkAnswer(int answer) {
// calculate correct answer
// use shunting yard algorithm
Deque<Integer> outputQueue = new ArrayDeque<>();
Deque<Operator> operatorStack = new ArrayDeque<>();
for (QuestionElement element : questionElements) {
outputQueue.push(element.getValue());
Operator op = element.getOperator();
if (op != null) {
while (!operatorStack.isEmpty() && op.getPrecedence() <= operatorStack.peek().getPrecedence()) {
int operand2 = outputQueue.pop();
int operand1 = outputQueue.pop();
outputQueue.push(operatorStack.pop().apply(operand1, operand2));
}
operatorStack.push(op);
}
}
while (!operatorStack.isEmpty()) {
int operand2 = outputQueue.pop();
int operand1 = outputQueue.pop();
outputQueue.push(operatorStack.pop().apply(operand1, operand2));
}
int result = outputQueue.pop();
assert outputQueue.isEmpty();
return answer == result;
}
You notice that I have put some new demands on your Operator enum too. It has a precedence. And the + operator must know how to do addition (through its apply method), and similarly for the other operators:
PLUS("+", 1) {
#Override
public int apply(int operand1, int operand2) {
return operand1 + operand2;
}
},
// etc.
public abstract int apply(int operand1, int operand2);
and so on. 1 is the precedence; * and / have higher precedence, for example 2.
Now in main() you just need to write:
if (question.checkAnswer(answer)) {
If you decide to explain to the user that strict left-to-right evaluation is applied, it’s getting somewhat simpler:
public boolean checkAnswer(int answer) {
// calculate correct answer
// do left to right calculation
int result = questionElements.get(0).getValue();
for (int elementIndex = 1; elementIndex < questionElements.size(); elementIndex++) {
Operator op = questionElements.get(elementIndex - 1).getOperator();
result = op.apply(result, questionElements.get(elementIndex).getValue());
}
return answer == result;
}
The operators still need to have the apply method, but they no longer need the precedence.
I have a bean called vulnerability. It is having a column "severity".
private String severity;
Severity can hold string value High,Medium and Low. Now whenever sorting of this bean on the basis of severity column is done it happens alphabetically i.e. High,Low and Medium. But i want the sorting to happen high,medium, low when descending and low, medium,high when ascending.
I was seeing comparator to make this custom sorting but it needs to cover lots of cases. Isn't their any other way?
You can (and should) use an enum - not a String nor a int:
enum Severity {
LOW, MEDIUM, HIGH;
}
Usage:
List<Severity> lst = new ArrayList<Severity>();
lst.add(Severity.MEDIUM);
lst.add(Severity.LOW);
lst.add(Severity.HIGH);
for (Severity s : lst)
System.out.println("s = " + s);
Collections.sort(lst);
System.out.println();
for (Severity s : lst)
System.out.println("s = " + s);
OUTPUT:
s = MEDIUM
s = LOW
s = HIGH
s = LOW
s = MEDIUM
s = HIGH
EDIT
Since the OP says he can't modify the usage of Strings, we can map the strings into a comparable values:
static Map<String, Integer> severities = new HashMap<String, Integer>();
static {
severities.put("LOW",1);
severities.put("MEDIUM",2);
severities.put("HIGH",3);
}
public static void main(String[] args) {
List<String> lst = new ArrayList<String>();
lst.add("MEDIUM");
lst.add("LOW");
lst.add("HIGH");
for (String s : lst)
System.out.println("s = " + s);
Collections.sort(lst, new Comparator<String>() {
public int compare(String a1, String a2) {
Integer v1 = severities.get(a1);
Integer v2 = severities.get(a2);
return v1.compareTo(v2);
}
});
System.out.println();
for (String s : lst)
System.out.println("s = " + s);
}
and if you want to order the items in descending order you can sort and then reverse:
Collections.sort(lst);
Collections.reverse(lst);
There is an implicit compareTo operator defined on enums, which takes their declaration order to mean "smaller than". No additional code is needed.
enum Severity { Low, Medium, High }
Low.compareTo(High); // returns -1
Medium.compareTo(Low); // returns 1
However, note that the names of the enum constants will be those printed by toString() (and therefore visible to users if you echo enums directly) - if you want to use different internal and external names, possibly to uphold code conventions (say, all-caps-constants), then you will need to add an enum constructor and override the enum's toString method to use the passed-in constructor attribute.
If you cannot use enums, and you cannot change your bean
Then build a Comparator for it:
public class SeverityComparator implements Comparator<String> {
private int direction;
public SeverityComparator(boolean reverse) {
this.direction = reverse ? -1 : 1;
}
private int severity(String s) {
if (s.equals("Low")) { // you really should have constants for the values...
return 0;
} else if (s.equals("Medium")) {
return 1;
} else if (s.equals("High")) {
return 2;
} else {
throw new IllegalArgumentException("Not a severity: " + s);
}
}
#Override
public int compareTo(String other) {
return direction * (severity(this) - severity(other));
}
}
Use as
Collections.sort(listOfSeverities, new SeverityComparator(false)); // ascending
Collections.sort(listOfSeverities, new SeverityComparator(true)); // descending
#alfasin answer is correct but i would suggest using guava's Ordering:
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import javax.annotation.Nullable;
import java.util.List;
public class SeveritySortTest {
private static final List<Severity> SEVERITY_LIST = ImmutableList.copyOf(Severity.values());
public static void main(String[] args) {
Ordering<Severity> severityOrdering = Ordering.natural().onResultOf(new Function<Severity, Integer>() {
#Nullable
#Override
public Integer apply(#Nullable Severity input) {
return input.getSeverity();
}
});
List<Severity> sortedAscending = severityOrdering.sortedCopy(SEVERITY_LIST);
List<Severity> sortedDescending = severityOrdering.reverse().sortedCopy(SEVERITY_LIST);
}
enum Severity {
LOW(1), MEDIUM(2), HIGH(3);
private int severity;
Severity(int s) {
severity = s;
}
int getSeverity() {
return severity;
}
}
}
Working Solution:
Collections.sort(recommendations, new Comparator() {
private int priority(String s) {
if (s.equalsIgnoreCase("Low")) {
return 1;
} else if (s.equalsIgnoreCase("Medium")) {
return 2;
} else if (s.equalsIgnoreCase("High")) {
return 3;
} else {
return 0;
}
}
#Override
public int compare(Recommendation o1, Recommendation o2) {
return -1 * (priority(o1.getPriority()) - priority(o2.getPriority()));
}
});
If you want the DB to do this through JPA/Hibernate you could create a sort expression based on a simple case statement, assuming your entity is called Case:
Expression exp = criteriaBuilder.selectCase(root.get(Case_.priority)).when("High", 1).when("Medium", 2).otherwise(3);
queryBuilder.orderBy(orderDir.isAscending() ? criteriaBuilder.asc(exp) : criteriaBuilder.desc(exp));
Using case statements in an order by clause isn't great for performance, but solves it. Works with Oracle.
I want the user to input a DNA sequence, if it doesn't have the letters A, C, T, or G then it should print out an error. But how can I scan the string entered for those specific characters in the constructot method DNASequence?
heres what I have so far.
import java.util.*;
public class DNASequence {
private String DNASequence;//create a private static variable that can be accessed
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please input a sequence of DNA: ");
String DNAInput = input.nextLine();
}
public DNASequence(String DNAStrand){//Constructor Method that takes parameter a string and checks to see if its only A, T, C, G.
DNASequence = DNAStrand;
// Invoke the countLetters method to count each letter
int[] counts = countLetters(DNAStrand.toUpperCase());
// Display results
for (int i = 0; i < counts.length; i++) {
if (counts[i] != 0)
System.out.println((char)('a' + i) + " appears " +
counts[i] + ((counts[i] == 1) ? " time" : " times"));
}
}
/** Count each letter in the string */
public static int[] countLetters(String s) {
int[] counts = new int[26];
for (int i = 0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)))
counts[s.charAt(i) - 'a']++;
}
return counts;
}
public String toString(){//Method that just returns the stored sequence
return DNASequence;
}
private static char NucleotideBaseCount(char BaseCount){//Method to count bases
}
private static boolean isSubsequenceOf(String DNAStrand){
}
}
You could use the following regular expression for this: ^[ACTG]+$.
To match the input string against the regex, use String.matches().
Here in a sample implementation based on #NPE 's comment:
import java.util.*;
public class DNASequence
{
private String DNASequence = null; //create a private static variable that can be accessed
public static void main(String[] args)
{
System.out.println("Please input a sequence of DNA: ");
DNASequence dnaS = new DNASequence((new Scanner(System.in)).nextLine().toUpperCase());
}
//Constructor Method that takes parameter a string and checks to see if its only A, T, C, G.
public DNASequence(String DNAStrand) throws IllegalArgumentException
{
if (DNAStrand.matches("^[ATCG]+$"))
{
DNASequence = DNAStrand;
}
else
{
throw new IllegalArgumentException("DNA Sequences should only contain A, T, C, G charaters");
}
}
/** Count each letter in the string */
public int[] countLetters() throws IllegalArgumentException
{
int[] counts = new int[4];
if (DNASequence != null)
{
for (int i = 0; i < DNASequence.length(); i++)
{
switch (DNASequence.charAt(i))
{
case 'A':
counts[0]++;
break;
case 'T':
counts[1]++;
break;
case 'C':
counts[2]++;
break;
case 'G':
counts[3]++;
break;
default:
throw new IllegalArgumentException("DNA Sequences should only contain A, T, C, G charaters, found: " + DNASequence.charAt(i));
}
}
}
return counts;
}
//Method that just returns the stored sequence
public String toString()
{
return DNASequence;
}
private char NucleotideBaseCount(char BaseCount){//Method to count bases
return 'a'; // replace with real implementation
}
private boolean isSubsequenceOf(String DNAStrand)
{
return false; // repalce with real implementation
}
}