CECS 174                                 QUIZ #4 SAMPLE

 

  1. Use nested for loops to display the following pattern:

      AAAAA

      AAAA

      AAA

      AA

      A

      Answer

         for(int i = 5; i >=1; i--)

      {              for(int j = 1; j <=i; j++)

               cout <<"A";

        cout<<endl;

       }

 

2.  [6 points] Use nested for loops to display the following pattern:

A

AA

AAA

AAAA

AAAAA
    

 Answer

                     for(int i = 1; i <=5; i++)

                   {             for(int j = 1; j <=i; j++)

                                      cout <<"A";

                                         cout<<endl;

                   }

     

  1. a. Fill in blanks to complete the following method called calculateArea defined in the class Turtle.
    The method calculateArea has two arguments to hold the length and width of a rectangle. It calculates and returns the rectangle area.

    public  _____   calculateArea(________, ________)
    {
        int Area;
        Area = _______________________;
        return ____________;
    }

    Answer

    public   int   calculateArea(int  length, int width)
    {
        int Area;
        Area = length * width;
        return Area;
    }



     

     b. Assume that an object called t of class Turtle has created in the main method, write Java statement(s) to call the 
         method calculateArea and display the area.

        Answer

   int Area;

  Area =t.calculateArea(10,5);
  System..out.println(Area);



 

  1. a. Fill in blanks to complete the following method called calculateArea defined in the class Turtle.
    The method calculateArea has two arguments to hold the length and width of a rectangle. It calculates and displays the area.
    Answer

    public   void  calculateArea(int  length, int width)
    {
        int Area;
        Area = length * width;
        System..out.println(Area);
    }


    b. Assume that an object called t of class Turtle has created in the main method, write Java statement(s) to call the 
         method calculateArea and display the area.
       Answer

       t.calculateArea(10,5);