-1
  1. Allow the user to enter two integers a and b. Have two methods called sum and product and when they are used with the input a and b, they return their sum and product.
  2. And another part that allows the user to enter 3 real numbers a b and c. Use the method Discriminant (with input a(!=0),b,c) to return the value of D=b^2-4ac. If a=0, the discriminant and the roots do not exist. Program should output the following:
    • you have entered a=..., b=..., c=...
    • the quadratic equation is axx+bx+c=0
    • the discriminant is D=...{D is calculated using the formula D=b*b-4*a*c}

Inside the main body of the program, using conditional statements check the following:

  • if D is positive call the method TwoRoots to return the two values of the roots

Output of the roots are x1=-b+Sqr D/2a and x1=-b-Sqr D/2a

  • if D is negative output is no real roots

I did the first part but it isn't working. This is my first time using methods and its a bit confusing, i only know how to use them without user input. But i need user input for this. These are the codes i tried: The first code is the with return method and the second code is the without return method.

class quadratics1 {

    int a,b;
    String input1=JOptionPane.showInputDialog("Please enter an integer");
    a=Integer.parseInt(input1);
    String input2=JOptionPane.showInputDialog("Please enter another integer");
    b=Integer.parseInt(input2);

    int sum1(int L, int W){
        int sum=(L+W);
        return sum;
    }

    int product1(int L, int W){
        int product=2*(L+W);
        return product;
    }

    public static void main(String str[]){
        quadratics1 m = new quadratics1();
        System.out.println(a+" " + b+ " " + m.sum1(a,b));
        System.out.println(a+" " + b+ " " + m.product1(a,b));
    }
}

The errors i am getting are: identifier expected for the variables a and b in lines 4 and 6

I also tried this:

import javax.swing.*;

 class quadratics1{

    void sum(int L, int W){
        int sum1=(L+W);
        System.out.println(a+" " + b+ " " + m.sum1(L,W));
    }

    void product(int L, int W){
        int product1=2*(L+W);
        System.out.println(a+" " + b+ " " + m.product1(L,W));
    }

    public static void main(String str[]){
        int a,b;
        quadratics1 m = new quadratics1();
        String input1=JOptionPane.showInputDialog("Please enter an integer");
        a=Integer.parseInt(input1);
        String input2=JOptionPane.showInputDialog("Please enter another integer");
        b=Integer.parseInt(input2);
    }
}

I do not know what i am doing wrong, I get errors for the variables. Any help would be greatly appreciated. I have been trying to figure this out for a few days. I'm new to coding and I have to have this done by tomorrow

For the second part, I wrote this code for the equations but i dont know how to make it into a method.

double a, b, c,d,r,rr;

    String input1 = JOptionPane.showInputDialog("Please, enter the first real number");
    a = Double.valueOf(input1).doubleValue();
    String input2 = JOptionPane.showInputDialog("Please, enter the second real number");
    b = Double.valueOf(input2).doubleValue();
    String input3 = JOptionPane.showInputDialog("Please, enter the third real number");
    c = Double.valueOf(input3).doubleValue();
    d= Math.pow(b,2) - 4*a*c;
    d = Math.round(d*100)/100.0;
    r= (-b + Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
    r = Math.round(r*100)/100.0;
    rr= (-b - Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
    rr = Math.round(rr*100)/100.0;  
    System.out.println("You have entered a = " + a + " b = " + b + " c = " + c);
    System.out.println("The quadratic equation is " + a + "x^2 " + b + "x " + c + " = 0" );
    System.out.println("The discriminant is D= " + d + "   D is calculated using the formula D= " + b + "^2 " + " - 4" + "("+ a + ")" + "("+ c + ")");
    System.out.println("The roots are " + r + " and " + rr);
J.Doe
  • 1
  • 6
  • @David He's calling them in the main within the sysout. – Yassin Hajaj Nov 19 '15 at 16:36
  • What isn't working in your first attempt? Your second attempt looks like the methods are infinitely recursive (though, thankfully, never initially invoked). – David Nov 19 '15 at 16:37
  • "i get errors for the variables" is not really specific. Please show us what you expect and what you are seeing, exactly. – BadZen Nov 19 '15 at 16:37
  • 1
    @David: The first attempt won't compile. He's trying to define logic in the class definition. – Mage Xy Nov 19 '15 at 16:37
  • You have to get compile errors...You're using `a` and `b` variables in `sum` and `product` methods and these variables aren't globals. – Miguel Nov 19 '15 at 16:38
  • 1
    It looks like there's *a lot* wrong here. To be honest, you may want to start with some simpler examples and work your way up. First just try to create a method that you can call from `main()`, regardless of what that method does. Then try to get user input in `main()`. Then try to pass that input to the method. Then try to return something from the method and output it in `main()`. And so on, and so on. This all-at-once attempt is creating a bit of a mess. – David Nov 19 '15 at 16:39

2 Answers2

0

Well for the second part I chosed to use your code in a method of a class , and call an instance from another class , in main. Here's the code

package StackOverFlow;


import javax.swing.JOptionPane;

class Discriminant{

    public double Discriminant(){
double a, b, c,d;
float e,r,rr;

    String input1 = JOptionPane.showInputDialog("Please, enter the first real number");
    a = Double.valueOf(input1).doubleValue();
    String input2 = JOptionPane.showInputDialog("Please, enter the second real number");
    b = Double.valueOf(input2).doubleValue();
    String input3 = JOptionPane.showInputDialog("Please, enter the third real number");
    c = Double.valueOf(input3).doubleValue();
    d= Math.pow(b,2) - 4*a*c;
    d = Math.round(d*100)/100.0;
    e=(float) Math.sqrt(d);
    r= (float) ((b*(-1) + e)/(2*a));
    r = (float) (Math.round(r*100)/100.0);
    rr= (float) ((b*(-1) - e)/(2*a));
    rr = (float) (Math.round(rr*100)/100.0);  
    System.out.println("You have entered a = " + a + " b = " + b + " c = " + c);
    System.out.println("The quadratic equation is " + a + "x^2 " + b + "x " + c + " = 0" );
    System.out.println("The discriminant is D= " + d + "   D is calculated using the formula D= " + b + "^2 " + " - 4" + "("+ a + ")" + "("+ c + ")");
    if (d>0){
        System.out.println("The roots are " + r + " and " + rr);
    }
    else if (d<0){
        System.out.print("There r not roots.");
    }


    return d;
}
}

And the class with the main , that calls the instance:

package StackOverFlow;


   public class test {


   public static void main(String[] args) {
       double dis=0;
Discriminant a=new Discriminant();
dis=a.Discriminant();
System.out.print("\n\n\n The discriminant is" + dis);
   }}

I hope that by "i dont know how to make it into a method" you meant something like that. This was my Java help.Now I have to fix the roots x1=[b*(-1)+sqrD]/2*a and x2=[b*(-1)-sqrD]/2*a

Also we forgot that when Discriminant is negative , there are no roots. I also fix the roots (you NEED d,r,rr as float) and I added and if statement.Now if D<0 , user gets a message that there r not roots else if D>0 the roots are printed.

0

And here is the first part.A few alterations here.

package StackOverFlow;

import javax.swing.JOptionPane;

class quadratics {



    int sum1(int L, int W){
        int sum=(L+W);
        return sum;
    }

    int product1(int L, int W){
        int product=2*(L+W);
        return product;
    }

    public static void main(String str[]){
    String input1=JOptionPane.showInputDialog("Please enter an integer");
    int a=Integer.parseInt(input1); //Declaration and put value in the same statement.
    String input2=JOptionPane.showInputDialog("Please enter another integer");
    int b=Integer.parseInt(input2);//Declaration and put value in the same statement.
        quadratics m = new quadratics();
        System.out.println(a+" " + b+ " " + m.sum1(a,b));
        System.out.println(a+" " + b+ " " + m.product1(a,b));
    }
}

Keep in mind that you have to fix the first line (package) according to the folder or package you will save them.