Practical Exercise 3 – Integers and reals This prac demonstrates how to work with integers (positive and negative whole numbers, as well as zero) and reals (what we would normally think of when we talk about numbers, these include rational and irrational numbers). import java.applet.Applet; import java.awt.*; public class Prac3 extends Applet { // global declarations of int and double variables int n, m, sum, diff, prod, quot, remainder; double x, y, sum_d, diff_d, prod_d, quot_d; public void init() { setSize(600,300); n = 25; m = 6; diff = n - m; prod = n*m; quot = n/m; remainder = n%m; x = 13.5; y = 2.2; sum_d = x + y; diff_d = x - y; prod_d = x * y; quot_d = x / y; } public void paint (Graphics g) { g.drawString("The integer 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); g.drawString("The g.drawString("The g.drawString("The g.drawString("The g.drawString("The real values are " + x + " and " + y, 325, 25); sum is " + sum_d, 325, 50); difference is " + diff_d, 325, 75); product is " + prod_d, 325, 100); quotient is " + quot_d, 325, 125); } } Claremont College 2015, adapted from Rosny College 2009 Page 1 of 4 Variables Variables are spaces in computer memory that store things such as numbers and characters. When you declare a variable you tell the class what type of value 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. When dealing with numerical values, we commonly use int or double. int – typically a 32-bit signed two’s complement integer. Other integer types are byte (8bit), short (16-bit) and long (64-bit). double – 64-bit floating point. Another real type is float (32-bit) that can be used to save memory, but double is usually the default option. To declare an int or double variable you use a line such as: int n; int n, m, sum; double x, y, total; //for one integer variable //for a number of different integer variables. // for a number of different double variables Variable names may contain letters and digits, and (by convention) start with a lower case letter, and are case-sensitive. E.g. total and TOTAL refer to different variables in Java. 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. Basic integer and real operations Assignment n = 4; x = 3.5; // Note “=” means "becomes equal to” Sum sum = 4 + 7; sum = n + 4; Product prod = n * 4; Integer division quot = n / 4; // eg. 7/4 => 1; 13/4 => 3 Integer remainder or “modulus” (often pronounced “mod”) result = n % 4; // the remainder after division eg. 7%4 => 3; 13%4 => 1 Real division result = 5.6 / 4 // the result is 1.4 Claremont College 2015, adapted from Rosny College 2009 Page 2 of 4 Writing the integer or real values to the applet window To write out the value of the variable use a line such as the following in the paint() method: g.drawString("The sum is " + sum, 25, 50); Note that the drawString() method displays a text string in the Applet. To display the value of a numerical variable you must convert it to a string. The easiest way is to “concatenate” it to a string using the + operator as has been done in the line above. C Standard Copy and run the above class. Modify this class to evaluate and print out the results of the following integer calculations: (n + m) / (n - m) n+n/m (n + m) * (n – m) n/m+n n / (m + n) B Standard Modify the class to evaluate and print out the results of the following double calculations: (x + y) / (x - y) x+x/y (x + y) * (x – y) x/y+x Claremont College 2015, adapted from Rosny College 2009 x / (y + x) Page 3 of 4 A Standard You may notice that the results from your calculations with variables of the type double don’t always 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 two extra g.drawString lines to round the results of the initial x * y and x / y calculations. Claremont College 2015, adapted from Rosny College 2009 Page 4 of 4