Link to home
Start Free TrialLog in
Avatar of nikkit5598
nikkit5598

asked on

Java inheritance program

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.

 
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nikkit5598
nikkit5598

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
                        
                  }
                  
                              
            }
      }
}
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