You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -85,6 +85,57 @@ Scanner scanner = new Scanner(System.in);
85
85
Double num = scanner.nextDouble();
86
86
```
87
87
88
+
### Problem: Square Area
89
+
90
+
For example, let's look at the following program which reads an **integer** from the console, multiplies it by itself (squares it), and prints the result from the multiplication. That's how we can calculate square area by side length:
91
+
92
+
```Java
93
+
// Put this code in the file: SquareArea.java
94
+
95
+
importjava.util.Scanner;
96
+
97
+
publicclassSquareArea {
98
+
publicstaticvoidmain(String[] args) {
99
+
Scanner scanner =newScanner(System.in);
100
+
101
+
System.out.print("a = ");
102
+
int a = scanner.nextInt();
103
+
int area = a * a;
104
+
105
+
System.out.print("Square area = ");
106
+
System.out.println(area);
107
+
}
108
+
}
109
+
```
110
+
111
+
Here is how the program will work with a side having a size of 15:
This is a normal phenomenon, because the **Java** language is strongly typed. Later we will earn how we can catch these type of errors and ask the user to type his input again.
120
+
121
+
#### How Does The Example Work?
122
+
123
+
On the first line **`Scanner scanner = new Scanner(System.in);`** create a new instance of the class **`Scanner`** with standart system code.
124
+
125
+
On the next line **`System.out.print("a = ");`** prints informative message, that asks the user to type the value for the side **a** of the square. If we want our next input to be on the same row we use **`System.out.print(…);`**, and not **`System.out.println(…);`** because this will move it to the next row.
126
+
127
+
On the next line **`int a = scanner.nextInt();`** reads the integer number from the console. The result is binded to the variable **`a`**
128
+
129
+
The next command **`int area = a * a;`** defines a new variable **`area`** with the result **`a * a`**
130
+
131
+
The next command **`System.out.print("Square area = ");`** prints the text without going to a new line. Again we are using **`System.out.print(…);`**, and not **`System.out.println("…");`**.
132
+
133
+
With the last command we will print the area of the square - **`System.out.println(area);`**
134
+
135
+
#### Testing in The Judge System
136
+
137
+
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#0](https://judge.softuni.org/Contests/Practice/Index/649#0).
138
+
88
139
### Problem: Inches to Centimeters
89
140
90
141
Write a program that reads a floating-point number (representing inches) as an input from the console, converts it to centimeters, and print the result as output:
@@ -171,7 +222,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#
171
222
172
223
Let’s look at the basic arithmetic operations in programming.
173
224
174
-
### Summing up numbers (operator**`+`**)
225
+
### Summing Up Numbers (Operator**`+`**)
175
226
176
227
We can sum up numbers using the operator **`+`**:
177
228
@@ -198,7 +249,7 @@ Here is the result of the execution of the program above (with numbers 10 and 3)
For multiplication of numbers we use the operator **`*`**:
204
255
@@ -255,7 +306,7 @@ When printing the values **∞** and **-∞** the console output may be
255
306
NaN
256
307
```
257
308
258
-
## Concatenating text and numbers
309
+
## Concatenating Text and Numbers
259
310
260
311
The operator **`+`** besides for summing up numbers is also used for joining text (concatenation of two strings one after another). In programming, joining text with other text or with number is called "concatenation". Here is how we can concatenate a text with a number with the operator **`+`**:
The standard rule for the precedence of arithmetic operations is applied: **multiplying and dividing are always done before adding and subtracting**. In the case of an **expression in brackets, it is calculated first**, but we already know all of that from math.
297
348
298
-
### Problem: Trapeziod Area
299
-
300
-
Write a program that reads as input from the console the lengths of the two bases of a trapezoid and its height (one floating-point number per line) and calculates the area of the trapezoid by the standard math formula.
In this problem, we must take into account that we can obtain the length of the rectangle if we subtract the smaller `x` from the bigger `x`. Identically, if we subtract the smaller `y` from the bigger `y`, we can obtain the rectangle height. Then we multiply both sides. Here is an example of an implementation of the described logic:
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#2](hhttps://judge.softuni.org/Contests/Practice/Index/649#2).
449
-
450
-
451
380
### Problem: Concatenating Text and Numbers
452
381
453
382
Write a Java program, that reads from the console a first name, last name, age, and city, and prints a message of the following kind: **`You are <firstName> <lastName>, a <age>-years old person from <town>.`**.
@@ -520,7 +449,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#
520
449
521
450
**A rectangle** is defined by the **coordinates** at two of its opposite angles (x1, y1) – (x2, y2). Calculate its **area and perimeter**. **The input** is read from the console. The numbers **x1, y1, x2, and y2** are given one per line. **The output** is printed on the console and it has to contain two lines, each with one number – the area and the perimeter.
@@ -629,29 +558,6 @@ Write a program for the **conversion of money from one currency into another**.
629
558
630
559
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#11](https://judge.softuni.org/Contests/Practice/Index/649#11).
631
560
632
-
633
-
### Problem: Currency Converter
634
-
635
-
Write a program that reads from the console a **birth date** in format **`dd-MM-yyyy`** then calculates the date on which **1000 days** are turned since this birth date, and prints the output in the same format.
636
-
637
-
#### Sample Input and Output
638
-
639
-
| Input | Output |
640
-
|--------|--------|
641
-
|25-02-1995|21-11-1997|
642
-
|07-11-2003|03-08-2006|
643
-
|30-12-2002|25-09-2005|
644
-
|01-01-2012|27-09-2014|
645
-
|14-06-1980|11-03-1983|
646
-
647
-
#### Hints and Guidelines
648
-
* Look for information about the types **`Date`**, **`Calendar`**, and **`SimpleDateFormat`** in Java, and in particular look at the methods **`Calendar.setTime(date)`**, **`Calendar.add(countDays)`** and **`SimpleDateFormat.format(date)`**. With their help, you can solve the problem without the need to calculate days, months, and leap years.
649
-
***Don't print** anything additional on the console except for the wanted date!
650
-
651
-
#### Testing in The Judge System
652
-
653
-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#12](https://judge.softuni.org/Contests/Practice/Index/649#12).
654
-
655
561
### Problem: 1000 Days After Birth
656
562
657
563
As an example, let us look at a program. The problem is to calculate the area of a square by a given side's length read as input from the console. The sample source code of the program is below. The code **reads an integer** as input from the console, **multiplies it** by itself (squares it), and as output **prints the result** from the multiplication. Save the code in a file with the name SquareArea.java, or else you will have a compile-time error:
@@ -700,4 +606,13 @@ At the last line **`System.out.println(area);`** prints the calculated value of
700
606
701
607
#### Testing in The Judge System
702
608
703
-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#0](https://judge.softuni.org/Contests/Practice/Index/649#0).
609
+
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#0](https://judge.softuni.org/Contests/Practice/Index/649#0).
610
+
611
+
## What Have We Learned from This Chapter?
612
+
613
+
To summarize what have we learned in this chapter of the book:
614
+
-**Reading a text**: **`String str = scanner.nextLine();`** (as we have written in advance **`Scanner scanner = new Scanner(System.in);`**).
615
+
-**Reading an integer**: **`int num = Integer.parseInt(scanner.nextLine());`**.
616
+
-**Reading a floating-point number**: **`double num = Double.parseDouble(scanner.nextLine());`**.
617
+
-**Calculations with numbers** and using the relevant **arithmetic operators**[`+`, `-`, `*`, `/`, `(`, `)`]: **`int sum = 5 + 3;`**.
618
+
-**Printing a text by placeholders** on the console: **`System.out.printf("%d + %d = %d", 3, 5, 3 + 5);`**.
0 commit comments