I'm doing my HW for Java clas, and I can't understand how am I able to output this task properly and solve it. User have to input the X value.
package lab1;
import java.util.Scanner;
public class Lab1 {
public static void main (String [ ] args) {
System.out.println("Input x: ");
Scanner scan = new Scanner(System.in);
double x = scan.nextInt();
double c = Math.pow(x, 2);
// x в квадрате
double a = Math.pow((3+x),6);
//3+х в степени 6
double b = Math.pow(Math.E,0);
//експонента
double v = Math.log(x);
double n = Math.asin(c);
double K = Math.sqrt((a - v) / b + n));
System.out.println("Your answer - " + K);
}
}
https://imgur.com/a/S4iSpO2
I need prog to solve this task when user input the x value
P.S I know It's very stupid question and easy task, but I'm just getting started with dev so I hope you'll understand)
If I understand you correctly you want to merge step by step the individual terms of the formula. Something like below should work. But note that the arcsin is defined in the range [-π/2, π/2]. Since you even have the factor 6 in there, your program only returns useful values for x values between [-0.4,0.4]
code
public class Lab1 {
public static void main(String[] args) {
System.out.println("Input x: ");
Scanner scan = new Scanner(System.in);
double x = scan.nextDouble();
// (3+x)^6
double a = Math.pow(3+x, 6);
// lnx
double b = Math.log(x);
// √(3+x)^6 - lnx
double c = Math.sqrt(a-b);
//e^0 [something^0 is always = 1, so you could also set it just to 1]
double e = Math.pow(Math.E, 0); //double e = 1;
//arcsin6x^2
double f = Math.asin(6 * Math.pow(x, 2));
double k = c / (e+f);
System.out.println("Your answer: " + k);
}
}
Related
after adding two int variables to (a) and (b),
I have to gamble (a) times values between 10 to 100 and
calculate which square root of these gambled numbers is the closest to (b).
For example a=3 and b=2
output:
Gambled 16,25,49.
The number 16 was chosen since it's square root (4) is the closest to b=2.
I am stuck in the part of calculation of the square root, and saving the closest value to b each time the loop runs, i'm not allowed to use arrays,
this is the third question of my first task and i'd appreciate any experienced ideas to be shared ^^.
(MyConsole is a replacement for the scan command)
int a = MyConsole.readInt("Enter value a:");
int b = MyConsole.readInt("Enter value b:");
for(int i = 0; i<a; a--){
int gambler = ((int)(Math.random() *91)+10);
double Root = Math.sqrt(gambler);
double Distance= Root-b;
{
System.out.println();
Here is how I found the minimum. You can also use arrays to store all the gambling values if you want for future use. I also used Scanner but you can use your MyConsole I'm just not familiar with it. I hope it helps. דש מישראל
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value a:");
int a = scanner.nextInt();
System.out.println("Enter value b:");
int b = scanner.nextInt();
double min = 100;
for (int i = 0; i < a; i++) {
int gambler = ((int) (Math.random() * 91) + 10);
double root = Math.sqrt(gambler);
double distance = Math.abs(root - b);
if (min > distance)
min = distance;
}
System.out.println("minimum value is: " + min);
}
import java.util.Scanner;
public class Test1C
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.print("Enter an integer between 1 and 50: ");
double quo;
int num = reader.nextInt();
for(double a = 1; a <= num; a++)
{
quo = (num / a);
System.out.println(quo);
}
}
}
This is the code I currently am making to evaluate the list of quotients based on what number was inputted. Now the only thing my brain keeps farting on about is how to convert the whole numbers that are currently decimals into integers. However, the doubles (ex: 1.333) that are printed by this code are fine as is. I just can't figure out how to convert the whole number decimals (ex: 12.0) into whole number integers. Can someone help me?
yes you can :
String s = "1.333";
double d = Double.parseDouble(s);
int i = (int) d;
or
String s="1.333";
int i= new Double(s).intValue();
you can do like that .
Double d = new Double(1.25);
int i = d.intValue();
System.out.println("The integer value is :"+ i);
There is no general way to detect whether a double is truly an integer and a comparison of double and double or int must, generally, be handled with much care, but for the range of numbers in your program (and the upper bound could be raised considerably), this will work as you want:
for(double a = 1; a <= num; a++) {
double quo = num/a;
int iquo = (int)quo;
if( iquo == quo ){
System.out.println(iquo);
} else {
System.out.println(quo);
}
}
I passed the var array as an argument to the quadraticalc method and used it to perform operations but i want to return two values..but giving me errors
package testingequality;
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
//comparison ();
double [] var = new double [3];
int counter;
Scanner input = new Scanner (System.in);
for (counter = 0; counter< var.length; counter++)
{
System.out.println ("Input value for a,b,c");
var [counter] = input.nextDouble();
}
double [] sum = quadraticalc (var);
for (int i = 0; i < sum.length; i++)
{
System.out.println (sum [i]);
}
}
public static double [] quadraticalc (double [] var)
{
double a,b,c,d,e;
double [] x = new double [2];
a = var [0];
b = var [1];
c = var [2] ;
d = (b*b)-(4*a*c);
e = Math.sqrt(d);
double x1 = (-b + e)/ (2 * a);
double x2 = (-b - e)/ (2 * a);
x [0] = x1;
x [1] = x2;
return x;
}
}
I made a little change to the code and i was able to get the right answer now.. thanks to all that contributed
Instead of printing the array reference, you should print the values inside the array.
Change:
System.out.println (sum);
to:
System.out.println (sum[0]);
System.out.println (sum[1]);
Going out on a limb here ...
You need to pretty print the contents of the array. Try
Arrays.toString(sum)
But, of course, you could have other issues ...
So I ran your code. There are no errors, but I think you're getting something printed like [D#330bedb4 and you believe it is an error. Rather this is perfectly acceptable behavior. At
System.out.println (sum);
I think that you expect the numbers to be printed. In reality though, Java prints the array object, which doesn't have a pretty string representation. So instead, you want to print the doubles that are found within the array. Something like this will do
for (int i=0; i < sum.length; i++) {
System.out.println(sum[i]);
}
I got NaN when I did this, so there must be something wrong with quadraticalc and how you are calculating values.
EDIT: This is because if d is negative, e (the sqrt of d) will not be a real number. If you are calculating the quadratic, you maybe want a check for that.
I changed your quadratic function for easier readability to
public static double [] quadraticalc (double [] var)
{
double a = var [0];
double b = var [1];
double c = var [2];
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
double root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
double root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
return new double[] { root1, root2 };
}
Example run (I printed the a, b, c, d, and e values for visibility)
Input value for a,b,c
10 100 10
Input value for a,b,c
Input value for a,b,c
a = 10.0, b = 100.0, c = 10.0, d = 9600.0, e = 97.97958971132712
-0.10102051443364388
-9.898979485566356
These answers are correct and can be verified # http://www.math.com/students/calculators/source/quadratic.htm
I tried to do it but i'm getting 1.0 as answer every time i run it. I'm not being able to find out what's wrong please help me. Here are the codes:
import java.util.Scanner;
public class Number23 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n=0;
float sum = 0,r = 0;
System.out.print("Enter a number for n: ");
n = input.nextInt();
for(int x = 1; x <= n; x++)
{
r = (1/x);
sum = sum + r;
}
System.out.print("The sum is "+sum);
}
}
In order to produce a reciprocal that has a floating point value it is not enough to declare r a float: the expression you assign it needs to be float as well. You can do it by using the f suffix on the constant 1 which you divide by x:
r = (1f / x);
Without the suffix, your expression represents an integer division, which produces an integer result, and drops the fraction. In your case, the only time that you get a value other than zero is when x is equal to 1.
class reciprocal
{
public static void main ( int n)
{
float i,a,s=0;
for(i=1;i<=n;i++)
{
a= 1/i;
s+=a;
}
System.out.print( "sum is "+s);
}
}
How can I work with an 'x' variable like in math?
I need to write a code that determines a polynomial function. For example if the input data is 2, 4, 8, 9 then the function would be 2 + 4x + 8x^2 + 9x^3.
Now I do know how to process the input data etc. but I don't know how to declare a variable that has no value, for example I declared a variable x = double, but I still have to initialize it but I don't want to give x a value at this point yet?
So how can I write a method that for example returns 'x' to the power of something?
This is what I have at the moment (it still doesn't work of course)
public class Interpol {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
//get user input (polynomial coefficients and interval values x1 and x2)
ArrayList<Double> polynomialCoefficients = new ArrayList<Double>();
int amountCoefficients = 0;
while (scanner.hasNextDouble()) {
polynomialCoefficients.add(scanner.nextDouble());
amountCoefficients++;
}
String in = scanner.next();
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
//call method f to determine the polynomial function
double x;
double[] termsFunction = new double[amountCoefficients];
for (int i = 0; i < polynomialCoefficients.size(); i++) {
termsFunction[i] = f(i, polynomialCoefficients.get(i));
}
//call method findaroot to determine the root
//print result
}
//determine function of which a root is to be found
public static double f(int i, double polynomialCoefficient) {
if (i == 0) {
return polynomialCoefficient;
}
double x;
return polynomialCoefficient * (Math.pow(x, i));
}
/* //rounds off d to 3 decimals
public static double rnd(double d) {
}
//returns a root in this interval, recursive function
public static double findaroot{double x1, double x2) {
}*/
}
You need to make a Class of objects that represent polynomials. The class will store the coefficients in an instance variable, and provide a method to evaluate the polynomial at a given value.
import java.util.*;
public class Foo {
public static void main(String[] args)
throws Exception
{
Scanner scanner = new Scanner(System.in);
//get user input (polynomial coefficients and interval values x1 and x2)
ArrayList<Double> polynomialCoefficients = new ArrayList<Double>();
int amountCoefficients = 0;
while (scanner.hasNextDouble()) {
polynomialCoefficients.add(scanner.nextDouble());
amountCoefficients++;
}
Polynomial polynomial = new Polynomial(polynomialCoefficients);
String in = scanner.next();
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
System.out.println("f(" + x + ") = " + polynomial.evaluate(x));
}
}
static class Polynomial {
ArrayList<Double> _coefficients;
public Polynomial(ArrayList<Double> coefficients) {
_coefficients = coefficients;
}
public double evaluate(double x) {
double result = 0;
// This algorithm is called Horner’s rule. See Knuth.
for (int i = _coefficients.size() - 1; i>= 0; i--) {
result *= x;
result += _coefficients.get(i);
}
return result;
}
}
}
If I run this program and type 2 4 8 9 x 2.7, then it prints f(2.7) = 248.267, which is correct according to Wolfram Alpha.