I have a java question regarding get/set and overloading constructors:
The problem is my calculations are not happening. Can someone tell me what needs to be done to get the calculation to work?
Thanks in advance .... Samm

The output is:
After attempting to set to invalid Values (0 & 10) :
Area = 01x10=00
Perimeter = 2(01+10)=00
After attempting to set to invalid Values (10 & 21):
Area = 10x01=00
Perimeter = 2(10+01)=00
After attempting to set to invalid Values (99 & 99) :
Area = 01x01=00
Perimeter = 2(01+01)=00
After attempting to set to valid Values (10 & 10) :
Area = 10x10=00
Perimeter = 2(10+10)=00
BUILD SUCCESSFUL (total time: 2 seconds)

I am running 2 classes. Size2 and Size2 test. Size2 Contains the variables and methods, Size2Test outputs. Code is as follows:

Size2 :

public class Size2        // Declare Class and initialize Variables ...........5.A
{
   private int length;    // length    variable
   private int width;     // width     variable
   private int area;      // area      variable
   private int perimeter; // perimeter variable ... End Declare Variables ....10.A

// SET METHODS ... SET METHODS ... SET METHODS ... SET METHODS ...............13.B


   public void setSize( int l, int w )//......................................16.C
   {
      setLength( l );     // set the length
      setWidth( w );      // set the width
   }                      // end method setSize...............................20.C


   public void setLength( int l ) // validate and set length .................23.D
   {
      length = ( ( l >= 1 && l < 20 ) ? l : 1 );
   }                      // end validate and set length Method ..............26.D

   
   public void setWidth( int w ) // validate and set width ...................29.E
   {
      width = ( ( w >= 1 && w < 20 ) ? w : 1 );
   }                      // end setWidth method .............................32.E

   
   public void setArea()                    // Calculate area Method .........35.F
   {
      area = width * length;
   }                                    // end method Calculate area Method...38.F

   
   public void setPerimeter()                // Calculate Perimeter Method ...41.G
   {
      perimeter = (2 * ( width + length));
   }                                      // end Calculate Perimeter Method...44.G


// GET METHODS ... GET METHODS ... GET METHODS ... GET METHODS ...............47.G


   public int getLength() // Get Length Methods ..............................50.H
   {
      return length;
   }                      // end getLength Method ............................53.H

   
   public int getWidth()  // get width Method.................................56.I
   {
      return width;
   }                      // end getWidth Method .............................59.I

   
   public int getArea()                      // Get area Method ..............62.J
   {
      return area;
   }                                         // End Get area Method ..........65.J


   public int getPerimeter()                 // Get Perimeter Method .........68.K
   {
      return perimeter;
   }                                         // end Get Perimeter Method .....71.K


   public String toAreaString()           // Method that returns the Area ....74.L
   {
      return String.format(
         "Area = %02dx%02d=%02d", getLength(), getWidth(), getArea() );
   }


   public String toPerimeterString()// Method that returns the Perimeter .....81.M
   {
      return String.format(
         "Perimeter = 2(%02d+%02d)=%02d", getLength(), getWidth(), getPerimeter() );
   }                                        // end method toUniversalString...85.M


}                                           // end class Size2 ...............88.N

Size2Test :

public class Size2Test // Declare class ......................................................5.A
{
   public static void main( String[] args ) // ..............Main Begins Pgm .................7.B
   {
      Size2 size = new Size2(); // .....................invokes Size2 constructor ............9.C



      size.setLength( 0 ); // ..........................Start Invalid Length Valid Width ....11.D
      size.setWidth( 10 );
      System.out.println(
         "After attempting to set to invalid Values (0 & 10) :" );
      System.out.printf( size.toAreaString() );
      System.out.println();    // Blank line
      System.out.printf( size.toPerimeterString() ); // ...End Invalid Length Valid Width ...16.D
      System.out.println();    // Blank line



      size.setLength( 10 ); // ..........................Start Valid Length inValid Width ...19.E
      size.setWidth( 21 );
      System.out.println(
         "After attempting to set to invalid Values (10 & 21):" );
      System.out.printf( size.toAreaString() );
      System.out.println();    // Blank line
      System.out.printf( size.toPerimeterString() ); // ...End Valid Length inValid Width ...24.E
      System.out.println();    // Blank line



      size.setLength( 99 ); // ........................Start Invalid Length InValid Width ...27.F
      size.setWidth( 99 );
      System.out.println(
         "After attempting to set to invalid Values (99 & 99) :" );
      System.out.printf( size.toAreaString() );
      System.out.println();    // Blank line
      System.out.printf( size.toPerimeterString() ); // ..End Invalid Length inValid Width ..32.F
      System.out.println();    // Blank line




      size.setLength( 10 ); // ..........................Start Valid Length Valid Width .....36.G
      size.setWidth( 10 );
      System.out.println(
         "After attempting to set to   valid Values (10 & 10) :" );
      System.out.printf( size.toAreaString() );
      System.out.println();    // Blank line
      System.out.printf( size.toPerimeterString() ); // ...End Valid Length Valid Width .....41.G
      System.out.println();    // Blank line



   }                           // end main
}                              // end class Size2Test

Thanks for looking into ... found that the calculation and return should not be separated.

p

ublic int getArea()                      // Get area Method ..............62.J
   {
      area = width * length;
      return area;
   }                                         // End Get area Method ..........65.J


   public int getPerimeter()                 // Get Perimeter Method .........68.K
   {
      perimeter = (2 * ( width + length));
      return perimeter;
   }                                         // end Get Perimeter Method .....71.K
This article has been dead for over six months. Start a new discussion instead.