Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain this code, please. package lab3; import java.io.PrintStream; import java

ID: 3780644 • Letter: E

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);

       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.length; 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<digits.length; i++) {

           ret = ret * base + digits[i];

       }

       return ret;

   }

  

   boolean equals(BasedNumber that) {

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

   }

}

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

Explanation / Answer

Below code will take base n from user and number in the form of base n from user.Again it takes
another base n1 and number1 in the form of base n1.It checks whether given entered numbers in two different bases
are same or not

package lab3;

import java.io.PrintStream;
import java.util.Scanner;


class Lab3 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
BasedNumber b1 = promptForNumber(in),
b2 = promptForNumber(in);
//Create 2 objects b1 and b2
//It callse promptForNumber which return object of BasedNumber
in.close();
//Check equality of numbers
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;//Create object to print on screen
  
out.println("Enter a base: ");
  
int base = Integer.parseInt(in.nextLine());//accept base of number and convert it to int from string
  
out.printf("Enter base-%d digits (separated by space): ", base);
  
String[] digitStrings = in.nextLine().split(" ");//accept number and convert it to array of string
  
return new BasedNumber(base, parse(digitStrings));
}

//This method will take array of string and convert it to array of int
//suppose,a[]={"0","1"},then it is converted to a[]={0,1}
private static int[] parse(String[] digitStrings) {
int[] digits = new int[digitStrings.length];
  
for(int i = 0; i<digits.length; i++) {
digits[i] = Integer.parseInt(digitStrings[i]);//Convert string to int
}
return digits;
}
}

//Class to hold base and number

class BasedNumber {
private int base;
private int[] digits;
  
//constructor
BasedNumber(int base, int[] digits) {
this.base = base;
this.digits = digits;
}
//Return base of class
int getBase() { return base; }

//Return digit at specif location in digits array
//a[]={1,0,1},for getDigit(0),it returns 1 i.e.digits[0]
int getDigit(int n) { return digits[n]; }
  

//It will convert given number to base 10,so that we can check for equality
int getValue() {
int ret = 0;
  
for(int i=0; i<digits.length; i++) {
ret = ret * base + digits[i];
}
return ret;
}
//It checks base 10 value valur of 2 numbers.If equal returns true else return false
boolean equals(BasedNumber that) {
return getValue() == that.getValue();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote