بالسلامة ي رب
هذا السؤال
----------
. Take two positive integers from the user.
a. You program will check that user has given the correct positive number (number > 0). If user
has given 0 or a negative number then your program repeatedly ask the user to give the
correct positive number until he gives the correct input.
b. If the first number is smaller than the second number, given by user, then swap the numbers.
2. Write a Method that will take an integer value as a parameter and convert the integer value into a
Hexadecimal number as a String and return the string back to the calling method.
a. public static String decimal2hexadecimal(int num) - header of the method
3. Write a Method that takes two Hexadecimal Numbers as strings and computes their sum as a string
and return the result to calling function.
a. public static String addHexadecimal(String num1, String num2) - header of the method that
computes num1 + num2 as a string.
4. Write a Method that takes two Hexadecimal Numbers as strings and computes their difference as a
string and return the result to calling function.
a. public static String subtractHexadecimal(String num1, String num2) - header of the
method that computes num1 – num2 as a string.
----------
بإضافة إلى الضرب
هذا حلّي
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the First number: ");
int num1 = input.nextInt();
while (num1<0){
System.out.print("Please Enter a First Positive Value: ");
num1 = input.nextInt();
}
System.out.print("Enter the Second number: ");
int num2 = input.nextInt();
while (num2<0 ){
System.out.print("Please Enter a Second Positive Value: ");
num2 = input.nextInt();
}
if(num1<num2){
int G = num1;
num1 = num2;
num2 = G;
}
System.out.println("\nThe hexadecimal number for the decimal " +
num1 + " is " + convertinghexa(num1)+"."+
"\nand hexadecimal number for the decimal "+num2 + " is "+convertinghexa(num2)+".");
String str_num1 = convertinghexa(num1);
String str_num2 = convertinghexa(num2);
System.out.println("\nThe Addtion of the hexadecimal is: "+ Addition(str_num1,str_num2));
System.out.println("\nThe Subtract of the hexadecimal is: "+ Subtract(str_num1,str_num2));
System.out.println("\nThe Multiplie of the hexadecimal is: "+ Multiplie(str_num1,str_num2));
}
public static String convertinghexa(int num1) {
String hex = "";
while (num1 != 0) {
int hexaValue = num1 % 16;
hex = toHexChar(hexaValue) + hex;
num1 = num1 / 16;
}
return hex;
}
public static char toHexChar(int hexValue) {
if (hexValue <= 9 && hexValue >= 0)
return (char)(hexValue + '0');
else
return (char)(hexValue - 10 + 'A');
}
public static int con(char A){
if (A == '0')
return 0;
else if (A == '1')
return 1;
else if ( A == '2')
return 2;
else if (A == '3')
return 3;
else if ( A == '4')
return 4;
else if (A == '5')
return 5;
else if (A == '6')
return 6;
else if ( A == '7')
return 7;
else if (A == '8')
return 8;
else if ( A == '9')
return 9;
else if ( A == 'A')
return 10;
else if (A == 'B')
return 11;
else if ( A == 'C')
return 12;
else if (A == 'D')
return 13;
else if ( A == 'E')
return 14;
else
return 15;
}
public static String Addition(String num1, String num2){
int sum,A,B, carry=0;
A = num1.length()-1;
B = num2.length()-1;
String astn="";
while ( B < A && B!= A){
num2 = '0' + num2;
B++;
}
while(B>=0){
sum = (con(num1.charAt(A))+con(num2.charAt(B))+carry)%16;
carry= (con(num1.charAt(A))+con(num2.charAt(B))+carry)/16;
astn = toHexChar(sum) + astn;
A--;
B--;
}
return astn ;
}
public static String Subtract(String num1, String num2){
int sum, A, B,carry=0;
A = num1.length()-1;
B = num2.length()-1;
String astn="";
while(B>=0){
sum = (con(num1.charAt(A))-con(num2.charAt(B)))%16;
if(B > A){
carry = (con(num1.charAt(A))-(con(num2.charAt(B))+16));
}
A--;
B--;
astn = toHexChar(sum) + astn;
}
return astn ;
}
public static String Multiplie(String num1, String num2){
int sum = 0, A, B,carry=0, sum1;
A = num1.length()-1;
B = num2.length()-1;
String astn= " ";
for(int i=0; i <= B; i++){
for(int j=A ; j <= A; j++){
sum =(con(num1.charAt(A))*con(num2.charAt(B))+carry)%16;
carry =(con(num1.charAt(A))*con(num2.charAt(B)))/16;
}
}
A--;
B--;
astn = toHexChar(sum) + astn;
return astn;
}
}