Question: In Class Exercise 6 < RectangleClass > // Use the previous version of < RectangleClass > and // < RectangleMain > in files for this

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

to pass length and width to this new class named

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 "class" will be the "class" or Retangles // And it can be "refered" to by the other Classes -ie

// 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

// 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 as object of type RectangleClass RectangleClass w= new RectangleClass(); // null //System.out.println("x," +x + " y," +y+" w "+w); // NOW , amd 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 y.length= 5; y.width=7; // instance values of lenght, width for // they are already declared a in the RectangleClass System.out.println("length of = "+ x.length + ", width of = "+ x.width); System.out.println("length of = " + y.length +", width of = "+ y.width); // The area of rectangle , areax=x.getArea(); // calls method in System.out.println("Area of rectangle ="+ areax); areay=y.getArea(); // calls method in System.out.println("Area of rectangle ="+ areay); // circumference of rectangle and circuf =x.Cercurference(); System.out.println("Circufrance rectangle ="+ circuf); circuf =y.Cercurference(); System.out.println("circumference of rectangle ="+ circuf);

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

} // 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 class

public class StudentClass {

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

People p1,p2,p3; // objects of the 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

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

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

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!