This is the task:
Implement a static-public method named "createDouble" in the class "Functionality.java". The method gets two integer values a and b as input and should transform them to a double value and return it as follows:
The first input value a, should be placed before the comma or dot.
The second input value b, should be after the comma or dot and superfluous zeros should be removed.
No imports may be used to solve this task. Also the use of the Math library or other libraries is prohibited. Implement an algorithm that contains at least one meaningful loop.
This was my idea:
public class Functionality {
public static double createDouble(int a, int b) {
double c = b;
double d = 1;
while (c >= 1) {
c /= 10;
d *= 10;
}
return a + b/d;
}
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(createDouble(12, Integer.MAX_VALUE));
}
}
The problem is I am using the Method Integer.MAX Value which I shouldn´t use. Is there another option to write this code ?
Your code looks sound, just a small tweek I would make. Your variable c will equal what you want b to be after it's done dividing so you can just use it directly. Otherwise, you don't really need to be using Integer.MAX_VALUE at all. Just use arbitrary values.
public class Functionality
{
public static double createDouble(int a, int b) {
double c = b;
while(c >= 1)
c /= 10;
return a + c;
}
public static void main(String[] args) {
System.out.println(createDouble(15, 351));
System.out.println(createDouble(32, 8452));
}
}
Output:
15.351
32.8452
Here my Implementation
public class Functionality {
private static double logb10(double num){
return (num > 1) ? 1 + logb10(num / 10) : 0;
}
public static double createOtherDouble(int a, int b) {
double c = a;
int len =(int) logb10(b);
double d = b;
for(int i = 0; i < len; i++){
d /= 10;
}
return c + d;
}
public static void main(String []args){
System.out.println(Integer.MAX_VALUE);
System.out.println(createOtherDouble(12, Integer.MAX_VALUE));
}
}
Related
I attempted to create a method which calculates the sum of the values of another method in the same way the capital sigma notation does in math. I wanted it to use successive natural numbers as input variables for the function and use recursion to sum it all up. However, as I wanted to create a method for general summation, I am not sure how to assign another (single variable) method as an input variable.
I thought of something like this:
public static int sum(int lowerbound, int upperbound, function(int x)){
int partialsum = 0;
for (int i = lowerbound; i <= upperbound; i++){
partialsum = partialsum + function(i);
}
return partialsum;
}
Is it possible?
Yes, it is possible; but you would need to pass a IntFunction (Java is not JavaScript). Like,
public static int sum(int lowerbound, int upperbound, IntFunction<Integer> function) {
int partialsum = 0;
for (int i = lowerbound; i <= upperbound; i++) {
partialsum = partialsum + function.apply(i);
}
return partialsum;
}
And then to use it, something like
public static void main(String[] args) {
System.out.println(sum(1, 10, a -> a));
}
Which outputs
55
It isn't clear what result you would expect (or what function you intended to pass).
You pass in a IntUnaryOperator
public static int sum(int lowerbound, int upperbound, IntUnaryOperator func) {
...
partialsum += func.applyAsInt(i);
Then, either pass a method in, or use a lambda:
sum(1, 2, n -> n * 2)
Why is the C equal to 0.
What should I do when I want calculate with the modified attributes.
Why the C calculate with the default set up variables and not with the modified ones.
public class Object{
int A;
int B;
int C=A+B;
int AddOnetoA(){
A=A+1;
return A;
}
int AddOnetoB(){
B=B+1;
return B;
}
void ShowA() {
System.out.println(A);
}
void ShowB() {
System.out.println(B);
}
void ShowC() {
System.out.println(C);
}
}
And:
public static void main(String[] args) {
Object obj =new Object();
obj.AddOnetoA();
obj.AddOnetoA();
obj.AddOnetoA();
obj.AddOnetoB();
obj.AddOnetoB();
obj.AddOnetoB();
obj.ShowA();
obj.ShowB();
obj.ShowC();
}
output:
3
3
0
C is computed only once. During the time of definition. The C=A+B gets executed during class loading time. You are updating A & B afterwards. So C stays as 0
In other words, the effect of your updates to a variable used in an expression are not retro-actively applied to those expressions.
The default values for integers which are not set is 0.
You do the following:
int A; // sets A to 0
int B; // sets B to 0
int C=A+B; // sets C to A + B = 0 + 0 = 0
The notation C=A+B doesn't mean that the value C will be updated with each update of A or B but defines only its initial value.
You have to update its value with each increment of A or B variables by yourself:
int AddOnetoA(){
A=A+1;
c = A + B; // here
return A;
}
int AddOnetoB(){
B=B+1;
c = A + B; // and here
return B;
}
Don't create an object with the name Object which is the one that each one inherits from. Use for example MyObject for this simple case.
Lastly, I highly recommend you follow the Java conventions and start the name of variables and methods with a lower-case letter, such as:
int addOnetoB(){
b = b + 1; // equal to b++;
c = a + b; // and here
return b;
}
If you want C to reflect always the sum of A+B, you can remove int C; and alter showC:
void ShowC() {
System.out.println(A+B);
}
This is a valid solutions if the effort to calculate C is low.
I want to get some values from one function inside of different class and use it on Main class. But it seems like I am doing somethings wrong.
public class test {
public static int enkucukbul ( double[] x){ // this method finds the smallest index
return IntStream.range(0, x.length)
.mapToObj(i -> i)
.min(Comparator.comparing(i -> x[i]))
.orElse(Integer.MIN_VALUE);
}
public static double tabu(double x, int isayi) {
Random rrandom = new Random();
float r;
double[] fxdizi = new double[4];
double[] xdizi = new double[4];
double[] hareket = new double[4];
for (int j = 0; j < isayi; j++) {
r = rrandom.nextFloat();
hareket[0] = x + 2 * r;
hareket[1] = x + 4 * r;
hareket[2] = x - 2 * r;
hareket[3] = x - 4 * r;
xdizi[0] = hareket[0];
xdizi[1] = hareket[1];
xdizi[2] = hareket[2];
xdizi[3] = hareket[3];
for (int i = 0; i < 4; i++) {
if (xdizi[i] <= 1) {
fxdizi[i] = xdizi[i] * xdizi[i];
} else {
fxdizi[i] = Math.pow((xdizi[i] - 3), 2) - 3;
}
} // for dongusu
int minIndex = enkucukbul(fxdizi); // found the smallest index
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
x = xdizi[minIndex]; // we found the smallest x
} // all things
}
}
Also my Main class:
public class Main {
public static void main(String[] args) {
test ts = new test();
System.out.println(ts.tabu(7.26,2));
}
}
I just want to get the values in return statements like x, j ...etc. But I get error "java:unreachable statement" on every return statement and also "java:missing return statement" in the end. Where do i do wrong?
Extra Note: I'm sorry for the localized variable names since this is my optimization class assignment
You've wrote multiple return statements in following lines :
return x;
return minIndex;
return j;
return xdizi[minIndex];
return fxdizi[minIndex];
It is not acceptable in java.
Also you are missing return value at the end of method tabuoutside of for loop. I see several logical mistakes in your code.
You need to refactor your code, also if you can explain what you are trying to achieve it will be much more clear.
In java a method can have only one return statement. You can't have more than one return statement per method unless used in an if-else block.
As the compiler tells you the remaining statements are unreachable after the first return statement.
As per your latest comment, you can create a wrapper class to hold multiple values that you want to return. For eg :
class Calculation {
public double a;
public double b;
// getters and setters
}
Now from your method you could capture the values of the variables and then store them in an object of the class created above :
public static Calculation tabu(double x, int y) {
Calculation cal = new Calculation();
// do something with x and y and other things
cal.setA(x);
cal.setB(y);
return cal;
}
I have an argument with my friend because I don't think fib_2() is recursion, but he says it is because it calls itself.
I don't think it is because one fib_2() doesn't have a return result for use as an argument for another fib_2().
I think fib_2() is the same with fib_3(),it's a iteration,not a recursion.
So is it a recursion or not ?
public class Demo {
public static void main(String[] args) {
System.out.printf("fib_1 -> %d\n", fib_1(10));
System.out.printf("fib_2 -> %d\n", fff(10));
System.out.printf("fib_3 -> %d\n", fib_3(10));
}
//This is recursion
public static int fib_1(int n) {
if (n == 1 || n == 2)
return 1;
return fib_1(n - 1) + fib_1(n - 2);
}
//Is this recursion or not ?
public static int fff(int n) {
int a = 1, b = 1, c = 0, count = 2;
return fib_2(a, b, n, c, count);
}
public static int fib_2(int a, int b, int n, int c, int count) {
if (count == n) {
return c;
}
int tmp = b;
b = a + b;
a = tmp;
c = b;
++count;
return fib_2(a, b, n, c, count);
}
public static int fib_3(int n) {
int a = 1, b = 1;
for (int i = 2; i < n; i++) {
int temp = b;
b = a + b;
a = temp;
}
return b;
}
}
fff is not recursive, because it does not calls itself. It calls fib_2 which has a recursive implementation, but it is not enough to make the fff method recursive.
fib_2, on the other hand, is textbook-recursive: it has a base case for count == n, and it has a recursive branch that calls fib_2 with new values of a, b, and c.
fib_2 is recursive. fff is not.
The first call of fib_2 uses returns (hence 'uses') the result of the second call.
Or formal:
Recursion is defined by two properties:
A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer
A set of rules that reduce all other cases toward the base case
Your if inside fib_2 fulfills the first property.
The call to fib_2 fulfills the second.
fib_3 is an iterative.
fib_2 is not equal to fib_3!
Two functions are equal (in a mathematical manner), if and only if they produce the same output for every given input! fib_2 and fib_3 have different parameters so this can't be true.
fib_3 may be equal to fff and/or fib_1
For equality in a computer science manner you have to consider things like side effects.
public static int fib_2(int a, int b, int n, int c, int count) {
if (count == n) {
return c;
}
int tmp = b;
b = a + b;
a = tmp;
c = b;
++count;
return fib_2(a, b, n, c, count);
}
I think in this code recussion is happening.
im having a little problem about my codes
public class ex{
public static void main(String[] args) {
int sum,int a = 1,int b = 2;
int c = 1,int d = 2;
if (a<b) {
sum = sum+1;
}
if (c<b) {
sum = sum+1;
}
System.out.println("output :"+sum);
}
}
I wanted to add a value of 1 in the int sum if the conditions are met. but its not compiling
the output should be like this:
output: 2
First things first.. If you are a beginner to Java, this is an advise for you to learn well about the syntax of Java declaration, initialization and usage.
Declaration:
If you want to declare variables separately, you have to do it as below:
int a;
int b;
int c;
If you want to declare multiple variables in a single line, you have to do it as below:
int a,b,c;
Initialization:
If you want to initialize multiple variables in a single line, do it as below:
int a=0, b=4, c=3;
Usage:
Important thing you would like to learn here is - you can always declare 'n' number of variables without initialization.. but if you want to use any of them, they must be initialized at least once before you use them. Using them also includes even to print them.
If you won't follow any of the above mentioned points, you must get a compilation error.
Here is the code you must follow:
public class ex{
public static void main(String[] args) {
int sum = 0 , a = 1, b = 2;
int c = 1, d = 2;
if (a < b) {
sum = sum + 1;
}
if (c < b) {
sum = sum + 1;
}
System.out.println("output :"+sum);
}
}
public class TestExample {
public static void main(String args[]){
int sum = 0 ;
int a = 1;
int b = 2;
int c = 1;
int d = 2;
if (a<b) {
sum = sum+1;
}
if (c<b) {
sum = sum+1;
}
System.out.println("output :"+sum);
}
}
declaration of variable is wrong you should not declare your variable like int a,int b= 10
avoid declaration of variable on same line.
your code gives compilation error try this one it will give output as your expectation
Don't declare variables on the same line like this, even when it's compilable. It compacts your code in a way that makes it difficult to understand, especially when you name them a,b,c and d.
int sum = 0;
int a = 1;
int b = 2;
int c = 1;
int d = 2;
Change your declaration of variables to that and the rest of the code will run fine. But I would recommend reading some basic Java tutorials so you understand how to write code that compiles. I would also suggest using an IDE so these kinds of errors are flagged while you write your code.
IDEOne (with compilation errors): http://ideone.com/rYzIf5
IDEOne (without compilation errors): http://ideone.com/rYzIf5
Try this:
int sum = 0,a = 1,b = 2;
int c = 1, d = 2;
if (a<b) {
sum++;
}
if (c<b) {
sum++;
}
System.out.println("output :"+sum);