Answered Essay: Java

Need help for Java program!!!

Construct an object oriiented java program that performs each of the following conversions. Note that we’re only dealing with non-negative numbers.

* Converts a decimal integer into a 32-bit binary number

* Converts a decimal integer into a 8-digit hexadecimal number

* Converts a 32-bit binary number into a decimal integer

* Converts a 32-bit binary number into 8-digit hexadecimal number

* Converts a 8-digit hexadecimal number into a decimal integer

* Converts a 8-digit hexadecimal number into a 32-bit binary number

Your program should be interactive, requiring the user to select hte appropriate option from a menu of options (no GUI required) Note that java’s scanner class will be very helpful reading data from the keyboard

&Computer Lab: Number Systems Construct an object-oriented Java program that performs each of the following conversions. Note that were only dealing with non-negative numbers. Converts a decimal integer into a 32-bit binary number .Converts a decimal integer into an 8-digit hexadecimal number . Converts a 32-bit binary number into a decimal integer . Converts a 32-bit binary number into an 8-digit hexadecimal number Converts an 8-digit hexadecimal number into a decimal integer Converts an 8-digit hexadecimal number into a 32-bit binary number Your program should be interactive, requiring the user to select the appropriate option from a menu of options (no GUI required). Note that Javas Scanner class will be very helpful reading data from the keyboard. The conversions you code must be direct. In other words, if you had to convert a base 2 number into base 16, the conversion should be made directly from base 2 to base 16 and not from base 2 to base 10 to base 16. You may not use any methods or utilities from the Java library that do the conversions for you, such as: Integer.toBinaryString) Integer.toHexString () Your program should send all output to a file called csis.txt. This output file will be submitted along with your source code for the lab. All information displayed in the terminal window, including input from the user, should also be sent to the output file.

&Computer Lab: Number Systems Construct an object-oriented Java program that performs each of the following conversions. Note that we’re only dealing with non-negative numbers. Converts a decimal integer into a 32-bit binary number .Converts a decimal integer into an 8-digit hexadecimal number . Converts a 32-bit binary number into a decimal integer . Converts a 32-bit binary number into an 8-digit hexadecimal number Converts an 8-digit hexadecimal number into a decimal integer Converts an 8-digit hexadecimal number into a 32-bit binary number Your program should be interactive, requiring the user to select the appropriate option from a menu of options (no GUI required). Note that Java’s Scanner class will be very helpful reading data from the keyboard. The conversions you code must be direct. In other words, if you had to convert a base 2 number into base 16, the conversion should be made directly from base 2 to base 16 and not from base 2 to base 10 to base 16. You may not use any methods or utilities from the Java library that do the conversions for you, such as: Integer.toBinaryString) Integer.toHexString () Your program should send all output to a file called csis.txt. This output file will be submitted along with your source code for the lab. All information displayed in the terminal window, including input from the user, should also be sent to the output file.

Expert Answer

 

Driver.java
—————————————–
import java.io.*;

public class Driver
{

public static void main(String[] args) throws IOException{
int choice;

PrintWriter pw = new PrintWriter (new FileWriter(“csis.txt”));
Decimal dec = new Decimal (pw);
Binary bin = new Binary (pw);
Hexadecimal hex = new Hexadecimal (pw);
Menu menu = new Menu(pw);

do {
menu.display();
choice = menu.getSelection();
switch (choice) {
case 1 : dec.decToBin(); break;
case 2 : dec.decToHex(); break;
case 3 : bin.binToDec(); break;
case 4 : bin.binToHex(); break;
case 5 : hex.hexToDec(); break;
case 6 : hex.hexToBin(); break;
}
} while (choice != 7);
pw.close();
}
}
—————————————————————-
Menu.java
————————————
import java.io.*;
import java.util.Scanner;

public class Menu
{
private PrintWriter pw;

/**
* Class constructor
*/
public Menu(PrintWriter pw)
{
// initialize instance PrinWriter
this.pw = pw;
}

/**
* Displays the menu options
*/
public void display()
{
System.out.println(” MENU “);
System.out.println(“1. Press 1 for decimal to binary transformation. “);
System.out.println(“2. Press 2 for decimal to hexadecimal transformation. “);
System.out.println(“3. Press 3 for binary to decimal transformation. “);
System.out.println(“4. Press 4 for binary to hexadecimal transformation. “);
System.out.println(“5. Press 5 for hexadecimal to decimal transformation. “);
System.out.println(“6. Press 6 for hexadecimal to binary transformation. “);
System.out.println(“7. Press 7 to exit the program. “);
}

/**
* Extracts the user inserted option
*/
public int getSelection() {
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
boolean check_quality = qualityCheck(input);
if (!check_quality) {
System.out.println(“Please enter a value between 1 and 7: “);
keyboard = new Scanner(System.in);
input = keyboard.nextInt();
check_quality = qualityCheck(input);
}
return input;
}

/**
* Method that check that the user inserts a correct value
*/
public boolean qualityCheck(int input) {
if (input != 1 || input != 2 ||input != 3 ||input != 4 ||input != 5 ||input != 6 ||input != 7) {
return true;
} else {
return false;
}
}
}
—————————————————————————————–
Binary.java
————————————————-
import java.io.*;
import java.util.Scanner;

public class Binary
{
private PrintWriter pw;

/**
* Constructor for objects of class Binary
*/
public Binary(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}

/**
* Method that transforms a binary number to decimal
*/
public void binToDec()
{
System.out.println(“Please enter a binary number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(” “, “”);
bin = new StringBuilder(bin).reverse().toString();
int decimal = 0;
for (int i = 0; i<bin.length(); i++) {
//int power = bin.length() – 1- i;
int temp = (int)Math.pow(2, i);
decimal = decimal + Character.getNumericValue(bin.charAt(i)) * temp;
}
System.out.println(“Binary ” + oldbin + ” = ” + decimal + ” in decimal”);
pw.write(“Binary ” + oldbin + ” = ” + decimal + ” in decimaln”);

}

/**
* Method that transforms a binary to hexadecimal
*/

public void binToHex()
{
System.out.println(“Please enter a binary number”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.replace(” “, “”).trim();
StringBuffer hex = new StringBuffer(“00000000”);
//String hex1 = “”;
int j = 0;
for (int i = 0; i < bin.length();) {
if (bin.substring(i, i+4).equals(“0000”)) {
hex.setCharAt(j, ‘0’);
} else if (bin.substring(i, i+4).equals(“0001”)) {
hex.setCharAt(j, ‘1’);
} else if (bin.substring(i, i+4).equals(“0010”)) {
hex.setCharAt(j, ‘2’);
} else if (bin.substring(i, i+4).equals(“0011”)) {
hex.setCharAt(j, ‘3’);
} else if (bin.substring(i, i+4).equals(“0100”)) {
hex.setCharAt(j, ‘4’);
} else if (bin.substring(i, i+4).equals(“0101”)) {
hex.setCharAt(j, ‘5’);
} else if (bin.substring(i, i+4).equals(“0110”)) {
hex.setCharAt(j, ‘6’);
} else if (bin.substring(i, i+4).equals(“0111”)) {
hex.setCharAt(j, ‘7’);
} else if (bin.substring(i, i+4).equals(“1000”)) {
hex.setCharAt(j, ‘8’);
} else if (bin.substring(i, i+4).equals(“1001”)) {
hex.setCharAt(j, ‘9’);
} else if (bin.substring(i, i+4).equals(“1010”)) {
hex.setCharAt(j, ‘A’);
} else if (bin.substring(i, i+4).equals(“1011”)) {
hex.setCharAt(j, ‘B’);
} else if (bin.substring(i, i+4).equals(“1100”)) {
hex.setCharAt(j, ‘C’);
} else if (bin.substring(i, i+4).equals(“1101”)) {
hex.setCharAt(j, ‘D’);
} else if (bin.substring(i, i+4).equals(“1110”)) {
hex.setCharAt(j, ‘E’);
} else if (bin.substring(i, i+4).equals(“1111”)) {
hex.setCharAt(j, ‘F’);
}
i = i+4;
j = j + 1;
}
System.out.println(“Binay ” + oldbin + ” = ” + hex + ” in hexadecimal”);
pw.write(“Binary ” + oldbin + ” = ” + hex + ” in hexadecimaln”);
}
}
——————————————————————————-
Decimal.java
—————————————–
import java.io.*;
import java.util.Scanner;

public class Decimal
{
private PrintWriter pw;

/**
* Constructor for objects of class Decimal
*/
public Decimal(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}

/**
* Method that transforms a decimal to binary
*/
public void decToBin()
{
//pw.write(“Decimal To Binary:”);
System.out.println(“Please enter a decimal number: “);
Scanner keyboard = new Scanner(System.in);
Long input = keyboard.nextLong();
String binary=””;
for ( Long decimal = input ; decimal > 0 ; decimal/=2 )
{
binary = decimal%2 + binary ;
}
System.out.println(“Decimal ” + input + ” = ” + binary + ” in binary”);
pw.write(“Decimal ” + input + ” = ” + binary + ” in binaryn”);
}

/**
* Method that transforms a decimal to Hexadecimal
*/
public void decToHex()
{
System.out.println(“Please enter a decimal number: “);
Scanner keyboard = new Scanner(System.in);
int decimal = keyboard.nextInt();
int input = decimal;
String hex=””;
while (input != 0) {
if (input%16 == 10) {
hex = hex.concat(“A”);
} else if (input%16 == 11) {
hex = hex.concat(“B”);
} else if (input%16 == 12) {
hex = hex.concat(“C”);
} else if (input%16 == 13) {
hex = hex.concat(“D”);
} else if (input%16 == 14) {
hex = hex.concat(“E”);
} else if (input%16 == 15) {
hex = hex.concat(“F”);
} else if (input%16 == 16) {
hex = hex.concat(“d”);
} else {
hex = hex.concat(Integer.toString(input%16));
}
input = (int) input/16;
}

hex = new StringBuilder(hex).reverse().toString();
System.out.println(“Decimal ” + decimal + ” = ” + hex + ” in hexadecimal”);
pw.write(“Decimal ” + decimal + ” = ” + hex + ” in hexadecimaln”);
}

}
—————————————————————————-
Hexadecimal.java
————————————-
import java.io.*;
import java.util.Scanner;

public class Hexadecimal
{
private PrintWriter pw;

/**
* Constructor for objects of class Binary

*/
public Hexadecimal(PrintWriter pw)
{
// initialise instance PrinWriter
this.pw = pw;
}

/**
* Method that transforms a binary number to decimal
*/
public void hexToDec()
{
System.out.println(“Please enter a hexadecimal number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
String oldbin = bin;
bin = bin.toLowerCase().trim();
int decimal = 0;
for (int i = bin.length()-1; i >= 0; i–) {
int power = bin.length() – 1- i;
int temp = (int)Math.pow(16, power);
int value = 0;
if (bin.charAt(i) == ‘a’) {
value = 10;
} else if (bin.charAt(i) == ‘b’) {
value = 11;
} else if (bin.charAt(i) == ‘c’) {
value = 12;
} else if (bin.charAt(i) == ‘d’) {
value = 13;
} else if ((bin.charAt(i) == ‘e’)) {
value = 14;
} else if (bin.charAt(i) == ‘f’) {
value = 15;
} else {
value = Character.getNumericValue(bin.charAt(i));
}
decimal = decimal + value * temp;
}
System.out.println(“Hexadecimal ” + oldbin + ” = ” + decimal + ” in decimal”);
pw.write(“Hexadecimal ” + oldbin + ” = ” + decimal + ” in decimaln”);

}

/**
* Method that transforms a binary to hexadecimal

*/

public void hexToBin()
{
System.out.println(“Please enter a hexadecimal number:”);
Scanner keyboard = new Scanner(System.in);
String bin = keyboard.nextLine();
bin = bin.trim();
String binary = “”;
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == ‘0’) {
binary = binary.concat(“0000 “);
} else if (bin.charAt(i) == ‘1’) {
binary = binary.concat(“0001 “);
} else if (bin.charAt(i) == ‘2’) {
binary = binary.concat(“0010 “);
} else if (bin.charAt(i) == ‘3’) {
binary = binary.concat(“0011 “);
} else if (bin.charAt(i) == ‘4’) {
binary = binary.concat(“0100 “);
}   else if (bin.charAt(i) == ‘5’) {
binary = binary.concat(“0101 “);
}   else if (bin.charAt(i) == ‘6’) {
binary = binary.concat(“0110 “);
}   else if (bin.charAt(i) == ‘7’) {
binary = binary.concat(“0111 “);
}   else if (bin.charAt(i) == ‘8’) {
binary = binary.concat(“1000 “);
}   else if (bin.charAt(i) == ‘9’) {
binary = binary.concat(“1001 “);
}   else if (bin.charAt(i) == ‘A’) {
binary = binary.concat(“1010 “);
}   else if (bin.charAt(i) == ‘B’) {
binary = binary.concat(“1011 “);
}   else if (bin.charAt(i) == ‘C’) {
binary = binary.concat(“1100 “);
}   else if (bin.charAt(i) == ‘D’) {
binary = binary.concat(“1101 “);
}   else if (bin.charAt(i) == ‘E’) {
binary = binary.concat(“1110 “);
}   else if (bin.charAt(i) == ‘F’) {
binary = binary.concat(“1111 “);
}
}
System.out.println(“Hexadecimal ” + bin + ” = ” + binary + ” in binary”);
pw.write(“Hexadecimal ” + bin + ” = ” + binary + ” in binaryn”);
}
}
—————————————————————————
csis.txt
————————————–
Decimal 65535 = 1111111111111111 in binary
Decimal 1000000 = 11110100001001000000 in binary
Decimal 1234567890 = 1001001100101100000001011010010 in binary
Decimal 65535 = FFFF in hexadecimal
Decimal 1000000 = F4240 in hexadecimal
Decimal 1234567890 = 499602D2 in hexadecimal
Binary 0110 0101 1100 1101 1010 1110 0000 0101 = 1707978245 in decimal
Binary 0111 1111 1111 1111 0010 0001 1101 0011 = 2147426771 in decimal
Binary 0000 0000 0000 0000 0000 0000 0000 0010 = 2 in decimal
Binary 0110 0101 1100 1101 1010 1110 0000 0101 = 65CDAE05 in hexadecimal
Binary 0111 1111 1111 1111 0010 0001 1101 0011 = 7FFF21D3 in hexadecimal
Binary 0010 1011 1010 1101 0111 0110 0011 1010 = 2BAD763A in hexadecimal
Hexadecimal 12345678 = 305419896 in decimal
Hexadecimal 2A3DF8A7 = 708704423 in decimal
Hexadecimal 00FF00FF = 16711935 in decimal
Hexadecimal 12345678 = 0001 0010 0011 0100 0101 0110 0111 1000 in binary
Hexadecimal 2A3DF8A7 = 0010 1010 0011 1101 1111 1000 1010 0111 in binary
Hexadecimal 00FF00FF = 0000 0000 1111 1111 0000 0000 1111 1111 in binary

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?