Java inheritance program

nikkit5598
nikkit5598 used Ask the Experts™
on
This is what I need to do, but i don't really know where to begin. Can anyone help of give tips?

Create a class LongInteger that represents a single long integer value.  The class should have methods that allow you to add, subtract, multiply, and divide two LongInteger objects.  Each of these methods should return a new LongInteger object.  There should also be get and set methods to allow the value of the integer to be changed.  Implement a toString() which returns the string representation of the integer.  Finally, implement the Comparable interface so that two LongInteger objects can be compared.

Derive a class from LongInteger called BinaryInteger, which is a binary (base 2) version of LongInteger.  It should be able to do all the things LongInteger does (add, subtract, multiply, divide, compare two BinaryInteger objects.)  Of course, the toString method needs to return a binary number in string format.

Derive a class from LongInteger called OctalInteger, which is an octal (base 8) version of LongInteger.  It should be able to do all the things LongInteger does (add, subtract, multiply, divide,  compare.)  The toString method needs to return an octal number in string format.

Derive a class from LongInteger called HexInteger, which is a hexadecimal (base 16) version of LongInteger.  As with BinaryInteger, HexInteger should have add, subtract, multiply, divide, and the ability to compare.  It should also have its own toString.

Create a class IntDriver which will contain a main method.  The class should do the following:

   1. Present the user with a menu that allows them to select one of the above three numbers.  The user is then supposed to enter a number of that type.  If the number they enter is not of the specified type, throw an exception to handle it.  The type of exception you throw is up to you, but you should include the type of number expected and the input the user entered.  This information should be printed to the screen (ex: BinaryInteger expected, user entered 1010102).  The user should then be re-prompted for an integer of the chosen type.

      Continue allowing the user to choose numbers until she either chooses to quit or 25 numbers are entered.
   2. Sort all the numbers entered by the user.  You are guaranteed at least two numbers will be entered and that there will be at least two numbers of the same type.
   3. Print the numbers to the screen, one per line.
   4. Go through the array and find two numbers of the same type.  Report their values then add them, subtract them, multiply them, and divide them.  Of course you'll want to print the results of each of those arithmetic operations.

 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Officer
Most Valuable Expert 2011
Commented:
Hello nikkit5598,

Since this appears to be a direct copy and paste from a "homework" assignment, I will try my best to do as you say and give some "tips" and will be here to help guide you code this yourself.  Since you indicated you were not sure where to start, I will have to presume you don't know any of the concepts below so forgive me if I repeat things you already know.

As you will see by my below reference(s), a good place to start is always the Java Tutorial online:
http://java.sun.com/docs/books/tutorial/java/

>>Create a class LongInteger that represents a single long integer value.  

Classes are created by using the class keyword followed by name and preceeded by optional visibility modifier.

class SomeThing {

}

Ref.: http://java.sun.com/docs/books/tutorial/java/concepts/class.html
(has nice information on other object-oriented concepts like inheritance also)

>>The class should have methods that allow you to add, subtract, multiply, and divide two LongInteger objects.  

Methods are add into classes as:

class SomeThing {
   modifier returntype someAction([parameter(s)]) {
      // do some special action
      return returntype;
   }
}

Where "modifier" is a keyword like public, private or protected that indicates the visibility of the method.  You can also define a method as static which means it will be a class level method such as main() usually is for a driver class.  "returntype" is void or the data type to be returned by the method.  Optionally, your method can take a parameter which would be defined by a name and a type.  For example, in your case, you are adding a LongInteger to another LongInteger; therefore, a possible parameter for an add method would be "LongInteger otherLI" where the first value is the data type of LongInteger and the second is the name of the variable otherLI.

Ref.: http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html
(has information on declaring class also and its members, so another multipurpose link)

>>Each of these methods should return a new LongInteger object.  

Should be able to figure out what this means to the above example by yourself.

>>There should also be get and set methods to allow the value of the integer to be changed.  

Go through the links and should find some useful information on members and accessor/mutator methods.  Accessors "get" a member's value; mutators, "set" value.  Since you know ahead of time that the LongInteger will be a base class, you have a step up in knowing you probably want to code with some generic-ness in mind; therefore, I would suggest the internal storage of the main value for LongInteger be placed in a variable called "value" that has a modifier of private.  Then you can have a "setValue" and "getValue" method that would make sense whether you are updating a LongInteger or a BinaryInteger.  Just a thought though.

>>Implement a toString() which returns the string representation of the integer.  

Should have what you need here.  They have given you the name of the method (e.g., toString()) and the return type (e.g., String).  Hint to complete the signature of the method: toString() is generally publicly visibile.

>>Finally, implement the Comparable interface so that two LongInteger objects can be compared.

Try this out and post back if you have issues.  This will be helpful for when you create the child objects like BinaryInteger as you are inheriting LongInteger which is similar especially when inheriting from an Abstract class (i.e., a base class that has not been fully implemented).  One key difference for you is that you inherit with the extends keyword but you implement with the implements keyword.  Both will have to implement (or override) required methods based on the new class's intened functionality or purpose.

class SomeThing implements IsSpecial {
   // implement required methods...
}

>>
Derive a class from LongInteger called BinaryInteger, which is a binary (base 2) version of LongInteger.  It should be able to do all the things LongInteger does (add, subtract, multiply, divide, compare two BinaryInteger objects.)  Of course, the toString method needs to return a binary number in string format.

Derive a class from LongInteger called OctalInteger, which is an octal (base 8) version of LongInteger.  It should be able to do all the things LongInteger does (add, subtract, multiply, divide,  compare.)  The toString method needs to return an octal number in string format.

Derive a class from LongInteger called HexInteger, which is a hexadecimal (base 16) version of LongInteger.  As with BinaryInteger, HexInteger should have add, subtract, multiply, divide, and the ability to compare.  It should also have its own toString.
<<

Once you go through the above, as stated, you should have a better understanding of what it means to derive/extend LongInteger here.

>> 
Create a class IntDriver which will contain a main method.  The class should do the following:

   1. Present the user with a menu that allows them to select one of the above three numbers.  The user is then supposed to enter a number of that type.  If the number they enter is not of the specified type, throw an exception to handle it.  The type of exception you throw is up to you, but you should include the type of number expected and the input the user entered.  This information should be printed to the screen (ex: BinaryInteger expected, user entered 1010102).  The user should then be re-prompted for an integer of the chosen type.

      Continue allowing the user to choose numbers until she either chooses to quit or 25 numbers are entered.
   2. Sort all the numbers entered by the user.  You are guaranteed at least two numbers will be entered and that there will be at least two numbers of the same type.
   3. Print the numbers to the screen, one per line.
   4. Go through the array and find two numbers of the same type.  Report their values then add them, subtract them, multiply them, and divide them.  Of course you'll want to print the results of each of those arithmetic operations.
<<

I would ensure you fully understand the previous sections and how to construct those classes, but you can always have the framework for this setup and ready to go.
Drivers are typically:

public class SomeDriver {
   public static void main(String[] args) {
      // initialize

      // read

      // calc

      // print

      // finalize
   }
}

As I have shown, you can typically break this down into read, calc and print segments while learning to program OR input, process and output.  In addition, you have prep work (initialization) and clean up (finalization).  In your case, [1] is read of user input; [2] and [4] are processing steps and [3] is print.  You can read from the command line or from input boxes or a form, so the how will depend on you -- suspect they want you to present the menu at command prompt and similarly print to command prompt so I will provide a reference below.  For the calc(ulation) step, using Comparable interface above, it should be easy to use the Collections/Arrays api to handle sorting and searching of List/Array.

Ref.: http://java.sun.com/docs/books/tutorial/essential/io/cl.html


Hope that helps get you going.  From here, you should give this a try in code and then post back with issues you encounter.  If compilation issues, then post the error / line of code the compiler is pointing to as the issue.  For runtime exceptions, post the stack trace.  For logical exceptions, give details on what you expected to happen and what happened instead.  This will help you in the future as well as you work on further units for this, you will want to lead in new questions with your existing code or portion of code and one of the three details above or targeted question on syntax for that piece of code as it will allow us to help you here while maintaining "academic honesty".

Best regards and happy coding,

--isa

Author

Commented:
ok. so ive looked over this and the assignment again (thank you for your help).
Something I'm still not understanding though, is the base 2, base 8, base 16.
Maybe I'm trying to think too much into it or something, but I'm not quite sure what I should be doing with that.
Can anyone explain?

Thank you again,
-N
Commented:
Everything that happens "inside" the machine is ultimately actually in binary. Nothing much to be done about that. The key thing the assignment specifies is "The toString method needs to return an octal number in string format". So for the various different formats you need to write a toString with the class that produces a representation of the number in the appropriate base.

Author

Commented:
ok. so ive got most everything done but the menu class. ive got only a small portion of it done (which probably wont even work) and have no idea how to proceed from here. anything will help. why does programming have to be soooo frustrating?

import java.util.Scanner;

public class IntDriver
{
      public static void main(String[] args)
      {
            menu();
      }
      public static void menu()
      {
            Scanner kb;
            kb = new Scanner(System.in);
            int choice = 0;
            
            do
            {
                  do
                  {
                        System.out.println("1. Select Binary Integer, Hex Integer, or Octal Integer");
                        System.out.println("2. Sort the numbers.");
                        System.out.println("3. Print the numbers.");
                        System.out.println("4. Find number of the same type and perform mathematical operations and print.");
                        System.out.println("5. Quit.");
                        
                        choice = kb.nextInt();
                        kb.nextInt();
                        
                  }while(choice < 1 || choice >4)
                  switch(choice)
                  {
                  case 1:
                        System.out.println("Would you like to enter a binary, octal, of hex integer?");
                        String integerType = kb.next();
                        System.out.println(integerType);
                        System.out.println("Enter the integer.");
                        int integer =  kb.nextInt();
                        System.out.println(integer);
                        break;
                  case 2:
                        
                  }
                  
                              
            }
      }
}
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Okay please read the requirements again.  The menu choices should be Binary Integer, Hex Integer, Octal Integer or Done Entry.  The program runs from there to do the sorting / calcuations and subsequently the printing which signifies the end of a run so quits after that.

Once a choice on type of number is selected, you are basing the type entry and exception handling on that choice.  Once "Done Entry" is selected, you break out of the entry loop (read section of code) and let the flow continue.

Remember:

// read -- menu / entry is here in loop

// calc

// print

Regards,

Kevin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial