Can someone help me please? I need help writing this code using Java language. S
ID: 3783519 • Letter: C
Question
Can someone help me please? I need help writing this code using Java language.
Step 1 The Char Type Create a complex type called Char that models the primitive type char. In your class you are going to want to have the following constructors: Java Description Charo Char0 Default constructor that sets the data section of the class to null (binary 0) overloaded constructor that takes a primitive char as an argument. It should set the data Char (char c) Char(char c) section of the class to the argument. overloaded constructor that takes a primitive int as a parameter. The data section of the Char(int co Char(int c) class should be set as a character from the argument. Chan(const Char(inal Overloaded constructor that takes the complex Char type as a parameter, The data section Char &c;) Char c) of the class should be set with the data section of the argument Char(string Char (String overloaded Constructor that takes a string type as a parameter. The data section of the class should be set to the first character in the string. Your class should have the following mutatorsExplanation / Answer
Contains:
1. Custom classes - Char.java and BigDecimal.java
2. Exception classes - CharException.java and BigDecimalException.java
3. Test Classes - CharTest.java and BigDecimalTest.java
Char.java
**
* Custom Char class
* @author Anonymous
*
*/
public class Char {
private char value;
public Char(){
this.value = '';
}
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<Char> list;
/**
* Overloaded Constructors
*/
public BigDecimal(){
list = new ArrayList<Char>();
list.add(new Char('0'));
list.add(new Char('.'));
list.add(new Char('0'));
}
public BigDecimal(String value) throws BigDecimalException{
list = new ArrayList<Char>();
//For numbers like .99
if(value.startsWith(".")){
list.add(new Char('0'));
}
validateString(value);
for(int i=0;i<value.length();i++){
list.add(new Char(value.charAt(i)));
}
}
public BigDecimal(final BigDecimal value){
list = new ArrayList<Char>();
List<Char> valList = value.getValue();
for(int i=0;i<valList.size();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<Char> 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<Char> 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<Char> 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<s.length();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<BigDecimal> numberList = new ArrayList<BigDecimal>();
try {
//TODO
FileInputStream fis = new FileInputStream("<<Your Text File Path Goes Here>>");
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<Integer> wholeList = new ArrayList<Integer>();
List<Double> fractionList = new ArrayList<Double>();
for(BigDecimal bd:numberList){
wholeList.add(bd.wholeNumber());
fractionList.add(bd.fraction());
}
//TODO
File file = new File("<<Output path for whole numbers>>/wholeNumber.txt");
File file1 = new File("<<Output path for whole numbers>>/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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.