Explore BrainMass
Share

# Program to print Numbers in Java

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Write a Java application that prints out numbers 2, 4 and 6 in row 1, numbers 3, 5 and 7 in row 2, numbers 8, 10 and 12 in row 3 and numbers 9, 11 and 13 in row 4. There should be an underline followed by a row of numbers that adds the numbers in the respective columns. The program should print out the information as follows:

2 4 6
3 5 7
8 10 12
9 11 13
__________

22 30 38

© BrainMass Inc. brainmass.com October 25, 2018, 4:49 am ad1c9bdddf
https://brainmass.com/engineering/electrical-engineering/395530

#### Solution Preview

1.public class PrintMatrix
2.{
3.
4. public static void main(String args[])
5. {
6. int[][] matrix = new int[4][3]; //matrix values
7. int sumCol0, sumCol1, sumCol2; //column sum variables
8.
9. //initialize sum values to 0
10. sumCol0 = 0;
11. sumCol1 = 0;
12. sumCol2 = 0;
13.
14. //Store row values
15. matrix[0][0] = 2;
16. matrix[0][1] = 4;
17. matrix[0][2] = 6;
18. matrix[1][0] = 3;
19. matrix[1][1] = 5;
20. ...

#### Solution Summary

This solution provides a detailed program to print numbers in rows and columns.

\$2.19

## Write a program that uses bitwise operations to: 1) generate and display all power-of-two numbers in the range +128 to -128, and 2) display an arbitrary user-input integer

Implement your solution using one class. In your class, provide two methods - main and display.

main method:

Your main method should:

· Declare int number = 128;
· Include a while loop that loops while number is >= -128.
· Call the display method, which prints the value of its passed-in number parameter.
· If number is greater than zero, use the >>= operator to do one arithmetic-right shift.
· If number equals zero, use the ~ operator to complement it.
· If number is less than zero, use the <<= operator to do one left shift.
· After the while loop, ask the user to input any number, and call the display method to print that number.

display(number) method:

Write the display(number) method like this:

· Receive a number parameter.
· Print number's value and a tab. (t is tab in java)
· Assign to a local variable named mask the value 1 shifted left 31 times. This puts a 1 in bit 31 and zeros in all other bits.
· Use a for loop to step through all 32 bits, doing the following in each step:
o Use a conditional operator whose condition is (mask & number != 0) to print either 1 or 0.
o After every fourth bit, print a single space to make the output readable.

Sample Session:

Decimal Binary

128 0000 0000 0000 0000 0000 0000 1000 0000
64 0000 0000 0000 0000 0000 0000 0100 0000
32 0000 0000 0000 0000 0000 0000 0010 0000
16 0000 0000 0000 0000 0000 0000 0001 0000
8 0000 0000 0000 0000 0000 0000 0000 1000
4 0000 0000 0000 0000 0000 0000 0000 0100
2 0000 0000 0000 0000 0000 0000 0000 0010
1 0000 0000 0000 0000 0000 0000 0000 0001
0 0000 0000 0000 0000 0000 0000 0000 0000
-1 1111 1111 1111 1111 1111 1111 1111 1111
-2 1111 1111 1111 1111 1111 1111 1111 1110
-4 1111 1111 1111 1111 1111 1111 1111 1100
-8 1111 1111 1111 1111 1111 1111 1111 1000
-16 1111 1111 1111 1111 1111 1111 1111 0000
-32 1111 1111 1111 1111 1111 1111 1110 0000
-64 1111 1111 1111 1111 1111 1111 1100 0000
-128 1111 1111 1111 1111 1111 1111 1000 0000

Enter any integer: 127

127 0000 0000 0000 0000 0000 0000 0111 1111

Attached is what I have come up with but am making a mistake somewhere because I can't get it to compile and run.