Jump to content

menios

Members
  • Posts

    55
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

menios's Achievements

Member

Member (2/5)

0

Reputation

  1. Hey guys. I hope someone can help me on this very irritating problem i cant seem to solve. Heres the brief of what i have to do: Design and implement a program that accepts three integer values from the user and then evaluates them to see if they would correspond to an equilateral, isosceles, or scalene triangle. (An equilateral triangle has three sides of equal length, an isosceles triangle has two sides of equal length, and a scalene triangle has no sides of equal length.) The result of the evaluation is then revealed to the user. *Note - you got to make use of the javax.swing class JOptionPane for all user interaction. two classes (in two files): the first being a “MyApplication” class that contains a main() method which instantiates an instance of a Triangle; and a second, separate class “Triangle”, that encapsulates the methods and data of a Triangle. Ok so basically from this, i have come up with this: package Triangle; /* * MyApplication.java */ import javax.swing.JOptionPane; public class MyApplication { /** Creates a new instance of myApplication */ public MyApplication() { } public static void main(String[] args ) { String message = "The Triangle Problem"; JOptionPane.showMessageDialog( null, message ); // obtain three integer values representing side lengths int[ ] intArray = new int[ 3 ]; for( int i = 0; i < intArray.length; i++ ) { message = "Enter an integer value for triangle side " + ( i + 1 ) + ":"; // gather integer value representing side here… String input = JOptionPane.showInputDialog(null, message ); intArray[i] = Integer.parseInt(input); } // construct and evaluate the triangle Triangle Triangle = new Triangle(intArray[0], intArray[1], intArray[2]); String outcome = Triangle.evaluate( ); intArray[0] = Side1; //or s1,s2,s3 don't work either here intArray[1] = Side2; intArray[2] = Side3; Triangle Triangle = new Triangle(intArray); Triangle.evaluate(); // present the outcome to the user JOptionPane.showMessageDialog( null, outcome ); } } & package Triangle; /** * Triangle.java */ import javax.swing.JOptionPane; public class Triangle { private static int Side1, Side2, Side3; public Triangle (int s1 ,int s2, int s3) { Side1 = s1; Side2 = s2; Side3 = s3; } private static void acceptThreeIntegers( ) { String strSide1 = JOptionPane.showInputDialog( "Enter side 1: "); Side1 = Integer.parseInt( strSide1 ); String strSide2 = JOptionPane.showInputDialog( "Enter Side 2: "); Side2 = Integer.parseInt( strSide2 ); String strSide3 = JOptionPane.showInputDialog( "Enter Side 3: "); Side3 = Integer.parseInt( strSide3 ); } private static void useMethod( ) { if( checkForNegatives( ) == false ) { if (checkForTriangle() == true) { String result = evaluate( ); JOptionPane.showMessageDialog( null, result ); } else JOptionPane.showMessageDialog( null, "Impossible", "Results", JOptionPane.PLAIN_MESSAGE ); } else JOptionPane.showMessageDialog( null, "Impossible", "Results", JOptionPane.PLAIN_MESSAGE ); } private static boolean checkForNegatives( ) { if (Side1 <=0 || Side2 <=0 || Side3 <=0) return true; else return false; } /** evaluate if a triangle is physically possible*/ private static boolean checkForTriangle( ) { if (Side1 + Side2 - Side3 <=0 || Side1 + Side3 - Side2 <=0 || Side2 + Side3 - Side1 <=0) return false; else return true; } public static String evaluate( ) { if (Side1 == Side2 && Side2 == Side3) { return "Equilateral"; } if (Side1 == Side2 && Side2 != Side3 || Side1 == Side3 && Side2 != Side3 || Side2 == Side3 && Side1 != Side2) { return "Isosceles "; } if (Side1 != Side2 && Side2 != Side3 || Side1 != Side2 && Side1 != Side3) { return "Scalene"; } return "test value"; } public static void main (String[] args) { acceptThreeIntegers( ); useMethod( ); } public static void Triangle (int[]intArray) { Side1 = intArray[0] ; Side2 = intArray[1] ; Side3 = intArray[2] ; } } I m not good at Java so im not 100% sure on setting the values into an array and then passing it to the triangle class would work. I tried something like this: intArray[0] = side1 intArray[1] = side2 etc...then call the triangle using New Triangle = triangle1(intArray) then Triangle.evaluate(). but this didnt work. I seem to have problems with the constructor i think. So yes, please can someone help me out, ive been trying and trying and i really wanna get this to work. Many thanks
  2. I thought it wouldn't be so safe. Thanks
  3. I m building a computer for a friend of mine with an intel 2 duo E2160 on gigabyte (ga-p31-ds3l) motherboard 2 gig ddr2 ocz memory i happen to have a basic anteq 350W psu. My question is the psu enough or should i use a minimum 550w psu? thanks in advance
  4. The main idea was to write the program in C then debugg it and use the debugger to create the assembler code.But i script it straight in assembly.And still i can't create the receiver:(
  5. I m trying to develop a point to point transfer system between 2 computers in assembly Ive managed to script the transmiter but im having difficulties with the receiver Any pointers? I can provide the code for the transmitter if it helps
  6. I have a simple cart that works with sessions when checkout is clicked the user is prompted with a form to fill with his details. I managed to do the validation and to insert the data from the form but how can i extract the items froms my session and link them with my form?
  7. This is the Code for my cart.All works fine but one details spoils everything and i can't figure the mistake.When a new item is added to the cart there are 2 buttons next to it add and remove.All fine here but when i try to add a second item in the cart the add and remove buttons appear but my add button actually works as a remove. Any suggestions on what i m doing wrong? This is my functions [hr]<?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>Your Cart is Empty</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>Your Cart contains <a href="cart.php">'.count($items).' book'.$s.' </a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table border="0">'; $output[] = '<tr>'; $output[] = '<th bgcolor=#62aac4><strong>Book</strong></th>'; $output[] = '<th bgcolor=#62aac4>Price</th>'; $output[] = '<th bgcolor=#62aac4>Quantity</th>'; $output[] = '<th bgcolor=#62aac4 colspan=3> </th>'; $output[] = '</tr>'; foreach ($contents as $pd_id=>$qty) { $sql = 'SELECT * FROM tbl_product WHERE pd_id = '.$pd_id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td align=center><strong>'.$pd_name.'</strong></td>'; $output[] = '<td align=center>£'.$pd_price.'</td>'; $output[] = '<td align=right ><input type="text" name="qty'.$pd_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td align=right><i>£'.($pd_price * $qty).'</i></td>'; $total += $pd_price * $qty; $output[] = '<td align=center><button type="submit">Add</button></td>'; $output[] = '</form>'; $output[] = '<form action="cart.php?action=delete&pd_id='.$pd_id.'" method="post" id="cart">'; $output[] = '<td align=center><button type="submit" class="r">Remove</button></td>'; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p><pre><strong><i> Total: <big></i>£'.$total.'</strong></big></pre></p>'; $output[] = '</form>'; $output[] = '<pre> <a href="testform/forma.php" target="_top">Checkout</a></pre>'; } else { $output[] = '<p>Your Cart is empty.</p>'; } return join('',$output); } ?> And this is the cart [hr]<?php // Include MySQL class require_once('inc/mysql.class.php'); // Include database connection require_once('inc/cartcon.php'); // Include functions require_once('inc/functions.inc.php'); // Start the session session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['pd_id']; } else { $cart = $_GET['pd_id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['pd_id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $pd_id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$pd_id; } else { $newcart = $pd_id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?> <html> <head> <title></title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div id="contents"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); echo showCart(); ?> <p><a href="iframe.html" target="display">Continue Shopping</a></p> </div> </body> </html>
  8. well i need to validate my page before submiting it to the database The question is how to submit it after vlidating it? The code for the form quite simple <?php // Create an empty array to hold the error messages. $arrErrors = array(); $pattern = "/^[A-z0-9\._-]+" . "@" . "[A-z0-9][A-z0-9-]*" . "(\.[A-z0-9_-]+)*" . "\.([A-z]{2,6})$/"; //Only validate if the Submit button was clicked. if (!empty($_POST['Submit'])) { // Each time there's an error, add an error message to the error array // using the field name as the key. if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['firstname'])) $arrErrors['fname']= 'Please provide your First Name.'; if (!preg_match("/^[a-zA-Z\-\'\ ]+$/u", $_POST['lastname'])) $arrErrors['lname']= 'Please provide your Last Name.'; if (!preg_match("/^[\w\ \+\-\'\"]+$/",$_POST['address'])) $arrErrors['address_format']= 'Please provide your Address.'; if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['town'])) $arrErrors['town']= 'Please provide your Town.'; if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['county'])) $arrErrors['county']= 'Please provide your County.'; if (!preg_match ("/^[a-zA-Z0-9_]{6}$/", $_POST['postcode'])) $arrErrors['postcode']= 'Please provide your Post Code.'; if (!preg_match ("/^[0-9_]{10}$/", $_POST['phone'])) $arrErrors['phone']= 'Please provide your Phone Number.'; if (!preg_match($pattern, $_POST['email'])) $arrErrors['email_format'] = 'A valid email format is required.'; if (count($arrErrors) == 0) { // If the error array is empty, there were no errors. // Insert form processing here. } else { // The error array had something in it. There was an error. // Start adding error text to an error string. $strError = '<div class="formerror1"><p><h4><img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p></h4>'; } } ?> <?php echo $strError; ?> <!-- For every form field, we do the following... Check to see if there's an error message for this form field. If there is, add the formerror class to the surrounding paragraph block. The formerror class contains the highlighted box. Insert the contents of what the user submitted bak into the form field. Check again to see if this field has an error message. If it does, show the error icon and the error message next to the field. --> <form method="post" action="<?php echo $PHP_SELF; ?>" onreset="return confirm('Do you want to reset the form?')"> <tr ><td width="20%"> </td><td ><p<?php if (!empty($arrErrors['fname'])) echo ' class="formerror"'; ?>> <label for="firstname">First Name:</label> <input name="firstname" type="text" id="firstname" size="30" maxlength="50" value="<?php echo $_POST['firstname'] ?>"> <?php if (!empty($arrErrors['fname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['fname'].'</span>'; ?></td> <td width="20%"> </td></tr> </p><br> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['lname'])) echo ' class="formerror"'; ?>> <label for="lastname">Last Name:</label> <input name="lastname" type="text" id="lastname" size="30" maxlength="50" value="<?php echo $_POST['lastname'] ?>"> <?php if (!empty($arrErrors['lname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['lname'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['address_format'])) echo ' class="formerror"'; ?>> <label for="address"> Address:</label> <input name="address" type="text" id="address" size="40" maxlength="70" value="<?php echo $_POST['address'] ?>"> <?php if (!empty($arrErrors['address_format'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['address_format'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['town'])) echo ' class="formerror"'; ?>> <label for="town"> Town:</label> <input name="town" type="text" id="town" size="30" maxlength="32" value="<?php echo $_POST['town'] ?>"> <?php if (!empty($arrErrors['town'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['town'].'</span>'; ?></td> <td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['county'])) echo ' class="formerror"'; ?>> <label for="county"> County:</label> <input name="county" type="text" id="county" size="30" maxlength="32" value="<?php echo $_POST['county'] ?>"> <?php if (!empty($arrErrors['county'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['county'].'</span>'; ?></td> <td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['postcode'])) echo ' class="formerror"'; ?>> <label for="postcode"> Post Code:</label> <input name="postcode" type="text" id="postcode"size="10" maxlength="10" value="<?php echo $_POST['postcode'] ?>"> <?php if (!empty($arrErrors['postcode'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['postcode'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; ?>> <label for="phone">Phone:</label> <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>"> <?php if (!empty($arrErrors['phone'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?></tr><td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['email_format'])) echo ' class="formerror"'; ?>> <label for="email">Email:</label> <input name="email" type="text" id="email" size="40" maxlength="80" value="<?php echo $_POST['email'] ?>"> <?php if (!empty($arrErrors['email_format'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['email_format'].'</span>'; ?></tr><td width="20%"> </td></tr> </p> <p> <tr><td colspan="2" align="right"> <input id="reset" type="reset" value="Reset"> <input type="submit" name="Submit" value="Submit"></tr></td> </p> </form> and for inserting the data to the db <?php require_once('../inc/global.inc.php'); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $conn); // MySQL DB Name $firstname=mysql_real_escape_string($_POST['firstname']); //This value has to be the same as in the HTML form file $lastname=mysql_real_escape_string($_POST['lastname']); $email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file $phone=mysql_real_escape_string($_POST['phone']); $town=mysql_real_escape_string($_POST['town']); $address=mysql_real_escape_string($_POST['address']); $postcode=mysql_real_escape_string($_POST['postcode']); $county=mysql_real_escape_string($_POST['county']); $sql="INSERT INTO tbl_order (firstname,lastname,email,phone,town,county,address,postcode) VALUES ('$firstname','$lastname','$email','$phone','$town','$county','$address','$postcode')"; /*Customers is the name of the MySQL table where the form data will be saved.*/ if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error()); } echo "Your Details have been succesfully added to our System."; header("Refresh:200; url=index.php"); echo ("If You are not redirected automatically press here"); mysql_close($con); ?> hope this helps Thanks mate
  9. My form uses php_self to validate, i also have the query to add the content of the form to my database but how can i add it when the user hits submit and the form is validated ??? <form method="post" action="<?php echo $PHP_SELF; ?>"> code.. <input type="submit" name="Submit" value="Submit"> </form>
  10. In case anyone interested the code for accepting only alphabetical characters is "/^[\w\ \+\-\'\"]+$/"
  11. I ve just added it and for some reason it doesn't accept anything now..
  12. what do i need to add to stop users from entering any special symbols and accept only letters?? The syntax for characters only is "/[a-zA-Z]/" if im not mistaken. But if the user adds any of special characters (*&^#;/\) next to a letter it gets accepted. How can i stop this?
  13. I just changed this <?php if ($_POST['email']=='') $arrErrors['email'] = 'A valid email address is required.'; to this and it works so i ll do sth similar for the rest. if (!preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $_POST['email'])) $arrErrors['email_format'] = 'A valid email format is required.'; ?> Thanks for the tips :)
  14. Ohh Yes I think i m getting it now i ll try to see if i can do it Thanks pocobueno
  15. I know about these expressions maybe i wasn't clear enough,sorry how do i add them in another file? or could i integrate them to my existing code and if yes where? thanks for your patience
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.