This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/9/17

quadratric equation calculator gives runtime error

divinity divinity

2017/9/17

#
hi danpost everyone i am doing an homework assignment where I have to create a quadratic calculator. the question goes like this; create a calculator that ; 1. let you know if you can solve a quadratic equation that has a valid solution 2. calculate the solution to the quadratic equation 3 lets you know if you can form a valid triangle 4. calculate the area of a triangle given S length 5. add two fraction and give the answer as a fraction this is what I have so far can you tell me where I have gone wrong and what i can do to fix it. here is my codes that I use
public Quadratric(){
      
    JTextField inputA, inputB, inputC, displayresults, displayresults2;
    
    this.setTitle("Quadratric Calculator");
    
    JLabel labelA = new JLabel("A", SwingConstants.RIGHT);
    inputA = new JTextField(6);//this is seting whatever input into A 
    JLabel labelB = new JLabel("B", SwingConstants.RIGHT);
    inputB = new JTextField(6);
    JLabel labelC = new JLabel("C", SwingConstants.RIGHT);
    inputC = new JTextField(6);
    JLabel labelresults = new JLabel("Results", SwingConstants.RIGHT);
    displayresults= new JTextField(6);
    displayresults.setEditable(false);  
    JLabel labelresults2 = new JLabel("", SwingConstants.RIGHT);
    displayresults2 = new JTextField(6);
    displayresults2.setEditable(false);
    JButton jb = new JButton("compute");
    jb.addActionListener(this);
    
   
    
   Container cont = getContentPane();
   cont.setBackground(Color.white);
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(5, 2, 5, 5 ));
    pan.add(labelA);
    pan.add(inputA);
    pan.add(labelB);
    pan.add(inputB);
    pan.add(labelC);
    pan.add(inputC);
    pan.add(labelresults);
    pan.add(displayresults);
    pan.add(displayresults2); 
    cont.add(pan, BorderLayout.CENTER);
    cont.add(jb, BorderLayout.SOUTH);
    
  }

    @Override
    public void actionPerformed(ActionEvent ae) {
        
        int A = Integer.parseInt(inputA.getText());
        int B = Integer.parseInt(inputB.getText());
        int C = Integer.parseInt(inputC.getText());
        double results = calculateresults(A,B,C);
        double  results2 = calculateresults(A,B,C);
        DecimalFormat df = new DecimalFormat("00.000");
        displayresults.setText(df.format(results));
        displayresults2.setText(df.format(results2));
        
    }
    private double calculateresults(int A, int B, int C){
      
        
        double result = -B + Math.sqrt((B*B) - (4 * A* C));
        return result/(2 * A);
    }
    private double Calculateresults2(int A, int B, int C){
        
        double results2 = -B -Math.sqrt((B*B) - (4 * A * C));
        
        return results2 /(2 * A);
    }
    
    
    public static void main(String[]args){
        
        Quadratric quad = new Quadratric();
        quad.setBounds(300, 300, 350, 250);
        quad.setDefaultCloseOperation(EXIT_ON_CLOSE);
       quad.setVisible(true);
    }
}
        
     
all help will be appreciated
danpost danpost

2017/9/17

#

quadratric equation calculator gives runtime error

What is the error and where is it thrown?
divinity divinity

2017/9/18

#
hi danpost I kinda fix that but not it is not giving me the correct answer. how can i a add two fraction together which will give the answer in a fraction. and how do i calculate the area of a triangle which give S length. here is my question create a calculator that will: 1. let you know if you can solve a quadratic equation that has a valid solution 2. calculate the solution to the quadratic equation 3 lets you know if you can form a valid triangle 4. calculate the area of a triangle given S length 5. add two fraction and give the answer as a fraction any help will be appreciated here is my revised codes.
package quadratric;

;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class Quadratric extends JFrame implements ActionListener {

    

    

    JTextField inputA, inputB, inputC, displayresults,displayresults2;
       
       
       
       
  public Quadratric(){
      
    JTextField sideA, sideB, sideC, displayresults, displayresults2;
    
    this.setTitle("Quadratric Calculator");
    
    JLabel labelA = new JLabel("A", SwingConstants.RIGHT);
    sideA = new JTextField(5);//this is seting whatever input into A 
    JLabel labelB = new JLabel("B", SwingConstants.RIGHT);
    sideB = new JTextField(5);
    JLabel labelC = new JLabel("C", SwingConstants.RIGHT);
    sideC = new JTextField(5);
    JLabel labelresults = new JLabel("Results", SwingConstants.RIGHT);
    displayresults= new JTextField(5);
    displayresults.setEditable(false);  
    JLabel labelresults2 = new JLabel("", SwingConstants.RIGHT);
    displayresults2 = new JTextField(5);
    displayresults2.setEditable(false);
    JButton jb = new JButton("compute");
    jb.addActionListener(this);
    
   
    
   Container cont = getContentPane();
   cont.setBackground(Color.white);
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(5, 2, 5, 5 ));
    pan.add(labelA);
    pan.add(sideA);
    pan.add(labelB);
    pan.add(sideB);
    pan.add(labelC);
    pan.add(sideC);
    pan.add(labelresults);
    pan.add(displayresults);
    pan.add(displayresults2); 
    cont.add(pan, BorderLayout.CENTER);
    cont.add(jb, BorderLayout.SOUTH);
    
  }

    @Override
    public void actionPerformed(ActionEvent ae) {
        
        double sideA=3, sideB = 3, sideC =3;
        
      if(validTriangle(sideA,sideB,sideC)== true){
          
          System.out.println(areaoftriangle(sideA,sideB,sideC));
          
      }
        
    }
    
    
    public static void main(String[]args){
        
        Quadratric quad = new Quadratric();
        quad.setBounds(300, 300, 350, 250);
        quad.setDefaultCloseOperation(EXIT_ON_CLOSE);
        quad.setVisible(true);
    }

    private boolean validTriangle(double sideA, double sideB, double sideC) {
        
        boolean isvalid = false;
        
        if(sideA + sideB > sideC && sideB + sideC > sideA && sideA + sideC > sideB){
             isvalid = true;
        }
        return isvalid;
    }

    private double areaoftriangle(double sideA, double sideB, double sideC) {
        
        double s = 0;
        double ans = 0;
        
         s = (sideA + sideB + sideC)/2;
         
         ans = Math.sqrt( s *(s - sideA) * (s - sideB) * (s - sideC));
         
         return ans;
    }
        
         
         
       
}
danpost danpost

2017/9/18

#
divinity wrote...
how can i a add two fraction together which will give the answer in a fraction
You will need to keep the numerator separated from the denominator. Finding and eliminating like factors will probably be necessary to reduce the fraction to its simplest form.
how do i calculate the area of a triangle which give S length
I think your division by 2 on line 109 is misplaced. Try dividing ans by 4 after line 111. Line 111 will need adjusted to subtract 2 times the side in each of the three factors.
divinity divinity

2017/9/19

#
hi danpost it is me again, I have since tried out something different again but this time it is not computing. I dont know for the life of me what have i done wrong or where I have gone wrong again. the question is the same as above, in my two previous post. so now it is not computing when i entered any equations. nothing is printing from the console, absolutely nothing, nada. here is the codes I have tried again and again. I have since add a main class to it. please help. here is the part with the gui
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quadratric;

;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class Quadratric extends JFrame implements ActionListener {

    JTextField inputA, inputB, inputC, displayresults,displayresults2;
       
  public Quadratric(){
      
    JTextField sideA, sideB, sideC, displayresults, displayresults2;
    
    this.setTitle("Quadratric Calculator");
    
    JLabel labelA = new JLabel("A", SwingConstants.RIGHT);
    sideA = new JTextField(5);//this is seting whatever input into A 
    JLabel labelB = new JLabel("B", SwingConstants.RIGHT);
    sideB = new JTextField(5);
    JLabel labelC = new JLabel("C", SwingConstants.RIGHT);
    sideC = new JTextField(5);
    JLabel labelresults = new JLabel("Results", SwingConstants.RIGHT);
    displayresults= new JTextField(5);
    displayresults.setEditable(false);  
    JLabel labelresults2 = new JLabel("", SwingConstants.RIGHT);
    displayresults2 = new JTextField(10);
    displayresults2.setEditable(false);
    JButton jb = new JButton("compute");
    jb.addActionListener(this);
    
   
    
   Container cont = getContentPane();
   cont.setBackground(Color.white);
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(5, 2, 5, 5 ));
    pan.add(labelA);
    pan.add(sideA);
    pan.add(labelB);
    pan.add(sideB);
    pan.add(labelC);
    pan.add(sideC);
    pan.add(labelresults);
    pan.add(displayresults);
    pan.add(labelresults2);
    pan.add(displayresults2); 
    cont.add(pan, BorderLayout.CENTER);
    cont.add(jb, BorderLayout.SOUTH);
    
  }

    @Override
    public void actionPerformed(ActionEvent ae) {
        

     //int A  = Integer.parseInt(get.Text());
     

        double sideA=3, sideB = 3, sideC =3;
        
      if(isvalidquad(sideA,sideB,sideC)== true){
          
          System.out.println(posquad(sideA,sideB,sideC));
           System.out.println(negquad(sideA,sideB,sideC));
          
      }
        
    }
    
    public static void main(String[]args){
        
        Quadratric quad = new Quadratric();
        quad.setSize(400, 300);
        quad.setBounds(300, 300, 350, 250);
        quad.setDefaultCloseOperation(EXIT_ON_CLOSE);
        quad.setVisible(true);
        
        
        
        
         
         
    }
      
       
    private boolean isvalidquad(double sideA, double sideB, double sideC) {
      boolean isvalid = false;
     
    
        return false;
}

    private boolean posquad(double sideA, double sideB, double sideC) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private boolean negquad(double sideA, double sideB, double sideC) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

}

 and here is the main class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quadratric;



/**
 *
 * @author luanataylor
 */
public class NewMain {
    
    
    public static void  main(String[]args){
        
     
        
         double sideA=2;
         double sideB =6;
        double sideC=4;
        
        if(isvalidquad(sideA, sideB,sideC)){
              
            System.out.println(posquad(sideA, sideB, sideC));
            System.out.println(negquad(sideA, sideB, sideC));

        }
                                                                                                                            
    }

    private static boolean isvalidquad(double sideA, double sideB, double sideC) {
        
        boolean isvalid = false;
        if( sideB * sideB >  4 * sideA * sideC){
             isvalid = true;
        
    }
           return isvalid;
    }

    private static double posquad(double sideA, double sideB, double sideC) {
        
            return (-sideB + num(sideA,sideB,sideC))/denom(sideA);

    }

    private static double negquad(double sideA, double sideB, double sideC) {
        return(-sideB - num(sideA,sideB,sideC))/denom(sideA);
    }
    
    private static double denom(double sideA) {
       return 2*sideA;
    } 
    private static double num(double sideA, double sideB, double sideC){
        
        double ans = (sideB * sideB) - (4 * sideA * sideC);
        
        return Math.sqrt(ans);
    }

    
        
}


 

        
     
danpost danpost

2017/9/19

#
You should probably have something sent to the console when an invalid set of values is given. Otherwise, you cannot absolutely say that nada goes to the console as if something must.
divinity divinity

2017/9/20

#
hi danpost when I said nada going to console, I meant nothing is being printed out. I dont know if it is the logic of the codes that I used. can you tell me what is wrong with the logic or if it is the logic itself why nothing is being computed.
danpost danpost

2017/9/20

#
divinity wrote...
can you tell me what is wrong with the logic or if it is the logic itself why nothing is being computed.
You do not have it coded so that some printout is given in all circumstances. Make sure that it prints something in all situations (including invalids) and then you can determine if calc or something else is wrong.
You need to login to post a reply.