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 ;
Related
I've been trying a lot but it only shows NaN. I'm not sure if I'm doing the right thing.
class Imaginary{
double a = 2;
double b = 3;
double c = 5;
double result = b * b - 4 * a * c;
if(result < 0.0){
double im1 = -2 + (Math.sqrt((result))/ 10);
double im2 = -2 - (Math.sqrt((result))/ 10);
System.out.println("x = " + imaginary1 + " or x = " + imaginary2);
}
}
You need to take negate result to make it positive before taking the square root (taking square roots of negative numbers always results in NaN) and append "i" before printing.
double real = -b / (2*a);
double img = Math.sqrt(-result) / (2*a);
System.out.println("x = " + real + " + " + img +"i or x = " + real + " - " + img + "i");
You shouldn't use sqrt(result) since it will always result in you taking the square root of a negative number (that is your condition for result). Instead try to use a formula (eg completing the square).
Hope it answers your question :)
Since you have a complex root, you need to work with complex numbers to solve the equation. Java lacks builtin support for complex numbers, but you can e.g. use Apache Commons:
if (result < 0.0) {
final Complex cb = new Complex(-b, 0.0);
final Complex root = new Complex(result, 0.0).sqrt();
final Complex r1 = cb.add(root).divide(2 * a);
final Complex r2 = cb.subtract(root).divide(2 * a);
}
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 4 years ago.
Improve this question
EDIT 1:
program should output this:
java CircleBugs 1.2 0.6 r = 1.2, t = 0.6 c = 7.5398223686155035 a = 4.523893421169302 x = 0.9904027378916139, y = 0.6775709680740424
My error:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
at debug.Debug.main(Debug.java:24)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
I am having an issue debugging this program:
public static void main(String[] args) {
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[]);
System.out.println("r = " + r + ", t = " + t); //Added the ";" to close the line of code
double c = 2 * Math.PI * r;
double A = Math.PI * r * r;
double x = r * Math.cos(Math.toRadians(t)); //fixed
double y = r * Math.sin(Math.toRadians(t)); //fixed
System.out.println("c = " + c );
System.out.println("A = " + A );
System.out.println("x = " + x + ", " + "y = " + y );
}
When using Double.parseDouble(args[0]) it assumes that you are passing values in command line for this arg values otherwise the array is empty(so you get indexoutofboundexception while calling the first element from an empty array). So just change code like this
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[1]);
And in command line execute this.
java test 5 4 //5 in place of t and 4 in place of r
I saved my file as test.java so replace test with your file name and run.
The args array is empty, and is causing this error.
The problem is that you don't give your program any inputs, so you try to parseDouble(args[0]) on an empty array.
Use something like this:
Scanner sc = new Scanner(System.in);
double t = sc.nextDouble();
int r = sc.nextInt();
Also you should use the methods like:
Math.cos(Math.toRadians(t))
Math.sin(Math.toRadians(t))
You have to use Math.cos() and Math.sin() methods.
double x = r * Math.cos(t);
double y = r * Math.sin(t);
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.
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 8 years ago.
Improve this question
Question That Needs To Be Solved: http://i62.tinypic.com/23rtieb.png
I have this problem I need to solve using Java. However I tried but was not able to. Here is what I have done so far.
public class Cirlce {
public static void main(String[] args) {
int counter = 0;
double pi = 0;
final String piCharacter = "\u03C0";
double num = 10000;
int e = 0;
for (int i = 0; i < 10000; i++) {
// r^2 = x^2 + y^2
double dx = Math.random();
double dy = Math.random();
double r = Math.sqrt(dx * dx + dy * dy);
if (r < 1) {
counter++;
pi = 4 * counter / 1000;
}
e++;
if (e == counter) {
System.out.println(pi);
counter += 1000;
}
System.out.println("The Approximated Value Of " + piCharacter + " is: " + pi);
}
}
}
The answer is that I need 10 outputs that are close to the value of pi.
One problem is in this line:
pi = 4 * counter / 1000;
4, counter and 1000 are all int, so you are doing integer arithmetic here. You'll need to make at least one of the values a double, for example:
pi = 4.0 * counter / 1000;
There are some other errors. For example, why are you doing counter += 1000;? Cleanup your code and make sure you understand exactly what each statement does.
I am new to Java and I'm trying to figure out how to dynamically calculate the change to the nearest 10 dollars. For instance, the user inputs a value (34.36), my code then calculates tip, tax, and total amount for the bill (total 44.24). Without user input, I need to calculate the change from $50.00. I've tried to round up to 50.00 from 44.24 with no luck, obviously I am doing something wrong. I've tried Math.round and tried to find the remainder using %. Any help on how to get the total change due to the nearest 10 dollar value would be great. Thank you in advance, below is my code:
Full dis-closer, this is a homework project.
import java.util.Scanner;
import java.text.NumberFormat;
import java.lang.Math.*;
public class test1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Get input from user
System.out.println("Enter Bill Value: ");
double x = sc.nextDouble();
//Calculate the total bill
double salesTax = .0875;
double tipPercent = .2;
double taxTotal = (x * salesTax);
double tipTotal = (x * tipPercent);
double totalWithTax = (x + taxTotal);
double totalWithTaxAndTip = (x + taxTotal + tipTotal);
//TODO: Test Case 34.36...returns amount due to lower 10 number
//This is where I am getting stuck
double totalChange = (totalWithTaxAndTip % 10);
//Format and display the results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
//Build Message / screen output
String message =
"Bill Value: " + currency.format(x) + "\n" +
"Tax Total: " + currency.format(taxTotal) + "\n" +
"Total with Tax: " + currency.format(totalWithTax) + "\n" +
"20 Percent Tip: " + currency.format(tipTotal) + "\n" +
"Total with Tax and 20 Percent Tip: " + currency.format(totalWithTaxAndTip) + "\n" +
"Total Change: " + currency.format(totalChange) + "\n";
System.out.println(message);
}
}
you make
double totalChange = round((totalWithTaxAndTip / 10)) * 10;
Math.round rounds a number to the nearest whole number, so as others have shown, you need to divide by 10, then multiply by 10 after rounding:
double totalChange = tenderedAmount - totalWithTaxAndTip;
double totalChangeRounded = 10 * Math.round(totalChange / 10);
Math.ceil(double) will round up a number. So what you need is something like that:
double totalChange = (int) Math.ceil(totalWithTaxAndTip / 10) * 10;
For totalWithTaxAndTip = 44.24, totalChange = 50.00
For totalWithTaxAndTip = 40.00, totalChange = 40.00
Everyone, thank you very much for helping me. I tested out everyone's solution. This is my final working code.....
double totalAmountPaid = totalWithTaxAndTip - (totalWithTaxAndTip % 10) + 10;
I tested it out using many different values and it seems to be working the way I want it to.
Again, I appreciate everyone for taking the time to help me out.