I need to build a loop that does this: 3 = Math.log10( result of number squared )/Math.log10( number inputed ) number_to_be_cubed
double cubed;
double answer;
answer = 1;
cubed = 0;
while (cubed <= 3) {
cubed = (double) Math.log( answer )/Math.log( number_to_be_cubed );
answer ++;
}
double answer_for_cubed = answer;
System.out.println("answer_for_cubed " + answer_for_cubed);
Based on the fact that A^3=B and 3=logB/logA are the same thing. Instead, I know I could use math.pow but I'm trying to solve x^3 with logs. I think it's not working because of the way java handles numbers. Is this possible? Am I looping the wrong way? I have been looking everywhere on the internet and i haven't found a problem like this.
this worked
public static void main(String[] args) {
Double cubed = 0d;
Double answer = 0d;
while (cubed.compareTo(3d) <0) {
answer ++;
cubed = Math.log( answer )/Math.log( 4 );
System.out.println(cubed + " " + answer);
}
double answer_for_cubed = answer;
System.out.println("answer_for_cubed " + answer_for_cubed);
}
result is:
answer_for_cubed 64.0
Related
∑i=1n1i
In other words, the method should generate the following sequence:
1+12+13+14+15+⋯
I've been stumped on this problem for quite a bit. Having a tough time understand what "n" stands for in the equation and applying it to my for loop.
Would a for loop be optimal for solving this? Or should I just use a formula and somehow solve it then?
public static void main(String[] args) {
//Chapter 4 Exercise 1
System.out.println("--Chapter 4, Exercise 1");
System.out.println("How many integers do you want?:");
Scanner console = new Scanner(System.in);
int numb = console.nextInt();
fractionSum(numb)
public static double fractionSum(int numb) {
for(int i = 1; i <numb; i++) {
if (i !=1)
System.out.print("1 + 1" + i);
else
System.out.print("1");
}
return(numb);
}
1+12+13+14+15+⋯
Should be the output.
My output is coming out as:
11 + 121 + 131 + 141 + 15
Answering the first part of your question:
∑i=1n1i is known as the Harmonic Sum - the output you give in your question is wrong, harmonic sum can be described as the sum of reciprocals of the positive integers - for example:
H1 = 1
H2 = 1 + 1/2 = 1.5
H3 = 1 + 1/2 + 1/3 = 1.8333
H4 = 1 + 1/2 + 1/3 + 1/4 = 2.0833
H(n) = 1 + 1/2 + 1/3 + 1/4 ... + 1/n = answer ---> see what n is now?
You should use a for-loop for this, and its not very complex. Hopefully, this answer will clear up the task for you (I assume homework from Chapter 4 Exercise 1) and you can try again.
Here:
class Harmonic {
public static void main(String… s) {
int n, i;
float sum = 0;
n = Integer.parseInt(s[0]);
for (i = 1; i <= n; i++) {
sum = sum + (float) 1 / i;
}
System.out.println(“nSum = ”+sum);
}
}
For a class that I am taking I am trying to create a program that produces a table of sin(), cos(), and tan() values for angles from 0 to 180 degrees in steps of 5 degrees.
!http://i65.tinypic.com/14ahliq.jpg
So far I have the following code, which produces an introduction and the first two lines of the table, but I cannot figure out how to get it to repeat.
import java.util.*;
public class Angles {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("This program computes the");
System.out.println("sin(), cos(), and tan() values");
System.out.println("for angles from 0 to 180 degrees");
System.out.println("in steps of 5 degrees.");
System.out.println("");
System.out.println("Angle\tSin()\tCos()\tTan()");
double Anglex = 0;
for(double i = 5;i <= Anglex;i += 5) {
Anglex = 0 + i;
}
double Sinx = Math.sin(Math.toRadians(Anglex));
double Cosx = Math.cos(Math.toRadians(Anglex));
double Tanx = Math.tan(Math.toRadians(Anglex));
System.out.println(Anglex + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);
}
}
Is not really ok that you ask people on forums to solve your assignments.
Otherwise, several issues with your little program (didn't test, please do it yourself).
anglex should start at 0 and stop at 180. So for(int anglex=0; anglex<=180; anglex+=5). Use anglex instead of i, inside the loop.
the calculations for sinx, cosx, tanx and the printing of the new line should be inside the curlies {}. As your code is right now, the only thing inside the loop is the increment of anglex.
Sorry for not providing the full solution, pretty sure you can do it.
Recast your for loop to
for (double Anglex = 0; Anglex <= 180; Anglex += 5){
Note well the opening brace to enclose multiple subsequent statements. Don't forget to balance it with a closing }; probably after the println call.
Using a double as a loop index is not to everyone's taste (you can get yourself into trouble if you are not using whole numbers), but this is fine in this instance, particularly also as you are using <= as the stopping condition.
Starting variable names with an upper case letter is also to be discouraged in Java as it's unconventional.
Your for loop only applies on the Anglex = 0+i line.
Add {} to the whole section that should be repeated.
public static void main(String[] args) {
System.out.println("This program computes the");
System.out.println("sin(), cos(), and tan() values");
System.out.println("for angles from 0 to 180 degrees");
System.out.println("in steps of 5 degrees.");
System.out.println("");
System.out.println("Angle\tSin()\tCos()\tTan()");
double maxAngleX = 180.0;
for (double angleX = 5; angleX <= maxAngleX; angleX += 5) {
double Sinx = Math.sin(Math.toRadians(angleX));
double Cosx = Math.cos(Math.toRadians(angleX));
double Tanx = Math.tan(Math.toRadians(angleX));
System.out.println(angleX + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);
}
}
for(double i = 5;i <= Anglex;i += 5) {
Anglex = 0 + i;
double Sinx = Math.sin(Math.toRadians(Anglex));
double Cosx = Math.cos(Math.toRadians(Anglex));
double Tanx = Math.tan(Math.toRadians(Anglex));
}
Enclose the above statements inside a { and }. The for loop applies for only the first statement in your code.
I need some help, this is the code I'm referring to:
int myId;
my Id = 20113275l
int numLet;
numLet = 6;
double doubleResult;
doubleResult = 10000 + (80 + ((myId - 123456) / ((numLet + 20)*(numLet + 20)) ));
System.out.println("Expression 5: " + doubleResult);
According to my calculator, the answer should be 39650.7, but when I run the program it's giving me 39650.0
Can anyone explain what's going on here?
You should use double values only to get the wanted result in this case :
int myId;
myId = 20113275;
int numLet;
Becomes
double myId = 20113275.0;
double numLet;
And here is the output :
Expression 5: 39650.73816568047
You're getting integer division, because none of your parameters are double. The result of dividing two integers is an integer. And your type is an int (adding an l at the end promotes to long). Anyway, cast one of the terms to a double and you'll get the result you expect. For one decimal of precision you can use formatted output. Something like,
public static void main(String[] args) {
int myId = 20113275; // <--- Not a long.
int numLet = 6;
double doubleResult = 10000 + //
(80 + ((myId - 123456) / (double) ((numLet + 20) * (numLet + 20))));
System.out.printf("Expression 5: %.1f%n", doubleResult);
}
Output is (as you requested)
Expression 5: 39650.7
So here is my task:
A postal company for a package charges $15 for the first
pound or a fraction thereof and $10 per pound for anything over one
pound. Write a program that prints the charge of a package.
Variables:
weight
First execution:
Weight? -12 Weight must be a positive number.
Second Execution:
Weight? 0 Weight must be a positive number.
Third Execution:
Weight? 2 Pay: $25.00
Forth Execution:
Weight? 2.8 Pay: $33.00
Fifth Execution:
Weight? 2.07 Pay: $25.70
and Here is the code I have developed so far:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double weight;
double cost = 15.00; // set first pound to $15
double output = 0;
System.out.print("Weight?: ");
weight = keyboard.nextDouble();
if (weight <= 0) {
System.out.println("Weight must be a positive number.");
} else if (weight == 1) {
// Print the charge of the package
output = output + cost;
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println("Pay: " + money.format(output));
} else {
for (double i = 1; i < weight; i = i + .01) {
if (weight > 1) {
output = output + (1 / 10.00);
}
}
// Print the charge of the package
output = output + cost;
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println("Pay: " + money.format(output));
}
}
}
Everything works, but what I can't figure out is why (especially in the Fourth and Fifth Execution) is the final output always .10 cents off. Can anyone help me get to the accuracy I need?
Here is what I came up with:
Scanner keyboard = new Scanner(System.in);
double weight;
double cost = 15.00; // set first pound to $15
double output = 0;
System.out.print("Weight?: ");
weight = keyboard.nextDouble();
if (weight <= 0) {
System.out.println("Weight must be a positive number.");
} else {
// Print the charge of the package
if (weight > 1) {
output = cost + ((weight-1) * 10);
} else {
output = cost;
}
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println("Pay: " + money.format(output));
}
This should handle all of your cases, as well as numbers between 0 and 1 assuming it's $1 per 0.1 lbs. Instead of your for-loop, you can just use the cost + ((weight-1) * 10) formula. I removed the check to see if weight was equal to 1 because it's handled in the else clause.
If I understand the question correctly, you should never have any fractional dollar amount because anything over a pound is automatically rounded up to the next pound. ie: 2.01 lbs would become 3 lbs. If this is correct, then you could use Math's ceil function to round the weight up to the nearest whole pound, then do something like this:
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double weight;
double cost = 15.00; // set first pound to $15
double output = 0;
System.out.print("Weight?: ");
weight = keyboard.nextDouble();
if (weight <= 0) {
System.out.println("Weight must be a positive number.");
} else if (weight == 1) {
// Print the charge of the package
output = output + cost;
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println("Pay: " + money.format(output));
} else {
double temp = (Math.ceil(weight)) - 1;
for(double i = temp; i > 0; i-- ) {
output += 10;
}
output += cost;
DecimalFormat money = new DecimalFormat("$0.00");
System.out.println("Pay: " + money.format(output));
}
}
}
This way, you don't need to bother with 10 cent increments. I hope this helps. Let me know if you have any questions.
This: double i = 1; i < weight; i = i + .01 could be your problem.
Doubles are not exact for decimal math. You're expecting i == weight, at which point the loop should stop, but it might not because i + .01 (however many times) is a tiny fraction less than weight.
My advice is to ditch the loop. If the package is over 1 lb, just subtract one pound from the weight, multiply by the $10 per pound, and then round to the two decimal places you need (NOTE: round it according to how it's spec'd to be rounded, don't just let the conversion from double to decimal do it on its own. There are multiple ways to round something, and decimal does not magically know which one is right for your problem.)
EDIT: Look at your solution, is it supposed to only work to a resolution of 1/10 of a lb? If so, start by rounding the weight. Again, round it according to how it needs to be rounded (down, up, or nearest).
I've been a lot of trouble figuring this class problem. My due date is tomorrow and I still don't know how to do it. I made a code when the input put by the user is converted into binary, octal, and hexadecimal. Now, the problem is that now they are asking me to modify the code in a way that the user inputs a Double or Floating point and convert it into decimal. My greatest problem is working with the decimal numbers; for example, 5*.987*. I will leave the code I already created. I would be very grateful is someone could help! thanks :)
import java.util.Scanner;
class EncodingTester {
public static void main(String args[]) {
byte largestPositiveByte = 127;
short largestPositiveShort = 32767;
int largestPositiveInt = 2147483647;
long largestPositiveLong = 9223372036854775807L;
long largestPositiveLongPlusOne = 9223372036854775807L;
Scanner in = new Scanner(System.in);
System.out.println("Next number (0 to stop): ");
long nextNumber = in.nextLong();
while (nextNumber != 0) {
int radix;
double logBase2 = Math.log(nextNumber) / Math.log(2);
double absoluteNumOfBits = 1 + Math.floor(logBase2);
double maxBits = Math.max(8, absoluteNumOfBits);
double numBits = Math.log(maxBits) / Math.log(2);
int newNumBits = (int) Math.pow(2, Math.ceil(numBits));
System.out.println("The absolute number of bits is: " +absoluteNumOfBits +". The final number of bits is: " +newNumBits +".");
radix = 10;
System.out.println("Decimal: " + String.format("%s", Double.toString(nextNumber)).replace(' ','0'));
radix = 2;
System.out.println("Binary: " + String.format("%"+newNumBits+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 8;
System.out.println("Octal: " + String.format("%"+((int) Math.ceil(newNumBits/3.0))+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 16;
System.out.println("Hexadecimal: 0x" + String.format("%"+newNumBits/4+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
System.out.println("");
System.out.println("Next number: (0 to stop)");
nextNumber = in.nextLong();
}
System.out.println("Good Bye");
}
}