***This is a Java question*** Write a class called MovableCircle that implements
ID: 3606617 • Letter: #
Question
***This is a Java question***
Write a class called MovableCircle that implements the Movable interface given in Lab1 problem. This class contains three int fields x, y, and r, that represents the center and radius of the circle, and one constructor that takes three parameters indicating the initial x and y and radius of the point. Write the constructor and the four methods
Note: for the MovableCircle, the size() and area() returns the radius and area of the circle,move() method shifts the circle to new position with the specified value of x & y-coordinates given by the user and the printShape prints a string on the console “this is a circle at location: xx, yy”, where the xx, and yy are the location x and y of the circle.
Extend the given testInterface_HW class to creates a MovablePoint object or a MovableCircle object depending on the user’s input.
Sample Output:
Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.
2
Enter the x coordinate of the circle as an integer
3
Enter the y coordinate of the circle as an integer
4
Enter the radius of the circle as an integer
5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
1
You have chosen to move the graph
Enter the x coordinate that your graph needs to be moved by
4
Enter the y coordinate that your graph needs to be moved by
5
The graph has been moved by 4,5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
4
This is a circle at the location: 7,9
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
2
The size of the graph is 5.0
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
3
The area of the graph is 78.53981633974483
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
5
You have successfully exited the program
-------------------------------------------------------------------------
***This is the movable interface given in lab 1***
public interface MovableGraph { //interface MovableGraph with 4 abstarct methods
public void move(int xMov, int yMov);
public double size();
public double area();
public void printShape();
}
---------------------------------------------------------------------------------------------
import java.util.Scanner;
public class testInterface_HW {
private static MovableGraph graph;
private static Scanner input = new Scanner(System.in);
public static void displayGraphOptions() {
System.out.println("Choose from one of the choices below");
System.out.println(
"1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate");
System.out.println("2. Display the size of the graph");
System.out.println("3. Display the area of the graph");
System.out.println("4. Display the location of the graph");
System.out.println("5. Exit the program");
executeGraphOptions(input.nextInt());
}
public static void executeGraphOptions(int choice) {
switch (choice) {
case 1: {
System.out.println("You have chosen to move the graph");
System.out.println("Enter the x coordinate that your graph needs to be moved by");
int xMov = input.nextInt();
System.out.println("Enter the y coordinate that your graph needs to be moved by");
int yMov = input.nextInt();
graph.move(xMov, yMov);
System.out.println("The graph has been moved by "+xMov+","+yMov);
displayGraphOptions();
break;
}
case 2: {
System.out.println("The size of the graph is " + graph.size());
displayGraphOptions();
break;
}
case 3: {
System.out.println("The area of the graph is " + graph.area());
displayGraphOptions();
break;
}
case 4: {
graph.printShape();
displayGraphOptions();
break;
}
case 5: {
System.out.println("You have successfully exited the program");
System.exit(1);
break;
}
default: {
System.out.println("Invalid choice, Please choose again");
displayGraphOptions();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.");
int choice = input.nextInt();
if (choice == 1) {
System.out.println("Enter the x coordinate of the Point as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the Point as an integer");
int y = input.nextInt();
graph = new MovablePoint(x, y);
displayGraphOptions();
}
else if (choice == 2) {
//Add java code -- for the takehome assignment
}
}
}
Explanation / Answer
Given below is the new file MovableCircle.java and modified file testInterface_HW.java. I assume, you already have the MovablePoint.java class.
To indent code in Eclipse IDE, you can select the code in each of the java file press "Ctrl + A" and then "Ctrl + i".
The output is as follows
========================
Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.
2
Enter the x coordinate of the circle as an integer
3
Enter the y coordinate of the circle as an integer
4
Enter the radius of the circle as an integer
5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
1
You have chosen to move the graph
Enter the x coordinate that your graph needs to be moved by
4
Enter the y coordinate that your graph needs to be moved by
5
The graph has been moved by 4,5
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
4
this is a circle at location: 7, 9
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
2
The size of the graph is 5.0
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
3
The area of the graph is 78.53981633974483
Choose from one of the choices below
1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate
2. Display the size of the graph
3. Display the area of the graph
4. Display the location of the graph
5. Exit the program
5
You have successfully exited the program
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//========= testInterface_HW.java =========
import java.util.Scanner;
public class testInterface_HW {
private static MovableGraph graph;
private static Scanner input = new Scanner(System.in);
public static void displayGraphOptions() {
System.out.println("Choose from one of the choices below");
System.out.println(
"1. Move the graph by specifying the x and y coordinates, your graph will be moved by the specified x and y coordinate");
System.out.println("2. Display the size of the graph");
System.out.println("3. Display the area of the graph");
System.out.println("4. Display the location of the graph");
System.out.println("5. Exit the program");
executeGraphOptions(input.nextInt());
}
public static void executeGraphOptions(int choice) {
switch (choice) {
case 1: {
System.out.println("You have chosen to move the graph");
System.out.println("Enter the x coordinate that your graph needs to be moved by");
int xMov = input.nextInt();
System.out.println("Enter the y coordinate that your graph needs to be moved by");
int yMov = input.nextInt();
graph.move(xMov, yMov);
System.out.println("The graph has been moved by "+xMov+","+yMov);
displayGraphOptions();
break;
}
case 2: {
System.out.println("The size of the graph is " + graph.size());
displayGraphOptions();
break;
}
case 3: {
System.out.println("The area of the graph is " + graph.area());
displayGraphOptions();
break;
}
case 4: {
graph.printShape();
displayGraphOptions();
break;
}
case 5: {
System.out.println("You have successfully exited the program");
System.exit(1);
break;
}
default: {
System.out.println("Invalid choice, Please choose again");
displayGraphOptions();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter 1 for movable point and 2 for movable circle. For the Lab assignment, enter only 1.");
int choice = input.nextInt();
if (choice == 1) {
System.out.println("Enter the x coordinate of the Point as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the Point as an integer");
int y = input.nextInt();
graph = new MovablePoint(x, y);
displayGraphOptions();
}
else if (choice == 2) {
System.out.println("Enter the x coordinate of the circle as an integer");
int x = input.nextInt();
System.out.println("Enter the y coordinate of the circle as an integer");
int y = input.nextInt();
System.out.println("Enter the radius of the circle as an integer");
int r = input.nextInt();
graph = new MovableCircle(x, y, r);
displayGraphOptions();
}
}
}
//==============MovableCircle.java ================
public class MovableCircle implements MovableGraph {
private int x, y, r;
public MovableCircle(int x, int y, int r)
{
this.x = x;
this.y = y;
this.r = r;
}
@Override
public void move(int xMov, int yMov) {
this.x += xMov;
this.y += yMov;
}
@Override
public double size() {
return r;
}
@Override
public double area() {
return Math.PI * r * r;
}
@Override
public void printShape() {
System.out.println("this is a circle at location: " + x +", " + y);
}
}
/////////////////////////////
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.