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? - This will all be handled in the isValid method. If input is invalid, we can show an error message based on an exception or isValid returning false.

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

 

 

Class name:  Base_Number.java throws InvalidInput Exception: InvalidInput Exeception is an oracle package (You also need permission to used according to its website). You should use IOException because you are dealing with standard input in general, go to this Link: java tutorial.

--ok so if u dont like the idea change it to IOException

 

                   (Extends Object)

 

todo

  1. What is the class name?  What is the inheritance?    //Class name is Base_Number and it does not need to inherit anything Base_Number's parents class is the Object class. -Are you saying to write "public class Base_Number extends Object"? Why do we need to do  that? Or are you just saying Base_Number will automatically/naturally inherit the Object class at instantiation? i don t think we need to say that it extends Object since it automatically does. That's right. but in API you need to states that your are extending from Object because people who would read our API, needs to know the class parent  of Base_Number. Look ,  go to this link : Writing a java API . Read this and you'll understand.. Ok. It was just that I thought you didn't have to mention it since it was automatic and I thought there was no way for a instantiated object to NOT inherit the object class.

     

     

  2. Would the constructor witout parma mean anything?
  3. how can we change the number?  via string?  via integer?  setNumber(int n) and setNumber(char[] charArray, int base), or replace char[] with String.
  4. Worry about a base we can't handle.                                    //We can check the base and not convert bases we can't handle --i think that should be done in the isValid()

 

API (Application Program Interface)

 

 

CLASS VARIABLES

private int num

// Changed by setNumber(int n) and setNumber(char[] charArray, int base).

// Output (conversion) with toString(int base).

 

 

CONSTRUCTORS

Base_Number() does a default constructor make sense? It could simply instaniate the class and set the num to 0.

 

Only if we add a setNumber method  Having non-static methods in a contructor  is not a good idea in OOP, go to this link: The Liskov Substitution Principle.

Understading that principle would be very important later on !

I agree with rglass.  A default constructor makes sense if we instantiate a variable before dealing with user input.  Refer to the following code flow:

Base_Number b = new Base_Number();

// User inputs base and number.

b.setNumber(charArray, base);

 

 

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

// Invokes toNumber() setNumber()

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

 

 

CLASS FUNCTIONS

 

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

Do we need the base a parameter? a number is number right? Is there any data validation on the constructor? what type of exception can this throw?

--we need the base as a parameter because we want to check to see if that number is of that base and if it is not we can throw an exception...we need to get the number and the base from the user

// Checks validity of charArray according to base

// example:  "FE" base 16 is valid, "78" base 8 is invalid

 

private void toNum(char[] charArray, int base) throws InvalidInput Exception

// Change this function to  public void setNumber(char[] charArray, int base) ?

 

 

IOException, but why it throws an exception? why void? why we need the base as parameter?--> REMEMBER : a number is NUMBER! a base is  DISPLAY FORMAT to represent a number!

--its void because we need to convert the user input to base 10 and store it in num...then we can use num to convert the user input to either base 2,8,10,or 16. There is no need to convert charArray to base 10  because a number is a number!! a base is just a  set of symbols to represent or display a number, look go to this link , so you have a clear understanding of numeral system: Numeral_System. Also in my API i gave an explanation of the relationship of a number and a base: SOFTWARE ENGINEERING

--uumm ok??

 

// Validates data using isValid()

// Uses Horner's method to convert char[] to int.

// converts the number stored in charArray to an int and stores it in num

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

 

public void printNumber(int base)

Why do you need a base as a paramater? What is the meaning or purpose of a parameter?

--we want to know what base the user wants to convert the number to...therefore we need a parameter. The user only enters the base of its array of characters representing the number, but the program doesn't need to ask the user for a conversion. if you read the specifications, your program has to convert to all four base representation.

The professor asked: "Write a Java or C++ program that will convert an array of char supplied in hex, octal, binary or decimal and print out a the number as a string or array of char in all four formats stated above."ya but then the professor asked what if the user wants to only convert the number to one base...which i believe is true...why would u need the output in all four bases at one time

 

What will printNumber do for our program? In the assignment printNumber is defined as "prints one character at a time, the contents of the data array". But today in class I believe the professor said that this is a debug method that will just print the number as a int without converting it to a string/char array. I think that would then leave our string/char converting to be done in toString. Will this method be used to debug or used as assigned?  If the decision is to dump it, then we dump it.  Don't always listen to me :-)  Think and rationalize. :-)

I think this method should be removed.  The purpose was to convert the int class variable to a desired base for output, but toString() takes care of that now.

--yes i agree...we should dump it because we already have a toString that can do the same thing for us

 

 

 

 

public String toString() // THIS IS INHERETED FROM THE  OBJECT CLASS - It doesn't matter if it is inheritted though. The toString method from the object class just prints out a hash code of the object. We want to print out the number as a string. We are going to override the Object class's toString anyway.- But you need to stated, read the next link so you have a better understanding on writing an API: Writing a java API A toString() would need the base.  Another possiblility is the one without a parameter could default to base 10.

 

 

public String toString(int base)

//Converts the number to a string and returns it

//Paramter base: base - only 2,8,10,16

 

 

public void setNumber(int n)

 

 n im just going to add a setBase cuz itz more convenient to set the base with a mutator rather than reinstantiating a whole new Base_Number object

public void setBase(int b)// Why?

Yes, we could use this.  We may be getting an integer from somewhere, like a file or as the result of a numerica calculation.  There are no input strings involved but only an output string.  Like today when I was showing an octal dump.

 

Why does setBase() make sense?  Java's int data type has no base.  Aren't we only specifying base for saving user input (setNumber) and output conversion (toString)?

I think toNum() and setBase() should be removed - we only needsetNumber(int n) and setNumber(char[] charArray, int base) to change the class variable.  The parameterized constructor would call setNumber(), and setNumber() would call isValid().  If true, it sets the class variable.

--ok...so i think u should include whatever u said in the api

 

 

 

 Comments on green is by Armando Fonseca. March 19, 2008.

Camron= blue comments

Vijay : Red

rglass: black with beige background 3/20

Dan: purple with white background

 

Ok guyz...i understand u all have a lot of great suggestions including me...but if we keep writing comments without writing ne code we cant get nething done...so if u dont like the changes i made n u have better suggestions please feel free to change it n dont just comment on it

 


Page Information

  • 5 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