I need help with all of the code for this assignment. Com S 227 Spring 2017 Mini
ID: 3815619 • Letter: I
Question
I need help with all of the code for this assignment.
Com S 227 Spring 2017 Miniassignment 1 40 points (estimated) Due Date: Tuesday, March 7, 11:59 pm (midnight) "Late" deadline (25% penalty): Wednesday, March 8, 11:59 pm General information This assignment is to be done on your own. See the Academic Dishonesty policy in the syllabus, http://www.cs.iastate.edu/2cs227/syllabus.html ad, for details. You will not be able to submit your work unless you have completed the Academic Dishonesty policy acknowledgement on the Homework page on Blackboard. Please do this right away. If you need help, see your instructor or one of the TAs. Lots of help is also available through the Piazza discussions Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit. Overview The purpose of this miniassignment is to give you lots of practice writing loops and working with strings. You will implement one class, called TallyNumber A tally number is a representation of a nonnegative integer that is written usingjust three symbols. In this implementation, a zero is represented by the digit '0' by itself, the character LINE (I') stands for the value 1, and the character STAR has a value of 5. The symbols can be combined to form a talby group whose value is the sum of the individual symbols; for example, "I I I is a tally group with value 19.Explanation / Answer
package mini1;
public class TallyNumber {
private String strValue;
private int intValue;
public TallyNumber(String strValue) {
this.strValue = strValue;
calulateIntValue();
}
public int getIntValue(){
return this.intValue;
}
public void setIntValue(int intValue){
this.intValue = intValue;
}
public void setStrValue(String strValue){
this.strValue = strValue;
}
public String getStrValue(){
return this.strValue;
}
private void calulateIntValue(){
int totValue = 0;
//Dividing into groups
String[] splitStrValue = strValue.split(" ");
for(int i=0;i<splitStrValue.length;i++){
//System.out.println("tot value:"+totValue+" group:"+splitStrValue[i]);
totValue = totValue*10+evaluateGroup(splitStrValue[i]);
}
this.intValue = totValue;
}
//Calculating value of individual group
private int evaluateGroup(String strGroup){
int value = 0;
for(int i=0;i<strGroup.length();i++){
if(strGroup.charAt(i) == '|'){
value += 1;
}
else if(strGroup.charAt(i) == '*'){
value +=5;
}
else if(strGroup.charAt(i)=='0'){
value +=0;
}
}
return value;
}
//normalizing the value
public void normalize(){
int value = intValue;
String newStrValue = "";
while(value>0){
//calculating value of invidual group
newStrValue = makeGroup(value%10)+" "+newStrValue;
value = value/10;
newStrValue.trim();
}
this.strValue = newStrValue;
}
private String makeGroup(int value){
String strValue = "";
if(value == 0){
strValue = value+"0";
}
if(value>=5){
strValue += "*";
value = value-5;
}
while(value>0){
value = value-1;
strValue = strValue+"|";
}
return strValue;
}
public void combine(TallyNumber tallyNumber){
//Adding two values
this.setIntValue(this.getIntValue()+tallyNumber.getIntValue());
this.normalize();
}
public static void main(String[] args) {
TallyNumber tn = new TallyNumber("||**|*|");
System.out.println(tn.getStrValue());
System.out.println(tn.getIntValue());
tn.normalize();
System.out.println(tn.getStrValue());
TallyNumber t1 = new TallyNumber("*** ||");
TallyNumber t2 = new TallyNumber("|||| *");
/*System.out.println(t1.getIntValue());
System.out.println(t1.getStrValue());
System.out.println(t2.getIntValue());
System.out.println(t2.getStrValue());*/
t1.combine(t2);
System.out.println(t1.getIntValue());
System.out.println(t1.getStrValue());
TallyNumber t3 = new TallyNumber("| *");
TallyNumber t4 = new TallyNumber("| ** 0 ||");
t3.combine(t4);
System.out.println(t3.getIntValue());
System.out.println(t3.getStrValue());
}
}
Output:
||**|*|
19
| *||||
197
| *|||| *||
2017
|| 00 | *||
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.