JavaJava · Chapterبەش 4 · Lectureوانە 4.4
Arithmetic, Increment and Assignment Operatorsکارپێکەرە ژمێرەییەکان و کارپێکەرەکانی دانانی نرخ، ++ و --
Expressions and statements (and javac's not a statement); Java's five arithmetic operators, integer division (toward zero) and % with negative numbers (Math.floorMod, Math.floorDiv), numeric promotion, precedence and left-to-right order, brackets, compound assignment with its hidden cast, ++ and -- before and after a variable, and the lab tasks with their floating-point surprise.
و (هەروەها not a statementـی javac)؛ پێنج Java، (بەرەو سفر) و % لەگەڵ ژمارەی نەرێنی (Math.floorMod، Math.floorDiv)، یەکخستنی جۆری ، و ڕیزبەندیی لە چەپەوە بۆ ڕاست، کەوانە، += و هاوشێوەکانی لەگەڵ شاراوەیان، ++ و -- پێش و دوای و ئەرکەکانی تاقیگە لەگەڵ سەرسوڕمانی .
What you will learnچی فێر دەبیت
- Tell an expression from a statement, and explain javac's
not a statementerror. - Use
+ - * / %and predict integer division and remainders, also with negative numbers. - Work out expressions with precedence, left-to-right order and brackets.
- Use compound assignment and
++/--, and explain the difference betweeni++and++i.
- لە جیا بکەیتەوە و هەڵەی
not a statementـی javac ڕوون بکەیتەوە. + - * / %بەکار بهێنیت و و پێشبینی بکەیت، بە ژمارەی نەرێنیشەوە.- بە ، ڕیزبەندیی لە چەپەوە بۆ ڕاست و کەوانە بژمێریت.
+=و هاوشێوەکانی و++/--بەکار بهێنیت و جیاوازییi++و++iڕوون بکەیتەوە.
Operators and operandsکارپێکەرەکان و کارپێکراوەکان
An is a symbol that does a job with values, such as + or *. The values it works on are its . Together they make an , which Java works out to one value. Most operators take two operands, such as a + b (a ); a few take one, such as -x (a ).
هێمایەکە کە کارێک بە دەکات، وەک + یان *. کار لەسەر چەند دەکات، کە پێیان دەگوترێت . پێکەوە دروست دەکەن کە Java دەیژمێرێت بۆ یەک . زۆربەی دوو وەردەگرن، وەک a + b ()؛ هەندێکیان یەکێک، وەک -x ().
This lesson teaches the arithmetic operators, ++ and --, and the assignment operators, as Python's do in Operators and operands. Java's comparison operators (==, <, …) come with if statements and the logical ones (&&, ||, !) in Logical operators.
ئەم وانەیە ، ++ و -- و فێر دەکات، وەک ئەوانەی Python لە کارپێکەرەکان و کارپێکراوەکان. Java (==، <، …) لەگەڵ ڕستەی if دێن و (&&، ||، !) لە کارپێکەرە لۆجیکییەکان.
Expressions and statementsدەربڕینەکان و ڕستەکان
An , as you saw above, works out to one value: a * c, a + b - (a * c), even a single 5 or x. A is something else: one complete instruction that does a job: it stores a value, prints something or calls a method. In Java a simple statement ends with a semicolon. A statement often holds expressions: in int z = a + b - (a * c); the part after = is an expression, and the whole line up to the ; is the statement.
، وەک لە سەرەوە بینیت، دەبێتە یەک : a * c، a + b - (a * c)، تەنانەت تەنیا 5 یان x. شتێکی ترە: یەک فەرمانی تەواوە کە کارێک ئەنجام دەدات: هەڵدەگرێت، شتێک چاپ دەکات یان بانگ دەکات. لە Javaـدا سادە بە کۆتایی دێت. زۆرجار لەخۆ دەگرێت: لە int z = a + b - (a * c);ـدا ئەو بەشەی دوای = و هەموو دێڕەکە تا ; .
The word comes from maths, where is an expression too: numbers, a variable and operators that together express one value. Java needs every operator written out, so the same expression is 7 * x - 2 * (3 * x + 14); 2(…) without *, an , is a . For x = 5 it is -23:
ئەم وشەیە لە بیرکارییەوە هاتووە، کە لەوێ (یان بە نووسینی کوردی ٧س − ٢(٣س + ١٤)) هەر : ژمارە، و کە پێکەوە یەک دەردەبڕن. Java دەیەوێت هەموو بنووسرێت، بۆیە هەمان دەبێتە 7 * x - 2 * (3 * x + 14)؛ 2(…) بەبێ *، واتە ، . بۆ x = 5 دەکاتە -23:
public class Expressions {
public static void main(String[] args) {
int a = 6, b = 4, c = 2;
int z = a + b - (a * c); // a statement: store the value of an expression
System.out.println(z); // a statement: print it
int x = 5;
System.out.println(7 * x - 2 * (3 * x + 14)); // maths: 7x - 2(3x + 14)
}
}$ java Expressions -2 -23
Expressions and statementsدەربڕینەکان و ڕستەکان
A value nobody uses: not a statementنرخێک کە کەس بەکاری ناهێنێت: not a statement
In Java, not every expression may stand alone. Followed by a ;, an expression becomes a statement only when it does something: an assignment, ++ or --, a method call, or creating an object with new. A calculation whose value would just be lost is refused:
لە Javaـدا هەموو ناتوانێت بەتەنیا بێت. کە ;ـی لە دوا بێت تەنیا کاتێک دەبێتە کە کارێک بکات: ، ++ یان --، بانگکردنی یان دروستکردنی بە new. ژمێرکارییەک کە بەبێ سوود لەدەست دەچێت ڕەت دەکرێتەوە:
public class NotAStatement {
public static void main(String[] args) {
int price = 500;
price * 3;
}
}$ javac NotAStatement.java
NotAStatement.java:4: error: not a statement
price * 3;
^
1 errorUse the value instead: store it (total = price * 3;) or print it (System.out.println(price * 3);). Python is less strict: there a line such as price * 3 is allowed, and its value is simply forgotten (Expressions in Python). The same rule explains why ?: cannot stand alone (Choosing a value in one line).
لە جیاتی ئەوە بەکار بهێنە: هەڵیبگرە (total = price * 3;) یان چاپی بکە (System.out.println(price * 3);). Python کەمتر توندە: لەوێ دێڕێکی وەک price * 3 ڕێگەپێدراوە و بە سادەیی لە بیر دەکرێت (دەربڕینەکان لە Pythonـدا). هەمان یاسا ڕوونی دەکاتەوە بۆچی ?: ناتوانێت بەتەنیا بێت (هەڵبژاردنی نرخێک لە یەک دێڕدا).
The five arithmetic operatorsپێنج کارپێکەرە ژمێرەییەکە
Java has five arithmetic operators. Two signs of school maths change: × is * and ÷ is / (Arithmetic signs). % is not percent: it gives the remainder of a division.
Java پێنج هەیە. دوو نیشانەی بیرکاریی قوتابخانە دەگۆڕێن: × دەبێتە * و ÷ دەبێتە / (نیشانەکانی ژماردن). % لە سەدا نییە: دابەشکردن دەدات.
public class NumericOperators {
public static void main(String[] args) {
System.out.println(34 + 1); // addition
System.out.println(34.0 - 0.1); // subtraction
System.out.println(300 * 30); // multiplication
System.out.println(1.0 / 2.0); // division
System.out.println(20 % 3); // remainder
}
}$ java NumericOperators 35 33.9 9000 0.5 2
Dividing whole numbersدابەشکردنی ژمارە تەواوەکان
When both operands of / are integers, the answer is an integer too: the fraction is cut off, toward zero. 5 / 2 is 2, not 2.5, and -5 / 2 is -2, not -2.5. This is . For a real division, make at least one operand a floating-point number: 5.0 / 2 is 2.5.
کاتێک هەردوو / بن، وەڵامەکەش : دەبڕدرێتەوە، بەرەو سفر. 5 / 2 دەکاتە 2، نەک 2.5 و -5 / 2 دەکاتە -2، نەک -2.5. ئەمە ـە. بۆ دابەشکردنی ڕاستەقینە، لانیکەم یەکێک لە بکە بە : 5.0 / 2 دەکاتە 2.5.
public class IntegerDivision {
public static void main(String[] args) {
System.out.println(5 / 2); // both whole numbers
System.out.println(-5 / 2);
System.out.println(5.0 / 2); // one double is enough
System.out.println(5 / 2.0);
System.out.println(1 / 2);
}
}$ java IntegerDivision 2 -2 2.5 2.5 0
1 / 2 is 0: the classic bug of 9 / 5 is 1. Dividing an integer by 0 stops the program with an ArithmeticException (Runtime errors). Python's // rounds down instead, so there -5 // 2 is -3 (Python's division).
1 / 2 دەکاتە 0: هەمان ناسراوی 9 / 5 دەکاتە 1. دابەشکردنی بەسەر 0دا بە ArithmeticException ڕادەگرێت (هەڵەکانی کاتی جێبەجێکردن). //ـی Python لە جیاتی ئەوە بەرەو خوار نزیک دەکاتەوە، بۆیە لەوێ -5 // 2 دەکاتە -3 (دابەشکردن لە Pythonـدا).
Dividing whole numbersدابەشکردنی ژمارە تەواوەکان
Mixing types in a calculationتێکەڵکردنی جۆرەکان لە ژماردندا
Before + - * / %, Java gives both operands the same type: if one is a double, the other is widened to double; otherwise to float or long if one of them is; otherwise both become at least int (numeric promotion, a kind of widening). So byte + byte is an int, and a char takes part as its number (A char is a number):
پێش + - * / %، Java هەردوو دەکات بە هەمان جۆر: ئەگەر یەکێکیان double بێت، ئەوی تر دەبێتە double؛ ئەگەر نا، ئەگەر یەکێکیان float یان long بێت دەبنە ئەو جۆرە؛ ئەگەر نا، هەردووکیان لانیکەم دەبنە int (numeric promotion، جۆرێکە لە فراوانکردنی جۆر). بۆیە byte + byte جۆری intـە و char بە ژمارەکەی بەشداری دەکات (charـێک ژمارەیەکە):
public class Promotion {
public static void main(String[] args) {
int apples = 7;
double kilos = 2.5;
System.out.println(apples * kilos); // int * double gives a double
byte a = 10, b = 20;
int sum = a + b; // byte + byte gives an int
System.out.println(sum);
char letter = 'A';
System.out.println(letter + 1); // char + int gives an int
System.out.println((char) (letter + 1)); // back to a char
}
}$ java Promotion 17.5 30 66 B
That is why byte c = a + b; does not compile: incompatible types: possible lossy conversion from int to byte. Store the result in an int, or cast it with (byte).
بۆیە byte c = a + b; کۆمپایل نابێت: incompatible types: possible lossy conversion from int to byte. ئەنجامەکە لە intـدا هەڵبگرە، یان بە (byte) جۆرەکەی بگۆڕە.
The remainder operator %کارپێکەری پاشماوە: %
%, the remainder (or modulo) operator, gives what is left over after a whole-number division. 26 glasses of tea go on trays of 8: 26 / 8 is 3 full trays and 26 % 8 is 2 glasses left over. More examples: 7 % 3 is 1, 3 % 7 is 3 (7 does not fit even once), 12 % 4 is 0 and 20 % 13 is 7.
%، (یان مۆد)، ئەوە دەدات کە دوای دابەشکردنی دەمێنێتەوە. 26 پەرداخ چا لەسەر سینیی 8 پەرداخی دادەنرێن: 26 / 8 دەکاتە 3 سینیی پڕ و 26 % 8 دەکاتە 2 پەرداخ کە دەمێننەوە. نموونەی تر: 7 % 3 دەکاتە 1، 3 % 7 دەکاتە 3، چونکە 7 تەنانەت جارێکیش ناچێتە ناو 3، 12 % 4 دەکاتە 0 و 20 % 13 دەکاتە 7.


dividend = quotient × divisor + remainder
دابەشکراو = ئەنجامی دابەشکردن × دابەشکەر +
public class Remainder {
public static void main(String[] args) {
System.out.println(7 % 3);
System.out.println(3 % 7);
System.out.println(12 % 4);
System.out.println(26 % 8);
System.out.println(20 % 13);
}
}$ java Remainder 1 3 0 2 7
The remainder operator %کارپێکەری پاشماوە: %
Taking a number apart: 1729لەیەککردنەوەی ژمارەیەک: 1729
With 10, / and % split a number into its digits. For n = 1729: n % 10 is always the last digit (9), n / 10 is n without its last digit (172), n % 100 the last two digits (29), and n / 10.0 keeps the fraction (172.9) because 10.0 is a double. n % 2 is 0 for an even number and 1 for an odd one.
بە 10، / و % ژمارەیەک دەکەن بە . بۆ n = 1729: n % 10 هەمیشە دوایین (9)، n / 10 ژمارەکەیە بەبێ دوایین (172)، n % 100 دوایین دوو (29) و n / 10.0 دەهێڵێتەوە (172.9) چونکە 10.0 جۆری doubleـە. n % 2 بۆ ژمارەی جووت 0ـە و بۆ ژمارەی تاک 1.
public class Digits {
public static void main(String[] args) {
int n = 1729;
System.out.println(n % 10); // the last digit
System.out.println(n / 10); // n without its last digit
System.out.println(n % 100); // the last two digits
System.out.println(n / 10.0); // a double: the fraction stays
System.out.println(-n % 10); // a negative number on the left
System.out.println(n % 2); // 0 for even, 1 for odd
}
}$ java Digits 9 172 29 172.9 -9 1
The remainder operator %کارپێکەری پاشماوە: %
Negative numbers and %ژمارەی نەرێنی و %
In Java the remainder takes the sign of the left operand: -n % 10 for n = 1729 is -9, and -7 % 3 is -1, while 7 % -3 is 1. Java keeps the rule (a / b) * b + (a % b) == a, with / cut toward zero. Languages do not agree here: Python's % takes the sign of the divisor (% and // in code). The modulo of mathematics, which has the sign of the divisor, is Math.floorMod; Math.floorDiv divides and rounds down:
لە Javaـدا نیشانەی چەپ وەردەگرێت: -n % 10 بۆ n = 1729 دەکاتە -9 و -7 % 3 دەکاتە -1، بەڵام 7 % -3 دەکاتە 1. Java ئەم یاسایە دەپارێزێت: (a / b) * b + (a % b) == a، کە / بەرەو سفر دەبڕێت. زمانەکان لێرەدا یەک ناگرنەوە: %ـی Python نیشانەی دابەشکەر وەردەگرێت (% و // لە کۆددا). مۆدی بیرکاری، کە نیشانەی دابەشکەری هەیە، Math.floorModـە؛ Math.floorDiv دابەش دەکات و بەرەو خوار نزیک دەکاتەوە:
public class NegativeRemainder {
public static void main(String[] args) {
System.out.println(-7 % 3); // sign of the left number
System.out.println(7 % -3);
System.out.println(Math.floorMod(-7, 3)); // sign of the right number
System.out.println(-7 / 3); // toward zero
System.out.println(Math.floorDiv(-7, 3)); // down
System.out.println(7.5 % 2); // % works on doubles too
}
}$ java NegativeRemainder -1 1 2 -2 -3 1.5
Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟
Java follows , like the order of operations of school maths (Order of operations): brackets first, then *, / and %, then + and -; the assignment = comes last. So 2 + 3 * 4 is 14, but (2 + 3) * 4 is 20.
Java پەیڕەوی دەکات، وەک ڕیزبەندیی کارەکان لە بیرکاریی قوتابخانەدا (ڕیزبەندیی کارەکان): سەرەتا کەوانە، پاشان *، / و %، پاشان + و -؛ =ـی دوایین دێت. بۆیە 2 + 3 * 4 دەکاتە 14، بەڵام (2 + 3) * 4 دەکاتە 20.
public class Precedence {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(10 / 3 * 3);
System.out.println(10 * 3 / 3);
System.out.println(10 - 2 + 5);
System.out.println(10 - (2 + 5));
}
}$ java Precedence 14 20 9 10 13 3
Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟
Same step: left to rightهەمان پلە: لە چەپەوە بۆ ڕاست
Operators on the same step are grouped from left to right: this is called . 10 - 4 - 3 means (10 - 4) - 3, which is 3. The order also decides whether + adds or joins: once a String is on the left, every later + joins text (Joining text with +). Among the binary operators, only the assignment operators go from right to left: a = b = c = 7; stores 7 in c, then in b, then in a.
لە هەمان پلەدا لە چەپەوە بۆ ڕاست گرووپ دەبن: پێی دەگوترێت . 10 - 4 - 3 واتە (10 - 4) - 3، کە دەکاتە 3. ڕیزبەندییەکە دیاری دەکات + کۆ دەکاتەوە یان دەلکێنێت: کاتێک Stringـێک لە چەپ بێت، هەموو +ـێکی دواتر دەق دەلکێنێت (لکاندنی دەق بە +). لە ، تەنیا لە ڕاستەوە بۆ چەپ دەڕۆن: a = b = c = 7; سەرەتا 7 لە c دادەنێت، پاشان لە b، پاشان لە a.
public class LeftToRight {
public static void main(String[] args) {
System.out.println(10 - 4 - 3); // (10 - 4) - 3
System.out.println(100 / 10 / 5); // (100 / 10) / 5
System.out.println("1" + 2 + 3); // ("1" + 2) + 3
System.out.println(1 + 2 + "3"); // (1 + 2) + "3"
int a, b, c;
a = b = c = 7; // a = (b = (c = 7))
System.out.println(a + b + c);
}
}$ java LeftToRight 3 2 123 33 21
Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟
Brackets: every ( needs a )کەوانە: هەموو ( پێویستی بە ) هەیە
Brackets make the order clear to Java and to the next reader. Every opening bracket needs a closing one; a missing one is a , so javac stops:
کەوانە ڕیزبەندییەکە بۆ Java و بۆ خوێنەری داهاتوو ڕوون دەکاتەوە. هەموو کەوانەیەکی کراوە پێویستی بە کەوانەیەکی داخراو هەیە؛ نەبوونی یەکێکیان ، بۆیە javac دەوەستێت:
public class Formula {
public static void main(String[] args) {
double a = 2, b = 4, t = 0.5;
double r = ((a + b) * t / 2 * (1 - t);
System.out.println(r);
}
}$ javac Formula.java
Formula.java:4: error: ')' expected
double r = ((a + b) * t / 2 * (1 - t);
^
1 errorWith the extra bracket removed, (a + b) * t / 2 * (1 - t) gives 0.75 for a = 2, b = 4 and t = 0.5. Brackets also fix real bugs: a + b / 2 divides only b, so for 8 and 6 it gives 11 instead of the average 7, which is (a + b) / 2.
بە لابردنی کەوانە زیادەکە، (a + b) * t / 2 * (1 - t) بۆ a = 2، b = 4 و t = 0.5 دەدات 0.75. کەوانە ڕاستەقینەش چاک دەکات: a + b / 2 تەنیا b دابەش دەکات، بۆیە بۆ 8 و 6 لە جیاتی تێکڕای 7 دەدات 11؛ تێکڕاکە (a + b) / 2ـە.
Compound assignment: += and friendsکارپێکەرەکانی دانانی نرخ: += و هاوشێوەکانی
A variable is often changed using its own old value: total = total + cans;. Java has a short form for it: total += cans;. There is one for each arithmetic operator: +=, -=, *=, /= and %=; total *= 2; is the short form of total = total * 2;.
زۆرجار بە ی خۆی دەگۆڕدرێت: total = total + cans;. Java شێوەیەکی کورتی بۆ هەیە: total += cans;. بۆ هەر یەکێک هەیە: +=، -=، *=، /= و %=؛ total *= 2; شێوە کورتەکەی total = total * 2;ـە.
public class Compound {
public static void main(String[] args) {
int total = 10;
int cans = 6;
total += cans; // total = total + cans
System.out.println(total);
total *= 2; // total = total * 2
System.out.println(total);
total -= 2; // total = total - 2
System.out.println(total);
total /= 3; // total = total / 3
System.out.println(total);
total %= 4; // total = total % 4
System.out.println(total);
}
}$ java Compound 16 32 30 10 2
++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە
Adding or taking away 1 is so common that Java has two short operators for it: ++ adds 1 to a variable () and -- takes 1 away (). i++; does the same as i = i + 1; and i += 1;.
زیادکردن یان کەمکردنەوەی 1 هێندە باوە کە Java دوو کورتی بۆ هەیە: ++ یەک بۆ زیاد دەکات () و -- یەک کەم دەکاتەوە (). i++; هەمان کاری i = i + 1; و i += 1; دەکات.
public class IncDec {
public static void main(String[] args) {
int i = 3, j = 3;
i++; // i becomes 4
j--; // j becomes 2
System.out.println(i + " " + j);
}
}$ java IncDec 4 2
++ یەک زیاد دەکات، -- یەک کەم دەکاتەوەCêITThey work only on variables (5++ is an error) and you will use them all the time to count in loops (The while loop). Python has no ++ (Short forms).
تەنیا لەسەر کار دەکەن (5++ هەڵەیە) و هەمیشە بۆ ژماردن لە ناو بەکاریان دەهێنیت (خولی while). Python ++ـی نییە (شێوە کورتەکان).
++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە
i++ or ++i?i++ یان ++i؟
The operator can come after the variable, i++ (postfix), or before it, ++i (prefix) (). On a line of its own, both just add 1. Inside a bigger expression they differ: postfix uses the old value and then adds 1; prefix adds 1 first and then uses the new value:
دەتوانێت دوای بێت، i++ (پاشگر)، یان پێشی بێت، ++i (پێشگر) (). لە دێڕێکی تەنیادا، هەردووکیان تەنیا یەک زیاد دەکەن. لە ناو گەورەتردا جیاوازن: پاشگر بەکار دەهێنێت و پاشان یەک زیاد دەکات؛ پێشگر سەرەتا یەک زیاد دەکات و پاشان بەکار دەهێنێت:
public class PostfixPrefix {
public static void main(String[] args) {
int i = 10;
int newNum = 10 * i++; // use 10, then i becomes 11
System.out.println("i is " + i + ", newNum is " + newNum);
i = 10;
newNum = 10 * (++i); // i becomes 11, then use 11
System.out.println("i is " + i + ", newNum is " + newNum);
}
}$ java PostfixPrefix i is 11, newNum is 100 i is 11, newNum is 110


++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە
The four formsچوار شێوەکە
Java has four forms. In each example i is 1 before the line runs:
Java چوار شێوەی هەیە. لە هەر نموونەیەکدا i پێش جێبەجێبوونی دێڕەکە 1ـە:
| Operator | Nameناو | What it doesچی دەکات | Exampleنموونە |
|---|---|---|---|
++var | preincrementزیادکردنی پێشگر | adds 1, then uses the new valueیەک زیاد دەکات، پاشان نرخە نوێیەکە بەکار دەهێنێت | int j = ++i; // j is 2, i is 2 |
var++ | postincrementزیادکردنی پاشگر | uses the old value, then adds 1نرخە کۆنەکە بەکار دەهێنێت، پاشان یەک زیاد دەکات | int j = i++; // j is 1, i is 2 |
--var | predecrementکەمکردنەوەی پێشگر | takes 1 away, then uses the new valueیەک کەم دەکاتەوە، پاشان نرخە نوێیەکە بەکار دەهێنێت | int j = --i; // j is 0, i is 0 |
var-- | postdecrementکەمکردنەوەی پاشگر | uses the old value, then takes 1 awayنرخە کۆنەکە بەکار دەهێنێت، پاشان یەک کەم دەکاتەوە | int j = i--; // j is 1, i is 0 |
j کەی کۆپی دەکرێت؟CêIT++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە
Practice and styleڕاهێنان و شێواز
Work out the four printed lines, then run the program to check.
چوار دێڕە چاپکراوەکە بژمێرە، پاشان جێبەجێ بکە بۆ پشکنین.
public class IncDecExercise {
public static void main(String[] args) {
int a = 6;
int b = ++a;
System.out.println("a = " + a);
System.out.println(" b = " + b);
a = 6;
b = --a;
System.out.println("a = " + a);
System.out.println(" b = " + b);
}
}Show answerوەڵامەکە پیشان بدە
a = 7 b = 7 a = 5 b = 5
++a adds 1 first, so a and b are both 7; then --a (with a set back to 6) takes 1 away first, so both are 5. Type the quotes yourself: quotes copied from slides or PDFs (“a = ”) give illegal character: '\u201c' (Copy-paste traps).
++a سەرەتا یەک زیاد دەکات، بۆیە a و b هەردووکیان 7ن؛ پاشان --a (کە a گەڕێندرایەوە بۆ 6) سەرەتا یەک کەم دەکاتەوە، بۆیە هەردووکیان 5ن. خۆت بنووسە: ئەوانەی لە سلاید یان PDF کۆپی دەکرێن (“a = ”) دەدەن illegal character: '\u201c' (تەڵەکانی کۆپی و لکاندن).
Lab: four operations on two numbersتاقیگە: چوار کار لەسەر دوو ژمارە
Declare variables for number1 = 240.8 and number2 = -6.25, print both, then print their sum, difference, quotient and product, each on its own line with a label.
دوو بۆ number1 = 240.8 و number2 = -6.25 بناسێنە، هەردووکیان چاپ بکە، پاشان کۆ، جیاوازی، ئەنجامی دابەشکردن و ئەنجامی لێکدانیان چاپ بکە، هەریەکە لە دێڕێکی خۆیدا لەگەڵ ناونیشانێک.
Hintئاماژە
Use double variables and four println statements.
double و چوار ڕستەی println بەکار بهێنە.
Show answerوەڵامەکە پیشان بدە
public class Task1 {
public static void main(String[] args) {
double number1 = 240.8;
double number2 = -6.25;
System.out.println("Number1 = " + number1);
System.out.println("Number2 = " + number2);
System.out.println("******** Task1 ******");
System.out.println("Summation = " + (number1 + number2));
System.out.println("Subtraction = " + (number1 - number2));
System.out.println("Division = " + (number1 / number2));
System.out.println("Multiplication = " + (number1 * number2));
}
}$ java Task1 Number1 = 240.8 Number2 = -6.25 ******** Task1 ****** Summation = 234.55 Subtraction = 247.05 Division = -38.528 Multiplication = -1505.0
Each calculation is in brackets, so it is done before the + that joins it to the label.
هەر ژماردنێک لە ناو کەوانەدایە، بۆیە پێش ئەو +ـە ئەنجام دەدرێت کە بە ناونیشانەکەوە دەیلکێنێت.
Lab: the volume of two water tanksتاقیگە: قەبارەی دوو تانکی ئاو
Many houses keep water in round tanks on the roof. A tank is a cylinder, and its volume is π × radius × radius × height:
زۆر ماڵ ئاو لە تانکی خڕی سەربان هەڵدەگرن. تانکێک سیلیندەرێکە و قەبارەکەی π × نیوەتیرە × نیوەتیرە × بەرزییە:
π is about 3.14; r is the radius and h the height
π نزیکەی 3.14ـە؛ r نیوەتیرە و h بەرزییە
Write a program that finds the volume of tank 1 (radius 15, height 75) and tank 2 (radius 12, height 80), using a constant PI = 3.14, and prints the radius, height and volume of each.
بنووسە کە قەبارەی تانکی 1 (نیوەتیرە 15، بەرزی 75) و تانکی 2 (نیوەتیرە 12، بەرزی 80) بدۆزێتەوە، بە بەکارهێنانی وەک PI = 3.14 و نیوەتیرە، بەرزی و قەبارەی هەریەکەیان چاپ بکات.
Hintئاماژە
final double PI = 3.14; and PI * radius1 * radius1 * height1.
final double PI = 3.14; و PI * radius1 * radius1 * height1.
Show answerوەڵامەکە پیشان بدە
public class Task2 {
public static void main(String[] args) {
final double PI = 3.14;
int radius1 = 15, height1 = 75;
int radius2 = 12, height2 = 80;
double volume1 = PI * radius1 * radius1 * height1;
double volume2 = PI * radius2 * radius2 * height2;
System.out.println("***** Task2 *****");
System.out.println("Cylinder 1 radius = " + radius1 + " height = " + height1);
System.out.println("The volume for Cylinder1 = " + volume1);
System.out.println("----------------------------------");
System.out.println("Cylinder 2 radius = " + radius2 + " height = " + height2);
System.out.println("The volume for Cylinder2 = " + volume2);
}
}$ java Task2 ***** Task2 ***** Cylinder 1 radius = 15 height = 75 The volume for Cylinder1 = 52987.5 ---------------------------------- Cylinder 2 radius = 12 height = 80 The volume for Cylinder2 = 36172.799999999996
Lab: the volume of two water tanksتاقیگە: قەبارەی دوو تانکی ئاو
Why 36172.799999999996?بۆچی 36172.799999999996؟
The exact answer is 36172.8, but a double stores numbers in binary, and 3.14 has no exact binary form. The tiny shows in the last digits, depending on the order of the multiplications (Why 0.1 + 0.2 is not 0.3):
وەڵامی ورد 36172.8ـە، بەڵام double ژمارەکان بە شێوەی هەڵدەگرێت و 3.14 شێوەیەکی وردی نییە. لە دوایین دەردەکەوێت و بە ڕیزبەندیی لێکدانەکانەوە بەندە (بۆچی 0.1 + 0.2 نابێتە 0.3):
public class Tail {
public static void main(String[] args) {
System.out.println(3.14 * 12 * 12 * 80);
System.out.println(3.14 * (12 * 12 * 80));
System.out.println(3.14 * 11520);
}
}$ java Tail 36172.799999999996 36172.8 36172.8
Two improvements: Java's own π, Math.PI, is far more exact than 3.14 (The Math class), and printf("%.2f%n", x) prints exactly two decimal places (Two decimal places, rounded):
دوو باشترکردن: Math.PI، کە πـی خودی Javaـیە، زۆر وردترە لە 3.14 (کلاسەکەی Math) و printf("%.2f%n", x) ڕێک دوو چاپ دەکات (دوو خانەی دەیی، نزیککراوە):
public class Cylinders {
public static void main(String[] args) {
int radius1 = 15, height1 = 75;
int radius2 = 12, height2 = 80;
double volume1 = Math.PI * radius1 * radius1 * height1;
double volume2 = Math.PI * radius2 * radius2 * height2;
System.out.println(volume1);
System.out.println(volume2);
System.out.printf("%.2f%n", volume1); // two decimal places
System.out.printf("%.2f%n", volume2);
}
}$ java Cylinders 53014.37602932776 36191.14736935442 53014.38 36191.15
Lab: Celsius to kelvinتاقیگە: سیلیزی بۆ کێلڤن
Scientists measure temperature in kelvin (K). The kelvin scale starts at the coldest possible temperature, so 0 °C is 273.15 K:
زانایان پلەی گەرمی بە کێلڤن (K) دەپێون. پێوانەی کێلڤن لە ساردترین پلەی گەرمی کە هەبێت دەست پێ دەکات، بۆیە 0 °C دەکاتە 273.15 K:
Write a program that converts 15 °C to kelvin and prints 15 Celsius = 288.15 Kelvin.
بنووسە کە 15 °C بگۆڕێت بۆ کێلڤن و چاپ بکات 15 Celsius = 288.15 Kelvin.
Show answerوەڵامەکە پیشان بدە
public class Task3 {
public static void main(String[] args) {
int celsius = 15;
double kelvin = celsius + 273.15;
System.out.println("******* Task 3 *******");
System.out.println(celsius + " Celsius = " + kelvin + " Kelvin");
}
}$ java Task3 ******* Task 3 ******* 15 Celsius = 288.15 Kelvin
celsius + 273.15 mixes an int and a double, so the result is a double.
celsius + 273.15 جۆرێکی int و جۆرێکی double تێکەڵ دەکات، بۆیە ئەنجامەکە doubleـە.
Common mistakesهەڵە باوەکان
| You wroteنووسیت | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
2 ^ 10 | 8: ^ is XOR, not a power8: ^ بریتییە لە XOR، نەک | Math.pow(2, 10) (The Math class)Math.pow(2, 10) (کلاسەکەی Math) |
"Sum: " + a + b | joins: Sum: 53دەلکێنێت: Sum: 53 | "Sum: " + (a + b)"Sum: " + (a + b) |
sum / count // both int | the fraction is cut off دەبڕدرێتەوە | (double) sum / count or / 2.0(double) sum / count یان / 2.0 |
n % 2 == 1 | false for negative odd numbersبۆ ژمارە تاکە نەرێنییەکان false | n % 2 != 0n % 2 != 0 |
x = x++; | x does not changex ناگۆڕێت | x++;x++; |
x =+ 5; | stores +5: x becomes 5+5 هەڵدەگرێت: x دەبێتە 5 | x += 5;x += 5; |
b = b + 1; // byte b | possible lossy conversion from int to bytepossible lossy conversion from int to byte | b += 1; or b++;b += 1; یان b++; |
((a + b) * t | ')' expected')' expected | Count the brackets.کەوانەکان بژمێرە. |
1 / 0 | ArithmeticException: / by zeroArithmeticException: / by zero | Check the divisor first (Runtime errors).سەرەتا دابەشکەرەکە بپشکنە (هەڵەکانی کاتی جێبەجێکردن). |
price * 3; | not a statement: the value would be lostnot a statement: لەدەست دەچێت | Store or print it: total = price * 3; (not a statement)هەڵیبگرە یان چاپی بکە: total = price * 3; (not a statement) |
2(x + 1) | ';' expected and not a statement: Java has no ';' expected و not a statement: Java نییە | 2 * (x + 1)2 * (x + 1) |
The x =+ 5 trap is real: Java reads it as x = +5, so the old value is lost without any message.
تەڵەی x =+ 5 ڕاستەقینەیە: Java وەک x = +5 دەیخوێنێتەوە، بۆیە بێ هیچ پەیامێک لەدەست دەچێت.
public class EqualsPlus {
public static void main(String[] args) {
int x = 10;
x =+ 5; // means x = +5
System.out.println(x);
x += 5; // means x = x + 5
System.out.println(x);
}
}$ java EqualsPlus 5 10
Practiceڕاهێنان
A song lasts 200 seconds. Use / and % to print it as minutes and seconds: 3 min 20 s.
گۆرانییەک 200 چرکە دەخایەنێت. بە / و % وەک خولەک و چرکە چاپی بکە: 3 min 20 s.
Show answerوەڵامەکە پیشان بدە
public class Minutes {
public static void main(String[] args) {
int seconds = 200;
int minutes = seconds / 60;
int rest = seconds % 60;
System.out.println(minutes + " min " + rest + " s");
}
}$ java Minutes 3 min 20 s
Print the sum of the digits of 472 (4 + 7 + 2 = 13), using % and / only.
کۆی 472 چاپ بکە (4 + 7 + 2 = 13)، تەنیا بە % و /.
Show answerوەڵامەکە پیشان بدە
public class DigitSum {
public static void main(String[] args) {
int n = 472;
int ones = n % 10; // 2
int tens = n / 10 % 10; // 7
int hundreds = n / 100; // 4
System.out.println(ones + tens + hundreds);
}
}$ java DigitSum 13
n / 10 % 10 first removes the last digit (47), then takes the new last digit (7).
n / 10 % 10 سەرەتا دوایین لادەبات (47)، پاشان دوایین نوێ وەردەگرێت (7).
What does each line print? 7 / 2 * 2 · 7 % 2 · 7.0 / 2 · -7 / 2 · -7 % 2 · 1 + 2 + "3" + 4 + 5
هەر دێڕێک چی چاپ دەکات؟ 7 / 2 * 2 · 7 % 2 · 7.0 / 2 · -7 / 2 · -7 % 2 · 1 + 2 + "3" + 4 + 5
Show answerوەڵامەکە پیشان بدە
public class Predict {
public static void main(String[] args) {
System.out.println(7 / 2 * 2);
System.out.println(7 % 2);
System.out.println(7.0 / 2);
System.out.println(-7 / 2);
System.out.println(-7 % 2);
System.out.println(1 + 2 + "3" + 4 + 5);
}
}$ java Predict 6 1 3.5 -3 -1 3345
7 / 2 is 3, so 7 / 2 * 2 is 6; the last line adds 1 + 2 = 3, then joins "3", 4 and 5: "3345".
7 / 2 دەکاتە 3، بۆیە 7 / 2 * 2 دەکاتە 6؛ دوایین دێڕ 1 + 2 = 3 کۆ دەکاتەوە، پاشان "3"، 4 و 5 دەلکێنێت: "3345".
Karwan's marks are 70, 85 and 90. This line prints 185.0 instead of the average. Fix it.
نمرەکانی کاروان 70، 85 و 90ن. ئەم دێڕە لە جیاتی تێکڕا 185.0 چاپ دەکات. چاکی بکە.
public class Average3 {
public static void main(String[] args) {
int mark1 = 70, mark2 = 85, mark3 = 90;
double average = mark1 + mark2 + mark3 / 3;
System.out.println(average);
}
}$ java Average3 185.0
Show answerوەڵامەکە پیشان بدە
public class Average3 {
public static void main(String[] args) {
int mark1 = 70, mark2 = 85, mark3 = 90;
double average = (mark1 + mark2 + mark3) / 3.0;
System.out.println(average);
}
}$ java Average3 81.66666666666667
Only mark3 / 3 (30) was divided. Brackets make the sum first, and 3.0 keeps the fraction.
تەنیا mark3 / 3 (30) دابەش کرابوو. کەوانە سەرەتا کۆکردنەوەکە دەکات و 3.0 دەهێڵێتەوە.
int x = 5; int y = x++ + ++x; What are x and y? Then say why you should not write lines like this.
int x = 5; int y = x++ + ++x; x و y چەندن؟ پاشان بڵێ بۆچی نابێت دێڕی وا بنووسیت.
Show answerوەڵامەکە پیشان بدە
public class Tricky {
public static void main(String[] args) {
int x = 5;
int y = x++ + ++x;
System.out.println("x = " + x + ", y = " + y);
}
}$ java Tricky x = 7, y = 12
Left to right: x++ gives 5 and makes x 6; ++x makes x 7 and gives 7; 5 + 7 = 12. Correct, but hard to read: one change per line is clearer.
لە چەپەوە بۆ ڕاست: x++ نرخی 5 دەدات و x دەکات بە 6؛ ++x x دەکات بە 7 و 7 دەدات؛ 5 + 7 = 12. دروستە، بەڵام خوێندنەوەی قورسە: یەک گۆڕان لە هەر دێڕێکدا ڕوونترە.
Summaryکورتە
- An expression works out to one value; a statement does a job and ends with
;. Only assignments,++/--, method calls andnewmay stand alone (otherwise:not a statement). - Arithmetic operators:
+ - * / %.%gives the remainder, with the sign of the left operand (Math.floorModgives the maths modulo). int / intis integer division: the fraction is cut off toward zero. Onedoubleoperand makes it real division.- Precedence: brackets, then
* / %, then+ -, then=. Same step: left to right; assignment: right to left. x += yisx = (type) (x + y): short, and with a hidden cast.i++uses the old value, then adds 1;++iadds 1, then uses the new value. Alone on a line they are the same.
- دەبێتە یەک ؛ کارێک دەکات و بە
;کۆتایی دێت. تەنیا ،++/--، بانگکردنی وnewدەتوانن بەتەنیا بن (ئەگینا:not a statement). - :
+ - * / %.%دەدات، بە نیشانەی چەپ (Math.floorModمۆدی بیرکاری دەدات). int / int: بەرەو سفر دەبڕدرێتەوە. یەکdoubleدەیکات بە دابەشکردنی ڕاستەقینە.- : کەوانە، پاشان
* / %، پاشان+ -، پاشان=. هەمان پلە: لە چەپەوە بۆ ڕاست؛ : لە ڕاستەوە بۆ چەپ. x += yواتەx = (type) (x + y): کورتە و شاراوەی تێدایە.i++نرخە کۆنەکە بەکار دەهێنێت، پاشان یەک زیاد دەکات؛++iیەک زیاد دەکات، پاشان نرخە نوێیەکە بەکار دەهێنێت. لە دێڕێکی تەنیادا وەک یەکن.
Why is -7 / 2 equal to -3 in Java, while Python's -7 // 2 is -4?بۆچی -7 / 2 لە Javaـدا -3ـە، بەڵام -7 // 2ـی Python -4ـە؟
Java's integer division cuts toward zero; Python's // rounds down. Use Math.floorDiv(-7, 2) for -4 in Java.
لە Javaـدا بەرەو سفر دەبڕێت؛ //ـی Python بەرەو خوار نزیک دەکاتەوە. بۆ -4 لە Javaـدا Math.floorDiv(-7, 2) بەکار بهێنە.
What does n % 10 give?n % 10 چی دەدات؟
The last digit of a positive whole number n.
دوایین ئەرێنیی n.
Why does small += 5 compile for a byte but small = small + 5 does not?بۆچی small += 5 بۆ byte کۆمپایل دەبێت بەڵام small = small + 5 نا؟
small + 5 is an int. The compound form contains a cast back to byte; the long form needs one written by hand.
small + 5 جۆری intـە. شێوە کورتەکە (+=) (byte)ـی تێدایە؛ شێوە درێژەکە پێویستی بە نووسینی بە دەستە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
The Math Classکلاسەکەی Math
Related lecturesوانە پەیوەندیدارەکان
- The Math Classکلاسەکەی Math
- Keyboard Input with Scanner and Formatted Outputنێوخستە لە کیبۆردەوە بە Scanner و دەرخستەی ڕێکخراو
- Primitive Data Typesجۆرە سەرەتاییەکان
- Operators and Expressionsکارپێکەرەکان و دەربڕینەکان
- Brackets and the Order of Operationsکەوانەکان و پێشینەیی کارپێکەرەکان
- Plus, Minus, Times, Divide and the Fraction Barکۆ، کەم، جاران، دابەش و هێڵی کەرت
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- W3Schools: Java Operators
- W3Schools: Java Arithmetic Operators
- W3Schools: Java Assignment Operators
- W3Schools: Java Comparison Operators
- W3Schools: Java Operator Precedence
- The Java Tutorials: Operators (Oracle) — the precedence table
- The Java Language Specification, Java SE 17 — chapter 15: expressions (§15.7 evaluation order, §15.14–15.15 ++ and --, §15.17 multiplicative operators, §15.18.1 string concatenation, §15.26 assignment operators)
- The Java Language Specification, Java SE 17 — chapter 14: statements (§14.8 expression statements)
- The Java Language Specification, Java SE 17 — §5.6 numeric promotion
- Java SE 17 API: java.lang.Math (floorDiv, floorMod)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.