Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 basic calculator
Index -> Programming, Java -> Java Submissions
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Wed May 03, 2006 11:30 pm   Post subject: (No subject)

You need to think abstractly.

Switch works on integer type expressions ->
The char type is a sort of integer ->
Switch works on chars.

You have a String type expression.
You need to deal with one character from that string.
If you can get that character, you're set.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Thu May 04, 2006 11:54 am   Post subject: (No subject)

Hmm, I'd suggest to keep the model and view separate.You see, that would work, but it would require a redesign when you decide to change to use _words_ to represent numbers (like "One" for 1), or if you internationalise it, etc.
cool dude




PostPosted: Thu May 04, 2006 4:09 pm   Post subject: (No subject)

k i thought about it and this is wat i think... take the Ascii value of the letter, thus u will have an integer that u can work with. the only problem though is i have no idea how i would take the Ascii value of the letter? also wouldn't that be more code, more troubles and if statements would be better? why is it better to use switch cases instead of if statements?
wtd




PostPosted: Thu May 04, 2006 4:17 pm   Post subject: (No subject)

In the switch statement you only state the name of the variable you're matching against once, and you don't write the == operator each time. This prevents typos, and the == operator can easily be confused with the = operator, which has dramatically different results.

Remember: all other things being equal, the more code you write, the more likely you are to screw something up. Smile

As for strings...

code:
String foo = "hello";
... bar = foo[0];

System.out.println(bar);


Fill in the ... part and tell me what the call to System.out.println prints.
cool dude




PostPosted: Thu May 04, 2006 4:36 pm   Post subject: (No subject)

i hava no clue wat you are doing here? Confused i guess it print watever value bar is although i don't know wat bar even means? Confused also how does this relate to wat we r talking about?
wtd




PostPosted: Thu May 04, 2006 4:51 pm   Post subject: (No subject)

A string is a series of characters. You need to be able to get just one of those characters out of a string. This what I'm trying to explain.
cool dude




PostPosted: Thu May 04, 2006 5:22 pm   Post subject: (No subject)

is this wat u mean?

code:

String hello = ("Hello");
System.out.println (hello.charAt(0));


if i do the same thing with the operation variable i could use it now as switch so thats good Smile but now when i say case "A", case "B", etc. it says incompatible types.

this is the part of the code

code:

operation2 = operation.charAt(0);

switch(operation2){
                case "A":
                    sum = inum1 + inum2;
                    System.out.println("the sum of the numbers is " + sum);
                    break;
                case "S":
                    subtraction = inum1 - inum2;
                    System.out.println("The subtracted numbers is equal to " + subtraction);
                    break;
                case "M":
                    product = inum1 * inum2;
                    System.out.println("The product of the numbers is " + product);
                    break;
                case "D":
                      quotient = inum1/inum2;
                      System.out.println("The quotient is " + quotient);
                      break;
                    }
wtd




PostPosted: Thu May 04, 2006 5:44 pm   Post subject: (No subject)

Is "A" of type char?
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Thu May 04, 2006 6:05 pm   Post subject: (No subject)

wtd wrote:
Is "A" of type char?
"A" is not of type char its of type string but then how do i take "A" and change it to type char? or do i have to do exactly wat i did with operation2? i.e make more variables and declare them of type char and then convert "A" into type char? (i tried doing that but it doesn't work)

***Edit*** did i convert operation into char correctly? is that how u do it?
wtd




PostPosted: Thu May 04, 2006 7:08 pm   Post subject: (No subject)

How did you declare operation2? If you don't know what I mean by "declare", then you have considerable fundamental reading to do.
cool dude




PostPosted: Thu May 04, 2006 7:40 pm   Post subject: (No subject)

wtd wrote:
How did you declare operation2? If you don't know what I mean by "declare", then you have considerable fundamental reading to do.


obviosly i know wat u mean by declare i know turing and VB so i think i know wat declare is. i declared operation2 as char.

code:

char operation2;
wtd




PostPosted: Thu May 04, 2006 7:50 pm   Post subject: (No subject)

I "knew" a lot before I knew much of anything, so I try not to assume. Wink

When in doubt, test with small apps.

code:
public class CharTest {
   public static void main(String[] args) {
      String foo = "foo";
      char bar = foo[0];
      System.out.println(bar);
   }
}
cool dude




PostPosted: Thu May 04, 2006 8:24 pm   Post subject: (No subject)

is the code supposed to not work? and can u be a little more descriptive because i think i'm a little lost on wat we r trying to do Confused
wtd




PostPosted: Thu May 04, 2006 8:32 pm   Post subject: (No subject)

If you want to figure out how fundamental little things like accessing characters in a string work, do so in a simple test app. That way you can be sure any flaws that might be in your larger program aren't distracting you.

Remeber, any complex problem is just a collection of smaller, simpler problems. Smile
cool dude




PostPosted: Thu May 04, 2006 10:43 pm   Post subject: (No subject)

wtd wrote:
If you want to figure out how fundamental little things like accessing characters in a string work, do so in a simple test app. That way you can be sure any flaws that might be in your larger program aren't distracting you.

Remeber, any complex problem is just a collection of smaller, simpler problems. Smile


thanks for the advice i usually do that especially when learning a new language. Smile when i tried that code u gave in a simple test app. it did not work and said array required. Confused anyhow wat is it supposed to do?
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 3  [ 38 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: