As far as I've researched, there's no way to match a String with a variable. probably I'm using the wrong word, here's what I mean by matching:
String grade="a";
double a=4.0;
And there's no way to associate the value of String grade with double a.
Similarly, what I want to do is associating value of a String with a method. Maybe I'm on the wrong track, let me briefly explain what I'm trying to achieve:
In the class player, there's a String name() method that returns This.name. There's no graphical design, and the only way for user to communicate with the program is typing. Basically, when person types name, I want name method to be ran. I'm pretty new, and the only way I can think of doing it is using a bunch of if statements, and adding another if statement for each method I add does not sound right to me.
Note: The reason I need String to be associated is because I'm going to use javax.swing.JTextArea to get input from the user, which returns String.
Thanks in advance
Yes. It's called a Map.
Here's some sample code of how to use one:
Map<String, Double> map = new HashMap<>();
map.put("grade", 4.0);
double a = map.get("grade");
If you want to store a variety of value types use Map<String, Object>, but you'll have to make unsafe casts when retrieving and using the values returned from get().
Java is not a dynamic interpreted language like Python or Perl.
To associate arbitrary strings with values you should use a Map<String,ValueClass> where ValueClass is the value to associate, such as Integer, Float, BigDecimal or your own value class.
Because grades are usually fixed from A to F, I would use an enum to map each grade to a numeric value:
enum Grade {
A(4.0), B(3.0) // etc...
private double val;
private Grade(double val) {
this.val = val;
}
public double getVal() {
return val;
}
}
Then use Grade.A.getVal() when you need the numeric value of the A grade.
Related
I have something like:
Map<String, Object> hashMap;
When I do something like:
hashMap.get("binary"), I get value as: {size=5642, name=abc}
Here key is binary and value is an Object of Type Object and is {size=5642, name=abc}
Note the values dont belong to a particular class.
In Python I can do something like hashMap["binary"]["size"], was wondering what would be the equivalent in java
How do I get the value of size directly without parsing the above string?
The value is not of Type Object, but of some type that extends from Object (in java everything extends Object implicitly). Let's call it "X"
Now, it doesn't work like python because unlike python java doesn't have that dynamic nature.
{size=5642, name=abc} is probably a string representation of that type X. This is what you see in a debugger or maybe when trying to print the value on console with System.out.println or something.
Now first of all figure out which type is it:
Object value = hashMap.get("binary")
System.out.println(value.getClass().getName());
It will print the class name
Then check the source of that class, probably it looks like this:
public class X {
private final int size;
private final String name;
... // constructor, other stuff maybe
// these are called "getters" in java world
public int getSize() {return size;}
public String getName() {return name;}
}
From that point you have 2 ways to get the size:
Object value = hashMap.get("binary");
int size = ((X)value).getSize(); // This is called downcasting
The drawback of this method is that you don't utilize the power of generics
So the better option is a refactoring if its possible of course:
Map<String, X> hashMap = ...
X value = hashMap.get("binary");
value.getSize();
One final note:
If it happens that the value is of type String, you won't be able to get the size other than parsing the value with regular expression or something. In this case consider a refactoring as a better option.
My professor wants us to make an enum called MedicalSpecialty, with GENERAL_MEDICINE, PEDIATRICS, and ONCOLOGY as members of the enumeration (so far so good).
Then he wants us to define a method called getFromString inside the MedicalSpecialty enum "that takes a String parameter and returns a MedicalSpecialty with the same name as the String parameter"
I'm not sure what he means, but then he says:
"Hint: use the toString() method from the MedicalSpecialty enum to perform your checks"
I'm not looking for a solution, but rather an explanation of what he is asking, if anyone understands. Is the getFromString method meant to take in a String like "general_medicine" and then output "GENERAL_MEDICINE" as type MedicalSpecialty? That seems useless and probably wrong...
Any help would be appreciated!
You have the right idea. Think of it this way:
Suppose you are designing a system that works with components that function across the globe and you use the internet to communicate between them. A component in Europe, wants to request a new doctor of Oncology to be transferred from the US component. It can't send a MedicalSpeciality enum over the wire, so instead it sends a String, e.g. "Oncology". Now, in the code of your US component, you want to translate that piece of text to something that your US component system understand: the enum.
You need to write a method that takes the input String sent over the wire and returns the corresponding Enum value.
He means that valid input for your function will be the following:
"GENERAL_MEDICINE", "PEDIATRICS", "ONCOLOGY"
Your task is to convert the type String to the type Enum.
He probably wants you to show that you know how to loop through all the elements of an enum and compare each toString result to the passed in string.
You're right, that's not the best way to do it.
public enum Medicine {
GENERAL_MEDICINE("general_medicine"),
PEDIATRICS("pediatrics");
private final String value;
Medicine(String v) {
value = v;
}
public String value() {
return value;
}
public static Medicine fromString(String v) {
for (Medicine c : Medicine.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
I'm a beginner in Java programming, and I'm trying to make a voting machine program, where you can vote for Republicans or Democrats. My question is, how can I edit my method so I would be able to return two strings with two distinct values?
For example, look at my code all the way in the bottom. It's wrong, but I wanted the tester to be able to print out Democrats: (some number) and Republicans: (some number) in one method. How can I do that?
import java.lang.String;
public class VotingMachine1 {
private double Democrats;
private double Republicans;
public VotingMachine1() {
Democrats = 0;
Republicans = 0;
}
public void voteRepublican() {
Republicans = Republicans + 1;
}
public void voteDemocrat() {
Democrats = Democrats + 1;
}
public void clearMachineState() {
Republicans = 0;
Democrats = 0;
}
//this is where I'm having difficulties. I know its wrong
public double getTallies() {
System.out.println("Democrats: ", return Democrats);
System.out.println("Republicans: ", return Republicans);
}
}
No return is necessary there, since you aren't leaving a function. To do what you seem to want to do, just replace that last method with the following:
public void getTallies()
{
System.out.println("Democrats: " + Double.toString(Democrats));
System.out.println("Republicans: " + Double.toString(Republicans));
}
Also, since your votecounts should only ever be integers, there's no reason to declare them as doubles instead of ints.
What you are looking for here is a format string. A format string is used when you know what your output should look like, and only have a few "holes" where unknown data should be filled in. To output your data using format strings, you would use the System.out.format(String, Object...) method:
System.out.format("Democrats: %f\n", Democrats);
System.out.format("Republicans: %f\n", Republicans);
In this case, the %f indicates that a floating-point number (since your variables are declared as double) will be printed instead of the %f. However, you may wish to consider declaring them as int (or long) instead, in which case you would use %d instead of %f in the format strings.
Finally, you ought to change your getTallies() method to return void instead of double, as you are printing the values, not returning them.
Your code and your description are so contradictory, it is not clear that you even know what you are trying to do. I believe that this is the real root of your problems.
Here goes:
public double getTallies()
{
System.out.println("Democrats: ", return Democrats);
System.out.println("Republicans: ", return Republicans);
}
First, your question says that you want to "return two strings with two values" ... but you have declared the method as returning one double.
Next, your code is printing values ... not returning them.
You've also made some major mistakes at the syntactic level, largely (I believe) because you are trying to do contradictory things:
return Republicans is not a valid Java expression, so you can't use it as a argument to the println method.
The println method can't be called with two arguments, as your code is trying to do. There is a zero argument version and a number of one argument overloads ... but no overloads with two or more arguments.
Basically, you need to start by making up your mind about what this method is supposed to do. Is it supposed to:
return the tallies (as two doubles)?
return a string representing the two tallies?
return nothing ... and output the two tallies to standard output?
do something else?
Once you've made up your mind:
code the method to do what you've decided it should do, and
chose a method name that correctly reflects what it is supposed to do. Hint: a method that starts with get is conventionally a "getter" that returns the attribute or attributes themselves ... not a String rendering.
double is a bad choice of type for a vote count too:
You cannot have a fractional vote.
You want to represent vote counts precisely and floating point types (like double) are not precise. (Or at least, not in the sense that you require.)
When you attempt to format or output a double, the resulting character string is likely to include a pesky decimal point ... or worse.
You should use int or long instead of double.
Finally, this is a serious Java style violation, and should get you a significant penalty if your marker is paying attention.
private double Democrats;
private double Republicans;
Variable names in Java should start with a LOWER CASE letter.
A few more random comments:
import java.lang.String; is superfluous as all classes in package java.lang are automatically imported in every Java source file.
Votes can not be fractional. People can't vote 0.75 candidate A, and 0.25 candidate B. If you use integer datatypes (int or long), you will be reflecting this fact better. Also, you will be saving yourself a lot of headache when you start obtaining results like 379857.999999. This is because floating point types have a better range, but worse precision (especially noticeable when working with pure integers).
According to Java usual naming conventions, variable names should start with a lowecase letter.
A better name for function getTallies is printTallies.
For output purposes, it's much better to use string formatting than concatenation. Some advantages are: multiple formats supported, ease of use, and internationalization.
Putting all together:
private int democratVotes;
private int republicanVotes;
public void printTallies() {
System.out.format("Democrats: %,d%n",democratVotes);
System.out.format("Republicans: %,d%n",republicanVotes);
}
In this particular case, votes will be printed with thousand separation (ex: 3,345,623 instead of 3345623). Check Java's Formatting Numeric Print Output tutorial.
Thinking better about it, there are some alternatives where getTallies would effectively be returning some form of value:
1) Make it to return a String with both tallies. It would be hard and inefficient to separate the tallies later, though.
public String getTallies() {
return "Democrats: %,d votes. Republicans: %,d votes.%n".format(democratVotes,republicanVotes);
}
2) Make it to return an array.
public int[] getTallies() {
return new int[2]{ democratVotes, republicanVotes };
}
public int[] getTallies1() { // Same as getTallies, but written step by step.
int[] result= new int[2] ;
result[0]= democratVotes ;
result[1]= republicanVotes ;
return result ;
}
3) Make it to return a class.
public VotingMachineResults getTallies() {
return VotingMachineResults(democratVotes,republicanVotes) ;
}
public static class VotingMachineResults {
private int democratVotes;
private int republicanVotes;
public VotingMachineResults(democratVotes,republicanVotes) {
this.democratVotes= democratVotes ; // `this` required to disambiguate field democratVotes from parameter democratVotes.
this.republicanVotes= republicanVotes ;
}
public int getDemocratVotes() {
return democratVotes ;
}
public int getRepublicanVotes() {
return republicanVotes ;
}
}
As you can see, this class is very similar to VotingMachine1, but it does not accept internal state changes. It is a "value" class.
In Java, you concatenate Strings with the + operator. Proper syntax for what you were trying to do looks like this:
System.out.println("Democrats: " + Democrats);
System.out.println("Republicans: " + Republicans);
A return statement is only used when you want to return some object or value to a method that called your current method. It is not appropriate in this place since you're only passing a value to another method (println()).
ALSO, you need to fix your getTallies() method. Make it return void instead of double since you aren't returning anything.
Here's something completely different: why not override toString()?
Presumably, any instance of VotingMachine1 will apply for all votes that you care about for that instance. That is to say, you don't create a new instance of a VotingMachine1 every time someone casts a vote.
So, what you can do is override the toString() method. We'll also use String.format() to handle the numerical values.
#Override
public String toString() {
// assumes that Democrats and Republicans are declared as int
// since it's pointless to indicate percentages of a vote
return String.format("Democrats: %d\nRepublicans: %d", Democrats, Republicans);
}
Now, whenever you vote, you can use the toString() method to get the information (which is called whenever one does System.out.println(object).
VotingMachine1 voter = new VotingMachine1();
voter.voteDemocrat();
voter.voteRepublican();
System.out.println(voter);
/* This prints:
Democrats: 1
Republicans: 1
*/
A less specific answer to your question would be to return an Object called (say) Votes
public class Vote {
int democratVotes
int republicanVotes
}
and then make your VotingMachine class simply return an instance of this object (suitably changed to make it immutable).
On my project we have created a generic version of this called a Tuple that returns a pair of values in a single object - it has an overloaded toString method for easy printing.
you can return an array with [0] and [1] as key and devide it on the basis of your need..
like
returnArray[0]="first string";
returnArray[1]="second string";
and use it ur way...
For later reference:
Operations o = new Operations(); //class containing the operation methods
HashMap<String, Method> ops = new HashMap<String, Method>();
I'm working on a program that will parse a mathematical expression input via console or eventually maybe a GUI.
Currently, I have a class called "Operations" which has various basic math functions (more will be added later, just testing right now). In another class, I have a method which calculates the result by taking an operand, the successive operator, and another operand, and invoking a method to evaluate the expression. It stores the info necessary to the calculation in:
double numOne = //...
char operator = //...
double numTwo = //...
double result = //...
Now I don't want to have a long switch/case statement or if statement saying:
if (operator.equals("+")) //I know .equals() doesn't use chars; it's an example
o.add(numOne, numTwo);
else if (operator.equals("-"))
o.subtract(numOne, numTwo);
on and on for every operation. That's why I tried creating a HashMap<String, Method> to store the operator (String) and the method which should be called. Basically, in the current class' constructor, I put:
ops.put("+", o.getClass().getMethod("add", double.class, double.class));
ops.put("-", o.getClass().getMethod("subtract", double.class, double.class));
//etc. Which in and of itself is also pretty lengthy
Now to once the method is identified via operator, I need another method to return a Method to call.
private Method getMethodFromKey(HashMap<String, Method> map, char op) {
Method method = null;
for (Map.Entry<String, Method> e: map.entrySet()) {
if (e.getKey().equals(op))
method = e.getValue();
}
return method;
}
Finally, once I have the correct method, I can call it with:
getMethodFromKey(ops, operator).invoke(o, numOne, numTwo);
That all works great. My problem is, the method(s) I am/will be invoking are getter methods; they return a double. Example:
public double add(double one, double two) {
double answer = 0;
answer = one + two;
return answer;
}
I guess this is all just a long-winded way of asking is there a way to assign the returned value of an innvoked method? Something like:
result = getMethodFromKey(ops, operator).invoke(o, numOne, numTwo); //This doesn't work :(
Any help is appreciated. Additionally, if my aproach is completely wrong, I would appreciate a bump in the right direction.
DISCLAIMER: I'm relatively inexperienced at Java and known for overcomplicating things, so please point out any grievous flaws in my design. :)
invoke() returns Object and since Java does not know how to assign an Object to a double this won't compile. invoke starts by boxing the double from the method into a Double. You have to cast it now from Object to Double (and could then call .doubleValue() but that's done automatically) for that to work.
I'm [...] known for overcomplicating things, so please point out any grievous flaws in my design. :)
Using reflection instead of an interface. A Method is a function object. But it's not type-safe to use. An interface can do the same without those problems.
interface Operation {
double evaluate(double a, double b);
}
Then put objects that implement the interface in your map:
ops.put("+", new Operation() {
public double evaluate(double a, double b) {
return a+b;
});
and you can do
double result = getMethodFromKey(ops, operator).evaluate(numOne, numTwo);
The need to cast is gone.
If you're sure that all of your operations are going to be on a single class (no extensibility), then you should consider using an enum instead. You can add an instance field to the enum to represent the character command corresponding to the operation and then have an abstract evaluate method that's implemented by each enum value.
If invoke() is returning an Object that you know is a double, you can cast it like so:
result = (Double) getMethodFromKey(ops, operator).invoke(o, numOne, numTwo);
Since double is a primitive, which is not of type Object, you need to cast it to a Double, and through unboxing, we get a double.
While reading Effective Java I came across the suggestion to "use enums instead of int constants". In a current project I am doing something similar to that below:
int COL_NAME = 0;
int COL_SURNAME = 1;
table[COL_NAME] = "JONES"
How would I use enums instead to achieve this? Due to the interface I'm forced to use, I must use an int for my index. The example above is just an example. I'm actually using an API that takes an int for index values.
Applying one usefull pattern together with an anti-pattern often fails ;-)
In your case using an array for not-really array-like data provides a problem when you want to replace int constants with enum values.
A clean(er) solution would be something like an EnumMap with the enum values as keys.
Alternatively you could use table[COL_NAME.ordinal()] if you absolutely must.
If some API forces you to pass around int values but you have control over the actual values (i.e. you could pass your own constants), then you could switch to using enum values in your code and convert to/from enum only at the places where your code interfaces with the API. The reverse operation of enumValue.ordinal() is EnumClass.values()[ordinal]).
It sounds like you are trying to use a EnumMap. This is a Map which wraps an array of values.
enum Column {
NAME, SURNAME
}
Map<Column, String> table = new EnumMap<Column, String>(Column.class);
table.put(Column.NAME, "JONES");
String name = table.get(Column.NAME);
This would much simpler if you used a POJO.
classPerson {
String name;
String surname;
}
Person person = new Person();
person.name = "JONES";
String name = person.name;
Depends on the requirements. You could use Enum.ordinal() to convert an enum to an int.
Note that it's not possible to pass an Enum directly as the index.
Edit:
Another possibility would be to use a Map<YourEnum, String> map and then use map.get(EnumValue).
Since you have to use that table and thus can't actually use an EnumMap, I personally think the best solution would be to stick with what you have. In an enum, the ordinal values of the elements are not supposed to have any intrinsic meaning, while in your case they do, since they are used as indices into the table.
The problem you have is not that you are not using enums, it's that you need magic values to extract column data out of a table. In this case, using integer constants is the right tool for the job, unless if you can tackle the underlying problem of that table.
Now you could tackle it by wrapping the table in your own class that accesses it, and use enums in and out that class. But this introduces more code, another layer of indirection and doesn't actually solve any problems for you, except that an enum is a bit easier to maintain than a list of int values (greatly offset by you having to maintain the wrapper you now wrote).
You could consider this work if you are writing a public API that other people will use, since it will avoid having them depend on some magic values that might change over time (tight coupling which will break things). If you are, then a wrapper which uses an EnumMap internally is likely the way to go.
Otherwise, leave it as is.
Depending on the values of your array, you might be looking at an object instead. For examples if your array looks something like this
person[FIRST_NAME] = "Jim Bob"
person[SURNAME] = "Jones"
person[ADDRESS] = "123 ABC St"
person[CITY] = "Pleasantville"
...
Then what you really want is something like this
Person jimBob = new Person("Jim Bob", "Jones");
jimBob.setAddress("123 ABC St", "Pleasantville", "SC");
....
See Refactoring: Replace Array with Object
I have also run into this problem, and having come from the C++ world I wasn't expecting to run into this.
The beauty of enum is that can create a series of related constants with unique values where the actual value is irrelevant. Their use makes code easier to read and guards against variables being set to invalid enum values (especially in Java), but in a situation like this it is a major pain. While you might have "enum Pets { CAT, BIRD, DOG }" and don't really care what value CAT, BIRD, and DOG actually represent, it's so nice and clean to write:
myPets[CAT] = "Dexter";
myPets[BIRD] = "Polly";
MyPets[DOG] = "Boo-Rooh";
In my situation I ended up writing a conversion function where you pass in an enum value and I return a constant. I hate doing this with a passion, but it's the only clean way I know to maintain the convenience and error checking of enum.
private int getPetsValue(Pets inPet) {
int value = 0;
switch (inPet) {
case CAT: value = 0; break;
case BIRD: value = 1; break;
case DOG: value = 2; break;
default:
assert(false);
value = 0;
break;
}
return value;
}
You can define an enum with a constructor like so:
public enum ArrayIndex {
COL_NAME(0), COL_SURNAME(1);
private int index;
private ArrayIndex(int index) {
this.index = index;
}
public int getIndex() {
return this.index;
}
};
And then use it like this:
public static void main (String args[]) {
System.out.println("index of COL_NAME is " + ArrayIndex.COL_NAME.getIndex());
}