Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I set the maximum and minimum depth of a mouse click? Right now, when I

ID: 3719605 • Letter: H

Question

How can I set the maximum and minimum depth of a mouse click? Right now, when I right click, it goes up 10 feet, but it also goes past zero which shows a negative number. I want it to stop at zero. And the other way around, when I left click, it goes down 10 feet, but I want it to stop at 1200. Any suggestions?

//Submarine depth

public void mouseClicked(MouseEvent event)
{
if (event.getButton() == MouseEvent.BUTTON1)
{   
current_depth=current_depth+10;
System.out.println ("You descended 10 feet. Your current depth is: " +current_depth);
}

if (event.getButton() == MouseEvent.BUTTON3)
{
current_depth=current_depth-10;
System.out.println ("You ascended 10 feet. Your current depth is: "+current_depth);
}

Explanation / Answer

You can set the maximum and minimum depth in the start and then modify the function as follows.

The changes are highlighted.

//Submarine depth

public void mouseClicked(MouseEvent event){

Max_depth = 1200;

Min_depth = 0;

if (event.getButton() == MouseEvent.BUTTON1)

{

if((current_depth+10)<=Max_depth){

current_depth= current_depth+10;

System.out.println ("You descended 10 feet. Your current depth is: " +current_depth);

}

else

{

current_depth= Max_depth;

System.out.println ("You reached maximum depth. Your current depth is: " +current_depth);

}

}

if (event.getButton() == MouseEvent.BUTTON3)

{

if((current_depth-10)>=Min_depth){

current_depth= current_depth-10;

System.out.println ("You ascended 10 feet. Your current depth is: " +current_depth);

}

else

{

current_depth= Min_depth;

System.out.println ("You reached minimum depth. Your current depth is: " +current_depth);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote