Question

In: Computer Science

WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c...

WRITE IN JAVA

1) Create a constructor that can create objects in the following way: c = Circle(X, Y, R);

2) Please add a constructor that only accepts R value and initializes the Circle at the origin.

3) Please implement a constructor that behaves the same way as the default constructor.

4) Add a method named “touches” to the Circle class which receives another Circle instance and checks whether these two circles touch each other externally and only at one point. The method should return a Boolean value and will be called like this: c1.touches(c2) //returns either true or false.

5) Please implement a Test class with main method that simulates each of the following
cases and prints their outcome to the screen.

Solutions

Expert Solution

Circle.java :

public class Circle {
    
    private int x,y,radius;
    
    // Constructor with no parameter (this Constructor will behave as default Constructor)
    public Circle() {
        
    }
    
    // Constructor with one parameter
    public Circle(int R) {
        /*
        R: radius of circle (integer value)
        initialize radius with provided radius and center as origin (x=0 and y=0) 
        */
        radius = R;
        x = 0;
        y = 0;
    }
    
    // Constructor with three parameter
    public Circle(int X,int Y,int R) {
        radius = R;
        x = X;
        y = Y;
    }
    // method to  check if two circle touches externally at one point or not.
    public boolean touches(Circle c) {
        /*
        two circle touches each other if distance between center of circles is equals to sum of radius of circles
        formula for distance between two point = sqrt((x1-x2)^2 + (y1-y2)^2)
        sqrt denotes square root.
        */
        double centerDistance = Math.sqrt(Math.pow((getX() - c.getX()),2) + Math.pow((getY() - c.getY()),2));
        int radiusSum = getRadius() + c.getRadius(); // sum of radius
        
        /*
        check if centerDistance and radiusSum is equal or not if yes then return true
        otherwise return false.
        */
        if(centerDistance == radiusSum) {
            return true;
        }
        return false;
    }
    
    /*
    getter methods for instance variables x , y and radius
    */
    
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public int getRadius() {
        return radius;
    }
}

Main.java (Test class with main method):-

public class Main
{
        public static void main(String[] args) {
            
            // creating two Circle object c1 and c2
                Circle c1 = new Circle(2,4,6);
                Circle c2 = new Circle(1,2,4);
                // Checking touches method of Circle class
                System.out.println("touches Circle(2,4,6) and Circle(1,2,4): ? "+c1.touches(c2));
                
                // creating two Circle object c3 and c4
                Circle c3 = new Circle(12,9,10);
                Circle c4 = new Circle(0,0,5);
        // Checking touches method of Circle class
                System.out.println("touches Circle(12,9,10) and Circle(0,0,5): ? "+c3.touches(c4));
        }
}

Sample Output :

Please refer to the Screenshot of the code given below to understand indentation of the code.

Screenshot of Circle.java

Screenshot of Main.java :-


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
Q.We design classes to create objects in java. Write step wise procedure for (a).How these objects...
Q.We design classes to create objects in java. Write step wise procedure for (a).How these objects created in memory? (b). Which area of memory is used for creation of the objects? (c). What is difference between stack and heap memory in java?
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the...
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the Rectangles takes an array of Rectangle objects. You can assume the array is filled Provide these methods An averageArea method that returns the average area of the Rectangle objects in the array. Only divide one time A method swapMaxAndMin which swaps the Rectangle with the largest area with the one with the smallest area in the array. Only use one loop A method toString...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor...
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor of the class Customer that takes the name and purchase amount of the customer as inputs. • Write getter methods getName and getPrice to return the name and price of the customer. You can write a toString() method which returns printed string of Customer name and its purchase. • (Step-2): Create a class called Restaurant. • Write a method addsale that takes customer name...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
How to identify when to use super() in constructor for java language in a easy way?
How to identify when to use super() in constructor for java language in a easy way?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT