Question

Java programming: The following java code is supposed to solve a linear equation of the form...

Java programming:

The following java code is supposed to solve a linear equation of the form aX+b = 0

where a and b are real numbers. However, there are some errors, nd and x them using a

combination of exceptions and control statements. Hint. What could go wrong in the execution

of this program?

/*********************************************/

// here is the code I have:

import java.util.*;

public class Solver{

public static void main(String[] args){

System.out.println("Let us solve an equation of the form aX + b = 0 with a and

b real numbers.");

Scanner in = new Scanner(System.in);

System.out.println("Enter a:");

double a = in.nextDouble();

System.out.println("Enter a:");

double b = in.nextDouble();

double x = -b / a;

System.out.println("The solution to this equation is: " + x);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

public class Solver{

    public static void main(String[] args){

        System.out.println("Let us solve an equation of the form aX + b = 0 with a and b real numbers.");

        Scanner in = new Scanner(System.in);

        System.out.println("Enter a:");

        double a = in.nextDouble();

        System.out.println("Enter b:");

        double b = in.nextDouble();

        double x;
        if(a!=0) {
            x = -b / a;
            System.out.println("The solution to this equation is: " + x);
        }
        else
            System.out.println("Value of a should not be zero");

    }

}
Add a comment
Know the answer?
Add Answer to:
Java programming: The following java code is supposed to solve a linear equation of the form...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • Java Programming Question. I am writing a code to calculate the roots of the quadratic equation...

    Java Programming Question. I am writing a code to calculate the roots of the quadratic equation based on the values of the coefficients A, B, and C. I am supposed to enter 5 values for each coefficient, and the output should be five different answers (see example) using a for loop and if else statements. However, when I run my code, I am only able to enter one value for each coefficient and the output is one answer repeated five...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Q: Create a version of the UnitConverter application to convert from inches to foot. Read the...

    Q: Create a version of the UnitConverter application to convert from inches to foot. Read the inches value from the user. A: import java.util.*; class UnitConverter{    public static void main(String[] args){ int inches; int foot; Scanner scan=new Scanner(System.in); System.out.println("Enter inches:"); inches=scan.nextInt(); System.out.println(inches/12+" feet and "+inches%12+" inches."); } } Q: Write a program that converts grams to pounds. (One pound equals 453.592 grams.) Read the grams value from the user as a floating point value. A: import java.util.*; class GramsToPounds{...

  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • This java code won't run and I can't figure out the problem with it. Please help...

    This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci {    // Returns n-th Fibonacci number    static BigInteger fib(int n)    {        BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE;           for (int j=2 ; j<=n ; j++)        {            Fibo[j] = Fibo[j-1].add(Fibo[j-2]);        }    return (Fibo[n]); }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT