I cannot convert c++ to java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I convert this C++ code to Java?
I could not convert parameter &poly_size in method clip.
This code from https://www.geeksforgeeks.org/polygon-clipping-sutherland-hodgman-algorithm-please-change-bmp-images-jpeg-png/
void suthHodgClip(int poly_points[][2], int poly_size, int clipper_points[][2], int clipper_size)
{
for (int i=0; i<clipper_size; i++)
{
int k = (i+1) % clipper_size;
clip(poly_points, poly_size, clipper_points[i][0], clipper_points[i][1], clipper_points[k][0], clipper_points[k][1]);
}
}
void clip(int poly_points[][2], int &poly_size, int x1, int y1, int x2, int y2)
{
int new_points[MAX_POINTS][2], new_poly_size = 0;
// blabla
poly_size = new_poly_size;
for (int i = 0; i < poly_size; i++)
{
poly_points[i][0] = new_points[i][0];
poly_points[i][1] = new_points[i][1];
}
}

First remember that Java isn't C++, so odds are a direct translation is going to be sub-optimal.
With that out of the way, The question is How do you pass an integer by reference in Java? The only reason in C++ you would pass poly_size by reference is if you wanted to change its value inside the function, and that happens at poly_size = new_poly_size;
My Java is tragically rusty, but I'd take advantage of the C++ version of clip not returning anything and make the Java version return new_poly_size;

Java doesn't give you choice over pass-by-value and pass-by-reference. Instead it passes primitives, such as int by value, and objects by reference.
Unfortunately the Java non-primitive Integer (which would be passed by reference) is immutable, so the only ways of doing this would be to either return new_poly_size, or make your own modifiable object containing this value, pass it to the function.

Related

Java's logic concerning methods that return values and their parameters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Java methods don't require return statements to match their parameters. Why?
Consider this trivial method that takes three parameters, x, y and z. The method works when only one return parameter is used (i.e. return x, y, or z). Why wouldn't Java require return x, y, z; or something like that? I'm a Java beginner so maybe I'll learn why when we get to OOP principles. Anyway, I'm curious if there is a logic behind it or that's just Java. Thanks.
public static int someNumbers(int x, int y, int z) {
if (y > z) {
System.out.printf("%d is greater than %d", y, z);
} else {
System.out.printf("Number three is %d", z);
}
return x;
}
Parameters and Return types don't really have any relation to each other. A method may have 0 or more parameters of any arbitrary type.
public int someMethod(int x, String y, Object z) {
//Does something
return x * 5 - 10;
}
But it may only have 0 or 1 return values. Where 0 return values must be defined as void:
public void someMethod(int x, String y, Object z) {
//Does something but returns nothing
}
When needing to return more than 1 value, such as 2 Integers, you could create an Object holding these values. This is kinda what defines Java as an Object Oriented Language.
class IntegerHolder{
int firstValue;
int secondValue;
//This is a constructor, it creates the object.
public IntegerHolder(int first, int second){
firstValue = first;
secondValue = second;
}
}
public IntegerHolder someMethod(int x, String y, Object z) {
return new IntegerHolder(x, x * 2);
}
I do advise to read up on some basic Java however. There are hundreds of resources around the web.
A well defined function can have only one output. It is a such a relationship between input and output that you can give multiple input but it will give only one output.
Java function definition just follow this rule.You can use some other features of java in order to get more than one result at a time. But should return on output as per the definition of function.You can also read this:
https://math.stackexchange.com/questions/271613/why-is-a-function-defined-as-having-only-one-y-value-output

Using functions in Java Programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have doubt in using functions in java. I have wrote code for sum of natural numbers using a recursive function but I don't understand the error I am getting. I know it's a silly question though I'm a beginner and I need a brief explanation.
Here is the code
import java.util.Scanner;
public class natural {
public static int main(String args[]){
int a, s = 0,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int x = in.nextInt();
public static int SN(y)
{
if(x==1)
{
return 1;
}
else{
int N = SN(x-1) + x;
return N;
System.out.println("THE SUM IS :"+x);
}
}
Several problems:
You cannot declare a method within a method. Your SN method must be declared outside of the main method.
The parameter y in your SN method must have a type. Based on usage, it is probably supposed to be an int, so the method signature should look like SN(int y).
Despite the method parameter being called y, you appear to be using x everywhere. You should change x to y in the SN method, since that is the label of the data being passed to the method.
As others have pointed out, statements after the return line are unreachable, and as Matt Coubrough said, your IDE is likely warning you about this. Place it before the return line.
Well, one problem here is that you have an unreachable statement. Your System.out.println("THE SUM IS...") is never reached.

Java, Methods? Or constructors ? I am not sure what to call it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am not sure if you see what I am trying to do here but basically I have a few questions and problems
1)The part that is called public MethodPractice() ... what is this called? is this considered a constructor a method or what?
2)The part named MethodPracticeDiff() . . . is this allowed and if so how do I insert it into the main method for execution...
Do you guys see what I am trying to do here? Basically I want to split the program up into different pieces for example let say I wanted my own space for a calculation method to add to numbers up
and another method to define the numbers like give them a value
and a last method with a for loop making the numbers printout 10times
Any who before I make this more confusing than what it is, my question is how do I make this program execute
public class MethodPractice {
public static void main (String[]args){
MethodPractice add = new MethodPractice();
//MethodPracticeDiff add2 = new MethodPracticeDiff();
}
public MethodPractice() {
int x = 0;
int y = 99 ;
int total = x + y;
System.out.println(total);
}
public void MethodPracticeDiff(){
int z = 10;
int k = 25;
int total = z + k;
System.out.println(total);
}
}
(1) If it's in class MethodPractice, it's a constructor.
(2) Yes, this is allowed. But it's a method not a constructor. Standard practice is to begin it with a lowercase letter.
As follows in the main() method:
MethodPractice add = new MethodPractice();
add.methodPracticeDiff();
MethodPractice() is a constructor -- it has no return value and matches the name of the class.
MethodPracticeDiff() is a method -- it has a return value and does not match the name of a class.
You call methods once you have an instance of the class. e.g.
MethodPractice add = new MethodPractice();
add.MethodPracticeDiff();

Why in java there is no reset option for variables [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Sometime when we declare and initialize a variable, say we have an int i =10; then after some code this variable would be modified like this code bellow
public class reset {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
int co = 1;
while (co < 10) {
i++;
System.out.println(i + "*" + co + "=" + i * co);
if (i == 99) {
i = 11; //line 11
co++;
}
}
}
}
then at some point (here at line 11) we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead for example
reset:i
I think it's very beneficial for productivity, isn't it?
we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead?
No
I think it's very beneficial for productivity, isn't it?
No
Resetting a variable to its start value is in many cases a sign that the scope of the variable is to large. So with clean code you hardly ever need such feature.
And of course every feature comes at the cost of complicating the language even more.
The initializer line
int i = 10;
simply creates byte code instructions to assign the value 10 to the variable. That assignment is no different than any other assignment.
To implement reset, there would need to be an extra bit of metadata kept for each variable to say what the special, initial value is.. That metadata is not currently kept in the symbol table, since there is no concept in Java for a 'initial value'. The additional overhead would trade off against the utility of the reset command.
Might be a good idea, but you can get the same thing by just declaring a constant, and reassigning to the constant.
What about this code? I can understand your question from a starter's perspective, but usually it requires a bit more practice to see why certain constructs are not required:
public class NoReset {
private static final int X_START = 11;
private static final int X_END = 99;
private static final int Y_START = 1;
private static final int Y_END = 9;
public static void main(String[] args) {
for (int y = Y_START; y <= Y_END; y++) {
for (int x = X_START; x <= X_END; x++) {
final int result = x * y;
System.out.printf("%d * %d = %d%n", x, y, result);
}
}
}
}
Note that you should not nest to many loops, but creating a "hidden loop" is at least as dangerous, it gets very hard to track variables such as i within your code.
If you want to use the current Java language to do what you want to do, then simply provide a function in each class that you want to perform a "reset" like:
private void reset() {
var = xxx;
var2 = yyy;
...
}

int A does not equal int B [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have two integers that I want to compare. The first integer is created from a byte of utf-8 and the second integer is the one I want to check to see if it equals.
int a = 106;
int b = (int)byteArray[0]; //which actually equals 106 when you printstatement it
but....
(a==b) //always equals false
int i = 0; While(b != i) { println(i); i++;} //eventually escapes the loop and is true
Are primitives also referenced when created? And why does a never equal b unless I count all the way up to 106?
Is there a better way to compare bytes? because I've tried all forms of variables and they do not work either.
The problem is somewhere else in your code (in the part that you are not showing). This is why you are being suggested to provide an SSCCE.
The following works as expected (i.e. prints true):
public class Test
{
public static void main(String[] args) throws Exception
{
byte[] byteArray = new byte[] { 106 };
int a = 106;
int b = (int) byteArray[0];
if (a == b)
System.out.println("true");
}
}
Most probably, in your code byteArray[0] does NOT contain 106. An SSCCE would show this.

Categories