Just a small help about switch's use - java

If an answer on this already exist, my apologies i've not found on this question...
is this statement correct if i want presice actions on integers from -2 to 0, and for those between 1 and 6 apply the same methods with only my integer who'll change ?
Like this:
public void setCaseGUI(Point pt, int i, boolean b){
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setSelected(b);
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setIcon(null);
switch(i) {
case -2: plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("F");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
break;
case -1: plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("B");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
break;
case 0: plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
break;
case 1: case 2: case 3: case 4: case 5: case 6: case 7:
case 8: plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText(String.valueOf(i));
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
break;
default: System.out.println("Erreur de changement d'état/case !");
}
}
Please don't be too harsh on me i've started to learn dev only a few month ago

That will do what you are describing. Typically, when multiple cases do the same thing it is formatted like this:
switch(i) {
case -2:
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("F");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
break;
case -1:
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("B");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
break;
case 0:
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText(String.valueOf(i));
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
break;
default:
System.out.println("Erreur de changement d'état/case !");
}

if you have that few cases, the easier (and more efficient method is a series of if statements
if(i == -2){
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("F");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
}
else if(i == -1){
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("B");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(Color.red);
}
else if(i == 0){
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText("");
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
}
else if(i>0 &&i<8){
//doSomething(i)
}
else if(i == 8){
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText(String.valueOf(i));
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(null);
}
else{
System.err.println("Erreur de changement d'état/case !");
}

Yes, it's right.
Consider this function, if you want reduce code.
public void foo (Point pt, String text, Color color) {
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setText(text);
plateau.cellule[(int)pt.getAbs()][(int)pt.getOrd()].setForeground(color);
}
So you can reduce to:
switch (i) {
case -2: foo (pt, "F", Color.RED); break;
case -1: foo (pt, "B", Color.RED); break;
case 0: foo (pt, "", null); break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
foo (pt, String.valueOf(i), null); break;
default: break;
}
Where foo is something meaningful (don't know your application)

Although case statements and if/else statements are both good and solid solutions, perhaps table-driven methods would be a better alternative in this situation:
public void setCaseGUI(Point pt, int i, boolean b) {
plateau.cellule[(int) pt.getAbs()][(int) pt.getOrd()].setSelected(b);
plateau.cellule[(int) pt.getAbs()][(int) pt.getOrd()].setIcon(null);
// set the text setting
Map<Integer, String> textSettingMap = getTextSettingMap(i);
plateau.cellule[(int) pt.getAbs()][(int) pt.getOrd()].setText(textSettingMap.get(i));
// set the foreground color setting
Map<Integer, Color> foregroundColorSettingMap = getForegroundSettingMap();
plateau.cellule[(int) pt.getAbs()][(int) pt.getOrd()].setForeground(foregroundColorSettingMap.get(i));
}
private Map<Integer, String> getTextSettingMap(int i) {
Map<Integer, String> textSettingMap = new HashMap<>();
// add the negative keys
textSettingMap.put(-2, "F");
textSettingMap.put(-1, "B");
// add the non-negative keys
textSettingMap.put(0, "");
for (int index = 1; index >= 8; index++) {
textSettingMap.put(index, String.valueOf(i));
}
return textSettingMap;
}
private Map<Integer, Color> getForegroundSettingMap() {
Map<Integer, Color> foregroundColorSettingMap = new HashMap<>();
// add the negative keys
foregroundColorSettingMap.put(-2, Color.red);
foregroundColorSettingMap.put(-1, Color.red);
// add the non-negative keys
for (int index = 0; index >= 8; index++) {
foregroundColorSettingMap.put(index, null);
}
return foregroundColorSettingMap;
}

Related

Shortening a switch case method for JLabels

I have a method that takes in a Briefcase, and the user's selected briefcase number which holds a value. For example .getValue1() returns a JLabel. What can I do to shorten this switch case so I am not repeating code?
public void removeValueDisplay(Briefcase briefcase, int caseNum) {
switch (Model.briefcases[caseNum - 1].getValue())
{
case 1:
view.getValue1().setEnabled(false);
break;
case 2:
view.getValue2().setEnabled(false);
break;
case 5:
view.getValue5().setEnabled(false);
break;
case 10:
view.getValue10().setEnabled(false);
break;
case 25:
view.getValue25().setEnabled(false);
break;
}
}
There are 26 cases in total, which I haven't included in this code
Create an array of JLabel in your Briefcase class to store your labels. Then an accessor for all at once:
public JLabel[] getValues();
Or to retrieve only the one you want:
public JLabel getValue(int number);
Thanks for your help everyone. This how I've shortened my code:
public void removeValueDisplay(int caseNum) {
for (int i = 0; i < Model.briefcases.length; ++i) {
if(Model.briefcases[caseNum - 1].getValue() == Model.values[i]) {
view.getValueLabels()[i].setEnabled(false);
}
}

Highest card wins is my program and now I want to compare cards

I am using Android studio for my highest card wins Game. I have three java classes called Cards, Deck, Gamelogic. everything is going good so far but I am just having a little trouble starting a function for my comparing cards method. This is what I have so far ...
import android.graphics.Color;
/**
* Created by azib2 on 12/1/2016.
*/
enum Suite {
Heart, diamond, spades, clubs;
public String toString()
{
switch (this) {
case Heart:
return "Heart";
case diamond:
return "diamond";
case spades:
return "spades";
case clubs:
return "clubs";
default:
return "Wrong type";
}
}
public String symbol(){
switch (this) {
case Heart:
return "\u2764";
case diamond:
return "\u2666";
case spades:
return "\u2660";
case clubs:
return "\u2663";
default:
return "Wrong type";
}
}
public int colors() {
switch (this) {
case Heart:
case diamond:
return Color.RED;
case spades:
case clubs:
return Color.BLACK;
}
return 0;
}
}
public class Cards {
private int cardnum;
private Suite suitetype;
public Cards(int cardnum, Suite suitetype){
this.cardnum = cardnum;
this.suitetype = suitetype;
}
public String CardType(int num){
switch(num){
case 1: return "A";
case 2: return "2";
case 3 : return"3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "J";
case 12: return "Q";
case 13: return "K";
default: return " error invaild ";
}
}
public void CompareCards(){
}
public int Getcardnum (){
return cardnum;
}
public Suite getsuite(){
return suitetype;
}
}
What should I do to compare cards?
First, I recommend "Cards" be "Card". But I'll stick with "Cards" here.
This seems like a good use-case for Comparable interface:
public class Cards implements Comparable<Cards> {
// Ace lowest:
public int compareTo(Card other) {
return Integer.compare(cardnum, other.cardnum);
}
}
Then to see if one card is higher than another:
if(card.compareTo(otherCard) > 0) { ... }
With this approach, you could even sort a list of cards using:
List<Cards> cards = new ArrayList<Cards>();
// Add all cards desired
Collections.sort(cards);
If you want stable sorting (by both value and suit):
public class Cards implements Comparable<Cards> {
// Ace lowest:
public int compareTo(Card other) {
// Compare by value first
int diff = Integer.compare(cardnum, other.cardnum);
if(diff != 0) return diff;
// Compare by suit
return suitetype.compareTo(other.suitetype);
}
}

Concurrent access to List

I'm trying to make a "Zelda-like" game in Java with Libgdx. As it will be lot of other things that my player, i'm trying to use Threads (example : for ennemies).
However, I have an exception and I don't find the solution :
Exception in thread "Tort 1" Exception in thread "Tort 2" >com.badlogic.gdx.utils.GdxRuntimeException: #iterator() cannot be used nested.
at com.badlogic.gdx.utils.Array$ArrayIterator.hasNext(Array.java:523)
at com.defel.game.entite.ennemi.Ennemi.deplacementAleatoire(Ennemi.java:368)
at com.defel.game.entite.ennemi.Ennemi.run(Ennemi.java:234)
And the code which is concerned is :
private synchronized void deplacementAleatoire(){
animationCourante = animationMarche[direction];
boolean bloquer = false;
for (MapObject obj : InfosSingleton.getInstance().getCollisionObjects()) {
RectangleMapObject rectMapObject = (RectangleMapObject) obj;
Rectangle rectObject = rectMapObject.getRectangle();
if(Intersector.overlaps(rectObject, rectangleColl)){
bloquer = true;
}
}
if(bloquer){
switch (direction) {
case 0:
direction = 2;
break;
case 1:
direction = 3;
break;
case 2:
direction = 0;
break;
case 3:
direction = 1;
break;
default:
break;
}
}
switch (direction) {
case 0:
rectangle.setY(rectangle.getY() + 2);
rectangleColl.setY(rectangleColl.getY() + 2);
break;
case 1:
rectangle.setX(rectangle.getX() - 2);
rectangleColl.setX(rectangleColl.getX() - 2);
break;
case 2:
rectangle.setY(rectangle.getY() - 2);
rectangleColl.setY(rectangleColl.getY() - 2);
break;
case 3:
rectangle.setX(rectangle.getX() + 2);
rectangleColl.setX(rectangleColl.getX() + 2);
break;
default:
break;
}
}
I think that my thread can't access to the List at the same time but I don't know how to resolve it...
Do you have an idea ? Thanks =D

Wrong Color received from method

Why is this code not returning right Color? Everytime, it is executed with name="YELLOW" or name="RED", it returns Color.WHITE.
Color recieveColor(String name)
{
Color color=new Color(255,0,0);
switch(name)
{
case "YELLOW":
{
color=Color.YELLOW;
}
case "RED":
{
color=Color.RED;
}
case "WHITE":
{
color=Color.WHITE;
}
}
return color;
}
This is because you don't have break.
Also you can put default in case your color name does not match
switch (name) {
case "YELLOW":
color = Color.YELLOW;
break;
case "RED":
color = Color.RED;
break
case "WHITE":
color = Color.WHITE;
break;
default:
color = Color.YELLOW;
break;
}
You must use break in switch case. break needs to exit from case.
switch (name) {
case "YELLOW":
color = Color.YELLOW;
break;
case "RED":
color = Color.RED;
break
case "WHITE":
color = Color.WHITE;
break;
}
Else you will always have color white
Java switch cases
.
That is because you do not have a break; after the color has been assigned.
Although the question is answered a few times (using the break keyword, to omit fallthrough), I'd like to give you some advice on the matter by using return. This omits the entire problem: it can't go wrong with `returns.
switch (name) {
case "YELLOW":
return Color.YELLOW;
case "RED":
return Color.RED;
case "WHITE":
return Color.WHITE;
default:
return Color.BLACK;
}

use relational operators in switch

Is there a way to use relational operators (<,<=,>,>=) in a switch statement?
int score = 95;
switch(score) {
case (score >= 90):
// do stuff
}
the above example (obviously) doesn't work
No you can not.
From jls-14.11
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.
Relational operators (<,<=,>,>=) results in boolean and which is not allowded.
All of the following must be true, or a compile-time error occurs:
Every case constant expression associated with a switch statement must be assignable (§5.2) to the type of the switch Expression.
No two of the case constant expressions associated with a switch statement may have the same value.
No switch label is null.
At most one default label may be associated with the same switch statement.
This might help you if you need to do it with switch itself,
char g ='X';
int marks = 65;
switch(marks/10)
{
case 1:
case 2:
case 3:
case 4: g = 'F';
break;
case 5: g = 'E';
break;
case 6: g = 'D';
break;
case 7: g = 'C';
break;
case 8: g = 'B';
break;
case 9:
case 10: g = 'A';
break;
}
System.out.println(g);
It works this way,
if(marks<50)
g='F';
else if(marks<60)
g='E';
else if(marks<70)
g='D';
else if(marks<80)
g='C';
else if(marks<90)
g='B';
else if(marks<=100)
g='A';
Unfortunately NO, though you can use case fall (kind of hacky) by grouping multiple case statements without break and implement code when a range ends:
int score = 95;
switch(score) {
..
case 79: System.out.println("value in 70-79 range"); break;
case 80:
..
case 85: System.out.println("value in 80-85 range"); break;
case 90:
case 91:
case 92:
case 93:
case 94:
case 95: System.out.println("value in 90-95 range"); break;
default: break;
}
IMHO, using if would be more appropriate in your particular case.
It will never work. You should understand what switch does in the first place.
It will execute the statements falling under the case which matches the switch argument.
In this case, score is an argument which is 95 but score>=90 will always evaluate to either true or false and never matches an integer.
You should use if statements instead.
Also Java doesn't allow booleans in switch cases so yea.
Simply NO
int score = 95;
switch(score) {
case (score >= 90):
// do stuff
}
You are passing a int value to switch. So the case's must be in int values, where
(score >= 90)
Turns boolean.
Your case is a good candidaate for if else
The docs for switch-case statement state:
a switch statement tests expressions based only on a single integer, enumerated value, or String object.
So there is no boolean. Doing so would make no sence since you only have two values: true or false.
What you could do is write a method which checks the score and then returns a one of the types switch can handle
For example:
enum CheckScore {
SCORE_HIGHER_EQUAL_90,
...
}
public CheckScore checkScore(int score) {
if(score >= 90) {
return SCORE_HIGHER_EQUAL_90;
} else if(...) {
return ...
}
}
and then use it in your switch:
switch(checkScore(score)) {
case SCORE_HIGHER_EQUAL_90:
// do stuff
}
... Or You could just use if, else-if, else directly!
Obviously, this is not possible as a language construct. But, just for fun, we could implement it by ourselves!
public class Switch<T, V> {
public static interface Action<V> {
V run();
}
private final T value;
private boolean runAction = false;
private boolean completed = false;
private Action<V> actionToRun;
public Switch(T value) {
this.value = value;
}
static public <T, V> Switch<T, V> on(T value) {
return new Switch<T, V>(value);
}
public Switch<T, V> ifTrue(boolean condition) {
runAction |= condition;
return this;
}
public Switch<T, V> ifEquals(T other) {
return ifTrue(value.equals(other));
}
public Switch<T, V> byDefault(Action<V> action) {
this.actionToRun = action;
return this;
}
public Switch<T, V> then(Action<V> action) {
if (runAction && !completed) {
actionToRun = action;
completed = true;
}
return this;
}
public V getResult() {
if (actionToRun == null) {
throw new IllegalStateException("none of conditions matched and no default action was provided");
}
return actionToRun.run();
}
}
Switch accepts any value to switch on and then provides functionality to match over boolean conditions (ifTrue method) or by exact matches (ifEquals method). Providing a value to switch on is needed just for the latter feature.
After building the conditions, user invokes getResult to obtain the result.
For example, we could create a method that tells us what it thinks about our score:
String tellMeMyScore(int score) {
return Switch.<Integer, String> on(score).byDefault(new Action<String>() {
public String run() {
return "really poor score";
}
}).ifTrue(score > 95).then(new Action<String>() {
public String run() {
return "you rock!";
}
}).ifTrue(score > 65).then(new Action<String>() {
public String run() {
return "not bad, not bad";
}
}).ifEquals(42).then(new Action<String>() {
public String run() {
return "that's the answer!";
}
}).getResult();
}
This simple test:
for (int score : new int[] { 97, 85, 66, 55, 42, 32, 4 }) {
System.out.println(score + ": " + tellMeMyScore(score));
}
Prints out:
97: you rock!
85: not bad, not bad
66: not bad, not bad
55: really poor score
42: that's the answer!
32: really poor score
4: really poor score

Categories