wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Quadratic Formula - Why Did I Get This Output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quadratic Formula - Why Did I Get This Output?" Watch "Quadratic Formula - Why Did I Get This Output?" New topic
Author

Quadratic Formula - Why Did I Get This Output?

Andrew McCarthy
Greenhorn

Joined: Jun 20, 2012
Posts: 22
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 20, 2012
Posts: 22
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Andrew McCarthy
Greenhorn

Joined: Jun 20, 2012
Posts: 22
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
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

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

Gotcha. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quadratic Formula - Why Did I Get This Output?
 
Similar Threads
Quadratic formula result problem
Confusion about wrapper classes.
Long Programming Problem
Quadratic Formula
Requesting a Code Review