SHould i use a switch or class

Hello matlabers,
So I have a question.
so i have 3 types of vehicles repressinting [0,1,2] i.e. 0 ='normal car', 1='emergency' and 2='pedestrain'
I want to write a program that alerts any type of those cars if another car is approaching it. eg. if emergency is at its station and pedestrain car is approacing it sends a message saying ''pedestrian car appoaching'' and the pedestrian car says "approaching emergency car".
Please help me guys thank you.

10 Comments

A switch-case would work well.
fadams18's answer moved here.
--------------------------------------------
So I did this
prompt = 'What is the ID? ';
ID = input(prompt);
switch ID
case 0
disp('Normal car approaching')
case 1
disp('emergency car approaching')
case 2
disp('pedestrain car approaching')
end
But i dont want to use an input. because in the project there is no user input. how no program it such that the alert each other once they meet.
You need to better describe what your program is doing. If there are multiple cars, all doing their own thing and broadcasting to each other their position than using OOP may be an option or you could use an array of categorical. The architecture of your code is going to depend more on what you are tracking.
Presumably the "ID" input comes from somewhere; it is the input to your function.
function announceApproachingObject(ID)
switch ID
case 0
disp('Normal car approaching')
case 1
disp('emergency car approaching')
case 2
disp('pedestrain car approaching')
end
end
Thats the problem im a noob. i dont know OOP.
there are 3 car types. normal, pedestrian, emergency. If they are driving on the street somewhere. and either of them approaches the other, they should just notify each other.
Guillaume
Guillaume on 11 Jul 2019
Edited: Guillaume on 11 Jul 2019
Again, you're explaining a small detail of the code, which is not what is going to dictate the overall structre.
What does the program do? What are the inputs and outputs of the program? How many cars (not types) is there?
What does they are driving on the street somewhere actually mean in term of your algorithm?
You could use a lookup table that returns multiple messages when provided with multiple IDs.
IDs =[1;2;3];
message = {'Normal';'Emergency';'Pedestrian'};
messageTable = table(IDs,message);
ID = [1,3]; %INPUT
messageTable.message(ismember(messageTable.IDs,ID)) %OUTPUT
@Guillame there are many cars. like a normal day in vegas, Im in my 'normal' car, moving on the streets. once i approach an emergency car like an ambulance, my car alerts me saying 'approaching an emergency car'.
Output is just an alert message stating the type of car im approaching.
Ok, there are many cars (thousands? millions?). You still haven't told us what the inputs of your program are, nor what your program does.
  • Your program could be a simulation of 1000s (millions?) of cars moving about. In which case, it needs to track all the car positions, all the time.
  • Your program could be a real-time detection program running in your own car. In which case, the structure is completely different, the inputs are just, there's a car of type X or there's no car.
  • It could be something else completely different.
@fadams18, a little bit of time spent formalizing your problem and mapping out a plan for your program will potentially save hours, days, or weeks of writing and re-writing it on a whim.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 11 Jul 2019

Commented:

on 11 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!