Question: ================================================================== import java.util.Scanner; public class Hex2Dec { private String hex;// NULL public Hex2Dec(String hexStr){ hex = hexStr.toUpperCase(); } // accessor or get method for member
==================================================================
import java.util.Scanner; public class Hex2Dec { private String hex;// NULL public Hex2Dec(String hexStr){ hex = hexStr.toUpperCase(); } // accessor or get method for member variable hex public String getHex(){return hex;} // mutator or set method for member variable hex public void setHex(String newHex){hex = newHex.toUpperCase();} private int hexToDecimal() { int decimalValue = 0; for (int i = 0; i < hex.length(); i++) { char hexChar = hex.charAt(i); decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar); } return decimalValue; }
public int hexCharToDecimal(char ch) { if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A'; else if (ch >= '0' && ch <= '9'){// ch is '0', '1', ..., or '9' return ch - '0'; } return 0; } @Override public String toString(){ return "Hex: " + hex + "; Decimal: " + hexToDecimal(); } public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Prompt the user to enter a string System.out.print("Enter a hex number: "); String hex = input.nextLine(); Hex2Dec hd = new Hex2Dec(hex); System.out.println("The decimal value for hex number " + hex + " is " + hd.hexToDecimal()); System.out.print("Enter a hex number again: "); hd.setHex(input.nextLine()); System.out.println(hd); } }
==========================================================
public class PrimeNumber {
public static void printPrimeNumbers(int numberOfPrimes) { final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness
// Repeatedly find prime numbers while (count < numberOfPrimes) { // Print the prime number and increase the count if (isPrime(number)) { count++; // Increase the count
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Print the number and advance to the new line System.out.printf("%-5s ", number); } else System.out.printf("%-5s", number); }
// Check if the next number is prime number++; } }
/** Check whether number is prime */ public static boolean isPrime(int number) { for (int divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) { // If true, number is not prime return false; // number is not a prime } }
return true; // number is prime } public static void main(String[] args) { System.out.println("The first 50 prime numbers are "); printPrimeNumbers(50); } }
*********************************************************************************************************************************************************************************************************************
Question:
Redesign PrimeNumber in the form of Hex2Dec class above.
*********************************************************************************************************************************************************************************************************************
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
