Practical Exercise 3 Integer and Real Variables Introduces the init() method int (integer) variables and basic integer operations double (real) variables basic double operations g.drawString used to print out integer and real values whole number variables – other integer types are byte, short long but int is most used real numbers are those with a fractional or decimal part – the other real type is float but double is most commonly used import java.applet.Applet; import java.awt.*; // Prac3 demonstrates the use of integer and real variables in a Java Applet public class Prac3 extends Applet { int double n, m, sum, diff, prod, quot, remainder; //global declaration of int variables x =13.5, y = 2.2, total, differ, mult, quotient; // global declaration of double //variables // variables can be assigned a value here too public void init() { int localSum; n = 25; m = 6; localSum = n + m; sum = localSum; diff = n - m; prod = n*m; quot = n/m; remainder = n%m; total = x + y; differ = x – y; mult = x * y; quotient = x / y; } // End of init //local declaration of an int variable. //initialise values of n and m. //These calculations could be carried out in paint() or in init. //Because the variables have been declared globally, the values //can be transferred between the methods paint() and init(). // Calculations involving double variables public void paint (Graphics g) { g.drawString("The original values are " + n + " and " + m, 25, 25); g.drawString("The sum is " + sum, 25, 50); g.drawString("The difference is " + diff, 25, 75); g.drawString("The product is " + prod, 25, 100); g.drawString("The quotient is " + quot, 25, 125); g.drawString("The remainder is " + remainder, 25, 150); } // End of paint } // End of Prac3 Rosny College 2009 Page 1 of 3 Computer Science Practical Exercise 3 Integer and Real Variables Variables Variables are spaces in computer memory which store things such as numbers and characters. When you declare a variable you tell the class what type of variable it is (eg integer or real number) and give it a name. The class then sets aside the appropriate memory space for the variable, so that you can use this throughout the class. To declare an integer variable you use a line such as: int n; //for one integer variable int n, m, sum; //for a number of different integer variables. double x, y, total; // for a number of different double variables Variable names should contain letters and digits, and (by convention) start with a lower case letter. A global declaration (immediately after the main class definition) means that all the “methods” of the class can use these variables and their values. A local declaration (within a particular method) means that the variable is only available inside that method. The init() method Like paint(), this is another standard applet method. It is used by the programmer to set up the initial conditions of a class. In this case it gives values to all of the variables that will be printed out in the paint() method. Breaking up the class into different methods in this way makes the class much easier to manage and more versatile. Basic integer and real operations Assignment: n = 4; x = 3.5; Sum: sum = 4 + 7; sum = n + 4; Product: prod = n * 4; Integer division: quot = n / 4; // assigns the value 4 to int variable n (Note = means "becomes equal to" ) // assigns the value 3.5 to the double variable x // the variable sum is assigned the value 4 + 7 // variable sum becomes n + 4 where n is another integer variable. // variable prod is assigned the value n * 4 // eg 7 / 4 = 1; 13 / 4 = 3 so / gives the whole number part of the result of // division Integer remainder or “modulus”: rem = n % 4; //eg 7 % 4 = 3; 13 % 4 = 1 so % gives the remainder after dividing 2 integers Real division: answer = 5.6 / 4 // the answer will be 1.4 as you would expect with normal division. Writing the integer or real values to the applet window The paint() method can use a line such as: g.drawString("The sum is " + sum, 25, 50); to write out the value of the variable. Note that to use drawString in this manner you must “concatenate” the integer to a string using the + operator. Rosny College 2009 Page 2 of 3 Computer Science Practical Exercise 3 Integer and Real Variables Practical Exercise C Standard 1 Type out the above class in a new project called Prac3. B Standard In questions 3 and 4, you might wish to declare new variable names for each part of the expression or simply write the complete expression in the g.drawString line of code. You will need to put the complete expression in an extra set of brackets if you use the g.drawString line of code. If not you will get some strange results for your output. Can you work out why? 2 Modify this class to evaluate and print out the following integer calculations: (n + m) / (n - m) (n + m) * (n – m) 3 n+n/m n/m+n n / (m + n) Modify this class to evaluate and print out the following double calculations: (x + y) / (x - y) (x + y) * (x – y) x+x/y x/y+x x / (y + x) A Standard 4 You will notice that the results from your real calculations with variables of the type double don’t give you the answer you would expect – you have inexact decimal answers. To avoid this problem you can use what is called precision formatting (which will round your answer to a specified number of decimal places). You will need to import another library: import java.text.DecimalFormat; Create a variable of the DecimalFormat type: DecimalFormat precision2; Specify what the precision is that you wish to use (in init()) precision2 = new DecimalFormat("#0.00"); Now use this precision in a g.drawString line of code in the following way: g.drawString("x * y = " + precision2.format(mult) 10, 350); where mult is the variable to be rounded correct to 2 decimal places (in this case) Write 2 extra g.drawString lines to round your earlier x * y and x / y calculations. Rosny College 2009 Page 3 of 3