Question

In: Computer Science

................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...

................................................

................................................

This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference.

…………………………...……..

…………………………...…….

Instructions

LAB5 Instructions

Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width.

Your submission to BB should be your “Rectangle.java” and “CompareUsingequalsMethod.java” files only, not included in a .zip file or a .gpj file.

..............................................................

.............................................................

QC5

Circle.java

/**circle class*/
public class Circle{
//fields
private int radius;
//constructor
public Circle(int radius){
this.radius = radius;
  
}
//methods
public int getRadius() {
return radius;
}
public boolean equals(Circle inC1, Circle inC2){
if(inC1.getRadius() == inC2.getRadius())
return true;
else
return false;
  
}
}

…………………………………………….

…………………………………………………………..

CompareUsingequalsMethod.java

/**Demos using the equals method*/
public class CompareUsingequalsMethod{
public static void main(String[] args){
Circle circle1 = new Circle(3);
Circle circle2 = new Circle(3);
Circle circle3 = new Circle(5);
if (circle1.equals(circle1,circle2)) {
System.out.println(" Circle1 with radius " + circle1.getRadius() +
" is equal to circle2 with radius " + circle2.getRadius());

}
else{
System.out.println(" circle1 with radius " + circle1.getRadius() + " Radius is not equal to circle2 with radius " + circle2.getRadius());
  
}
if(circle3.equals(circle3,circle2)) {
System.out.println(" Circle3 with radius" + circle3.getRadius() +
" is equal to circle2 with radius" + circle2.getRadius());
}
else{
System.out.println(" circle3 with radius " + circle3.getRadius() + " Radius is not equal to circle2 with radius " + circle2.getRadius());
  

}
}
}

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Rectangle.java

public class Rectangle {
   //Declaring instance variables
   private double length;
   private double width;

   //Parameterized constructor
   public Rectangle(double length, double width) {
       this.length = length;
       this.width = width;
   }

   // getters and setters
   public double getLength() {
       return length;
   }

   public void setLength(double length) {
       this.length = length;
   }

   public double getWidth() {
       return width;
   }

   public void setWidth(double width) {
       this.width = width;
   }

   public double area() {
       return length * width;
   }

   //This method will compare two Rectangle Objects
   public boolean equals(Rectangle r1, Rectangle r2) {
       if (r1.area() == r2.area())
           return true;
       else
           return false;

   }

   //toString method is used to display the contents of an object inside it
   public String toString() {
       return "Rectangle : Length=" + length + ", Width=" + width;
   }
  

}
______________________

// CompareUsingequalsMethod.java

public class CompareUsingequalsMethod {

   public static void main(String[] args) {
       //Creating an Instance of Rectangle class obejcts
       Rectangle r1 = new Rectangle(3, 4);
       Rectangle r2 = new Rectangle(2, 6);
      
       //Comparing two Rectangle class Objects
       if (r1.equals(r1, r2)) {
           System.out.println("Rectangle#1 with area " + r1.area()
                   + " is equal to Rectangle#2 with area " + r2.area());
       } else {
           System.out.println("Rectangle#1 with area " + r1.area()
                   + " is not equal to Rectangle#2 with area " + r2.area());
       }
   }

}
_____________________________

Output:

Rectangle#1 with area 12.0 is equal to Rectangle#2 with area 12.0

_______________Could you plz rate me well.Thank You


Related Solutions

JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart or another diagramming tool. If you can’t find a diagramming tool, you can hand draw the diagram but make sure it is legible. Points to remember: • Include all classes on your diagram. There are nine of them. • Include all the properties and methods on your diagram. • Include the access modifiers on the diagram. + for public, - for private, ~ for...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an...
Programming assignment (75 pts): The Lab 1 development assignment was largely an exercise in completing an already started implementation. The Lab 2 development assignment will call on you to implement a program from scratch. It’s an exercise in learning more about Java basics, core Java Classes and Class/ method-level modularity. Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented. NumericAnalyzer will accept a list of 1 or more numbers as command line arguments. These numeric...
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two...
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two dice consisting of 6 sides are rolled. Write a program that simulates this. The user will be asked if he/she wishes to continue the roll of dice. If the answer is, “Y” or “y”, then your application will continue to roll the two dice. Each roll of the dice must occur 6 times, each time the user agrees to continue (see sample output). Use...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions. Please keep all of your code in one file for this week. We will separate...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT