Question:
Java code Can you use switch statements and objects together?
halpin
2010-04-25 03:20:44 UTC
I have a switch statement That has
1,Add one
2Delete one
3List all
4,Exit
I have the switch statement but im unsure if i can use objects.
Just not making sense in my head
Also the Add one code e.g Customer name,Customer ID,Customer Age
The delete code asks for Customer ID if its there its deleted
List All Lists everything in the object array
I dont want you to complete the program just is it possible to use objects and if so how would i use them with switch statements
Four answers:
matt m
2010-04-25 03:23:54 UTC
I'm not really sure what you mean, but can you make a class.



something like

customers.add('name',21,1)

customers.list()



The class would contain those functions inside.
2016-04-14 03:58:25 UTC
14. Well, first off, x is 0, so x > 0 is false. So what this boils down to is: y = x++; Remember that the ++ operator increases the value by 1, but if placed AFTER the value, it increments the value AFTER returning it. So the return value of x++ is the value of x BEFORE being incremented, which is 0. 15. Since x is 1, x > 0 is true. The first branch of the outer if statement will be executed. Then, since y is -1, y > 0 is false, and the print statement will not be executed. So nothing is printed. 16. Not much to say about this one. It's perfectly valid code. There is no need to put { braces } around a conditional block if it only contains one statement. 17. Note the use of the = operator rather than == in the conditional. This is what's causing the condition to be true. 18. Not sure what to say here. The operator is != as defined in the language spec. 19. b is incorrect because it tries to compare a boolean value and an integer using <. c is using an assignment as a truth value, which is technically valid syntax but is almost always not what you want. d is syntactically incorrect; presumably != was meant instead of =! in the second portion. 20. Pretty straightforward. If x is less than 0, only the second branch of the conditional will be executed. 21. See my comments on #14 for how the ++ operator works. 22. That's how the language is defined. 23. Since there are no braces, only the first statement after the if statement is subject to the conditional. Thus, the second print statement will always be executed, though the first will only be executed if the condition is true. 24. The % operator returns the remainder when an integer division is performed. The remainder of 345 / 2 is 1, so 1 != 1 is false. 25. XOR returns true when exactly one of its arguments is true. If both are true, or both are false, it returns false.
deonejuan
2010-04-25 05:18:41 UTC
switch will branch on enums or primitives, but not Strings. So, if you have a terminal input, which is String, I would get the char



Please make a selection:

1. Add one

2. Delete one

3. list all

4. exit



String input in sc.nextLine();

char[] c = input.toCharArray();

switch( c[ 0 ] ) {

case '1' :

//

break;

case '2':

// method to delete

break;

case '3':

//

break;

default:

}
Sayee
2010-04-25 05:09:54 UTC
Yes can do.

Switch statement just control the flow of the program. So if you something like



pubic class Temp{

Object obj; // or ArrayList list = new ArrayList();

temp(){ (constructor or in other methods....)

somecode...

switch statement

manipuates object (obj or list) in various places

end switch

}

}



Thats fine. Hope it helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...