Create an application that allows the user to enter the lengths for the three sides of a triangle as integers. Your application should then deter- mine whether the triangle is a right triangle (two sides of the triangle form a 90-degree angle), an equilateral triangle (all sides of equal length) or neither. Your application’s GUI is completed for you (Fig. 18.39). You must create a class to represent a triangle object and declare the createJButtonActionPerformed method.
a) Copying the template to your working directory. Copy the C:Examples Tutorial18ExercisesTriangleCreator directory to your C:SimplyJava direc- tory.
b) Opening the template files. Open the TriangleCreator.java and Triangle.java
files in your text editor.
c) Declaring variables. View the Triangle.java file and declare three int variables (side1, side2, side3) starting on line 6 to hold the length of each side. They should all be declared private so that only the methods of this class can access them.
d) Declaring the necessary get and set methods. After the instance variable declarations declare a constructor that will take the lengths of the three sides of a triangle as argu- ments. Your constructor should always set instance variables by using corresponding set methods. Following the constructor, create three pairs of get and set methods that enable clients to access and modify the lengths of the three sides. If the user enters a negative value, that side should be assigned the
```
1 // TriangleCreator.java
2 // This application determines types of triangles, based on user
3 // input.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class TriangleCreator extends JFrame
9 {
10 // JLabel and JTextArea for side1
11 private JLabel side1JLabel;
12 private JTextField side1JTextField;
13
14 // JLabel and JTextArea for side2
15 private JLabel side2JLabel;
16 private JTextField side2JTextField;
17
18 // JLabel and JTextArea for side3
19 private JLabel side3JLabel;
20 private JTextField side3JTextField;
21
22 // JTextField for displaying message
23 private JTextField messageJTextField;
24
25 // JButton to create triangle
26 private JButton createJButton;
27
28 // no-argument constructor
29 public TriangleCreator()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // get content pane for attaching GUI components
38 Container contentPane = getContentPane();
39
40 // enable explicit positioning of GUI components
41 contentPane.setLayout( null );
42
43 // set up side1JLabel
44 side1JLabel = new JLabel();
45 side1JLabel.setBounds( 16, 16, 40, 24 );
46 side1JLabel.setText( "Side1:" );
47 contentPane.add( side1JLabel );
48
49 // set up side1JTextField
50 side1JTextField = new JTextField();
51 side1JTextField.setBounds( 72, 16, 90, 24 );
52 side1JTextField.setHorizontalAlignment( JTextField.RIGHT );
53 contentPane.add( side1JTextField );
54
55 // set up side2JLabel
56 side2JLabel = new JLabel();
57 side2JLabel.setBounds( 16, 56, 40, 24 );
58 side2JLabel.setText( "Side2:" );
59 contentPane.add( side2JLabel );
60
61 // set up side2JTextField
62 side2JTextField = new JTextField();
63 side2JTextField.setBounds( 72, 56, 90, 24 );
64 side2JTextField.setHorizontalAlignment( JTextField.RIGHT );
65 contentPane.add( side2JTextField );
66
67 // set up side3JLabel
68 side3JLabel = new JLabel();
69 side3JLabel.setBounds( 16, 96, 40, 24 );
70 side3JLabel.setText( "Side3:" );
71 contentPane.add( side3JLabel );
72
73 // set up side3JTextField
74 side3JTextField = new JTextField();
75 side3JTextField.setBounds( 72, 96, 90, 24 );
76 side3JTextField.setHorizontalAlignment( JTextField.RIGHT );
77 contentPane.add( side3JTextField );
78
79 // set up messageJTextField
80 messageJTextField = new JTextField();
81 messageJTextField.setBounds( 16, 140, 252, 24 );
82 messageJTextField.setEditable( false );
83 contentPane.add( messageJTextField );
84
85 // set up createJButton
86 createJButton = new JButton();
87 createJButton.setBounds( 178, 16, 90, 24 );
88 createJButton.setText( "Create" );
89 contentPane.add( createJButton );
90 createJButton.addActionListener(
91
92 new ActionListener() // anonymous inner class
93 {
94 // event handler called when createJButton is pressed
95 public void actionPerformed( ActionEvent event )
96 {
97 createJButtonActionPerformed( event );
98 }
99
100 } // end anonymous inner class
101
102 ); // end call to addActionListener
103
104 // set properties of application’s window
105 setTitle( "Triangle Creator" ); // set title bar string
106 setSize( 290, 208 ); // set window size
107 setVisible( true ); // display window
108
109 } // end method createUserInterface
110
111 // get user input and display correct text
112 private void createJButtonActionPerformed( ActionEvent event )
113 {
114 // get user input
115 int side1 = Integer.parseInt( side1JTextField.getText() );
116 int side2 = Integer.parseInt( side2JTextField.getText() );
117 int side3 = Integer.parseInt( side3JTextField.getText() );
118
119 // add user input to a Triangle object
120 Triangle triangle = new Triangle( side1, side2, side3 );
121
122 // if user creates an equilateral triangle
123 if ( triangle.isEquilateral() )
124 {
125 messageJTextField.setText(
126 "You created an equilateral triangle!" );
127 }
128 // if user creates a right triangle
129 else if ( triangle.isRightTriangle() )
130 {
131 messageJTextField.setText(
132 "You created a right triangle!" );
133 }
134 // if neither right nor equilateral
135 else
136 {
137 messageJTextField.setText( "Neither an equilateral nor a " +
138 + "right triangle!" );
139 }
140
141 } // end method createJButtonActionPerformed
142
143 // main method
144 public static void main( String[] args )
145 {
146 TriangleCreator application = new TriangleCreator();
147 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
148
149 } // end method main
150
151 } // end class TriangleCreator
```
```
1 // Triangle.java
2 // This class holds the properties of a triangle.
3
4 public class Triangle
5 {
6 // integers for storing triangle sides
7 private int side1;
8 private int side2;
9 private int side3;
10
11 // Triangle constructor, three sides supplied
12 public Triangle( int side1, int side2, int side3 )
13 {
14 setSide1( side1 );
15 setSide2( side2 );
16 setSide3( side3 );
17
18 } // end constructor
19
20 //
21 public int getSide1()
22 {
23 return side1;
24
25 } // end method getSide1
26
27 //
28 public void setSide1( int side )
29 {
30 if ( side < 0 )
31 {
32 side1 = 0;
33 }
34 else
35 {
36 side1 = side;
37 }
38
39 } // end method setSide1
40
41 //
42 public int getSide2()
43 {
44 return side2;
45
46 } // end method getSide2
47
48 //
49 public void setSide2( int side )
50 {
51 if ( side < 0 )
52 {
53 side2 = 0;
54 }
55 else
56 {
57 side2 = side;
58 }
59
60 } // end method setSide2
61
62 //
63 public int getSide3()
64 {
65 return side3;
66
67 } // end method getSide3
68
69 //
70 public void setSide3( int side )
71 {
72 if ( side < 0 )
73 {
74 side3 = 0;
75 }
76 else
77 {
79 side3 = side;
79 }
80
81 } // end method setSide3
82
83 // return whether or not triangle is a right triangle
84 public boolean isRightTriangle()
85 {
86 // if it is a right triangle, return true
87 if ( ( Math.pow ( side1, 2 ) + Math.pow (side2, 2 ) ) ==
88 Math.pow( side3, 2 ) )
89 {
90 return true;
91 }
92 else if ( ( Math.pow ( side2, 2 ) + Math.pow (side3, 2 ) ) ==
93 Math.pow( side1, 2 ) )
94 {
95 return true;
96 }
97 else if ( ( Math.pow ( side3, 2 ) + Math.pow (side1, 2 ) ) ==
98 Math.pow( side2, 2 ) )
99 {
100 return true;
101 }
102 else // it is not a right triangle
103 {
104 return false;
105 }
106
107 } // end method isRightTriangle
108
109 // return whether or not triangle is equilateral
110 public boolean isEquilateral()
111 {
112 // determine if the sides are equivalent
113 if ( ( side1 == side2 ) && ( side2 == side3 ) &&
114 ( side3 == side1 ) )
115 {
116 return true;
117 }
118 else // it is not equilateral
119 {
120 return false;
121 }
122
123 } // end method isEquilateral
124
125 } // end class Triangle
```
You might also like to view...
Is the measure null-invariant?
Consider the interestingness measure, M = P (B|A)?P (B)
1?P (B) , for an association
rule A ?? B.
A cell located in the third row and forth column is named _____.
A. 3D B. C4 C. D3 D. 4C