Results 1 to 3 of 3
Thread: How to check for validation?
- 09-30-2012, 02:05 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 5
- Rep Power
- 0
How to check for validation?
This is the assignment I have, and I managed to complete it to an extent but some trouble and unsure about the method that I applied.
c)(a) Write a Java class InvalidValueException. Objects of this class, when created, will
show a simple message informing you of the nature of the exception thrown.
(b) Given three straight lines a, b & c. They will be able to form a triangle provided that sum
of any two lines is always greater than the third line (i.e. a + b > c and b + c > a and a + c> b).
Write a Java class Triangle with the following:
Attributes:
length of the three sides of the triangle
Behaviour:
- Constructor that sets the length of the three sides to the values passed in. The
constructor should throw an InvalidValueException object when the values
are not able to form a triangle.
- findArea() method to calculate the area of the Triangle object using the
formula (some formula lets just say a+b+c)

So these are the three classes that I have presently
Triangle.java (the part b of the question)
InavlidValueException.java (the part a of the question)
Traingle.java (This is the GUI class as for part c)
Triangle.java
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ict201_qn3; /** * * @author jackandjill */ public class Triangle { private double side_a, side_b, side_c,result; public Triangle(double side_a, double side_b, double side_c) { this.side_a = side_a; this.side_b = side_b; this.side_c = side_c; calculateArea(side_a,side_b,side_c); } public double getsSide_A() { return side_a; } public void setsSide_A(double side_a) { this.side_a = side_a; } public double getsSide_B() { return side_b; } public void setsSide_B(double side_b) { this.side_b = side_b; } public double getsSide_C() { return side_c; } public void setsSide_C(double side_c) { this.side_c = side_c; } public double calculateArea(double a, double b, double c) { double s; s = ((a+b+c)/2); result = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return result; } // findArea() // { // // } // //public void Calculate }
Traingle.java
InvalidValueException.javaJava Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ict201_qn3; /** * * @author bharath */ import javax.swing.*; public class Traingle extends javax.swing.JFrame { /** * Creates new form Triangle */ public Traingle() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); side1_tb = new javax.swing.JTextField(); side3_tb = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); result_lbl = new javax.swing.JLabel(); side2_tb = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Bharath"); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Area of Triangle"); jLabel1.setToolTipText("label"); jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jLabel1.setName("title_lbl"); jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jLabel2.setText("Side 1:"); jLabel2.setName("s1_lbl"); jLabel3.setText("Side 2:"); jLabel3.setName("s1_lbl"); jLabel4.setText("Side 3:"); jLabel4.setName("s1_lbl"); side1_tb.setText("25.3"); side1_tb.setName("side1_tb"); // NOI18N side1_tb.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { side1_tbActionPerformed(evt); } }); side3_tb.setText("53"); side3_tb.setName("side3_tb"); // NOI18N jButton1.setText("Calculate Area"); jButton1.setName("ca_btn"); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); result_lbl.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); result_lbl.setName("result_lbl"); // NOI18N side2_tb.setText("26.0"); side2_tb.setName("side2_tb"); // NOI18N javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(result_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(108, 108, 108) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(side1_tb, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(side2_tb, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(6, 6, 6)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(side3_tb, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(88, 88, 88) .addComponent(jLabel1)))) .addContainerGap(99, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(38, 38, 38) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(side1_tb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(28, 28, 28) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(side2_tb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(29, 29, 29) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(side3_tb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(result_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 20, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> private void side1_tbActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JOptionPane.showMessageDialog(null, "hello"); Triangle actual_triangle = new Triangle(Double.parseDouble(side1_tb.getText()),Double.parseDouble(side1_tb.getText()),Double.parseDouble(side1_tb.getText())); result_lbl.setText(Double.toString(actual_triangle.calculateArea(Double.parseDouble(side1_tb.getText()),Double.parseDouble(side1_tb.getText()),Double.parseDouble(side1_tb.getText())))); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Traingle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Traingle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Traingle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Traingle.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Traingle().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JLabel result_lbl; private javax.swing.JTextField side1_tb; private javax.swing.JTextField side2_tb; private javax.swing.JTextField side3_tb; // End of variables declaration }
Problem 1Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ict201_qn3; /** * * @author jackandjill */ public class InvalidValueException { String message; double a_side, b_side,c_side,result; public String checkvalidation(double a_side, double b_side, double c_side) { if((a_side + b_side > c_side)||(a_side + c_side > b_side)||(c_side + b_side > a_side)) { //calculateArea(a_side,b_side,c_side); message= " "; } // else if(a_side) // { // // } else { message = "The values you entered cannot form a triangle"; } return message; } }
So now my issue is I am able to get the result fine, but the checking for validation in the InvalidValueException class, how to pass parameters from the Triangle constructor to the InvalidValueException class to check and return back the control to Triangle class?
Problem 2
How to generate error message if the text enetered is not a double value OR any of the textfield is left empty (all this should be checked in the InvalidValueException class and passed back control to the Triangle class. Tks for the help.
- 09-30-2012, 02:22 PM #2
Re: How to check for validation?
Moved here, nothing to do with applets.
If you don't understand my response, don't ignore it, ask a question.
- 09-30-2012, 03:13 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Validation
By Johnny2009 in forum New To JavaReplies: 2Last Post: 09-23-2012, 11:11 PM -
XML validation
By Onra in forum New To JavaReplies: 0Last Post: 03-24-2011, 06:14 PM -
XML Validation
By sehudson in forum XMLReplies: 5Last Post: 03-21-2011, 12:38 PM -
Check validation of the Regex
By itaipee in forum New To JavaReplies: 4Last Post: 05-26-2009, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks