Hi Ruth
The death certificate of John can be obtained from Scotlands People and will show a cause of death:
http://www.scotlandspeople.gov.uk/Free to register, free to search. Pay to download the certificate (very good value for money - 30 credits will buy you, for example 6 certificates).
The is no death cert for William however, as he died before 1855 (when civil registration commenced in Scotland). You may find a burial record, or a memorial inscription (even he was not buried in Scotland, he may be mentioned on a family headstone) but neither is guaranteed to tell you what he died of. Some Burial registers to indeed recall the cause of death, but no requirement for this information to be included. Nor are all burial records complete/surviving.
On Scotland's People, it looks as if there was a will, or maybe an Admon (if he died intestate). This is from the WILLS section on SP, and this is what is free for you to view:
ANDERSON WILLIAM
Date of administration: 20/03/1838
LIEUTENANT, ROYAL NAVY, RESIDING IN KIRKTOWN OF LARGO
Ref: CUPAR SHERIFF COURT SC20/50/10
Cheers
AMBLY