Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

}

}

image text in transcribedimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions

Question

=+industrial action Under what circumstances can unions strike?

Answered: 1 week ago

Question

=+What forms of industrial action are common?

Answered: 1 week ago