View the step-by-step solution to:

pentadeca class. i did little but i cant get the answer i want. can you check my algorithm, and tell me why it is not working. my algorithm:

pentadeca class. i did little but i cant get the answer i want. can you check my algorithm, and tell me why it is not working.

my algorithm:

package ppppp;

public class PentaDecca{

private String pvalue;

private int intValue;


public PentaDecca(){

 pvalue = "0";

 intValue = 0;

}

private boolean check(String val){

 for(int i = 0;i<val.length();i++){

  switch(val.charAt(i)){

  case '-':

   if(i != 0)return false;

  case '0':

  case '1':

  case '2':

  case '3':

  case '4':

  case '5':

  case '6':

  case '7':

  case '8':

  case '9':

  case 'A':

  case 'B':

  case 'C':

  case 'D':

  case 'E':break;

  default:return false;

  }

 }

 return true;

}

private int toIntVal(char val){

 switch(val){

  case '-':return -1;

  case '0':return 0;

  case '1':return 1;

  case '2':return 2;

  case '3':return 3;

  case '4':return 4;

  case '5':return 5;

  case '6':return 6;

  case '7':return 7;

  case '8':return 8;

  case '9':return 9;

  case 'A':return 10;

  case 'B':return 11;

  case 'C':return 12;

  case 'D':return 13;

  case 'E':return 14;

 }

 return -2;

}

private int pToInt(String val){

 int value = 0;

 int cval = 0;

 int st = 0;

 int ind = val.length()-1;

 for(int i = 0;i<ind;i++){

  cval = toIntVal(val.charAt(i));

  if(cval == -1){

  st = 1;

  continue;

  }

  value = value + (int)Math.pow(15,i)*cval;

 }

 if(st == 1){

  value = value * (-1);

 }

 return value;

}

public PentaDecca(String val){

 this();

 if(check(val)){

  intValue = pToInt(val);

  pvalue = val;

 }

}

private String iToPval(int val){

 String s = "";

 if(val < 0){

  s = "-"+s;

 }

 int pv=0;

 while(val != 0){

  pv = val%15;

  if(pv <= 9){

  s = pv + s;

  }else{

  switch(pv){

   case 10:s = "A"+s;break;

   case 11:s = "B"+s;break;

   case 12:s = "C"+s;break;

   case 13:s = "D"+s;break;

   case 14:s = "E"+s;break;

  }

  }

  val = val/15;

 }

 return s;

}

public PentaDecca(int val){

 intValue = val;

 pvalue = iToPval(val);

}


public void read(){

 System.out.println("PentaDecca value = "+pvalue);

}


public void set(String val){

 if(check(val)){

  intValue = pToInt(val);

  pvalue = val;

 }

}


public void set(int val){

 intValue = val;

 pvalue = iToPval(val);

}


public void set(PentaDecca p){

 intValue = p.toInt();

 pvalue = p.toString();

}

public PentaDecca get(){

 return this;

}

public int toInt(){

 return intValue;

}

public PentaDecca add(PentaDecca p){

 return new PentaDecca(intValue + p.toInt());

}

public PentaDecca subtract(PentaDecca p){

 return new PentaDecca(intValue - p.toInt());

}

public PentaDecca divide(PentaDecca p){

 return new PentaDecca(intValue / p.toInt());

}

public PentaDecca multiply(PentaDecca p){

 return new PentaDecca(intValue * p.toInt());

}

public PentaDecca add(int p){

 return new PentaDecca(intValue + p);

}

public PentaDecca subtract(int p){

 return new PentaDecca(intValue - p);

}

public PentaDecca divide(int p){

 return new PentaDecca(intValue / p);

}

public PentaDecca multiply(int p){

 return new PentaDecca(intValue * p);

}

public PentaDecca add(String p){

 return new PentaDecca(intValue + pToInt(p));

}

public PentaDecca subtract(String p){

 return new PentaDecca(intValue - pToInt(p));

}

public PentaDecca divide(String p){

 return new PentaDecca(intValue / pToInt(p));

}

public PentaDecca multiply(String p){

 return new PentaDecca(intValue * pToInt(p));

}

public boolean equals(PentaDecca p){

 if(intValue == p.toInt()){

  return true;

 }

 return false;

}

public String toString(){

 return pvalue;

}

}

//public static void main(String[] args) {

// PentaDecca p = new PentaDecca("-0123DE");

// System.out.println(p.toInt());

// System.out.println(p);

my demo

package ppppp;

public class PentadecaDemo

{

public static void main(String[] args)

{

PentaDecca Pentadeca1 = new PentaDecca ("123p");

PentaDecca Pentadeca2 = new PentaDecca ("126p");

PentaDecca Pentadeca4, Pentadeca5, Pentadeca6, Pentadeca7;

PentaDecca Pentadeca8 = new PentaDecca(1999);

System.out.println ("Pentadeca number Pentadeca8: " + Pentadeca8);

System.out.println ("First Pentadeca number: " + Pentadeca1);

System.out.println ("Second Pentadeca number: " + Pentadeca2);

if (Pentadeca1.equals(Pentadeca2))

System.out.println ("Pentadeca1 and Pentadeca2 are equal.");

else

System.out.println ("Pentadeca1 and Pentadeca2 are NOT equal.");

Pentadeca4 = Pentadeca1.add(Pentadeca2);

Pentadeca5 = Pentadeca1.subtract(Pentadeca2);

Pentadeca6 = Pentadeca1.multiply(Pentadeca2);

Pentadeca7 = Pentadeca1.divide(Pentadeca2);

System.out.println (Pentadeca1 + " + " + Pentadeca2 + " is: " +

Pentadeca4);

System.out.println (Pentadeca1 + " - " + Pentadeca2 + " is: " +

Pentadeca5);

System.out.println (Pentadeca1 + " * " + Pentadeca2 + " is: " +

Pentadeca6);

System.out.println (Pentadeca1 + " / " + Pentadeca2 + " is: " +

Pentadeca7);

System.out.println ();

int number = 6;

System.out.println ("using the integer " + number + " as the argument "

+

"to the math operators ");

Pentadeca4 = Pentadeca1.add(number);

Pentadeca5 = Pentadeca1.subtract(number);

Pentadeca6 = Pentadeca1.multiply(number);

Pentadeca7 = Pentadeca1.divide(number);

System.out.println (Pentadeca1 + " + " + number + " is: " + Pentadeca4);

System.out.println (Pentadeca1 + " - " + number + " is: " + Pentadeca5);

System.out.println (Pentadeca1 + " * " + number + " is: " + Pentadeca6);

System.out.println (Pentadeca1 + " / " + number + " is: " + Pentadeca7);

String value = "6p";

System.out.println ("using the String " + """ + value + """ + " as the argument " + "to the math operators ");

Pentadeca4 = Pentadeca1.add(value);

Pentadeca5 = Pentadeca1.subtract(value);

Pentadeca6 = Pentadeca1.multiply(value);

Pentadeca7 = Pentadeca1.divide(value);

System.out.println (Pentadeca1 + " + " + value + " is: " + Pentadeca4);

System.out.println (Pentadeca1 + " - " + value + " is: " + Pentadeca5);

System.out.println (Pentadeca1 + " * " + value + " is: " + Pentadeca6);

System.out.println (Pentadeca1 + " / " + value + " is: " + Pentadeca7);

}

}

expected answer:

//Pentadeca number Pentadeca8: 8D4p

//First Pentadeca number: 123p

//Second Pentadeca number: 126p

//Pentadeca1 and Pentadeca2 are NOT equal.

//123p + 126p is: 249p

//123p - 126p is: -3p

//123p * 126p is: 14E43p

//123p / 126p is: 0

//using the integer 6 as the argument to the math operators

//123p + 6 is: 129p

//123p - 6 is: 11Cp

//123p * 6 is: 6D3p

//123p / 6 is: 2Dp

//using the String "6p" as the argument to the math operators

//123p + 6p is: 129p

//123p - 6p is: 11Cp

//123p * 6p is: 6D3p

//123p / 6p is: 2Dp

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question