Question

In: Computer Science

In Lab 4, you made a class with static methods. The static methods converted an integer...

  • In Lab 4, you made a class with static methods.
  • The static methods converted an integer in binary, Hex, and Octal.
  • To do this, you made a class of static methods.
  • The class did not have any fields. The static methods received the integer value as a parameter.
  • For this lab, make all of the static methods, non-static methods.

Lab 5 Requirements

  1. Create a class named “Lab5_IntConv_Class
  2. Have one private field name intValue.
  3. Add two constructors:
    1. One is a default contructor.
    2. The other constructor accepts an integer.
  4. Make the 4 methods non-static.
  5. Add 2 more methods called setInteger() and getInteger().
  6. When one of the 4 methods are called they are to use the field intValue as the value to convert.
  7. These four methods will still return the answer, to the caller.
  8. Use the two supplied java files from the last assignment. Or the two java files your created from the ones given.

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!

Solutions

Expert Solution

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.


Related Solutions

You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
In this lab you will learn how to use methods from the Math class in order...
In this lab you will learn how to use methods from the Math class in order to calculate the area or the volume of several different shapes. If you are confused about the Methods you can access from the Math class and would like to see some examples click here. Hint: Most of these methods can be done in one line. Step 1 - circleArea In this method you will calculate the area of a circle. Before you can calculate...
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
When you create a C++ class, you are given 4 default methods. What are they and...
When you create a C++ class, you are given 4 default methods. What are they and why is it important that you remember that these exist?
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT