Comments (4)
Why does this not work? What does this LargeNumber.java class contain?
from javaparser.
this is my LargeNumber.java class:
public class LargeNumber {
private String number;
public LargeNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public String add(String operand) {
StringBuilder result = new StringBuilder();
int carry = 0;
int i = number.length() - 1;
int j = operand.length() - 1;
while (i >= 0 || j >= 0 || carry != 0) {
int digit1 = (i >= 0) ? Character.getNumericValue(number.charAt(i)) : 0;
int digit2 = (j >= 0) ? Character.getNumericValue(operand.charAt(j)) : 0;
int sum = digit1 + digit2 + carry;
int newDigit = sum % 10;
carry = sum / 10;
result.insert(0, newDigit);
i--;
j--;
}
return result.toString();
}
public String subtract(String operand) {
StringBuilder result = new StringBuilder();
int borrow = 0;
int i = number.length() - 1;
int j = operand.length() - 1;
while (i >= 0 || j >= 0) {
int digit1 = (i >= 0) ? Character.getNumericValue(number.charAt(i)) : 0;
int digit2 = (j >= 0) ? Character.getNumericValue(operand.charAt(j)) : 0;
int diff = digit1 - digit2 - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result.insert(0, diff);
i--;
j--;
}
number="12313";
return result.toString();
}
public String multiply(String operand) {
int n = number.length();
int m = operand.length();
int[] result = new int[n + m];
for (int i = n - 1; i >= 0; i--) {
int digit1 = Character.getNumericValue(number.charAt(i));
int carry = 0;
for (int j = m - 1; j >= 0; j--) {
int digit2 = Character.getNumericValue(operand.charAt(j));
int product = digit1 * digit2 + carry + result[i + j + 1];
int newDigit = product % 10;
carry = product / 10;
result[i + j + 1] = newDigit;
}
result[i] = carry;
}
StringBuilder resultString = new StringBuilder();
for (int digit : result) {
resultString.append(digit);
}
// حذف ارقام صفر اضافی در ابتدا
int i = 0;
while (i < resultString.length() - 1 && resultString.charAt(i) == '0') {
i++;
}
resultString.delete(0, i);
number++;
return resultString.toString();
}
public String divide(String operand) {
if (operand.equals("0")) {
throw new ArithmeticException("تقسیم بر صفر ممکن نیست");
}
LargeNumber dividend = new LargeNumber(number);
LargeNumber divisor = new LargeNumber(operand);
LargeNumber quotient = new LargeNumber("0");
while (dividend.isGreaterThanOrEqual(divisor)) {
dividend = dividend.subtract(divisor.getNumber());
quotient = quotient.add("1");
}
return quotient.getNumber();
}
}
and this is output:
Class: LargeNumber
Method: getNumber
Method: add
Method: subtract
Method: multiply
Method: divide
I use JavaParser 3.25.8,
another example, for Example.java:
public class Example {
private int field1;
private String field2;
public void method1() {
field1 = 10;
System.out.println(field2);
}
public void method2() {
field2 = "Hello";
}
}
output should be :
Class: Example
Method: method1
Field Access: field1
Field Access: field2
Method: method2
Field Access: field2
but the output is:
Class: Example
Method: method1
Field Access: out
Method: method2
from javaparser.
The result is correct. The expression below is an assignment (AssignExpr).
field2 = "Hello";
from javaparser.
If we've answered your question, could you close this issue? Thank you.
from javaparser.
Related Issues (20)
- Strange error when trying to find erasure of generic type where one of two type parameters is an array HOT 2
- Unable to check equality of `ResolvedReferenceType` with a `LazyType`
- Regression from #602 HOT 6
- StackOverflowError with import static in circular dependencies HOT 1
- Question about switch statements and pattern representation HOT 4
- Children may be out of order after adding a statement HOT 4
- Jar file location for JarTypeSolver HOT 5
- Disappearing comments with LexicalPreservingPrinter HOT 1
- 【Question】Can a API be provided with a very concise judgment of whether the current variable is a local variable or a ginseng or field? HOT 2
- 【Question】Should the results of `Expression#RESOLVE` cache? HOT 1
- Record Patterns Implementation Proposal HOT 12
- Different nodes have same hashcode HOT 3
- Meaning of LanguageLevel HOT 3
- Fail to use symbol resolver HOT 8
- Arbitrary new line when refactoring HOT 4
- MethodCallExpr inside lambda in assignment expression cannot be resolved HOT 6
- JavaParser finds "." in place of first element after a yeild statement. HOT 12
- Missing tokens in parent nodes HOT 4
- Spotless formatting implementation proposal HOT 13
- Tests systematically failing on Windows HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javaparser.