Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2007
    Posts
    2

    Unanswered: help in pl/sql program

    hi
    i am trying to solve this in oracle
    but having some trouble
    1. The triangle table has three columns, side_a, side_b and side_c. These represent the three sides of a triangle. Write a function or procedure in a package that will read all rows out of this table and output to a different table of your own creation whether the values contained form an equilateral triangle, an isosceles triangle or a scalene triangle, based on:
    An equilateral triangle is a triangle with all three sides of equal length.
    An isosceles triangle is a triangle with any two sides of equal length.
    A scalene triangle is a triangle with no two sides equal.

    2. In a separate package, create a function or procedure to adequately test your code by populating values to the triangle table, executing the code in step 1, reading the output from the output table and validating the output to the expected values. Your test code should essentially assert that three particular values result in some expected output. Your tests should not contain their own logic except to determine whether or not your assertion matches what your program outputs.

    so far i was able to do the first part
    i need help in second part

    thanks in advance

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Do you understand what you need to do? Is everything clear? If not, spend some more time thinking about the problem until you - in your mind or on a piece of a paper - know what to do.

    How is another problem. As you managed to code first part of an assignment, I believe you are able to do the second part as well. So, did you start coding? What have you done so far? What problems did you encounter? Are those logical (i.e. you have a required package/function/whatever which compiles but the result is not adequate), or did you get a error message (ORA-xxxxx, for example) which prevents you to test it?

    It would help if you post what you have done so far (including CREATE TABLE and INSERT INTO sample data scripts) so that people who might help you do not have to start from a scratch.

  3. #3
    Join Date
    Apr 2007
    Posts
    2
    i worked till here

    Create table triangle (side_a number, side_b number, side_c number);
    commit;
    Insert into triangle (side_a,side_b,side_c) values (3,3,3);
    Insert into triangle (side_a,side_b,side_c) values (13,13,19);
    Insert into triangle (side_a,side_b,side_c) values (13,14,15);
    Insert into triangle (side_a,side_b,side_c) values (3,30,45);
    Insert into triangle (side_a,side_b,side_c) values (6,6,6);
    Insert into triangle (side_a,side_b,side_c) values (8,8,14);
    Create table triangle1 (a1 number, b1 number, c1 number,d1 varchar2(10));

    Create package triangle as

    Procedure Addsides ( side_a IN number, side_b IN number, side_c number)
    End triangle ;

    Create or replace package body triangle as
    Procedure sides (side_a number, side_b number, side_c number, d varchar2)
    Is
    CREATE OR REPLACE Procedure sides
    Is
    Cursor c_traingle is
    Select side_a,side_b,side_c from triangle;
    p varchar2(40);
    Begin
    For rec in c_traingle
    loop
    if (rec.side_a = rec.side_b and rec.side_a = rec.side_c and rec.side_b = rec.side_c)
    then
    p := 'Equilate';

    elsif (rec.side_a = rec.side_b or rec.side_b = rec.side_c or rec.side_a = rec.side_c)
    then
    p := 'Isoscel';

    elsif (rec.side_a + rec.side_b > rec.side_c or rec.side_a + rec.side_c >rec.side_b or rec.side_b + rec.side_c >rec. side_a )
    then
    p := 'Scalen';
    end if ;


    Insert into triangle1 values(rec.side_a, rec.side_b, rec.side_c, p);

    dbms_output.put_line(p);
    end loop;

    commit;
    end sides;

    i am having trouble in doing part 2

  4. #4
    Join Date
    Dec 2003
    Posts
    1,074
    Code:
    Create or replace package body triangle as
      Procedure sides ( side_a IN number, side_b IN number, side_c number) 
      Is
      Cursor c_traingle is
      ...
      end sides;
    end triangle;
    Looks like you've got some syntax errors. THe "sides" procedure declaration in the BODY should match the declaration in the SPEC exactly, down to the use of the words "IN". And you need to add an END your PACKAGE BODY. There may be a couple of others, but it looks like you have a good start.

    Also, you may want to consider COMMITTING outside of the procedure, possibly as a second step in your calling program.
    -=Chuck

  5. #5
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    If table name is called a 'triangle', no other object can have the same name; therefore, rename package and package body (or a table).

    Do you use SQL*Plus as a tool? After you try to create a package or its body, you'll get an error message saying that, for example, "Warning: PACKAGE BODY created with compilation errors." To see what errors Oracle complains about, type SHOW ERRORS at the SQL*Plus prompt. Go step by step and fix those errors.

    After everything compiles correctly, test it. Finally, when testing it done, it will be the moment when you can say "OK, part 1 is finished".

    You still didn't do that - code you provided is full of errors. Fix them first, then come back, post working code and we'll see what next. Saying that "you are having trouble in doing part 2" implies that you'd like some of us do your job. Well, it doesn't work that way. The right way is the one you started: tried it and requested help. Now, when we told you what is wrong, take another step. Good luck!

  6. #6
    Join Date
    Nov 2002
    Posts
    272
    I would normalize a bit and create a table tSides (TriangleID, Length), which gets three rows for each triangle. A Count() of distinct Lengths per triangle would return 1, 2 or 3, corresponding with equilateral, isosceles and scalene respectively.

  7. #7
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    Code:
    CREATE OR REPLACE PACKAGE SCAN_SIDES AS
    
       PROCEDURE SIDES;
    END;
    /
    CREATE OR REPLACE Procedure sides
    Is
      Cursor c_traingle is
      Select side_a,side_b,side_c from triangle;
    p varchar2(40);
    Begin
    For PNT in c_traingle loop
      IF PNT.side_a = PNT.side_b and PNT.side_b = PNT.side_c then
        P := 'Equilate';
      ELSIF PNT.side_a <> PNT.side_b and PNT.side_a <> PNT.side_c and PNT.side_b <> PNT.side_c then
        P := 'Scalen';
      ELSE
        P := 'Isoscel';
      END IF;
     Insert into triangle1 values(PNT.side_a, PNT.side_b, PNT.side_c, p);
    end loop;
    commit;
    end sides;
    /
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  8. #8
    Join Date
    Dec 2003
    Posts
    1,074
    I was thinking, this may not fit your assignment, or you could just place this inside a FUNCTION, but you should be able to do all of this inside SQL

    Code:
    insert into triangle1
    select side_a,
           side_b,
           side_c,
          (CASE 
             WHEN (side_a = side_b and side_a = side_c and side_b = side_c)
               then 'Equilate'
             WHEN (side_a = side_b or side_b = side_c or side_a = side_c)
               then 'Isoscel'
             WHEN (((side_a + side_b) > side_c) or ((side_a + side_c) > side_b) or ((side_b + side_c) >rec. side_a) )
               then 'Scalen'
             ELSE
               then 'Something Else'
           END) as triangle_type
    from triangle
    ---=Chuck

  9. #9
    Join Date
    Jun 2007
    Posts
    1
    can anyone send me the code for these questions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •