In: Computer Science
Lab 5 Requirements
Submit the main() java file and the java file that has the class.
Static method code so far from Lab 4
1 import java.util.*;
2 import java.util.Scanner;
3 class Lab4_IntConv
4 {
5
6 public static String Conv_SignedInt2Binary (int intValue) //
convert signed integer to binary
7 {
8
9 return Integer.toBinaryString(intValue);
10 }
11
12
13 public static String Conv_UnsignedInt2Binary (int intValue) //
convert unsigned integer to binary
14 {
15 String test = "";
16 intValue=Math.abs(intValue);
17 while (intValue > 0)
18 {
19 test = ( (intValue & 1 ) == 0 ? '0' : '1') +test;
20 intValue = intValue>>1;
21 }
22 return test;
23 }
24
25 public static String Conv_unsignedInt2Hex(int intValue) //
convert unsigned integer to Hexadecimal
26 {
27 intValue=Math.abs(intValue);// convert signed integer to
unsigned integer
28 return Integer.toHexString(intValue);
29 }
30
31
32 public static String Conv_UnsignedInt2Octal(int intValue) //
convert signed integer to Octal
33 {
34 intValue=Math.abs(intValue); // convert signed integer to
unsigned integer
35 return Integer.toOctalString(intValue);
36 }
37
38 }
Main method so far from lab 4
1 import java.util.*;
2 import java.util.Scanner;
3
4 public class Lab4_IntConv_Main
5 {
6
7
8 public static void main (String[] args)
9 {
10
11 int i, item = 0;
12 int intValue;
13
14 Scanner kb = new Scanner(System.in); //Scanner method is used to
connect keyboard
15 final int EXIT_OPTION = 5;
16 do
17 {
18
19 System.out.println( "1. Convert Integer (signed) to Binary.\n2.
Convert Integer (unsigned) to Binary \n3. Convert Integer
(unsigned) to Hexadecimal.\n4. Convert Integer (unsigned) to
Octal.\n5. Exit Program.\n");
20 item = kb.nextInt();//get option from user
21
22
23 if ((item < 1) || (item > EXIT_OPTION))
24 System.out.println("Selection"+item+" is not a vald
option.\n\n");
25
26 else
27 {
28
29 switch (item)
30 {
31 case 1: System.out.println("Enter the integer value, to convert:
");
32 intValue = kb.nextInt();
33 System.out.println("Binary
Value="+Lab4_IntConv.Conv_SignedInt2Binary(intValue)); //call the
method signed integer to binary
34 break;
35 case 2: System.out.println("Enter the integer value, to convert:
");
36 intValue = kb.nextInt();
37 System.out.println("Binary
Value="+Lab4_IntConv.Conv_UnsignedInt2Binary(intValue)); //call the
method unsigned integer to binary
38 break;
39 case 3: System.out.println("Enter the integer value, to convert:
");
40 intValue = kb.nextInt();
41 System.out.println("Hexa
Value="+Lab4_IntConv.Conv_unsignedInt2Hex(intValue)); //call the
method unsigned integer to Hexadecimal
42 break;
43 case 4: System.out.println("Enter the integer value, to convert:
");
44 intValue = kb.nextInt();
45 System.out.println("Octal
Value="+Lab4_IntConv.Conv_UnsignedInt2Octal(intValue)); //call the
method unsigned integer to Octal
46 break;
47 case 5: System.out.println ("\n\t----- Goodbye -----\n");
48 System.exit(0); //exit from the program
49 }
50 }
51 }while (item != EXIT_OPTION); // do-while
52 }
53 }
If anyone could help me with these codes with the instructions given above that would be great!
All the explanations is given in the comments of the code itself.
Code--
import java.util.Scanner;
class Lab5_IntConv_Class
{
private int intValue;
//default constructor
public Lab5_IntConv_Class()
{
this.intValue=0;
}
// convert signed integer to binary
public String Conv_SignedInt2Binary()
{
return
Integer.toBinaryString(this.intValue);
}
// convert unsigned integer to binary
public String Conv_UnsignedInt2Binary()
{
String test = "";
int
temp=Math.abs(this.intValue);
while (temp > 0)
{
test = ( (temp
& 1 ) == 0 ? '0' : '1') +test;
temp =
temp>>1;
}
return test;
}
// convert unsigned integer to Hexadecimal
public String Conv_unsignedInt2Hex()
{
// convert signed integer to
unsigned integer
int
temp=Math.abs(this.intValue);
return
Integer.toHexString(temp);
}
// convert signed integer to Octal
public String Conv_UnsignedInt2Octal()
{
// convert signed integer to
unsigned integer
int
temp=Math.abs(this.intValue);
return
Integer.toOctalString(temp);
}
//constructor that accepts integer
public Lab5_IntConv_Class(int intValue)
{
this.intValue = intValue;
}
//getters and setters
public int getIntValue()
{
return intValue;
}
public void setIntValue(int intValue)
{
this.intValue = intValue;
}
}
//Main method so far from lab 4
public class Lab5_IntConv_Main
{
public static void main (String[] args)
{
int i, item = 0;
int intValue;
Lab5_IntConv_Class t;
Scanner kb = new
Scanner(System.in); //Scanner method is used to connect
keyboard
final int EXIT_OPTION = 5;
do
{
System.out.println( "1. Convert Integer (signed) to Binary.\n2.
Convert Integer (unsigned) to Binary \n3. Convert Integer
(unsigned) to Hexadecimal.\n4. Convert Integer (unsigned) to
Octal.\n5. Exit Program.\n");
item =
kb.nextInt();//get option from user
if ((item <
1) || (item > EXIT_OPTION))
System.out.println("Selection"+item+" is not a
vald option.\n\n");
else
{
switch (item)
{
case 1:
System.out.println("Enter the integer value, to convert: ");
t=new
Lab5_IntConv_Class(kb.nextInt());
System.out.println("Binary Value="+t.Conv_SignedInt2Binary());
//call the method signed integer to binary
break;
case 2:
System.out.println("Enter the integer value, to convert: ");
t=new
Lab5_IntConv_Class(kb.nextInt());
System.out.println("Binary Value="+t.Conv_UnsignedInt2Binary());
//call the method unsigned integer to binary
break;
case 3:
System.out.println("Enter the integer value, to convert: ");
t=new
Lab5_IntConv_Class(kb.nextInt());
System.out.println("Hexa Value="+t.Conv_unsignedInt2Hex()); //call
the method unsigned integer to Hexadecimal
break;
case 4:
System.out.println("Enter the integer value, to convert: ");
t=new
Lab5_IntConv_Class(kb.nextInt());
System.out.println("Octal Value="+t.Conv_UnsignedInt2Octal());
//call the method unsigned integer to Octal
break;
case 5:
System.out.println ("\n\t----- Goodbye -----\n");
System.exit(0); //exit from the program
}
}
}while (item != EXIT_OPTION); //
do-while
}
}
Code Screenshot--
Note--
Please upvote if you like the effort.