Lab3 - California State University, Northridge



Rational String To Rational Number

Inheritance:

Observation:

We add a new method SignStrToNumConversion() into the class StrToNum. This method will take care of the case the string has a sign symbol. That means it is an extension of the method StrToNumConversion().

In reality we might not have a source code for the class StrToNum to add the new method SignStrToNumConversion() because the class might be written by someone else. Hence, in this case we have no choice but to leave class StrToNum untouchable. However, we can bild the new method, let’s name it StrToInt, which is an extension of the class StrToNum.

BEFORE:

import java.io.*;

import java.lang.*; // include it because of String

class StrToNum //

{

// characteristics or component

String strNum; //

// constructor to create an object from the caller who provides parameter

public StrToNum(String InputStrNum)

{ strNum = InputStrNum;}

// method to convert strNum into a number which is returned as an integer.

public int StrToNumConversion()

{

// < body of the conversion>

int Num = 0;

int totalNum = 0;

char firstChar;

while (strNum != null)

{ // body of while

//get the first char and save it to a char var

firstChar = strNum.charAt(0);

//reduce the strNum

if (strNum.length() == 1) strNum = null;

else strNum = strNum.substring(1);

//convert this char into a number

if (firstChar == '0') Num = 0;

else if (firstChar == '1') Num = 1;

else if (firstChar == '2') Num = 2;

else if (firstChar == '3') Num = 3;

else if (firstChar == '4') Num = 4;

else if (firstChar == '5') Num = 5;

else if (firstChar == '6') Num = 6;

else if (firstChar == '7') Num = 7;

else if (firstChar == '8') Num = 8;

else if (firstChar == '9') Num = 9;

else System.out.println ("ERROR");

totalNum = totalNum* 10 + Num;

} // End of While

// System.out.println (" After the while loop, total Number is " + totalNum);

return totalNum;

} // End of StrToNumConversion

} //End of class StrToNum

AFTER:

import java.io.*;

import java.lang.*; // include it because of String

class StrToNum //

{

// characteristics or component

String strNum; //

// constructor to create an object from the caller who provides parameter

public StrToNum(String InputStrNum)

{ strNum = InputStrNum;}

// method to convert strNum into a number which is returned as an integer.

public int StrToNumConversion()

{

// < body of the conversion>

int Num = 0;

int totalNum = 0;

char firstChar;

while (strNum != null)

{ // body of while

//get the first char and save it to a char var

firstChar = strNum.charAt(0);

//reduce the strNum

if (strNum.length() == 1) strNum = null;

else strNum = strNum.substring(1);

//convert this char into a number

if (firstChar == '0') Num = 0;

else if (firstChar == '1') Num = 1;

else if (firstChar == '2') Num = 2;

else if (firstChar == '3') Num = 3;

else if (firstChar == '4') Num = 4;

else if (firstChar == '5') Num = 5;

else if (firstChar == '6') Num = 6;

else if (firstChar == '7') Num = 7;

else if (firstChar == '8') Num = 8;

else if (firstChar == '9') Num = 9;

else System.out.println ("ERROR");

totalNum = totalNum* 10 + Num;

} // End of While

// System.out.println (" After the while loop, total Number is " + totalNum);

return totalNum;

} // End of StrToNumConversion

} //End of class StrToNum

import java.io.*;

import java.lang.*; // include it because of String

import StrToNum.*;

class SampleCaller

{

// method to call, in this case, the main

public static void main(String[] args)

{

// read from the keyboard

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(reader);

System.out.print("please enter a string number>");

String input="";

try { input = console.readLine();}

catch (IOException e ){ System.out.println(e); System.exit(1); }

// declare an object variable the wrapping input

StrToNum A;

A = new StrToNum (input);

int Value = A.StrToNumConversion();

System.out.println("The equivalent value =" + Value);

} // End of main

} //End of class SampleCaller

import java.io.*;

import java.lang.*;

import StrToInt.*;

class SampleCaller

{

// method to call, in this case, the main

public static void main(String[] args)

{

// read from the keyboard

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader console = new BufferedReader(reader);

System.out.print("please enter a string number>");

String input="";

try { input = console.readLine(); }

catch(IOException e) { System.out.println( e);System.exit(1);}

// declare an object variable the wrapping input

StrToInt A;

A = new StrToInt (input);

int value = A.SignStrToNumConversion();

System.out.println("The equivalent value =" + value);

} // End of main

} //End of class SampleCaller

import java.io.*;

import java.lang.*;

import StrToNum.*;

class StrToInt extends StrToNum

{

public StrToInt(String strNum)

{ super(strNum);}

public int SignStrToNumConversion()

{

char firstChar= strNum.charAt(0);

int signValue = 1;

if (firstChar == '+'){ strNum = strNum.substring(1);}

else if (firstChar == '-')

{ signValue = -1; strNum = strNum.substring(1);}

int value = StrToNumConversion();

value = signValue*value;

System.out.println(" FromStrToInt, value = " + value);

return value;

} // End of SignStrToNumConversion

}

-----------------------

New class, an extenssion of StrToNum

StrToNum is changed to StrToInt

identical

Since the constructor is not inherited automatically, we must build a new constructor and use the old constructor by using the instruction super(strNum);

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download