90.9k views
5 votes
Coding Problem please review my work!

Part 1: Design a Class
You’ll design a class named Car that has the following fields:

yearModel—An Integer that holds the car’s year model
make—A String that holds the make of the car
speed—An Integer that holds the car’s current speed
The class should have the following constructor and other methods:

The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field.
Design appropriate accessor methods to get the values stored in an object’s yearModel, make, and speed fields.
The accelerate method should add 5 to the speed field each time it’s called.
The brake method should subtract 5 from the speed field each time it’s called.

My coding ( it's in pseudocode!)
Class Car
Private Interger yearModel
Private String Make
Private Interger Speed

//Constructor
Public Module Car (Interger y, String m, Interger s)
Set yearModel = y
Set Make = m
Set Speed = s
End Module

//Mutators
Public module setYearModel (Interger y)
Set yearModel = y
End Module

Public module setMake (String m)
Set Make = m
End Module

Public module setSpeed (Interger s)
Set Speed = s
End Module

//Accesors
Public Function Interger getYearModel()
Return yearModel
End Function

Public Function String getMake()
Return make
End Function

Public Function Interger getSpeed()
Return speed
End Function

//Accelerate
Public Module accelerate()
set speed = speed + 5
End Module

//Brakes
Public Module brake()
set speed = speed - 5
End Module

End Class

Part 2: Design a Program

You’ll create both pseudocode and a flowchart to design a program that creates a Car object and then calls the accelerate method five times.

After each call to the accelerate method, get the current speed of the car and display it.
Then, call the brake method five times. After each call to the brake method, get the current speed of the car and display it. Take a screenshot of the results after your fifth time calling the method.

My coding ( it's in Pseudocode!)
Module Main()
Call accelerate(5)
End Module

Module accelerate(Interger times)
If times > 0 Then
Display " The car has increased its speed by 5"
Display "The cars current speed is, 'speed'.
Call accelerate (times - 1)End if
End Module
Module Main()
Call brake(5)
End Module

Module brake (Interger times)
If times > 0 Then
Display " The cars brake has been Increased by 5"
Display " The cars current brake is, 'brake'.
Call brake(times - 1)
End If
End Module

1 Answer

2 votes

Answer:

Try this!!!

Step-by-step explanation:

Class Car

Private Integer yearModel

Private String make

Private Integer speed

// Constructor

Public Module Car(Integer y, String m)

Set yearModel = y

Set make = m

Set speed = 0

End Module

// Accessors

Public Function Integer getYearModel()

Return yearModel

End Function

Public Function String getMake()

Return make

End Function

Public Function Integer getSpeed()

Return speed

End Function

// Mutators

Public Module accelerate()

Set speed = speed + 5

End Module

Public Module brake()

Set speed = speed - 5

End Module

End Class

// Main program

Module Main()

// Create a new car object

Set car = new Car(2022, "Tesla")

sql :

// Call the accelerate method five times and display the current speed each time

Display "Accelerating..."

Repeat 5 times

Call car.accelerate()

Display "Current speed: " + car.getSpeed()

End Repeat

// Call the brake method five times and display the current speed each time

Display "Braking..."

Repeat 5 times

Call car.brake()

Display "Current speed: " + car.getSpeed()

End Repeat

rust:

START

-> Create a new Car object with year model 2022 and make Tesla

-> Display "Accelerating..."

-> Repeat 5 times:

-> Call the car's accelerate method

-> Display "Current speed: " + car.getSpeed()

-> Display "Braking..."

-> Repeat 5 times:

-> Call the car's brake method

-> Display "Current speed: " + car.getSpeed()

END

User Bsplosion
by
8.2k points