Empty single quotation [duplicate] - java

This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 5 years ago.
Can anyone explain why adding an empty single quotation (at the end of this code) creates 32? I have changed the integers 3 and 4 and every time I do this, the base number seems to be 32.
public static void main(String[] args) {
int number = 5;
System.out.println("Initial Value: " + number);
number = number*2;
number = number*2;
System.out.println("\n1. After doubling it twice: " + number);
number = number + 3;
number += 3;
System.out.println("\n2. After adding 3 twice: " + number);
number -= 12;
System.out.println("\n3. After subtracting 12: " + number);
number = number / 3;
System.out.println("\n4. After dividing by 3: " + number);
System.out.println();
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
System.out.println("\n5. After adding 1 four times: " + number);
number -= 1;
System.out.println("\n6. After decrementing once: " + number);
int remainder = number%= 3;;
System.out.println("\n7. Remainder when dividing by 3 is :" + remainder);
int a = 2, b = 3, c = 5;
double d1, d2, d3, d4;
d1 = a + b * c / 2;
d2 = (a + b * c) / 2;
d3 = (a + b) * c / 2;
d4 = (a + b) * (c / 2);
System.out.println("\n8. Values: " + d1 + " : " + d2 + " : " + d3
+ " : " + d4);
int p, q;
p = 10;
q = 10;
p += q++;
System.out.println("\n9. Result is: " + (p + q));
double d7 = 4.3, d8 = 34.7;
double truncatedSum = (4.3 + 34.7);
System.out.println("\n10. Sum is " + truncatedSum);
System.out.println("\n11.");
System.out.println("fred " + 3 + 4);
System.out.println(3 + 4 + " fred");
System.out.println("" + 3 + 4);
**System.out.println(' ' + 3 + 4);**
}
OUTPUT
Initial Value: 5
After doubling it twice: 20
After adding 3 twice: 26
After subtracting 12: 14
After dividing by 3: 4
add 1: 5
add 1: 6
add 1: 7
add 1: 8
After adding 1 four times: 8
After decrementing once: 7
Remainder when dividing by 3 is :1
Values: 9.0 : 8.0 : 12.0 : 10.0
Result is: 31
Sum is 39.0
11.
fred 34
7 fred
34
39

Here the thing
System.out.println(' ' + 3 + 4);
' ' is of char type which is a numeric data type in Java:
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
and it corresponds to a numeric value of space character in UNICODE chart, e.g. 32.
Therefore, the result of this computation will be 32 + 3 + 4 or 39 as you can see in your output.

' ' + 3 + 4
as chars are numeric types in Java, ' ' stands for space character int value
Space is 32 in ASCII, so the result is:
32 + 3 + 4 = 39

Related

I'm stuck with this calculator program, I'm not getting the right answer [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
My programming course wants me to have this kind of endcome:
Enter the first number!
9
Enter the Second number!
5
9 + 5 = 14
9 - 5 = 4
9 * 5 = 45
9 / 5 = 1.8 and this is the problem, the program I've written only gives me 1.0 as an answer. How can I get this number to be 1.8 not 1.0?
public class Nelilaskin {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter the first number!");
int first = Integer.valueOf(reader.nextLine());
System.out.println("Enter the second number!");
int second = Integer.valueOf(reader.nextLine());
int plus = (first + second);
int minus = (first - second);
int multi = (first * second);
double division = (first / second * 1.0);
System.out.println(first + " + " + second + " = " + plus);
System.out.println(first + " - " + second + " = " + minus);
System.out.println(first + " * " + second + " = " + multi);
System.out.println(first + " / " + second + " = " + division);
}
}
Consider replacing the data type for first and second as Float.
And store the resultant in a float variable as well, then the output would be as required.
float plus = (first + second);
float minus = (first - second);
float multi = (first * second);
float division = first / second;
this is because you are dividing two int values, try to cast at least one of them to double..
double division = (double)first / (double)second ;

Why is my test case failing for the hailstones method?

Returns a string consisting of a Hailstone sequence beginning with the positive integer n and ending with 1. The
string should consist of a sequence of numerals, with each numeral followed by a single space. When a numeral m
(other than 1) appears in the sequence, it should be followed by nextHailstone(m).
Examples: nextHailstone(1) is "1 " and nextHailstone(5) is "5 16 8 4 2 1 ".
public static String hailstones (int n)
{
int calculation = 1;
System.out.print(n + " ");
while (n > 1)
{
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = (n*3) + 1;
}
calculation++;
System.out.print(n + " ");
}
return " ";
}
The code works fine when I call the method in the main method but the test case for it is failing.
#Test
public void testHailstones ()
{
assertEquals("1 ", hailstones(1));
assertEquals("16 8 4 2 1 ", hailstones(16));
assertEquals("7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 ", hailstones(7));
}
Those are the test cases. any insight into this would be great. thanks!
You return " " every time the function is called. You need to build up an internal string and return that as result.

How to convert char to decimal using an ascii table?

I'm working on a java program to convert a 4 digit binary number to decimal. I need to enter the binary as a String, convert to a char, and then to a decimal. I cannot use something like:
int decimal = Integer.parseInt("1010", 2);
Here is my code so far:
import java.util.Scanner;
public class BinaryConvert2 {
public static void main(String[] args){
System.out.println("Please enter a 4 digit binary number: ");
Scanner s = new Scanner(System.in);
String binaryNumber = s.next();
char a, b, c, d;
a = binaryNumber.charAt(0);
a = (char) (a*2*2*2);
b = binaryNumber.charAt(1);
b = (char) (b*2*2);
c = binaryNumber.charAt(2);
c = (char) (c*2);
d = binaryNumber.charAt(3);
d = (char) (d*1);
System.out.println(binaryNumber + " in decimal is: " + a + b + c + d);
}
}
I'm trying to multiply the char values by powers of 2 so that it will convert to decimal, but when I run the program, I get weird answers such as :
Please enter a 4 digit binary number:
1010
1010 in decimal is: ?Àb0
The ascii (char) value of 0 is 48 and the value if 1 is 49,
so you need to subtract 48 from the value
a = binaryNumber.charAt(0);
int aInt = (a - 48) * 2 * 2* 2;
....
System.out.println(binaryNumber + " in decimal is: " + (aInt + bInt + cInt + dInt));
The problem is you are printing the a b c and d as chars so it will print what ever decimal value of a b c and d correspond to in the ascii table. If you want to print out decimals you will have to convert the value to decimal by subtracting 48 add them together and then print.
Has to be like this:
1010 = 8 + 0 + 2 + 0 = 10 then print 10. You are on the right track
get the numeric value and do multiplication, if you do with char it will use the ASCII value
int num = 0;
a = binaryNumber.charAt(0);
num += (Character.getNumericValue(a) * 2 * 2 * 2);
b = binaryNumber.charAt(1);
num += (Character.getNumericValue(b) * 2 * 2);
c = binaryNumber.charAt(2);
num += (Character.getNumericValue(c) * 2);
d = binaryNumber.charAt(3);
num += (Character.getNumericValue(d) * 1);
System.out.println(binaryNumber + " in decimal is: " + num);

How do i get my System.out.prints to line up under each other?

I want all the responses to line up under each other
example:
x=y
y=x
ect...
not
x=y y=x ect...
// Julian Vizcarra
// Lab 05 question 2
public class Lab05_02 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Enter an integer
System.out.print("Enter an integer: ");
int number = input.nextInt();
//compute math
int ok = number/5;
int ok2= number/6;
//If statement
if (number % 5 == 0 && number % 6 == 0){
System.out.print("Is " + number + " divisible by 5 and 6?" + " True " );}
else {System.out.print("Is " + number + " divisible by 5 and 6?" + " False " ); }
if (number % 5 == 0 || number % 6 == 0) {
System.out.print("Is " + number + " divisible by 5 or 6?" + " True " );}
else {System.out.print("Is " + number + " divisible by 5 or 6?" + " False " );}
if (number % 5 == 0 ^ number % 6 == 0) {
System.out.print("Is " + number + " divisible by 5 or 6, but not both" + " True");}
else {System.out.print("Is " + number + " divisible by 5 or 6, but not both" + " False");}
}
}
my output is:
Enter an integer: 60
Is 60 divisible by 5 and 6? True Is 60 divisible by 5 or 6? True Is 60 divisible by 5 or 6, but not both False
Use System.out.println() instead of System.out.print().
System.out.println() or System.out.println("\n") do what you want

Align decimals in a list of doubles in Java

Here's the code section:
System.out.println("The values of pi from term " + (userNum - 9) + " to term " + userNum + " are:");
for (int x = 1; x < userNum; x++)
{
if (x % 2 == 1)
sum = sum + (4.0/(2 * x - 1));
else
sum = sum - (4.0/(2 * x - 1));
System.out.println("Term " + (x) + ": " + sum);
}
And here's the output:
The values of pi from term 2 to term 11 are:
Term 1: 4.0
Term 2: 2.666666666666667
Term 3: 3.466666666666667
Term 4: 2.8952380952380956
Term 5: 3.3396825396825403
Term 6: 2.9760461760461765
Term 7: 3.2837384837384844
Term 8: 3.017071817071818
Term 9: 3.2523659347188767
Term 10: 3.0418396189294032
Term 10's decimal needs to align with the others. How can that be done in Java? Thanks.
System.out.printf resembles (but is not) the C printf method. You can use it do to something like:
int spaces = (int)Math.log10(Math.abs(userNum)) + 1;
String myFormat = "Term %" + spaces +"d: %.10f\n";
//inside the for loop
System.out.printf(myFormat , x, sum);
Edited: You can use %2d to have a number using 2 spaces, if the number is bigger then it will use more spaces.
More info:
PrintStream‌​ (search for printf method)
If you use Apache commons lang, you could use the StringUtils class to accomplish what you need:
System.out.println("Term " +
StringUtils.leftPad("" + x, (userNum.length() - x.length()), ' ')
+ ": " + sum);
It seems you want to add space after Term depending on the number of digits of x:
int j = String.valueOf(x).length();
String sp = "";
while( j++ < String.valueOf(userNum).length() )
sp += " ";
System.out.printf("Term %s %d : %.15f\n", sp, x, sum);

Categories

Resources