Question
In_this_question, you_are_to_complete_the_definition_of_a_Java_class_called_Car (which_exists_in_the_model_package). The_Car_class_has_two_fields, posX_and_posY, that_remember_the_position_of_a_car_object_created_from_the_Car_class; actually,_posX_and_posY_reflects_the_top-left_coordinates_of_the_car_object. We_can_move_the_car_object_by_calling_the_moveCar_method_and_providing_increments (deltaX_and_deltaY), which_are_added_to_the_posX_and_posY_position,_respectively. For_example, once_you_finish_the_definition_of_the_class_car_and_create_an_object_from_it, we_can_move_the_car_object_p_50_increments_in_the_x_position_and_70_increments_in_y_position_using: p.carMove(50,70); A_car_object_has_to_exist_in_some_coordinate_system. In_this_question, a_car_can_move_in_a_window_where_the_top_left_point_of_the_window_is_given_by_coordinates_(10,35)_and_the_bottom_right_of_the_rectangle_is_given_by_coordinates_(390,440). When_a_car_object_is_first_created,_it_is_positioned_at_coordinates_(200,_200)_of_the_window. When_a_car_object_is_moved,_it_cannot_move_outside_this_given_window_defined_by_(10,35)_and_(390,440). Instead, you_can_imagine_it_bumping_on_the_sides_of_the_rectangle_if_it_is_asked_to_move_increments_that_would_take_it_out_of_the_window's_coordinate_space. As_a_result, for_instance, no_matter_how_much_you_try_to_move_the_car_to_the_right, its_posX_coordinate_can_never_be_more_than_390
In_this_question, you_are_to_complete_the_definition_of_a_Java_class_called_Car (which_exists_in_the_model_package). The_Car_class_has_two_fields, posX_and_posY, that_remember_the_position_of_a_car_object_created_from_the_Car_class; actually,_posX_and_posY_reflects_the_top-left_coordinates_of_the_car_object. We_can_move_the_car_object_by_calling_the_moveCar_method_and_providing_increments (deltaX_and_deltaY), which_are_added_to_the_posX_and_posY_position,_respectively.
For_example, once_you_finish_the_definition_of_the_class_car_and_create_an_object_from_it, we_can_move_the_car_object_p_50_increments_in_the_x_position_and_70_increments_in_y_position_using:
p.carMove(50,70);
A_car_object_has_to_exist_in_some_coordinate_system. In_this_question, a_car_can_move_in_a_window_where_the_top_left_point_of_the_window_is_given_by_coordinates_(10,35)_and_the_bottom_right_of_the_rectangle_is_given_by_coordinates_(390,440). When_a_car_object_is_first_created,_it_is_positioned_at_coordinates_(200,_200)_of_the_window.
When_a_car_object_is_moved,_it_cannot_move_outside_this_given_window_defined_by_(10,35)_and_(390,440). Instead, you_can_imagine_it_bumping_on_the_sides_of_the_rectangle_if_it_is_asked_to_move_increments_that_would_take_it_out_of_the_window's_coordinate_space. As_a_result, for_instance, no_matter_how_much_you_try_to_move_the_car_to_the_right, its_posX_coordinate_can_never_be_more_than_390 (we_are_taking_into_account_the_width_of_the_car_since_posX_is_the_top-right_corner_of_the_car).
Your_job_is_to_fill_in_this_partial_definition_of_the_class_and_create_a_car_object_such_that_it_behaves_exactly_as_described_above.
Please, help me to complete the codes below (Where its written Write your code here)
Main.java
package ui;
import javax.swing.*;
//need to import the car class to be able to use it
import model.Car;
public class Main {
public static void main(String[] args) throws InterruptedException {
//need_to_create_a_new_object_of_the_class_car_and_call_it_p
WRITE YOUR CODE HERE
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setVisible(true);
Thread.sleep(3000);
//need to move the car. call the method called moverCar and use different deltaX and deltaY values
WRITE YOUR CODE HERE
}
}
package model;
import javax.swing.*;
import java.awt.*;
public class Car extends JFrame {
public int posX;
public int posY;
public Car() {
posX=200;
posY=200;
setTitle("Assign 1");
setSize(500,500);
}
public void moveCar(int deltaX, int deltaY){
//insert_code_here_to_implement_the_logic_for_moving_the_car
//the_new_x_position_and_y_position_should_be_incremented_by_deltaX_and_deltaY_respectively
//make_sure_to_check_boundary_values_as_described_in_the_assign
WRITE YOUR CODE HERE
//Do not change this statament
repaint();
}
public void paint(Graphics g){
g.clearRect(0, 0, 500, 500);
g.setColor(Color.blue);
g.drawRect(posX,posY,100,50);
g.setColor(Color.orange);
g.fillRect(posX,posY,100,50);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started