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

2017/10/17

class or method

1
2
3
divinity divinity

2017/10/17

#
when i put like this: this.add(pack) it is telling me to create a variable called pack, do i have to
Super_Hippo Super_Hippo

2017/10/17

#
I think you misunderstood what you have to do:
this.add(jp);
pack();
divinity divinity

2017/10/17

#
hi danpost i got it and thanks a lots super_hippo
divinity divinity

2017/10/18

#
hi danpost I fixed the error, apparently I need to rearrange some codes to get it to work, so I am having some errors but it is with the calculation part of if. I am not going to post the whole code just the part where I am getting errors. anyhelp will be appreciated.. here it is can someone explain the illegal start of expression and how to fix it
         if(Triangle_Calculator(1,2,3) == true){ this is where the error is. illegal start of expression
             
            System.out.println(isvalid(1,2,3));
     }
     

    private double doublearea(double  side1, double  side2, double  side3) {
        
        double side;
        double area;
        double s ;
        
        s = ( 1 + 2 + 3)/2.0;
        side = 2 * (side1 + side2 +side3);
        area = Math.sqrt(s *(s - side1) * (s - side2) * (s - side3));
        
        return area;
    }
     private boolean isvalid(double side1, double side2, double side3) {
        boolean isvalid = false;
       
        
        if((side1 + side2 > side3) && (side1 + side2 > side 3  > side2) && (side2 + side3 > side1)){         
         
         return isvalid;
    }
    }

    

    private double calculatearea2( double side1, double  side2, double  side3) {
        return 0;
    }
    
     public static void main(String[]args){
     
          Triangle_Calculator trical = new Triangle_Calculator();          
          trical.setBounds(300, 300,350, 250);
          trical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
          
          
  }

   
        
  }
    
    
danpost danpost

2017/10/18

#
Need to see code before the first line because that error is generated due to an invalid transition to that line. Also, what is your 'boolean' method 'Triangle_Calculator' with three numeric parameters?
divinity divinity

2017/10/18

#
ok danpost here it is
public class TriangleCalculator extends JFrame implements ActionListener {

    JTextField side1, side2, side3, displayArea, displayArea2, exit;

    public TriangleCalculator() {

        this.setTitle("Triangle Calculator");
        this.setSize(400, 400);

        JLabel lblA = new JLabel("1", SwingConstants.RIGHT);
        side1 = new JTextField(5);
        JLabel lblB = new JLabel("2", SwingConstants.RIGHT);
        side2 = new JTextField(5);
        JLabel lblC = new JLabel("3", SwingConstants.RIGHT);
        side3 = new JTextField(5);
        JLabel lblArea = new JLabel("Area", SwingConstants.RIGHT);
        displayArea = new JTextField(5);
        displayArea.setEditable(false);
        JLabel lblArea2 = new JLabel("", SwingConstants.RIGHT);
        displayArea2 = new JTextField(5);
        displayArea2.setEditable(false);
        JButton compute = new JButton("compute");
        compute.addActionListener(this);

        JPanel jp = new JPanel();
        jp.setBackground(Color.yellow);
        jp.setLayout(new GridLayout(6, 4, 6, 6));
        jp.add(new JButton("Button 1"));
        jp.add(lblA);
        jp.add(side1);
        jp.add(lblB);
        jp.add(side2);
        jp.add(lblC);
        jp.add(side3);
        jp.add(lblArea);
        jp.add(displayArea);
        jp.add(lblArea2);
        jp.add(displayArea2);
        this.add(jp);

        Container cont = getContentPane();
        cont.add(jp, BorderLayout.CENTER);
        cont.add(compute, BorderLayout.SOUTH);
        pack();
        this.setVisible(true);

    }

    
    public void actionPerformed(ActionEvent ae) {

        double A = Double.parseDouble(side1.getText());
        double B = Double.parseDouble(side2.getText());
        double C = Double.parseDouble(side3.getText());

        double area = doublearea(1, 2, 3);
        double area2 = calculatearea2(1, 2, 3);
        DecimalFormat fmt = new DecimalFormat("0,#");

    }

    private double doublearea(double side1, double side2, double side3) {

        double side;
        double area;
        double s;

        s = (1 + 2 + 3) / 2.0;
        side = 2 * (side1 + side2 + side3);
        area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

        return area;
    }

    if(TriangleCalculator(side1,side2,side3){this is where the error is
             
            System.out.println(isvalid(1, 2, 3));

    }

    private boolean isvalid(double side1, double side2, double side3) {
        boolean isvalid = false;
        double side = 0;

        if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {

        }
        return isvalid;

    }

    private double calculatearea2(double side1, double side2, double side3) {
        return 0;
    }

    public static void main(String[] args) {

        TriangleCalculator trical = new TriangleCalculator();
        trical.setBounds(300, 300, 350, 250);
        trical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
divinity divinity

2017/10/18

#
the error is on line 75
danpost danpost

2017/10/18

#
divinity wrote...
the error is on line 75
That line is not within a method -- it is placed between two different methods.
divinity divinity

2017/10/18

#
hi well danpost I got rid of line 75, no matter if ah put it in a method, I was still getting the error but at some point i might have to add it back along with the stupid error but for now, can u tell me what is wrong with my calculation because it is not printing out anything when i run it and add some numbers to it.
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 javax.swing.*;

public class TriangleCalculator extends JFrame implements ActionListener {

    JTextField side1, side2, side3, displayArea, displayArea2, exit;

    public TriangleCalculator() {

        this.setTitle("Triangle Calculator");
        this.setSize(400, 400);

        JLabel lblA = new JLabel("1", SwingConstants.RIGHT);
        side1 = new JTextField(5);
        JLabel lblB = new JLabel("2", SwingConstants.RIGHT);
        side2 = new JTextField(5);
        JLabel lblC = new JLabel("3", SwingConstants.RIGHT);
        side3 = new JTextField(5);
        JLabel lblArea = new JLabel("Area", SwingConstants.RIGHT);
        displayArea = new JTextField(5);
        displayArea.setEditable(false);
        JLabel lblArea2 = new JLabel("", SwingConstants.RIGHT);
        displayArea2 = new JTextField(5);
        displayArea2.setEditable(false);
        JButton compute = new JButton("compute");
        compute.addActionListener(this);

        JPanel jp = new JPanel();
        jp.setBackground(Color.yellow);
        jp.setLayout(new GridLayout(6, 4, 6, 6));       
        jp.add(lblA);
        jp.add(side1);
        jp.add(lblB);
        jp.add(side2);
        jp.add(lblC);
        jp.add(side3);
        jp.add(lblArea);
        jp.add(displayArea);
        jp.add(lblArea2);
        jp.add(displayArea2);
        this.add(jp);

        Container cont = getContentPane();
        cont.add(jp, BorderLayout.CENTER);
        cont.add(compute, BorderLayout.SOUTH);
        pack();
        this.setVisible(true);

    }

    
    @Override
    public void actionPerformed(ActionEvent ae) {

        double A = Double.parseDouble(side1.getText());
        double B = Double.parseDouble(side2.getText());
        double C = Double.parseDouble(side3.getText());

       double area = doublearea(1, 2, 3);
       double area2 = calculatearea2(1, 2, 3);
     

    }

    private double doublearea(double side1, double side2, double side3) {

        double side;
        double area;
        double s;

        s = (1 + 2 + 3) / 2.0;
        side = 2 * (side1 + side2 + side3);
        area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

        return area;
    }

   

    private boolean isvalid(double side1, double side2, double side3) {
        boolean isvalid = false;
        double side = 0;
        
        if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {

        }
        return isvalid;

    }

    private double calculatearea2(double side1, double side2, double side3) {
        
       return 0;
    }
      
    public static void main(String[] args) {

        TriangleCalculator trical = new TriangleCalculator();
        trical.setBounds(300, 300, 350, 250);
        trical.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}
danpost danpost

2017/10/18

#
Follow what you are doing with the results of your calculations -- especially with regard to having them displayed.
divinity divinity

2017/10/21

#
hey danpost I dont understand what you mean by your last post above, can u specifically say what is wrong with my calculation, is it wrong,
danpost danpost

2017/10/22

#
divinity wrote...
I dont understand what you mean by your last post above, can u specifically say what is wrong with my calculation, is it wrong,
I did not say anything about the calculation -- I only was saying that nothing is being done with the result of it such that it is being displayed.
divinity divinity

2017/11/2

#
hi I am back again with the calculation of this triangle area calculation. why am i still getting a zero output. can sometell me what is wrong, is it my calculation or the logic and tell me how to fix it.
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 javax.swing.*;

public class HeronFormulaCalculator extends JFrame implements ActionListener {

    JTextField side1, side2, side3, displayArea, displayArea2, exit;

    public HeronFormulaCalculator() {

        this.setTitle("Triangle Calculator");
        this.setSize(400, 400);

        JLabel lblA = new JLabel("1", SwingConstants.RIGHT);
        side1 = new JTextField(5);
        JLabel lblB = new JLabel("2", SwingConstants.RIGHT);
        side2 = new JTextField(5);
        JLabel lblC = new JLabel("3", SwingConstants.RIGHT);
        side3 = new JTextField(5);
        JLabel lblArea = new JLabel("Area", SwingConstants.RIGHT);
        displayArea = new JTextField(5);
        displayArea.setEditable(false);
        JLabel lblArea2 = new JLabel("", SwingConstants.RIGHT);
        displayArea2 = new JTextField(5);
        displayArea2.setEditable(false);
        JButton compute = new JButton("compute");
        compute.addActionListener(this);

        JPanel jp = new JPanel();
        jp.setBackground(Color.orange);
        jp.setLayout(new GridLayout(6, 4, 6, 6));
        jp.add(lblA);
        jp.add(side1);
        jp.add(lblB);
        jp.add(side2);
        jp.add(lblC);
        jp.add(side3);
        jp.add(lblArea);
        jp.add(displayArea);
        jp.add(lblArea2);
        jp.add(displayArea2);
        this.add(jp);

        Container cont = getContentPane();
        cont.add(jp, BorderLayout.CENTER);
        cont.add(compute, BorderLayout.SOUTH);
        pack();
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        double A = Double.parseDouble(side1.getText());
        double B = Double.parseDouble(side2.getText());
        double C = Double.parseDouble(side3.getText());
        
        double area=  doublearea(1, 2, 3);        
        displayArea.setText( area +"");

    }
        
        public double doublearea(double side1, double side2, double side3){
         
            double s;
            double area;
            
            s = (1 + 2 +3)/2;
            area = Math.sqrt(s *(s - 1) * (s - 2) * (s - 3));
            
            return area;
        }
       
     /*  public static  boolean isvalid(double side1, double side2, double side3) {
        
        return (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1); 
        
   }*/
    
    public static  void main(String[] args) {

        HeronFormulaCalculator hfc = new HeronFormulaCalculator();
        hfc.setBounds(300, 300, 350, 250);
        hfc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

}
danpost danpost

2017/11/2

#
A couple of problems remain. The 'doublearea' method does not use the parameter values passed to it and the values you are using (both those being passed to the method and those actually used in the method) will actually produce a result of zero.
divinity divinity

2017/11/2

#
how can i fix that, is there a way to fix it.
There are more replies on the next page.
1
2
3