Question: Explain this code, please. package lab3; import java.io.PrintStream; import java.util.Scanner; public class Lab3 { public static void main(String[] args) { Scanner in = new Scanner(System.in);

Explain this code, please.

package lab3;

import java.io.PrintStream;

import java.util.Scanner;

public class Lab3 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

BasedNumber b1 = promptForNumber(in),

b2 = promptForNumber(in);

in.close();

System.out.printf("These numbers represent %s! ", b1.equals(b2)? "the same value" : "the different values");

}

private static BasedNumber promptForNumber(Scanner in) {

PrintStream out = System.out;

out.println("Enter a base: ");

int base = Integer.parseInt(in.nextLine());

out.printf("Enter base-%d digits (separated by space): ", base);

String[] digitStrings = in.nextLine().split(" ");

return new BasedNumber(base, parse(digitStrings));

}

private static int[] parse(String[] digitStrings) {

int[] digits = new int[digitStrings.length];

for(int i = 0; i

digits[i] = Integer.parseInt(digitStrings[i]);

}

return digits;

}

}

class BasedNumber {

private int base;

private int[] digits;

BasedNumber(int base, int[] digits) {

this.base = base;

this.digits = digits;

}

int getBase() { return base; }

int getDigit(int n) { return digits[n]; }

int getValue() {

int ret = 0;

for(int i=0; i

ret = ret * base + digits[i];

}

return ret;

}

boolean equals(BasedNumber that) {

return getValue() == that.getValue();

}

}

Explain this code, please. package lab3; import java.io.PrintStream; import java.util.Scanner; public classLab3 { public static void main(String[] args) { Scanner in = new

Lab 3 September 23, 2016 Overview In this lab, you will implement a class that represents base n number. A number ed by a on-empty sequence of digits, eac the range Oase- s represe Co; n). We can convert a sequence of digits in base n to base 10 with a simple formula. For example, the following converts the base-7 er 1234 into base- 10: (1 x 73) (2 x 72) 3 x 71) (4 x 7 and the following converts the sequence of digits (10,14,0,4) from base-15 to base 10: (4 x 15 4 x 10 x 15 0 x 15 15 We can convert any sequence of digits from base n to base 10 by multiplying each digit from right to left by increasing powers of n and summing the result together You must implement the Based umber class according to the following UML diagram. You do not need to validate the input of digits (although an actual application would require that each digit is in the correct range). You must also implement the equals method which returns true if both numbers represent the same value (regardless of base) Based Number get Basel int get Val lei): int equals(other: BasedNumher: hoolean Main You will also implement an int parse (String C digit strings) method in the Main class. This class implements the method promptForNumber (Scanner in

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!