Question: Please translate this Java code to C++... Char.java ** * Custom Char class * @author Anonymous * */ public class Char { private char value;

Please translate this Java code to C++...

Char.java

** * Custom Char class * @author Anonymous * */ public class Char { private char value; public Char(){ this.value = '\0'; } public Char(char c){ this.value = c; } public Char(int c){ this.value = (char)c; } public Char(final Char c){ this.value = c.getValue(); }

public Char(String s){ this.value = s.charAt(0); } public char getValue(){ return this.value; } public void equals(final Char c){ this.value = c.getValue(); } public void equals(char c){ this.value = c; } public void equals(int c) throws CharException{ if(c<32 || c>127) throw new CharException("Invalid Character"); this.value = (char) c; } public char toChar(){ return this.value; } public int toInt(){ return (int)this.value; } public String toString(){ return String.valueOf(this.value); } /** * To convert char to Hex string * @return */ public String toHexString(){ return String.format("%04x", (int)this.value); } public String add(char c){ return toString().concat(String.valueOf(c)); } public String add(Char c){ return toString().concat(String.valueOf(c.getValue())); } }

BigDecimal.java

import java.util.ArrayList; import java.util.List;

/** * Custom BigDecimal class * @author Anonymous * */

public class BigDecimal { List list; /** * Overloaded Constructors */ public BigDecimal(){ list = new ArrayList(); list.add(new Char('0')); list.add(new Char('.')); list.add(new Char('0')); } public BigDecimal(String value) throws BigDecimalException{ list = new ArrayList(); //For numbers like .99 if(value.startsWith(".")){ list.add(new Char('0')); } validateString(value); for(int i=0;i list.add(new Char(value.charAt(i))); } } public BigDecimal(final BigDecimal value){ list = new ArrayList(); List valList = value.getValue(); for(int i=0;i list.add(new Char(valList.get(i))); } } /** * @param ch * @throws BigDecimalException */ public void equals(char ch) throws BigDecimalException{ BigDecimal number = new BigDecimal(String.valueOf(ch)); this.list = number.getValue(); } /** * * @param s * @throws BigDecimalException */ public void equals(String s) throws BigDecimalException{ validateString(s); BigDecimal number = new BigDecimal(s); this.list = number.getValue(); } public List getValue(){ return this.list; } /** * Add Method * @param other * @return * @throws BigDecimalException */ public BigDecimal add(BigDecimal other) throws BigDecimalException{ double num1 = this.wholeNumber()+this.fraction(); double num2 = other.wholeNumber()+other.fraction(); return new BigDecimal(String.valueOf(num1+num2)); } /** * Subtract Method * @param other * @return * @throws BigDecimalException */ public BigDecimal subtract(BigDecimal other) throws BigDecimalException{ double num1 = this.wholeNumber()+this.fraction(); double num2 = other.wholeNumber()+other.fraction(); return new BigDecimal(String.valueOf(num1-num2)); } /** * Get Whole Number * @return */ public int wholeNumber(){ List subList; if(containsDecimal()){ int index=0; for(Char c:this.list){ if('.' == c.toChar()){ break; } index++; } subList = this.list.subList(0,index); }else{ subList = this.list; } String s=""; for(Char c:subList){ s+=c.toString(); } return Integer.parseInt(s); } /** * Get fraction part of whole number * @return */ public double fraction(){ String fraction; List subList; if(containsDecimal()){ int index=0; for(Char c:this.list){ if('.' == c.toChar()){ break; } index++; } subList = this.list.subList(index+1, this.list.size()); fraction="0."; for(Char c:subList){ fraction+=c.toString(); } } else{ //If no fraction part, return default fraction = 0.0 fraction = "0.0"; } return Double.parseDouble(fraction); } public double toDouble(){ return this.wholeNumber()+this.fraction(); } public String toString(){ return String.valueOf(toDouble()); } public Char at(int index){ return this.list.get(index); } private boolean isNumber(Char ch){ char c = ch.toChar(); return (c>='0' && c<='9'); } /** * Private method to check if the number has a decimal point * @return */ private boolean containsDecimal() { boolean contains = false; for(Char c:this.list){ if('.' == c.toChar()){ contains = true; } } return contains; } /** * Private Method to validate if the string contains a valid number * @param s * @throws BigDecimalException */ private void validateString(String s) throws BigDecimalException{ //For numbers like 499. if(s.endsWith(".")){ throw new BigDecimalException("Format Exception: Number cannot end with decimal point"); } int decimalCount = 0; for(int i=0;i Char c = new Char(s.charAt(i)); if(!(isNumber(c) || '.' == c.toChar())) throw new BigDecimalException("Invalid Number. String contains a non-numeric character"); if('.' == c.toChar()){ decimalCount++; } } //if more than 1 decimal point if(decimalCount>1) throw new BigDecimalException("Invalid Number. String contains more than 1 decimal point"); } }

CharException.java

public class CharException extends Exception{ private static final long serialVersionUID = 1L; public CharException(String message){ super(message); } }

BigDecimalException.java

public class BigDecimalException extends CharException { private static final long serialVersionUID = 1L;

public BigDecimalException(String message) { super(message); } }

CharTest.java

/** * * @author Anonymous * */ public class CharTest { public static void main(String args[]){ Char ch = new Char('A'); Char c = new Char('B'); System.out.println(ch.add(c)); System.out.println(ch.toChar() + " In Hex: "+ch.toHexString()); System.out.println(ch.toChar() + " In Int: "+ch.toInt()); try{ ch.equals(140); System.out.println(ch.toChar()); }catch(CharException ce){ System.out.println(ce.getMessage()); } Char x = new Char(34); System.out.println(x.toString()); }

}

BigDecimalTest.java

/** * BigDecimal Test Class * Reads numbers from a file * Outputs 2 file - 1 file contains whole part and 2 file contains fraction part * @author Anonymous * */ public class BigDecimalTest { public static void main(String args[]) throws IOException{ List numberList = new ArrayList(); try { //TODO FileInputStream fis = new FileInputStream("<>"); Scanner sc = new Scanner(fis); while(sc.hasNextLine()){ try{ BigDecimal b = new BigDecimal(sc.nextLine()); numberList.add(b); }catch(BigDecimalException bde){ System.out.println(bde.getMessage()); } } List wholeList = new ArrayList(); List fractionList = new ArrayList(); for(BigDecimal bd:numberList){ wholeList.add(bd.wholeNumber()); fractionList.add(bd.fraction()); } //TODO File file = new File("<>/wholeNumber.txt"); File file1 = new File("<>/fraction.txt"); if(!file.exists()) file.createNewFile(); if(!file1.exists()) file1.createNewFile(); PrintWriter pw = null; //Writing Whole Numbers pw = new PrintWriter(file); for(Integer in:wholeList){ pw.write(String.valueOf(in)); pw.println(); } pw.flush(); pw.close(); //Writing Fraction Numbers pw = new PrintWriter(file1); for(Double db:fractionList){ pw.write(String.valueOf(db)); pw.println(); } pw.flush(); pw.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

}

So far I have...

#include #include #include using namespace std;

class Char { char crctr; public: Char() { this->crctr = '\0'; } Char(char c) { this->crctr = c; } Char(int c) { this->crctr = (char)c; } Char(const Char &c) { this->crctr = c.crctr; } Char(string s) { this->crctr = s[0]; } void equals(const Char &c) { this->crctr = c.crctr; } void equals(char c) { this->crctr = c; } void equals(int); char toChar() const { return this->crctr; } int toInt() const { return (int)this->crctr; } string toString() { return to_string(this->crctr); } string toHexString() { return };

void Char::equals(int c) { if (c<32 || c>127) { cout<<"Invalid Character"; } this->crctr = (char)c; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!