4 Replies - 344 Views - Last Post: 07 November 2017 - 08:07 PM Rate Topic: -----

#1 camerontoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 17

Help with linear equations program

Posted 07 November 2017 - 10:04 AM

Here is the assignment
Program Description: Write a program that solves the following linear equations in the form Ax + B = 0.

4x - 5 = 0
7x + 12 = 0
0x + 0 = 0
0x + 7 = 0
3.5x - 1.6 = 0
5x + 1.3 = 0

Read A and B from the keyboard. Be sure to account for all possible cases; that is:

• When A and B are both zero
• When A is non-zero
• When A is zero and B is non-zero

Required Statements: input, output, loop control, decision making, Be sure to run all of the test cases including at least one test case of your own that produces a different result from those given..

here is my code

[import java.io.*;
import java.util.*;
public class prog213g{
   public static void main (String args[]){
     Scanner scan = new Scanner(System.in);
     System.out.println("Enter A:");
     double a = scan.nextDouble();
     System.out.println("Enter B:");
     double b = scan.nextDouble();
     
     do{
     double x= (-(b/a));
    System.out.println(x);
   }while(a>=1);
   do{
    double x= (-(b/a));
    if(a==0 && b==0){
    System.out.println("There are infinite solutions");
    }
    }while(a==0 && b==0);
    do{
    double x= (-(b/a));
    if(a==0){
     System.out.println("There are no solution");
    }
   }while(a==0);
}
}]
:code:

the loop keeps running and running

Is This A Good Question/Topic? 0
  • +

Replies To: Help with linear equations program

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13484
  • View blog
  • Posts: 53,826
  • Joined: 12-June 08

Re: Help with linear equations program

Posted 07 November 2017 - 10:13 AM

Your conditions all seem to be reliant on some variable.. if that variable never changes then the condition is always true and it won't stop. Perhaps reevaluate the conditions and/or where you should be updating variables.
Was This Post Helpful? 0
  • +
  • -

#3 camerontoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 17

Re: Help with linear equations program

Posted 07 November 2017 - 10:17 AM

I changed my code:
[import java.io.*;
import java.util.*;
public class prog213g{
   public static void main (String args[]){
     Scanner scan = new Scanner(System.in);
     double a;
     double b;
     do{
         System.out.println("Enter A:");
     a = scan.nextDouble();
     System.out.println("Enter B:");
     b = scan.nextDouble();
     double x= (-(b/a));
     if(a==0 && b==0){
    System.out.println("There are infinite solutions");
    }
    if(a==0){
     System.out.println("There are no solution");
    }
    System.out.println(x);
   }while(a>-10000);
  
}
}]
:code:

but when I enter the zero for a or the zero for a and b i get this

Enter A:
0
Enter B:
0
There are infinite solutions
There are no solution
NaN
Enter A:
0
Enter B:
7
There are no solution
-Infinity

I am supposed to only get this

Enter A: 0
Enter B: 0
0x +0 = 0-->There are infinite solutions

Enter A: 0
Enter B: 7
0x +7 = 0-->There are no solutions







View Postmodi123_1, on 07 November 2017 - 10:13 AM, said:

Your conditions all seem to be reliant on some variable.. if that variable never changes then the condition is always true and it won't stop. Perhaps reevaluate the conditions and/or where you should be updating variables.

This post has been edited by modi123_1: 07 November 2017 - 10:18 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Help with linear equations program

Posted 07 November 2017 - 12:10 PM

Why do you think there are infinite solutions to a linear equation at all ?

4x +32 = 0

Answer is x = -8

0x + 76 = 0

Answer is A cannot be zero

50x + 0 = 0


Answer is x = 0



Its a series of if .. if else statements you need not do while loops...
Was This Post Helpful? 0
  • +
  • -

#5 camerontoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 17

Re: Help with linear equations program

Posted 07 November 2017 - 08:07 PM

The directions of the program say to output that when those 0's are put in. Also, a loop is required.

This post has been edited by ndc85430: 07 November 2017 - 11:43 PM
Reason for edit:: Removed quote of previous post. Just press "Reply".

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1