In: Computer Science
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.
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;
}
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