Saturday, October 20, 2018

Java Assignments and Answers

Create a class called MyPoint, which models a 2D point with x and y cordinates. It contains:

  1. Two instance variables x(int) and y(int)
  2. A "no-arguments" contructor that contruct a point at (0,0).
  3. A constructor that constructs a point with given x and y cordinates.
  4. Getter and setter for the instance variables x and y.
    • setX(int x): set the value of x coordinate by passing the value.
    • setY(int y): set the value of y coordinate by passing the value.
    • getX(): return the value of x coordinate.
    • getY(): return the value of y coordinate.
  5. A method setXY() to set both x and y.
  6. A toString() method that returns a string description of the instance in the format "(x,y)"
  7. A method called distance(int x, int y) that returns the distance as a double value from this point to another point at the given (x,y) cordinate.
  8. An overloaded distance(MyPoint p) that returns the distance as a double value from this point to the given Mypoint instance p.

Distance of two points as calculates as given below.
distance of two points= sqrt((x1-x2)2 + (y1-y2)2)


Answer
 
class MyPoint
{
 int x;
 int y;

 public MyPoint()
 {
  this.x=0;
  this.y=0;
 }

 public MyPoint(int x,int y)
 {
  this.x=x;
  this.y=y;
 }

 public void setX(int x)
 {
  this.x=x;
 }

 public void setY(int x)
 {
  this.y=y;
 }
 
 
 public int getX()
 {
  return this.x;
 }

 public int getY()
 {
  return this.y;
 }

 public void setXY(int x,int y)
 {

  this.x=x;
  this.y=y;
 }

 public String toString()
 {
  return "(" + this.x + "," + this.y + ")";
 }


 public double distance(int x,int y)
 {
   
  double d1;
  d1=((this.x - x)*(this.x - x)) + ((this.y -y)* (this.y -y));
  return Math.sqrt(d1);
 }

 public double distance(MyPoint p)
 {
   
  MyPoint p1=new MyPoint();
  p1=p;
  double d1;
  d1=(this.x - p1.getX())*(this.x - p1.getX())+ (this.y - p1.getY())*(this.y - p1.getY());
  return Math.sqrt(d1);
 }
}

Create a class called MyCircle, which models a circle with a center(x,y) and a radius.The MyCircle class uses an instance of MyPoint class (created in the previous exercise) as its center.
The class should contain:

  1. Two private instance variables center(an instance of MyPoint) and radius(int).
  2. A constructor that construct a  circle with the given center's(x,y) coordinate and radius.
  3. An overloaded constructor that constructs a MyCircle by giving a MyPoint instance as center, and radius.
  4. Various getters and setter.
    • setCenter(MyPoint p): set the center coordinates by passing the MyPoint instance as center.
    • getCenter(): return the coordinate of the center point as MyPoint
    • setRadius(int r): set the value of radius by passing the value
    • getRadius(): return the value of the radius


Answer
 
class MyCircle
{
 private MyPoint center;
 private int radious;

 public MyCircle(int x,int y,int r)
 {
  center=new MyPoint();
  center.setXY(x,y);
  this.radious=r;
 }

 public MyCircle(MyPoint p,int r)
 {
  center=new MyPoint();
  center.setXY(p.getX(),p.getY());
  this.radious=r;
 }


 public void setCenter(MyPoint p)
 {
  center.setXY(p.getX(),p.getY());
  
 }

 public MyPoint getCenter()
 {
  return center;
  
 }

 public void setRadious(int r)
 {
  this.radious=r;
  
 }

 public int getRadious()
 {
  return radious;
  
 }

 public String toString()
 {
    
  return " Circle @ (" + center.getX() + "," + center.getY() + ")  radious = " + radious;
 }

 public double getArea()
 {
  return (22/7)*radious*radious;
 }

}



Create a public class called TestMyCirlce and do the following:

  1. Create point p1 using no argument constructor.
  2. Set the X,Y coordinates
  3. Create point p2 using two arguments constructor.
  4. Display the distance between p1 and p2 points.
  5. Display the distance between p1 and given point by passing x,y coordinates of that point.
  6. Create a circle c1 using two arguments constructor. Pass the point p1 as the center.
  7. Display the area of the circle.
  8. Create a circle c2 using three arguments constructor.
  9. Display the area of the cirlce c2.


Answer
 
public class TestMyCircle
{
 public static void main(String arg[])
 {
  MyPoint p1=new MyPoint();
  p1.setXY(50,50);
  MyPoint p2=new MyPoint(100,100);
  System.out.println("Distance between p1 and p2  : " + p1.distance(p2));
 System.out.println("Distance between p1 and given point 200,200 : " + p1.distance(200,200));
  MyCircle c1=new MyCircle(p1,50);
  System.out.println("Area of the circle - two argument : " + c1.getArea());
  MyCircle c2=new MyCircle(250,250,150); 
  System.out.println("Area of the circle -three arguement : " + c2.getArea());

  
 }
}




No comments:

Post a Comment