-1

I need to have x go + in 0.1 steps till it reaches 1.0.

For every step I need output for the function y=4x^2 + 5x -3

The output my Java gives me is not what I expect.

I tried a while loop. I have to mention I am a completely new beginner.

public class Main {
    public static void main(String[] args)
    {
        double x = 0.1;
        double x2 = Math.pow(x,2);
        double y = 4*x2 + 5*x - 3;
        double counter = 0.1;

        while(counter <= 1.0)
        {
            System.out.print(y);
            counter =+ 0.1;
            x =+ 0.1;             
        }
    }
}

The output is just a long 2.46-2.46-2.46-2.46-....-...-2.46

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Laffa Yett
  • 17
  • 3

5 Answers5

2

This is because you are not updating y as x changes. You define y as:

double y = 4*x2 + 5*x - 3;

At the beginning, but you need to update it as x incrementally changes. You can do:

while(counter <= 1.0)
{
    y = 4*x*x + 5*x - 3;
    System.out.print(y);
    counter += 0.1;
    x += 0.1;             
}

But here's the beautiful thing, you can use x as your counter to make your code simpler:

while(x <= 1.0)
{
    y = 4*x*x + 5*x - 3;
    System.out.print(y);
    x += 0.1;             
}

There are more ways you can simplify this code, and I encourage you to play around with it and try to figure out some ways to improve it!

1

Welcome to the world of coding! When you're running a loop, the code inside of it will be executed several times. While it's intuitive to think that by defining y as a function of x means y updates when x does, this unfortunately isn't the case. In order to update the value, you have to re-evaluate it each time you run through the loop.

public static void main(String[] args) {
    double x = 0.1;
    double x2 = Math.pow(x,2);


    double y = 4*x2 + 5*x - 3;
    double counter = 0.1;

    while(counter <= 1.0)
    {
        System.out.print(y);

        counter += 0.1;

        //re-evaluate x, x2, and y here
        x += 0.1;
        x2 = Math.pow(x,2);
        y = 4*x2 + 5*x - 3;
    }
}

This works, but we can do better. If you'd like to try updating y dynamically with respect to x, consider writing a function:

double calculateY(double x) {
  double value = 4*(x*x) + 5*x - 3;
  return value;
}

In your loop, you would call the function like this:

y = calculateY(x);

Functions are a very good way to quickly and easily perform complex sets of code. As a bonus, if you want to calculate y somewhere else in your code, you don't have to copy-paste from your loop. This is good practice, because if you later need to change the equation, you only need to change it once, in the function, instead of in several places, where you might make mistakes.

Here's what the modified code might look like. Note that I start the variables at 0 - this can help reduce confusion.

double calculateY(double x) {
  double value = 4*(x*x) + 5*x - 3;
  return value;
}

public static void main(String[] args) {
  double x = 0;
  double y = 0;

  while (x <= 1.0) {
    x += 0.1;
    y = calculateY(x);
    System.out.print(y);
  }
}

Much less clutter! This code is easy to read, and easier to edit. If you want to calculate y a different way, you need only modify the calculateY function - with the added benefit that you don't need to recalculate, or even include, the x2 or counter variables.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • You should increment x after the calculation. – Filburt Jul 09 '19 at 15:16
  • Since `y` is printing before both the x increment and the calculation, I think the (untested) code runs fine - I do agree, though, convention would say increment after the calculation. – Nick Reed Jul 09 '19 at 15:22
  • Starting out with `double x = 0;` of course amends it. – Filburt Jul 09 '19 at 15:30
  • You're quite right! Instead of just saying it's good convention in the comments, I decided to show it in the answer. Thank you for pointing it out! – Nick Reed Jul 09 '19 at 15:33
  • one question though, when i try to do it your way, it says something like : "non static method cannot be referenced from a static context" – Laffa Yett Aug 17 '19 at 11:30
  • @LaffaYett Java likely wants the function declared as `public static double calculateY(double x)`. Why it wants this is a more complicated bit of reasoning - the quick version is, in Java, `static` functions and variables "always exist"; non `static` needs an "object" first. Check out [this](https://stackoverflow.com/a/2559596/7431860) answer for more info on objects, static vs non static, and "classes", one of the most important parts of Java. – Nick Reed Aug 17 '19 at 16:08
0

Need calculation in every loop.

double getY(double x){
...
}

while(counter <= 1.0)
    {
        System.out.print(getY(x));
        counter += 0.1;
        x += 0.1;
    }
Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
0
    public static void main(String[] args)
    {
        double x = 0.1;
        double x2 = Math.pow(x,2);
        double y;
        double counter = 0.1;

        while(counter <= 1.0)
        {
            y = 4*x2 + 5*x - 3;
            System.out.print(y);
            counter =+ 0.1;
            x =+ 0.1;             
        }
    }
}

You'll need to re-calculate y's value.

rmssoares
  • 175
  • 1
  • 11
0

The problem is that you are not re-calculating y after to you update x. Here is a sample that might work:


public class Main {

    double static calcY(double x) {
        double x2 = Math.pow(x,2);
        double y = 4*x2 + 5*x - 3;
        return y;
    }

    public static void main(String[] args)
    {
        double x = 0.1;
        double counter = 0.1;

        while(counter <= 1.0)
        {
            double y = calcY(x);
            System.out.print(y);
            counter += 0.1;
            x += 0.1;             
        }
    }
}
tantalum
  • 2,354
  • 1
  • 15
  • 22
  • 2
    `=+` needs to be `+=`, they are not equivalent. This will run forever in the current state. – Nexevis Jul 09 '19 at 15:10
  • it says something like "non static method cannot be referenced from a static context" – Laffa Yett Aug 17 '19 at 11:30
  • Yes because `calcY` is an instance method that is being called from `main` which is a static method. @LaffaYett. I've edited the sample to fix that issue. – tantalum Aug 19 '19 at 16:16