3 . Write a program that prints a certain string in reverse order. For example i
ID: 3572646 • Letter: 3
Question
3. Write a program that prints a certain string in reverse order. For example imagine that the entered string is "Geerlings" then the printed string must be "sgnilreeG".
Make use of a for-statement. What is the starting value of the index in this for-statement? What is the criterion for continuation? With what value needs the index be changed after each time the loop is executed?
4. Make a program with two textfields for the conversion from binary to decimal and vice versa. When a binary number is entered in the left textfield (e.g. 01011011) and <enter> is pressed then in the right textfield the decimal value 91 must appear. When a decimal number is entered in the right textfield (e.g. 27) and <enter> is pressed then in the left textfield the binary value 11011 must appear. N.b. the length of the entered bitstring is in principle not limited and is only limited by the integer-range of java. Mark you the actual algorithm for conversion (the actionPerformed) consist of 20 lines of code at most.
Example of the applet.
5. Make a program with two textfields for the conversion from hexadecimal to decimal and vice versa. When a hexadecimal number is entered in the left textfield (e.g. 5B) and <enter> is pressed then in the right textfield the decimal value 91 must appear. When a decimal number is entered in the right textfield (e.g. 1007) and <enter> is pressed then in the left textfield the hexadecimal value 3EF must appear. N.b. the length of the entered string is in principle not limited and is only limited by the integer-range of java. Also here the actual algorithm for conversion (the actionPerformed) consist of 25 lines of code at most!
Hint: make use of the properties of the ASCII-table.
Example of the applet.
The hexadecimal numeration system is based on radix 16 (4 bits). The coding of the numbers 10 to 15 is as follows:
Explanation / Answer
3)
public class StringReverseExample{
public static void main(String[] args){
String string="abcdef";
String reverse = new StringBuffer(string).
reverse().toString();
System.out.println(" String before reverse:
"+string);
System.out.println("String after reverse:
"+reverse);
}
}
4)
import java.util.Scanner;
public class BinaryToDecimalAndBackConverter{
static String convertDecimalToBinary(String decimal){
int integer = Integer.valueOf(decimal);
String result = new String();
while(integer > 0){
result+=String.valueOf(integer%2);
integer/=2;
}
result = new StringBuffer(result).reverse().toString();
return result;
}
static int convertBinaryToDecimal(String binary){
int result = 0;
for(int reverseCounter = 0; reverseCounter < binary.length(); reverseCounter++){
char currentChar = binary.charAt(binary.length() - reverseCounter - 1);
int numericValue = Character.getNumericValue(currentChar);
result+=Math.pow(2, reverseCounter) * numericValue;
// System.out.println(Math.pow(2, reverseCounter) * Character.getNumericValue(binary.charAt(binary.length() - reverseCounter - 1)) + " reverseCounter " + reverseCounter);
}
return result;
}
static String isDecimalOrBinary(String input){
for(int counter = 0; counter < input.length(); counter++){
if(input.charAt(counter) != '0' && input.charAt(counter) != '1'){
return "decimal";
}
}
return "binary";
}
public static void main(String[] args){
Scanner inputScanner = new Scanner(System.in);
System.out.print("Please enter the binary/decimal number to convert:");
String input = inputScanner.nextLine();
switch(isDecimalOrBinary(input)){
case "decimal":
System.out.println(convertDecimalToBinary(input));
break;
case "binary":
System.out.println(convertBinaryToDecimal(input));
break;
}
}
}
5)
import java.util.Scanner;
public class JavaProgram
{
public static int hex2decimal(String s)
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static void main(String args[])
{
String hexdecnum;
int decnum;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Hexadecimal Number : ");
hexdecnum = scan.nextLine();
decnum = hex2decimal(hexdecnum);
System.out.print("Equivalent Decimal Number is " + decnum);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.