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
// 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
// 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
// Diagonal of rectangles
} // 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
public class StudentClass {
public static void main(String[] args) { // Declare varablesCall the constructor
People p1,p2,p3; // objects of the
// 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
Get step-by-step solutions from verified subject matter experts
