• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quadratic Formula - Why Did I Get This Output?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently taking a class on object oriented programming using java. In the lad, I was told to copy code from the book for a "Quadratic Formula" program. We were then given several print statements to add to the end. The last one is included in the code below (it's the last line of code). However, I'm not sure why I received the output I did. Both the program ond output are below, so if anyone could explain it to me, I would be grateful.



I added the command line arguments "-3.0" and "2.0" and I received the output "2.0", "1.0", and "e". I have no idea what "e" is or why I got it.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you added the integer value 4 to the char value 'a'. When you do that, your char value is converted to the equivalent integer value. The way that's done is to use the character's Unicode code point, which is 97. Adding 4 gives you 101. And then you cast it back to a char value, that produces the character whose Unicode code point is 101, namely 'e'.

Here's a link to a page which describes some Unicode characters, including the Latin character set: List of Unicode characters. Note that the code points are expressed in hexadecimal.

I'm not sure why you're doing that, though, since it doesn't have anything to do with solving quadratic equations.
 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was simply something I was instructed as part of the laboratory assignment in my class. Hearing your explination, I don't think we were really supposed to understand it on our own, since we haven't covered anything like that yet. Thank you very much for explaining it to me.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what are you trying to do here:


edit - too late...


It was simply something I was instructed as part of the laboratory assignment in my class.


are you sure you got it right then? Because it really makes no sense in terms of anything else you have coded...
 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I'm quite sure. I'll post the instructions that I was given below. It wasn't meant to have anything to do with the quadratic formula, it was simply to teach a concept.


Step 1. Write Program 1.2.3 Quadratic formula. Copy the program from the textbook. DO NOT download the program from the web, or copy the program from some other source. Type it in yourself.

^^^^This was where the code for the quadratic formula that I posted came from.

Step 2. Now extend the above program and add the following print statements and explain the outcome of each print statement.
a. System.out.println(2 + "bc");
b. System.out.println(2 + 3 + "bc");
c. System.out.println('b' + 'c');
d. System.out.println((char) ('a' + 4));

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew McCarthy wrote:Yes, I'm quite sure. I'll post the instructions that I was given below.


Gotcha. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic