Starting from:

$30

CSC220-Lab 5 Solved

Write complete Java programs.

 

Java programs:

1.     Bits. Write a program Bits.java that takes a command-line argument N and uses a while loop to compute the number of times you need to divide N by 2 until it is strictly less than 1. Print out an error message if the integer N is negative. 

% java Bits 0                     % java Bits 8

0                                                4

 

% java Bits 1                     % java Bits 16

1                                                5

 

% java Bits 2                     % java Bits 1000

2                                                10

 

% java Bits 4                     % java Bits -23

3                                                Illegal input
 

2.     Design and implement an application named Swap.java that reads a string and two index values from the user, then swaps the characters with the specified index values, and save the resulting string in a variable. The program should have an input verification loop ensuring that the specified index values are within range, and a loop that asks users if they want to run the program again.

 

Compiling and running Java programs (reminder):

1.     Compile your programs using the command javac filename

For example: javac myProgram.java

If you receive errors during the compilation phase, re-edit the source code file and attempt to correct them.

 

2.     Once a file successfully compiles, execute it using the java program.  For example: java MyProgram  

More products