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 7 years ago.
Improve this question
How do I make a util class with a method name that is add, and returns the result by adding two numbers.This is in java but in the program eclipse.
This is what I've got:
public class Util {
public static int add(int firstNumber, int secondNumber) {
int first = 2;
int second =3;
int second - Util.add(int, int)
}
}
As a Java novice myself, this is the first Stack Overflow question I've actually been capable of answering. I agree with other commenters that this is probably better answered by reviewing a basic Java textbook of some kind, but here you go:
public class Util {
public static int add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
}
Related
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 3 months ago.
Improve this question
I don't know why my defined function is not working smoothly.
in this I am trying to build a function to convert a binary number to a decimal number
public class binarrytodec {
public static void bintodec(int n){
int p=0;
int dec=0;
while(n>0){
int ld=n%10;
dec = dec + (ld*(int)Math.pow(2, p));
p++;
n=n%10;
}
System.out.print(dec);
}
public static void main(String args[]) {
bintodec(1110001);
}
}
% is not division, it's a remainder operation - it shows how much is left after integer division. 1 % 10 == 1. Thus your n is never becoming 0, and your code enters an infinite loop.
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 1 year ago.
Improve this question
I need help converting this while loop method to recursive method
public static int diaseneliminar(double cantidamedicina, double porcentajeaeliminar){
double basedivisorporcentaje = 100;
double cantidadmedicinarestante = cantidamedicina*(1-(porcentajeaeliminar/basedivisorporcentaje));
int contadordia=0;
while(cantidamedicina>cantidadmedicinarestante){
cantidamedicina = cantidamedicina - (cantidamedicina * 0.2);
contadordia++;
System.out.println("cantidamedicina:"+cantidamedicina+"cantidadmedicinarestante:"+cantidadmedicinarestante+"contadordia:"+contadordia);
}
return contadordia;
}
Thanks in advance
Just split it up in initialisation and the recursion, making the negation of the while-critearia to the return-criteria:
public static int diaseneliminarRecursive(double cantidamedicina, double porcentajeaeliminar){
double basedivisorporcentaje = 100;
double cantidadmedicinarestante = cantidamedicina*(1-(porcentajeaeliminar/basedivisorporcentaje));
return recursion(0, cantidamedicina, cantidadmedicinarestante);
}
private static int recursion(int contadordia, double cantidamedicina, double cantidadmedicinarestante) {
System.out.println("cantidamedicina:"+cantidamedicina+"cantidadmedicinarestante:"+cantidadmedicinarestante+"contadordia:"+contadordia);
if(cantidamedicina<=cantidadmedicinarestante){
return contadordia;
}
return recursion(++contadordia, cantidamedicina - (cantidamedicina * 0.2), cantidadmedicinarestante) ;
}
TEST
System.out.println(diaseneliminar(100, 80));
System.out.println(diaseneliminarRecursive(100, 80));
Output original:
cantidamedicina:80.0cantidadmedicinarestante:19.999999999999996contadordia:1
cantidamedicina:64.0cantidadmedicinarestante:19.999999999999996contadordia:2
cantidamedicina:51.2cantidadmedicinarestante:19.999999999999996contadordia:3
cantidamedicina:40.96cantidadmedicinarestante:19.999999999999996contadordia:4
cantidamedicina:32.768cantidadmedicinarestante:19.999999999999996contadordia:5
cantidamedicina:26.2144cantidadmedicinarestante:19.999999999999996contadordia:6
cantidamedicina:20.97152cantidadmedicinarestante:19.999999999999996contadordia:7
cantidamedicina:16.777216000000003cantidadmedicinarestante:19.999999999999996contadordia:8
8
Output recursive
cantidamedicina:100.0cantidadmedicinarestante:19.999999999999996contadordia:0
cantidamedicina:80.0cantidadmedicinarestante:19.999999999999996contadordia:1
cantidamedicina:64.0cantidadmedicinarestante:19.999999999999996contadordia:2
cantidamedicina:51.2cantidadmedicinarestante:19.999999999999996contadordia:3
cantidamedicina:40.96cantidadmedicinarestante:19.999999999999996contadordia:4
cantidamedicina:32.768cantidadmedicinarestante:19.999999999999996contadordia:5
cantidamedicina:26.2144cantidadmedicinarestante:19.999999999999996contadordia:6
cantidamedicina:20.97152cantidadmedicinarestante:19.999999999999996contadordia:7
cantidamedicina:16.777216000000003cantidadmedicinarestante:19.999999999999996contadordia:8
8
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 2 years ago.
Improve this question
static final int WIN = 3;
static final int TIE = 1;
static final int LOSS = 0;
public static int berekenWedstrijdPunten(int[] mpUserTeamPunten, int[] mpTegenstanderTeamPunten){
if (mpUserTeamPunten > mpTegenstanderTeamPunten){
return WIN;
}
if (mpUserTeamPunten == mpTegenstanderTeamPunten){
return TIE;
}
if (mpUserTeamPunten < mpTegenstanderTeamPunten) {
return LOSS;
}
}
getting error lines under the if statements with > & <, trying to get a return of the win/tie/loss point into a print statement.
You can't compare two arrays with logical operators.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
class A
{
public static int Factorial(int n)
{
return (n*Factorial(n-1));
system.out.println("The Factorial of "+n+" is "+(n*Factorial(n-1)));
}
}
I want to create a .class file that contains all my functions.I can't compile it. what should i import?
You will also never see the print because the return comes before that. It's also recursive which will make this a loop. And please use lower case for method names.
Try this
public class A {
public static int factorial(int n) {
if (n == 0) { // stop recursion
return 1;
}
return n * factorial(n - 1);
/**
* The code here will be ignored
*/
}
public static void main(String[] args) {
int n = 5;
System.out.println(n + "! = " + factorial(n));
}
}
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 5 years ago.
Improve this question
Why is this giving me 10 as output, rather than an error?
public class A {
static int a = m1();
static int m1(){
return 10;
}
public static void main(String args[]) {
A a1 = null;
System.out.println(a1.a);
}
}
Because compiler is so intelligent here,it basically replaces
System.out.println(a1.a);
with
System.out.println(A.a); //The name of your class 'A'
Because a is a static variable, so the reference to A a1 isn't dereferenced. You might want to write A.a instead to make the code more intuitive.