In: Computer Science
Write a java program that asks the user for the starting X and Y position of a mouse cursor as well as the starting X and Y movement, then prints out its position until X < 0 or X >100
Example 2:
Enter the starting X position: 60
Enter the starting Y position: 60
Enter the starting X velocity: 4.7
Enter the starting Y velocity: 1
X:60 Y:60
X:64.7 Y:61
X:69.4 Y:63
X:74.1 Y:65
X:78.8 Y:67
X:83.5 Y:69
X:88.2 Y:71
X:92.9 Y:73
X:97.6 Y:75
X:102.3 Y:77
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new java program with name "CursorMovement.java" is created, which contains following code.
CursorMovement.java :
//import package
import java.util.*;
//java class
public class CursorMovement {
//entry point of program , main() method
public static void main(String[] args) {
//creating object of Scanner
class
Scanner sc=new
Scanner(System.in);
//asking user starting X
position
System.out.print("Enter the
starting X position: ");
double x=sc.nextDouble();//reading
x position
//asking user starting y
position
System.out.print("Enter the
starting Y position: ");
double y=sc.nextDouble();//reading
y position
//asking user starting x
velocity
System.out.print("Enter the
starting X velocity: ");
double
velocityX=sc.nextDouble();//reading X velocity
//asking user starting y
velocity
System.out.print("Enter the
starting Y velocity: ");
double
velocityY=sc.nextDouble();//reading Y velocity
//using while loop
while(x>0 &&
x<=100)
{
//print X and Y
position
System.out.printf("X:%.1f",x);
System.out.print(" ");//used for space
System.out.printf("Y:%.1f",y);
System.out.println();//used for new line
x=x+velocityX;//increment position by adding velocity
y=y+velocityY;//increment position by adding velocity
}
System.out.printf("X:%.1f",x);
System.out.print(" ");//used for
space
System.out.printf("Y:%.1f",y);
}
}
======================================================
Output : Compile and Run CursorMovement.java to get the screen as shown below
Screen 1 :CursorMovement.java
Screen 2 :
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.