CS401 - Intermediate Programming Using JAVA (Recitation)

 

Open Lab - Implementing given functions


1. Convert a non-negative integer n to binary notation

Convert the base-10 (decimal) integer to base-2 (binary) integer:

	
while N is greater than 0 do
	Quot = N / 2        //quotient of N divided by 2
	Rem  = N % 2        //remainder of N divided by 2
	Display Rem to the left of the last output or to the left of "^" mark
	N = Quot
end while

 

Example: N = 57

 N    Quot    Rem       Output
-----------------------------------
 57    28      1      1 1 1 0 0 1
 28    14      0               ^
 14     7      0      
  7     3      1
  3     1      1
  1     0      1

 

Java interface of the function:

	public static String toBinary(int n) {}

 


2.Print the divisors of positive integer N

Find all the divisors of a positive integer N:

Variables - 
	N = positive integer; 
	n = divisor of N

read N
d = 1
while d less than or equal to N
	if(d divides evenly into N)
	print d
	increment d by one
end while

 

Example: N = 10

 N      d        d <= N?      d divdes N?        output
==============================================================
  10     1          yes            yes               1
 -------------------------------------------------------------
         2          yes            yes               2
 -------------------------------------------------------------
         3          yes            no                -
 -------------------------------------------------------------
         4          yes            no                -
 -------------------------------------------------------------
         5          yes            yes               5
 -------------------------------------------------------------
         6          yes            no                -
 -------------------------------------------------------------
         7          yes            no                -
 -------------------------------------------------------------
         8          yes            no                -
 -------------------------------------------------------------
         9          yes            no                -
 -------------------------------------------------------------
        10          yes            yes              10
 -------------------------------------------------------------
        11          no          
 -------------------------------------------------------------

 

Java interface of the function:

	public static void printDivisors(int n) {}

 


3.Reversing a string

Reverse the input string without using the built-in Java functions. Example, if you're given a string "reviled did I live", the reverse of the string will be "evil I did deliver".

Variables -
	inpStr = the input string
	revStr = the reverse of the input string

read inpStr
for(i = length of the inpStr - 1; i>=0; i++)
	revStr = revStr + i th character  inpStr
endfor

 

Java interface of the function:

	public static String reverse(String str) {}

 

Hint: you can use the "substring" method of String class with the following interface:

public String substring(int beginIndex, int endIndex)

Example of using "substring" method:

String inpStr = "ABCD";
String thirdLetter = inpStr.substring(2,3);