Professor Glass' comments

IMPORTANT: Everyone will be required to eventually have their code meet these specifications.  Others will be using your code and you will need to use others.  You will not see the souce but have to match the specifications listed here and be able to use the code in your programs.  Therefore, if you think these defintions are lacking, incomplete or not clear, change these specifications. - Dr. Glass

 

Think of this. 

using only the functions on this page, outline / write to access the data (other than needing additional I/O) 

  • A program that will read two strings in possibly different bases and add them together and print the output in a selected base.
  • Assuming you had a method that read 16 bit integer directly from data file, and output the integer in Hex.
  • Reading a character, write a program that will print the ascii code of the character in binary.

 

I read the comments and some very good points were raised.

  1. First, do not assume that the first program I assigned is great, the best or anything of that nature or really should be any driving force in ths.
  2. The discussion made me realize that there are several possible errors:
    1. An invalid number ex: hello.
    2. An invalid number as function of the base.  ex: 1234 base 2
    3. An unsupported base, a base not equal to 2, 8, 10 or 16.

Question is, how do toy think it should be handled?

but how about on output?  I call MyNumber.toString(4);  there should be an exception, no?  We do not handle base 4. 

 

We decided that two different exceptions should be thrown.  One for invalid number and one for an unsupported base.

 

Time To Code

The following people will have the following responsibilites.  Attach your code (see Attach File button) next to your name (attaching makes it a link).  You have 3 days (4//8, 4/9 and 4/10) to meet and to get this done. There is nothing wrong in adapting the code from your first assignments.

 

Dan: Base_number() SoftEng constructors.java

Camron: toString.java

RoyRubio: Everything else in the class other than the constructors and ToString() methods.

ViJay: Collecting it all and making a package

Armando: A main() that tests all methods including catching the exceptions.

Sunho: Re-do assignment 1 using this new package.

 

 

 

API (Application Program Interface) for Java class Base_Number

 

CLASS VARIABLES
private int num Changed by setNumber(int) and setNumber(char[], int).
String output (and conversion) with toString(int) and integer output using getNumber().

CONSTRUCTORS
Base_Number()

Instantiates the class and set the num to 0.
A default constructor makes sense if we instantiate before dealing with user input.  Refer to the following code:

Base_Number b = new Base_Number();
// User inputs base and number.
b.setNumber(charArray, base);

Base_Number(char[] charArray, int base) throws Exception

Invokes setNumber() to set class variable.

charArray is the input and base is the current base of charArray.

 
private boolean isValid(char[] charArray, int base) throws Exception

Checks validity of charArray according to base as well as base itself using supportedBase().

Example:  "FE" base 16 is valid, "78" base 8 is invalid.

May throw two exceptions:  invalid base or invalid number.

private boolean supportedBase(int base) Checks validity of base (currently 2, 8, 10, or 16 are valid).

public void setNumber(int n) throws Exception

Sets class variable num via an integer.
public void setNumber(char[] charArray, int base) throws Exception

Sets class variable num via a string.

Uses Horner's method to convert between bases.

Example:  AA base 16 --> base 10 = 10*16^1 + 10*16^0 = 170

Validates data using isValid() and therefore may throw one of two exceptions.

OUTPUT
public int getNumber()
Useful for mathematical operations.
public String toString() Output defaults to base 10.

public String toString(int base) throws Exception

Converts the number to the desired base and returns a string.

Validates base using supportedBase() and throws an exception if unsupported.


Page Information

  • 4 months ago [history]
  • View page source
  • You're not logged in
  • Recent comments:
    armando:Alright, i would fix them!
    Richard Glass:Several comments: Why is isValid() static? I do not see any data method definitions. There is no input method for the string or the base. There is no validation of the base. I cannot change the base of a number easily. the code to print the number in different bases is convoluted.
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts