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
I want to make a method that analyze a given value
void method(int value)
if this value equals for example 7, I want output to be like this :
7 = 12 * 0 + 7
if value = 13 output : 13 = 12 * 1 + 1
if value = 24 output : 24 = 12 * 2 + 0
if value = 39 output : 39 = 12 * 3 + 3
if value = 289 output : 289 = 12 * 24 + 1
and so on
12 is constant
How can I make this ?
Looks like you are looking for
12 * x + y
This means:
x = value / 12;
y = value % 12;
void method(int value) {
int x = value / 12;
int y = value % 12;
System.out.print(value + " = 12 * " + x + " + " + y);
}
The method that you are looking for could be something like the snippet that I have written above.
Here is the complete code:
public class SingleItemView {
public static void main(String[] args) {
SingleItemView.getNumber(14); // Pass the value for which you need the expression
}
public static void getNumber(int n) {
int temp1 = 0;
int temp2 = 0;
if (n > 12) {
temp1 = n / 12;
temp2 = n % 12;
System.out.println(n + " = 12 * " + temp1 + " + " + temp2);
} else {
System.out.println(n + " = 12 * 0 + " + n);
}
}
}
In your main method you are passing value to the method for which you need to expression.
Related
I'm new to recursion and don't understand how it works.
This was a classwork problem that had the answer 18, but I don't understand how. From what I know, this should return 6 + 5 + 4 + 3 (3 + m-1 on the recursive line)?
Are the subtraction signs not indicative of subtraction? (assuming that m = 5)
public int test(int m)
{
int value;
if (m == 0)
value = 3;
else
value = test(m - 1) + 3;
return value;
}
6 + 5 + 4 + 3 (3 + m-1 on the recursive line)? Are the subtraction
signs not indicative of subtraction?
No the +3 will happen for every one of the recursive calls, actually what your function is doing is given the value of (m times 3) + 3.
So for m=5 the recursive calls will be like:
Is m = 0 ? No so let us called recursively:
test(4) + 3
m = 4; then test(3) + 3 + 3
m = 3; then test(2) + 3 + 3 + 3
m = 2; then test(1) + 3 + 3 + 3 + 3
m = 1; then test(0) + 3 + 3 + 3 + 3 + 3
m = 0; then exit with 3 + 3 + 3 + 3 + 3 + 3
Hence, for m=5 you get 18.
A side-note you can use the ternary operator to simplify your method to:
static public int test(int m) {
return (m == 0) ? 3 : test(m - 1) + 3;
}
For visualizing what happens, scatter the code with messages:
public int test(int m)
{
System.out.println("entering test("+m+")");
int value;
if (m == 0)
value = 3;
else
value = test(m - 1) + 3;
System.out.println("returning "+value+" from test("+m+")");
return value;
}
Of course this is just the minimal program, you could also show which branch of the if was taken, m-1, and so on.
JavaScript equivalent, so it can run here in the browser:
function test(m) {
console.log("entering test(" + m + ")");
var value;
if (m == 0)
value = 3;
else
value = test(m - 1) + 3;
console.log("returning " + value + " from test(" + m + ")");
return value;
}
console.log("result: "+test(3));
On the longer run it is a good idea to learn using the debugger of the environment you are using. Among other things, debuggers can step through code line-by-line.
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 4 years ago.
Improve this question
I'm a java learner and I have a task to do from a forum to learn more.
The challenge is receive a number and multiply it by it's own digit like:
input 999
output `9*9*9 = 729 then 7*2*9 =126 again 1*2*6 = 12 finally 1*2=2 until it hits only one single digit.
I got this issue which I ask a variable to add the multiplication of an array of length of 2 it returns me this
1------------------1
49-----------------2
final result 2450
And this is the code..
class Persist {
public static void persistence(long n) {
String number = String.valueOf((int)n);
char[] digits1 = number.toCharArray();
int value = 1;
for(int i = 0 ;i <= digits1.length -1;i++) {
System.out.println(value + "-----------------" + digits1[i]);
value = value* (int)digits1[i];
}
System.out.println((int)value);
}
public static void main(String [] args) {
persistence(12);
}
}
I can try to fix this but I'm interested to know whats wrong.Thank you all in advanced for the help and just by passing by.
You are using the ASCII values of the numbers (see https://en.wikipedia.org/wiki/ASCII) i.e. 1=49 and 2=50
49 * 50 = 2450
You can use the Character.getNumericValue to get the numerical value of the char instead, i.e.
class Persist {
public static void persistence(long n) {
String number = String.valueOf((int)n);
char[] digits1 = number.toCharArray();
int value = 1;
for(int i = 0 ;i <= digits1.length -1;i++) {
System.out.println(value + "-----------------" + digits1[i]);
value = value* Character.getNumericValue((int)digits1[i]);
}
System.out.println((int)value);
}
public static void main(String [] args) {
persistence(12);
}
It is easiest to understand if you look at the values with a debugger. During the actual calculation value = value * (int)digits1[i] you are not using the value of the digits but the ASCII value of the chars. This is 1->49 and 2->50 so you are calculating 49*50=2450.
Change the line to value = value* Character.getNumericValue(digits1[i]); and you get what you are looking for.
Here's how I would do this. I adjusted it so that a 0 value digit is converted to 1. You can comment that out (did this because any digit of 0 turns the answer into 0). This version will keep re-multiplying until it's down to one digit.
public class Persist {
public static void main(String [] args) {
int answer = persistence(234);
System.out.println(" length: " + String.valueOf(answer).length());
while ( String.valueOf(answer).length() > 1) {
answer = persistence(answer);
} }
static int persistence(int n) {
int value = 1;
String myStr = String.valueOf(n);
int myStrLen = myStr.length();
int[] finalIntAr = new int[myStrLen];
for (int v=0; v< myStrLen; v++) {
String subS= myStr.substring(v,v+1);
System.out.println("the char/int : " + subS);
Integer bigI = Integer.valueOf(subS);
if (bigI == 0) { bigI = 1; } // take this out if you want 0 to perform as 0
finalIntAr[v] = bigI.intValue();
}
for (int i = 0 ; i < myStrLen; i++) {
System.out.println(" ----- first= " + value + " multiplied by : " + finalIntAr[i]);
value = value * finalIntAr[i];
}
System.out.println("\n CURRENT FINAL VALUE *** : " + value);
return value;
} }
Output:
the char/int : 2
the char/int : 3
the char/int : 4
----- first= 1 multiplied by : 2
----- first= 2 multiplied by : 3
----- first= 6 multiplied by : 4
CURRENT FINAL VALUE *** : 24
length: 2
the char/int : 2
the char/int : 4
----- first= 1 multiplied by : 2
----- first= 2 multiplied by : 4
CURRENT FINAL VALUE *** : 8
Since nobody posted an answer using no strings or chars, I will. Technically not an answer to your question, though.
public static void persistence(long x)
{
long answer = 1;
while (x != 0) {
long onesDigit = x % 10;
answer *= onesDigit;
x /= 10;
}
System.out.println(answer);
}
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
Im having a hard time finishing the problem. I don't know what to do with the int and double values for the calculations. How would i go through with solving them. I am using Jgrasp for my class.
Calculations:
Standard: (20.0 + 40.0 + 5.0 + 60.0 + 30.0 ) / 5
Weighted: 20.0 * 0.15 + 40.0 * 0.1 + 4.0 * 0.2 + 60.0 * 0.25 + 30.0 * 0.3
standard carpet cost: $620.0
weighted carpet cost: $640.0
The Code that I have is
import javax.swing.JOptionPane;
public class Project2A
public static void main(String[] args)
{
int sqftbrm = 20;
int sqftdrm = 40;
int sqfthal = 5;
int sqftgrm = 60;
int sqftstr = 30;
double bf = .15;
double df = .10;
double hf = .20;
double gf = 0.25;
double sf = .30;
char stndCst[] = {"(sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr)"/5};
String stdcst = new String(stndCst);
System.out.print(stdcst);
char wtAvg[] = {"sqftbrm*bf" + "sqftdrm*df" + "sqfthal*hf" + "sqftgrm*gf" + "sqftstr*sf");
String wtAverage = new String(wtAvg);
System.out.print(wtAverage);
}
Outside of the fact that you should probably be using an IDE to easily identify your mistakes.... Semi colon on main method, missing brackets, invalid variable types, etc.
All you have is two arrays with one string value each.
char stndCst[] = {"(sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr)"/5};
char wtAvg[] = {"sqftbrm*bf" + "sqftdrm*df" + "sqfthal*hf" + "sqftgrm*gf" + "sqftstr*sf"} ;
If you want to do math with some variables, then remove the quotes.
For example,
double stndCst = (sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr) / 5.0;
double wtAvg = sqftbrm*bf + sqftdrm*df + sqfthal*hf + sqftgrm*gf + sqftstr*sf ;
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
What I'm trying to do is print the results in increments of 5 - DONE. Then, print only the last line of the loop. For ex: if z=26, print results of z # 5, 10, 15, 20, 25. Then println with z # 26. I'm stuck and can't figure it out.
for (int i = 1; i <= z; i++) {
b = b + (b * y) + x + w;
if (i % 5 == 0)
//input 1
System.out.println("In " + i + " years, IRA value: " + b);
//input 2 - value of IRA when retirement is reached
System.out.println();
I don't know what initial values b, x, w and y got so I just initialized them somehow ..
But I think you want something like this:
public class HelloWorld{
public static void main(String []args){
int z = 26;
int b = 0;
int x = 1;
int w = 2;
int y = 1;
for (int i = 1;i<=z;i++) {
b = b+(b*y)+x+w;
if (i % 5 == 0)
System.out.println("In " + i + " years, IRA value: " + b);
}
System.out.print(z);
}
}
Output:
In 5 years, IRA value: 93
In 10 years, IRA value: 3069
In 15 years, IRA value: 98301
In 20 years, IRA value: 3145725
In 25 years, IRA value: 100663293
26
Change your condition to
if(i%5==0 || i==z)
Adding an or (|| i==z) in the if will do the work
for (int i = 1;i<=z;i++) {
b = b+(b*y)+x+w;
if (i%5==0 || i==z)
//input 1
System.out.println("In " + i + " years, IRA value: " + b);
//input 2 - value of IRA when retirement is reached
System.out.println();
//end
}
The following Java code calculates the factorial of a given number and also prints the intermediate steps.
public class Factorial {
public static int getFactorial(int number) {
int n = number;
int factorial = 1;
while(n > 1) {
factorial *= n;
n--;
System.out.println(factorial + " * " + n + " = " + factorial);
}
return factorial;
}
}
When the method is called:
Factorial.getFactorial(4);
Should print to the console something like the following:
4 * 3 = 12
12 * 2 = 24
24 * 1 = 24
But instead it prints something like the following:
4 * 3 = 4
12 * 2 = 12
24 * 1 = 24
What am I doing wrong?
That is happening because you're printing the factorial variable only. Replace:
System.out.println(factorial + " * " + n + " = " + factorial);
with:
System.out.println(factorial + " * " + n + " = " + factorial * n);