-1

My first assignment requires me to write a program that expresses a formula. The formula as written in the assignment :

y   =   4x^3 + 8x^2 - 31x - 35 / (3x2 + 1)^1/2  + 2 * | x - 1.5 |  

          

| means absolute value; (...)1/2 means square root

Also how should I create a code that prints how many times the formula prints out a positive or negative number or zero?

Here is how I created my assignment's program:

import java.io.*;

public class hw1 {

    public static void main(String[] args) throws IOException
    {
        System.out.println("My name is Jibran Khan and this is the output of my first program.");
        
        double x; double y;
        
        PrintWriter outFile = new PrintWriter("testfile.txt");
    
        for(x=-3.00; x <= 3.0; x = x + 0.50)
        { 
        
        y=((4*(x*x*x))) + (8*(x*x))-(31*x)-35/Math.sqrt(3*x*x+1)+2* Math.abs(x-1.5);
        System.out.println("X = " + x + "Y = " + y );
        
        if (y<=0.0) System.out.println("Y is negative");
        if (y>=0.0) System.out.println("Y is positive");
        if (y == 0.0) System.out.println("Y is zero");
        
        }
        
        
        System.out.println();
        System.out.println("My first program is complete");
        
        outFile.close();
        

    }
    
}
Som
  • 1,522
  • 1
  • 15
  • 48
Jibran Khan
  • 11
  • 1
  • 6
  • so what is wrong with what you have? – Scary Wombat Sep 30 '20 at 02:02
  • my formula is incorrect as well as not having coded a program that prints if a number is positive negative or zero. – Jibran Khan Sep 30 '20 at 02:04
  • 1
    If y is zero then `if (y<=0.0)` is true, but so is `if (y>=0.0)` – Scary Wombat Sep 30 '20 at 02:07
  • 1
    As for your logic, figure out the precedence order of calculations and then use brackets / or intermediate variables to ensure that the order is as you want. – Scary Wombat Sep 30 '20 at 02:08
  • How do you know that your formula is incorrect? – tgdavies Sep 30 '20 at 02:52
  • I'm still unsure of how to do any of that. – Jibran Khan Sep 30 '20 at 03:02
  • You have issue if y is equal to 0 ... You can write this: if (y< 0.0) { System.out.println("Y is negative"); } else if (y > 0.0) { System.out.println("Y is positive"); } else { System.out.println("Y is zero"); } – davidvera Sep 30 '20 at 15:07
  • You also have to prints how many times the formula prints out a positive or negative number or zero... just create 3 variables of type int : positive, negative, equalZero and give them default value 0. In your for loop and more specifically in the if statements you can modify the code this way : if (y< 0.0) { System.out.println("Y is negative"); negative++; } ... in the end of the for loop, you can just make a print: System.out.println("negative results : " + negative); – davidvera Sep 30 '20 at 15:11

2 Answers2

1

OK, you want to make a test on a simple math formula. You have to test the results and define how many results are positive, negative or equal to 0 ... You have to update your tests... It result is equal to 0, your test returns positive, negative and null results.

You also have to count results. So implement counters for each type of results and increment them in the if statement.

public static void main(String[] args) throws IOException {
    double x; double y;
    int positive = 0;
    int negative = 0;    
    int equalZero = 0;
    PrintWriter outFile = new PrintWriter("testfile.txt");

    for(x=-3.00; x <= 3.0; x = x + 0.50) { 
        y = Math.pow(4,3) + ... ( your formula) 
        System.out.println("X = " + x + "Y = " + y );
        if (y < 0.0) {
            System.out.println("Y is negative");
            negative++;
        } else if (y > 0.0) {
            System.out.println("Y is positive");
            positive++;
        } else { 
            System.out.println("Y is zero");
            equalZero++;
        }
    }

    System.out.println();
    System.out.println("Positive results : " + positive);
    System.out.println("Negative results : " + negative);
    System.out.println("Equal to zero   : " + equalZero);
   
    outFile.close();
}
davidvera
  • 1,292
  • 2
  • 24
  • 55
0

Calculate the Denominator and Numerator in different variables, that will give more readability and you will be able to spot the mistakes easily.

Also, the conditions you gave for y: (y <= 0.0) and (y >= 0.0) will be true for zero so the last condition y == 0.0 is not reachable.

Shubham Verma
  • 55
  • 1
  • 10