Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


S.J.S. tutorial 6: class variables

SECTION(6, Tutorial 6) m5_question(BO(Question 6.1:) Study, compile and run the following code. Note the use of the EM(class variable) TT(myMoney). A class variable is different from a variable that is local to a function because the lifetime of the class variable is for the duration that the program is run, whereas the lifetime of a local variable is for the duration of the function call. In the code that follows, the variable TT(myMoney) is used to store a numerical value, for how much money you have. m4_begin_indent NU()CLASS CLSS(Money) EOL BEGIN EOL PD SCOM(/** Property myMoney stores money value in dollars */) EOL PD CLASS_VAR INT myMoney; EOL EOL PD FUNCTION VOID FUNC(spend)(STRING VARI(item), INT VARI(value)) EOL PD BEGIN EOL PD PD myMoney = myMoney - value; EOL PD PD SYSTEM_OUT_PRINTLN(STRI("*** spent DOLLAR") + EOL PD PD PD PD PD PD PD PD value + EOL PD PD PD PD PD PD PD PD STRI(" on ") + item + EOL PD PD PD PD PD PD PD PD STRI("COMMA() leaving you with DOLLAR") + myMoney); EOL PD PD END EOL PD END EOL PD BEGIN_MAIN EOL PD PD myMoney = 100; EOL PD PD spend(STRI("aquarium"),NUMB(50)); EOL PD PD spend(STRI("shoes"),NUMB(100)); EOL PD PD spend(STRI("lipstick"),NUMB(20)); EOL PD END_MAIN EOL END EOL m4_end_indent ) m5_question(BO(Question 6.2:) Change the TT(myMoney) class variable so that it is a EM(double) (short for double-precision floating point) rather than an EM(int). You will need to add a new function TT(money2string) that converts double values into strings. For example the floating point number 1.2345 should be printed out as DOLLAR()1.23. If TT(x) is a double then the following expression converts TT(x) from a double into a number of dollars TT((INT)x) and the following expression converts TT(x) into a number of cents TT((int)(money * 100) - 100 * dollars). Note that you will need to make it so that DOLLAR(1.03) prints out as this value.) m5_question(BO(Question 6.3:) Add an EM(if) statement to the TT(spend) function so that it uses SYSTEM_OUT_PRINTLN to print out an error message if the person does not have enough funds in their bank account to pay for the item parameter.) m5_question(BO(Question 6.4:) Add a new class variable TT(double governmentsMoney) and make it so that 12.5% of the cost of each item goes to the government in the form of GST.) m5_question(BO(Question 6.5:) Add a new class variable TT(numBattleships) that records how many batteships are owned by the government. Write a function TT(buyBattleShips) that causes the government to buy as many battleships as it can afford. Make it so that the TT(buyBattleShips) function prints out how many battleships were purchased. Let the cost of each battleship be one million dollars and store this value in a variable called TT(costOfShip). Please note that if the government's money is less the one million dollars then no battleships will be purchased.) m5_question(BO(Question 6.6:) Set the initial value for TT(governmentsMoney) to be two millions dollars, then call the TT(buyBattleShips) function and verify that two battleships were purchased.)
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:26:46 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.