Question

In: Computer Science

In this lab, we will build a couple of classes, where one will be composed into...

In this lab, we will build a couple of classes, where one will be composed into the other. Work in pairs if you wish.

Problem

We saw Point.java in class last week. Do the following, one step at a time, making sure it works before you go on to the next step. Be sure to get help if you have any difficulties.

  1. You may use Point.java from last week or build a new one of your own. If you are not confident that you can build one of your own, try to build one anyway rather than using the one from last week.
  2. Build a class called Circle which uses a Point object as its center. This is where object composition is used.
  3. In this lab, you will be using three classes: Point, Circle, and UseCircle. You will not need UsePoint unless you want to test your Point using UsePoint. To compile your overall program, you can issue the javac command only once with UseCircle.java, right?
  4. In the Circle class, add the usual things such as constructor(s), getter(s), setter(s), and other methods. You should add only what is necessary for the class to work properly. Don't blindly add constructors, getters, and setters. Other methods that you will add is partly determined by what is used in UseCircle.java. Why did I say partly instead of entirely? Well, the fact that some things are not used in UseCircle.java does not necessarily mean that they should not be added to Circle. Think about it! If UseCircle is written to test all possible things that Circle should provide, then yes, but it may not be written that way. In other words, the person who designs Circle should think about what should be included in it. Think about what would be useful in different cases for someone else who will use Circle. In most cases for us, the person who designs the class X and the the person who writes UseX are the same person. But, in general that is not the case, e.g., we did not write java.util.Random but we use it.
  5. In UseCircle though, I want you to include a call to do each of the following at a minimum:
    1. Use a constructor that takes two parameters: one Point object and the radius of the circle object that you are creating.
    2. Use a getter to get the radius and then print it.
    3. Use a getter to get the center of the circle, which would be a Point object. The fact that you passed a Point object when you called the constructor naturally matches the fact that the getter should be returning a Point object. Once you get the point object, print the x and y values in it with two separate calls to the getters in Point.
    4. Use a setter to set the radius to a new value. How many parameters would the setter take?
    5. After you change the radius, get it again and print it to see if the change was done as expected.
    6. Use a method called area that computes the area of the circle and print it.
    7. Use a method called scale to enlarge or shrink the circle by a scale_factor. You will have to pass a scale factor as an argument into the method scale. How many parameters will the scale method have? If you said 2, you would be wrong. If you said 1, you would be right. Think about it! After you scale it, make sure your scale method did the right thing by reexamining the radius of the circle (use the getter method and then print the value).
    8. Write a method called translate to move the circle to a new location by passing a delta_x and delta_y to move in both x- and y-directions by that much. Use the method to translate the circle by some distance along the x and y axis. Again make sure your changes were done correctly by using getters in Point to fetch the x- and y-values of the new location. Here again you will be getting a Point object and look inside to get the x- and y-values, right?
    9. Use a method that computes the distance from the center of the circle to the origin and print it.
    10. Create a second circle object.
    11. Use a method that computes the distance between two circle objects and print the distance.
    12. Add other functionalities if you wish. Use your imagination!!!

Point.java

public class Point {
   private int x;
   private int y;
  
   public Point() {
       x = 0;
       y = 0;
   }
   public Point(int initX, int initY) {
       x = initX;
       y = initY;
   }
   public int getX() {
       return x;
   }
   public int getY() {
       return y;
   }
   public void setX(int newX) {
       x = newX;
   }
   public void setY(int newY) {
       y = newY;
   }  

Solutions

Expert Solution

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

// Point.java

public class Point {
   private int x;
   private int y;

   public Point() {
       x = 0;
       y = 0;
   }

   public Point(int initX, int initY) {
       x = initX;
       y = initY;
   }

   public int getX() {
       return x;
   }

   public int getY() {
       return y;
   }

   public void setX(int newX) {
       x = newX;
   }

   public void setY(int newY) {
       y = newY;
   }
}

==========================================

==========================================

// Circle.java

public class Circle {
   private Point center;
   private double radius;

   /**
   * @param center
   * @param radius
   */
   public Circle(Point center, double radius) {
       this.center = center;
       this.radius = radius;
   }

   /**
   * @return the center
   */
   public Point getCenter() {
       return center;
   }

   /**
   * @param center
   * the center to set
   */
   public void setCenter(Point center) {
       this.center = center;
   }

   /**
   * @return the radius
   */
   public double getRadius() {
       return radius;
   }

   /**
   * @param radius
   * the radius to set
   */
   public void setRadius(double radius) {
       this.radius = radius;
   }

   public void area() {
       double ar = Math.PI * radius * radius;
       System.out.printf("Area of the Circle :%.2f\n", ar);
   }

   public void scale(double scale_factor) {
       double sf = (scale_factor / 100) * getRadius();
       if (radius - sf >= 0) {
           this.radius -= sf;
           System.out.println("New Radius :" + getRadius());
       }

   }

   public void translate(int delta_x, int delta_y) {
center.setX(center.getX()+delta_x);
center.setY(center.getY()+delta_y);
   }
  
   public double distanceFromCenter()
   {
       double dist=Math.sqrt(Math.pow(center.getX(),2)+Math.pow(getCenter().getY(),2));
       return dist;
   }
  
   public double distanceFromCircle(Circle c)
   {
       double dist=Math.sqrt(Math.pow((center.getX()-c.getCenter().getX()),2)+Math.pow((center.getY()-c.getCenter().getY()), 2));
       return dist;
   }

}

==========================================

==========================================

// UseCircle.java

import java.util.Scanner;

public class UseCircle {

   public static void main(String[] args) {

       /*
       * Declaring variables
       */
       int x1, y1, x2, y2;
       double r1, r2;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Getting the input entered by the user
       System.out.println(":: Center ::");
       System.out.print("Enter x:");
       x1 = sc.nextInt();
       System.out.print("Enter y:");
       y1 = sc.nextInt();
       System.out.print("Enter radius:");
       r1 = sc.nextDouble();

       // Creating Point class instance
       Point center = new Point(x1, y1);
       // Creating Circle class instance
       Circle c1 = new Circle(center, r1);
       c1.area();
      
       // Getting the input entered by the user
       System.out.print("Enter new x-Coordinate:");
       int deltaX = sc.nextInt();
       System.out.print("Enter new y-coordinate:");
       int deltaY = sc.nextInt();
      
       // Calling the method
       c1.translate(deltaX, deltaY);
System.out.print("Circle New Center :("+c1.getCenter().getX()+","+c1.getCenter().getY()+")");
  
   // Getting the input entered by the user
       System.out.println("\n:: Center ::");
       System.out.print("Enter x:");
       x2 = sc.nextInt();
       System.out.print("Enter y:");
       y2 = sc.nextInt();
      
       System.out.print("Enter radius:");
       r2 = sc.nextDouble();
  
       // Creating Point class instance
       Point center2 = new Point(x2, y2);
      
       // Creating Circle class instance
       Circle c2 = new Circle(center2, r2);
      
       System.out.println("Distance between two circles :"+c1.distanceFromCircle(c2));
   }

}

==========================================

==========================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
We have a couple of leases, one that we record as a liability and one that...
We have a couple of leases, one that we record as a liability and one that we do not. How will this new lease requirement impact us? They also talked about adopting early. I may be interested in early adoption. What would we need to do to implement this change this year? Upon investigation of Victory's records, you found that Victory had had two leases. One that is currently being accounted for as a capital lease and one being treated...
Internal Resistance of a battery lab I'm doing this lab where we have to find the...
Internal Resistance of a battery lab I'm doing this lab where we have to find the internal resistance of a charged and uncharged battery. We got our data by plotting a V vs I graph, and found the internal resistance of each battery using the slopes of the graphs. From my graphs, I can see that the internal resistance of the charged battery is higher than that of the uncharged one. Which makes sense. However, I'm having a hard time...
One of your friends has missed a couple of consolidations classes due to illness. She was...
One of your friends has missed a couple of consolidations classes due to illness. She was reading through the professor’s PowerPoint slides and was confused as to why the appreciation of the Canadian dollar (relative to the US dollar) can result in a foreign exchange gain in one case and a foreign exchange loss in another case. Briefly describe what these cases are (i.e. temporal/current rate method) and why one case gives rise to a foreign exchange gain while the...
The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
Hello! We just had a lab where we took measurements of a water-ethanol mix with a...
Hello! We just had a lab where we took measurements of a water-ethanol mix with a densitometer and got volume of ethanol/(total volume of ethanol and water together) and I am stuck on how to convert that to mole fraction of ethanol? Thank you so much!
Heat of Vaporization Questions We did a lab where we placed a graduated cylinder upside down...
Heat of Vaporization Questions We did a lab where we placed a graduated cylinder upside down in a beaker to observe the air bubble and how it was affected by temperature. There are a few questions I need help with: 1. You have assumed the vapor pressure of water below 5 degrees Celsius to be negligible. How would the inclusion of its actual vapor pressure affect your results? 2. Assume the graph of ln(P) versus 1/T results in a curved...
-What are Serial dilutions? how to do one, why we do one IN THE LAB and...
-What are Serial dilutions? how to do one, why we do one IN THE LAB and the proper procedure. -EXPLAIN what a titer is and how to determine it. -How to determine the concentration based on the dilution.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT