public class Client public static void main(String[] args) //create an object a
ID: 3708095 • Letter: P
Question
public class Client public static void main(String[] args) //create an object a representing 111001 in binary //display object a //display the decimal value of object a //display base-6 value of object a //create object b with parameter ""1a.23069" //in base 8 and challenging set to false //display object b I/display decimal value of object b //create object d in base-7 holding decimal value 255 //display object d //display the comparison of d with b //display the comparison of a with b //IMPORTANT!!! use print for the last output,NOT pritnExplanation / Answer
Client .java
public class Client {
public static void main(String[] args) {
// String Object a in Binary
String a = "111001";
// String Object b in Base 8
String b = "10.23069";
String d = "555";
// Print a
System.out.println("Binary Value of a is = " + a);
// get Decimal using Standard method
System.out.println("Decimal Value of a = " + getDecimal(a, 2));
// get Base 6 using Standard method
System.out.println("Base 6 Value of a = " + getDecimal(a, 6));
// Print b
System.out.println("Value of b is = " + b);
System.out.println("Decimal Value of b = " + getDecimal(b, 8));
// Print c
System.out.println("Value of d is = " + d);
System.out.println("Decimal Value of d = " + getDecimal(d, 7));
System.out.println("Comparison of b and d");
if (compare(d, b) > 0) {
System.out.print("D is gretaer than B");
} else {
System.out.print("B is gretaer than D");
}
// In same way a and b can be compare
}
// This method will convert from Base to Decimal
public static double getDecimal(String value, int base) {
if (value.contains(".")) {
String[] arr = value.split("\."); // split fraction part
return integerPart(arr[0], base) + fractionalPart(arr[1], base);
} else {
return integerPart(value, base);
}
}
// conversion of integer part to some other base
public static double integerPart(String value, int base) {
double sum = 0;
int l = value.length();
for (int i = 0; i < value.length(); i++) {
int num = Integer.parseInt(String.valueOf(value.charAt(l - 1)));
sum += num * Math.pow(base, i);
l--;
}
return sum;
}
// conversion of decimal part to some other base
public static double fractionalPart(String value, int base) {
double sum = 0;
for (int i = 0; i < value.length(); i++) {
int power = -i - 1;
int num = Integer.parseInt(String.valueOf(value.charAt(i)));
sum += num * Math.pow(base, power);
}
return sum;
}
// compare d with b
public static int compare(String d, String b) {
return (int) (getDecimal(d, 7) - getDecimal(b, 8));
}
}
Output:
Binary Value of a is = 111001
Decimal Value of a = 57.0
Base 6 Value of a = 9289.0
Value of b is = 10.23069
Decimal Value of b = 8.298614501953125
Value of d is = 555
Decimal Value of d = 285.0
Comparison of b and d
D is gretaer than B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.