Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Class Exercise 6 < RectangleClass > // Use the previous version of < Rectangl

ID: 3843930 • Letter: I

Question

In Class Exercise 6 <RectangleClass>

// Use the previous version of <RectangleClass> and   //<RectangleMain> in files for this course (Assignment Class Ex,

Chapter 5 )  also see  “public class People “   a few slide back

for an example of a constructor.

// Modify <RectangleClass> to use a <constructor>

to pass length and width to this new class named

   <public class ConstrucRactangle>

// Rename <RectangleMain> to   <ConstrucRectangleMain>

// Change the “default” constructors in RectangleMain to use

// ConstrucRactangle(double length, double width) in the new class.

// Use the same numeric values for length and width(as before) to pass to the constructor in classConstrucRactangle

// All the methods in ConstrucRactangle will work the same

// When run you should get the same results as the old version //<RectangleClass> , <RectangleMain>

Upload: ConstrucRectangleMain.java, ConstrucRactangle.java

Example:


// The <RectangleClass> "class" will be the "class" or Retangles
// And it can be "refered" to by the other Classes -ie <Main>
// We will define what this RectangleClass is and what is doese
// It has both data types,
// and executable methods that deal with retangle-Area,and Diagonal
//
public class RectangleClass {
// define data types (primatives)
   public double length;
   public double width;
// Fucntions and Methiods for RectangleClass
// These are visible to all classes in the package
  
   // 1) Find the area using a methiod in this class
public double getArea(){ // this is at the class level and
return length*width;
  
   } //End method getArea
//
   // 2)The circumference of the rectangle
   //
public double Cercurference() {
double crfurn;
crfurn=2* length +2*width;
return crfurn;
   } // end method Cercurference
//
   // 3) Method for finding rectangle Diagonal
public double Diagional() {
double diag;
diag= Math.sqrt(length*length + width*width);
return diag;
   } // end method Diagional
  
} //End Class <RectangleClass>


// Rectangle example of Classes
// This RectangleMain will (use) test RectangleClass
//
public class RectangleMain{
  
   public static void main(String[] args) {
   System.out.println("Rectangle example of Classes");
       double areay; double areax; // Primative decaration for
       double diagonal;
       double circuf;
      
// Class declarations
RectangleClass x; // x is now typed as the calss "RectangleClass"
x=new RectangleClass(); // Instance x (make an instance of) for this calss
RectangleClass y= new RectangleClass();//declaring & Instance <y> as object of type RectangleClass
RectangleClass w= new RectangleClass(); // null
//System.out.println("x," +x + " y," +y+" w "+w);
// NOW <x> , <y> amd <w> are all of type RectangleClass and have an instance
// set values for length and width for each instance   
x.length= 3; x.width=4; // instance values of lenght, width for <x>
y.length= 5; y.width=7; // instance values of lenght, width for <y>
// they are already declared a <double> in the RectangleClass
System.out.println("length of <x>= "+ x.length + ", width of <x>= "+ x.width);
System.out.println("length of <y>= " + y.length +", width of <y>= "+ y.width);

// The area of rectangle <x>, <y>
areax=x.getArea(); // calls method in <RectangleClass>
System.out.println("Area of rectangle <x>="+ areax);
areay=y.getArea(); // calls method in <RectangleClass>
System.out.println("Area of rectangle <y>="+ areay);
// circumference of rectangle <x> and <y>
circuf =x.Cercurference();
System.out.println("Circufrance rectangle <x>="+ circuf);
circuf =y.Cercurference();
System.out.println("circumference of rectangle <y>="+ circuf);

// Diagonal of rectangles <x> and <y>
diagonal=x.Diagional();
System.out.println("Diagonal of rectangle <x>="+ diagonal);
diagonal=y.Diagional();
System.out.println("Diagonal of rectangle <y>="+ diagonal);
  

   } //End <Main>

} // End Class

public class People {

public String firstName;

public String secondName;

public int idNumber;

public static int numberPeople =0;//Constructor, Not a method,

for < People > classpublic People(String firstName, String secondName,int idNumber) {

this.firstName= firstName;    //Initialized to null

this.secondName= secondName;     //Initialized to null

this.idNumber=idNumber;//numberPeople++;

System.out.println(numberPeople);

} // end constructor

}

In Class Example - Call constructor in <People> class

public class StudentClass {

public static void main(String[] args) {
// Declare varablesCall the constructor

People p1,p2,p3; // objects of the <People> Class

// Call the Object constructor for each instance

p1 = new People("Peter ","Baker ",+(int)(Math.random()*1000) );

//Object instance

p2 = new People("John ","Smith ", +(int)(Math.random()*1000)); //Object instance

p3 = new People("Mary" ,"Smith ", +(int)(Math.random()*1000)); //Object instance

System.out.println(p1.firstName + p1.secondName +p1.idNumber);

System.out.println(p2.firstName + p2.secondName +p2.idNumber);

System.out.println(p3.firstName + p3.secondName +p3.idNumber);

System.out.println("number of people   "+People.numberPeople); //number people not in builder

// where are these objects of class <People>

System.out.println("p1 " +p1);System.out.println("p2 " +p2);

System.out.println("p3 " +p3);

// Re-construct just P1

//    p1 = new People("Peter ","Baker ",+(int)(Math.random()*1000) );

//construct Object

//   System.out.println(p1.firstName + p1.secondName +p1.idNumber);

// System.out.println(p2.firstName + p2.secondName +p2.idNumber);

// System.out.println(p3.firstName + p3.secondName +p3.idNumber);

// System.out.println("number of people   "+People.numberPeople);

//number People in different fil

// where are these objects of class <People>

// System.out.println("p1 " +p1);// System.out.println("p2 " +p2);

}

}

Explanation / Answer

PROGRAM CODE:

ConstrucRectangle.java

import java.util.*;
import java.lang.*;
import java.io.*;

class ConstrucRectangle {
// define data types (primatives)
public double length;
public double width;

//adding a constructor here
public ConstrucRectangle(double length, double width)
{
    this.length = length;
    this.width = width;
}
// Fucntions and Methiods for ConstrucRectangle
// These are visible to all classes in the package
  
// 1) Find the area using a methiod in this class
public double getArea(){ // this is at the class level and
return length*width;
  
} //End method getArea
//
// 2)The circumference of the rectangle
//
public double Cercurference() {
double crfurn;
crfurn=2* length +2*width;
return crfurn;
} // end method Cercurference
//
// 3) Method for finding rectangle Diagonal
public double Diagional() {
double diag;
diag= Math.sqrt(length*length + width*width);
return diag;
} // end method Diagional
  
} //End Class <ConstrucRectangle>

ConstrucRectangleMain.java

import java.util.*;
import java.lang.*;
import java.io.*;

// Rectangle example of Classes
// This ConstrucRectangleMain will (use) test ConstrucRectangle
//
class ConstrucRectangleMain{
  
public static void main(String[] args) {
System.out.println("Rectangle example of Classes");
double areay; double areax; // Primative decaration for
double diagonal;
double circuf;
  
// Class declarations
ConstrucRectangle x; // x is now typed as the calss "ConstrucRectangle"
x=new ConstrucRectangle(3, 4); // Instance x (make an instance of) for this calss
ConstrucRectangle y= new ConstrucRectangle(5, 7);//declaring & Instance <y> as object of type RectangleClass
ConstrucRectangle w= new ConstrucRectangle(3, 3); // null
//System.out.println("x," +x + " y," +y+" w "+w);
// NOW <x> , <y> amd <w> are all of type RectangleClass and have an instance
// set values for length and width for each instance   
//x.length= 3; x.width=4; // instance values of lenght, width for <x>
//y.length= 5; y.width=7; // instance values of lenght, width for <y>
// they are already declared a <double> in the RectangleClass
System.out.println("length of <x>= "+ x.length + ", width of <x>= "+ x.width);
System.out.println("length of <y>= " + y.length +", width of <y>= "+ y.width);

// The area of rectangle <x>, <y>
areax=x.getArea(); // calls method in <ConstrucRectangle>
System.out.println("Area of rectangle <x>="+ areax);
areay=y.getArea(); // calls method in <ConstrucRectangle>
System.out.println("Area of rectangle <y>="+ areay);
// circumference of rectangle <x> and <y>
circuf =x.Cercurference();
System.out.println("Circufrance rectangle <x>="+ circuf);
circuf =y.Cercurference();
System.out.println("circumference of rectangle <y>="+ circuf);

// Diagonal of rectangles <x> and <y>
diagonal=x.Diagional();
System.out.println("Diagonal of rectangle <x>="+ diagonal);
diagonal=y.Diagional();
System.out.println("Diagonal of rectangle <y>="+ diagonal);
  

} //End <Main>

} // End Class

OUTPUT: