Mutually orthogonal complementary sets (MOCSs) have many applications in practical scenarios such as synthetic aperture imaging systems, orthogonal frequency division multiplexing code division multiple access (OFDM-CDMA) systems and multicarrier code division multiple access (MC-CDMA) systems. Z-complementary code sets
(ZCCSs) will be useful if the practical situation focuses more on the set size. Most ofthe known constructions of MOCSs and ZCCSs based on generalized Boolean functions (GBFs) have lengths with the form of 2m or 2m+2t. Some constructions of MOCSs and ZCCSs based on other methods mostly have restrictive lengths. In this paper, we not only present constructions of an optimal ZCCS, but also construct MOCSs with flexible lengths. Both these constructions are based on extended Boolean functions. Though our proposed constructions generalize several previously known methods, we show that the parameters of these constructions are new and include previous parameters as special cases. In addition, a wide range of q-ary MOCSs and ZCCSs can be obtained by assigning different values to q.