|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-22-2007, 10:14 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 40
|
|
|
Help with images...
I have a coding problem... and if someone can help me finish it.. it would be grateful...
I can't make the numbers to respond to the answers...... and follow the image says...
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class TriangleArea extends Applet implements ActionListener {
private Label lblInstructions, lblSideA, lblSideB, lblSideC,
lblSelect, lblAngles, lblArea, lblRightAngle;
private TextField txtSideA, txtSideB, txtSideC;
private Button btnCM, btnM, btnKM;
double[] angles = new double[3];
double radians;
public void init() {
setLayout(null);
setupGUIComponents();
final int WIDTH = 470, HEIGHT = 220;
setSize(WIDTH, HEIGHT);
}
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
lblAngles.setText("Three angles are "+angles[0]+"degree, "+angles[1]+"degree and "+angles[2]+"degree");
lblArea.setText("Area of the triangle is "+command);
lblRightAngle.setText("It is a "+command+" triangle");
}
private void setupGUIComponents() {
// initiaze the components
lblInstructions = new Label("Enter the length of the sides of a triangle");
lblSideA = new Label("a"); txtSideA = new TextField(5);
lblSideB = new Label("b"); txtSideB = new TextField(5);
lblSideC = new Label("c"); txtSideC = new TextField(5);
lblSelect = new Label("Select unit:");
btnCM = new Button("cm"); btnM = new Button("m"); btnKM = new Button("km");
lblAngles = new Label();
lblArea = new Label();
lblRightAngle = new Label();
// add action-listeners to the buttons
btnCM.addActionListener(this);
btnM.addActionListener(this);
btnKM.addActionListener(this);
// allign the components to the frame
lblInstructions.setBounds(100,10,250,20);
lblSideA.setBounds(100,35,20,20); txtSideA.setBounds(220,35,115,20);
lblSideB.setBounds(100,55,20,20); txtSideB.setBounds(220,55,115,20);
lblSideC.setBounds(100,75,20,20); txtSideC.setBounds(220,75,115,20);
lblSelect.setBounds(130,100,70,20);
btnCM.setBounds(210,100,30,20);
btnM.setBounds(245,100,30,20);
btnKM.setBounds(280,100,30,20);
lblAngles.setBounds(20,140,410,20);
lblArea.setBounds(20,165,410,20);
lblRightAngle.setBounds(20,190,410,20);
// add the components to the frame
add(lblInstructions);
add(lblSideA); add(txtSideA);
add(lblSideB); add(txtSideB);
add(lblSideC); add(txtSideC);
add(lblSelect); add(btnCM); add(btnM); add(btnKM);
add(lblAngles); add(lblArea); add(lblRightAngle);
}
private double[] getAngles(int a, int b, int c)
{
// create an array to store the 3 angles
double[] angles = new double[3];
double radians;
// cast all the int values to double
double dA=(double)a, dB=(double)b, dC=(double)c;
// angle A in radians = cos^-1 (b^2 + c^2 - a^2 / 2bc)
// and then convert the radians to degrees
radians = Math.acos((dB*dB + dC*dC - dA*dA) / (2.0*dB*dC));
angles[0] = radians * 180.0 / Math.PI;
// angle B in radians = cos^-1 (a^2 + c^2 - b^2 / 2ac)
// and then convert the radians to degrees
radians = Math.acos((dA*dA + dC*dC - dB*dB) / (2.0*dA*dC));
angles[1] = radians * 180.0 / Math.PI;
// angle C in degrees = 180 - degrees A - degrees B
angles[2] = 180.0 - angles[0] - angles[1];
// return the 3 angles
return angles;
}
}
When i try to input the numbers into the textfield....
No number came out to the output....
If anyway.... can help to complete the java code...??
Thanks
|
|

08-04-2007, 06:25 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
// <applet code="TA" width="400" height="400"></applet>
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class TA extends Applet implements ActionListener {
private Label lblSideA, lblSideB, lblSideC,
lblAngles, lblArea, lblRightAngle;
private TextField txtSideA, txtSideB, txtSideC;
NumberFormat nf;
public void init() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
setLayout(null);
setupGUIComponents();
final int WIDTH = 470, HEIGHT = 220;
setSize(WIDTH, HEIGHT);
}
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
int a = Integer.parseInt(txtSideA.getText().trim());
int b = Integer.parseInt(txtSideB.getText().trim());
int c = Integer.parseInt(txtSideC.getText().trim());
double[] angles = getAngles(a, b, c);
lblAngles.setText("Three angles are " +
nf.format(angles[0]) + " degree, " +
nf.format(angles[1]) + " degree and " +
nf.format(angles[2]) + " degree");
lblArea.setText("Area of the triangle is " +
nf.format(getArea(a, b, c)) + " " + command);
String s = isRightTriangle(angles) ? "" : "not ";
lblRightAngle.setText("It is " + s + "a right triangle");
}
private void setupGUIComponents() {
// initiaze the components
Label lblInstructions =
new Label("Enter the length of the sides of a triangle");
Label lblSideA = new Label("a"); txtSideA = new TextField(5);
Label lblSideB = new Label("b"); txtSideB = new TextField(5);
Label lblSideC = new Label("c"); txtSideC = new TextField(5);
Label lblSelect = new Label("Select unit:");
Button btnCM = new Button("cm");
Button btnM = new Button("m");
Button btnKM = new Button("km");
lblAngles = new Label();
lblArea = new Label();
lblRightAngle = new Label();
// add action-listeners to the buttons
btnCM.addActionListener(this);
btnM.addActionListener(this);
btnKM.addActionListener(this);
// allign the components to the frame
lblInstructions.setBounds(100,10,250,20);
lblSideA.setBounds(100,35,20,20); txtSideA.setBounds(220,35,115,20);
lblSideB.setBounds(100,55,20,20); txtSideB.setBounds(220,55,115,20);
lblSideC.setBounds(100,75,20,20); txtSideC.setBounds(220,75,115,20);
lblSelect.setBounds(130,100,70,20);
btnCM.setBounds(210,100,30,20);
btnM.setBounds(245,100,30,20);
btnKM.setBounds(280,100,30,20);
lblAngles.setBounds(20,140,410,20);
lblArea.setBounds(20,165,410,20);
lblRightAngle.setBounds(20,190,410,20);
// add the components to the frame
add(lblInstructions);
add(lblSideA); add(txtSideA);
add(lblSideB); add(txtSideB);
add(lblSideC); add(txtSideC);
add(lblSelect); add(btnCM); add(btnM); add(btnKM);
add(lblAngles); add(lblArea); add(lblRightAngle);
}
private double[] getAngles(int a, int b, int c) {
// create an array to store the 3 angles
double[] angles = new double[3];
double radians;
// cast all the int values to double
double dA=(double)a, dB=(double)b, dC=(double)c;
// angle A in radians = cos^-1 (b^2 + c^2 - a^2 / 2bc)
// and then convert the radians to degrees
radians = Math.acos((dB*dB + dC*dC - dA*dA) / (2.0*dB*dC));
angles[0] = radians * 180.0 / Math.PI;
// angle B in radians = cos^-1 (a^2 + c^2 - b^2 / 2ac)
// and then convert the radians to degrees
radians = Math.acos((dA*dA + dC*dC - dB*dB) / (2.0*dA*dC));
angles[1] = radians * 180.0 / Math.PI;
// angle C in degrees = 180 - degrees A - degrees B
angles[2] = 180.0 - angles[0] - angles[1];
// return the 3 angles
return angles;
}
private double getArea(int a, int b, int c) {
// Heron's formula
// s = (a + b + c)/2
// area = sqrt( s(s - a)(s - b)(s - c) )
double s = (a + b + c)/2.0;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
private boolean isRightTriangle(double[] angles) {
for(int j = 0; j < angles.length; j++) {
if(Math.abs(angles[j] - 90.0) < 0.05)
return true;
}
return false;
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|