Skip to content

Commit 491ae50

Browse files
committed
Add Missing Problem
1 parent 35f0077 commit 491ae50

File tree

1 file changed

+65
-150
lines changed

1 file changed

+65
-150
lines changed

chapter-02-simple-calculations.md

Lines changed: 65 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,57 @@ Scanner scanner = new Scanner(System.in);
8585
Double num = scanner.nextDouble();
8686
```
8787

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+
import java.util.Scanner;
96+
97+
public class SquareArea {
98+
public static void main(String[] args) {
99+
Scanner scanner = new Scanner(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:
112+
113+
![](/assets/chapter-2-1-images/01.Square-area-01.jpg)
114+
115+
If we change the number to string - "**hello**", we will get an exception error.
116+
117+
![](/assets/chapter-2-1-images/02.Exception-hello-01.jpg)
118+
119+
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+
88139
### Problem: Inches to Centimeters
89140

90141
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#
171222

172223
Let’s look at the basic arithmetic operations in programming.
173224

174-
### Summing up numbers (operator **`+`**)
225+
### Summing Up Numbers (Operator **`+`**)
175226

176227
We can sum up numbers using the operator **`+`**:
177228

@@ -198,7 +249,7 @@ Here is the result of the execution of the program above (with numbers 10 and 3)
198249

199250
![](assets/chapter-2-1-images/04.Console-output-04.png)
200251

201-
### Multiplying Numbers (operator **`*`**)
252+
### Multiplying Numbers (Operator **`*`**)
202253

203254
For multiplication of numbers we use the operator **`*`**:
204255

@@ -255,7 +306,7 @@ When printing the values **∞** and **-∞** the console output may be
255306
NaN
256307
```
257308

258-
## Concatenating text and numbers
309+
## Concatenating Text and Numbers
259310

260311
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 **`+`**:
261312

@@ -295,105 +346,6 @@ int expr = (3 + 5) * (4 – 2);
295346
```
296347
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.
297348

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.
301-
302-
```Java
303-
Scanner scanner = new Scanner(System.in);
304-
305-
double b1 = Double.parseDouble(scanner.nextLine());
306-
double b2 = Double.parseDouble(scanner.nextLine());
307-
double h = Double.parseDouble(scanner.nextLine());
308-
double area = (b1 + b2) * h / 2.0;
309-
System.out.println("Trapezoid area = " + area);
310-
```
311-
312-
If we start the program and enter values for sides: 3, 4, and 5, we will obtain the following result:
313-
```
314-
3
315-
4
316-
5
317-
Trapezoid area = 17.5
318-
```
319-
320-
#### Testing in The Judge System
321-
322-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#4](https://judge.softuni.org/Contests/Practice/Index/649#4).
323-
324-
325-
### Problem: Circle Area and Perimeter
326-
327-
Write a program that reads as input from the console a **radius r** of a circle, and **calculates its area and perimeter**.
328-
329-
Formulas:
330-
- Area = π \* r \* r
331-
- Perimeter = 2 \* π \* r
332-
- π ≈ 3.14159265358979323846…
333-
334-
```Java
335-
Scanner scanner = new Scanner(System.in);
336-
337-
System.out.print("Enter circle radius. r = ");
338-
double r = Double.parseDouble(scanner.nextLine());
339-
System.out.println("Area = " + Math.PI * r * r);
340-
// Math.PI - built-in function in Java
341-
System.out.println("Perimeter = " + 2 * Math.PI * r);
342-
```
343-
Let’s test the program with **radius `r = 10`**:
344-
345-
![](/assets/chapter-2-1-images/04.Console-output-05.png)
346-
347-
#### Testing in The Judge System
348-
349-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#5](https://judge.softuni.org/Contests/Practice/Index/649#5).
350-
351-
352-
### Problem: 2D Rectangle Area
353-
354-
A rectangle is given with the **coordinates of two of its opposite angles**. Write a program that calculates its **area and perimeter**:
355-
356-
<img alt="rectangleArea" src="/assets/chapter-2-1-images/05.Rectangle-area-01.png" width="250" height="200" />
357-
358-
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:
359-
360-
```Java
361-
Scanner scanner = new Scanner(System.in);
362-
363-
double x1 = Double.parseDouble(scanner.nextLine());
364-
double y1 = Double.parseDouble(scanner.nextLine());
365-
double x2 = Double.parseDouble(scanner.nextLine());
366-
double y2 = Double.parseDouble(scanner.nextLine());
367-
368-
// Calculate the sides of the rectangle:
369-
double width = Math.max(x1, x2) - Math.min(x1, x2);
370-
double height = Math.max(y1, y2) - Math.min(y1, y2);
371-
372-
DecimalFormat decimalFormat = new DecimalFormat("#.#########");
373-
System.out.println(decimalFormat.format(width * height));
374-
System.out.println(decimalFormat.format(2 * (width + height)));
375-
```
376-
377-
We use **`Math.max(a, b)`** to find the bigger value from **`a`** and **`b`** and identically **`Math.min(a, b)`** to find the smaller of both values.
378-
379-
When the program is executed with the values from the coordinate system above we obtain the following result:
380-
381-
![](/assets/chapter-2-1-images/04.Console-output-06.png)
382-
383-
#### Testing in The Judge System
384-
385-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#6](https://judge.softuni.org/Contests/Practice/Index/649#6).
386-
387-
388-
## What Have We Learned from This Chapter?
389-
390-
To summarize what have we learned in this chapter of the book:
391-
- **Reading a text**: **`String str = scanner.nextLine();`** (as we have written in advance **`Scanner scanner = new Scanner(System.in);`**).
392-
- **Reading an integer**: **`int num = Integer.parseInt(scanner.nextLine());`**.
393-
- **Reading a floating-point number**: **`double num = Double.parseDouble(scanner.nextLine());`**.
394-
- **Calculations with numbers** and using the relevant **arithmetic operators** [`+`, `-`, `*`, `/`, `(`, `)`]: **`int sum = 5 + 3;`**.
395-
- **Printing a text by placeholders** on the console: **`System.out.printf("%d + %d = %d", 3, 5, 3 + 5);`**.
396-
397349
## Problems Simple Calculations
398350

399351
To consolidate our knowledge of simple calculations, let's solve several problems.
@@ -425,29 +377,6 @@ Now we have an **empty IntelliJ IDEA Project** (without any Java classes in it w
425377

426378
The purpose of this project is to add in it **`.java` class per problem** from the problems.
427379

428-
### Problem: Greeting by name
429-
430-
Write a program that **reads from the console a person's name** and prints **`Hello, <name>!`**, where **`<name>`** is the name entered earlier.
431-
432-
#### Hints and Guidelines
433-
434-
First, we create a **new Java class** with the name “Greeting” in the project “SimpleCalculations”:
435-
436-
![](assets/chapter-2-1-images/09.Greeting-by-name-01.png)
437-
438-
**Next, we have to write the code** of the program. If you have any difficulties, you can use the code from the example below:
439-
440-
![](assets/chapter-2-1-images/09.Greeting-by-name-02.png)
441-
442-
**Run** the program with [**Ctrl+Shift+F10**] and test if it works:
443-
444-
![](assets/chapter-2-1-images/09.Greeting-by-name-03.png)
445-
446-
#### Testing in The Judge System
447-
448-
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#2](hhttps://judge.softuni.org/Contests/Practice/Index/649#2).
449-
450-
451380
### Problem: Concatenating Text and Numbers
452381

453382
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#
520449

521450
**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.
522451

523-
![](/assets/chapter-2-1-images/12.Rectangle-area-01.png)
452+
<img src="/assets/chapter-2-1-images/12.Rectangle-area-01.png" alt="Image" style="width:400px">
524453

525454
#### Sample Input and Output
526455

@@ -629,29 +558,6 @@ Write a program for the **conversion of money from one currency into another**.
629558

630559
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/649#11](https://judge.softuni.org/Contests/Practice/Index/649#11).
631560

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-
655561
### Problem: 1000 Days After Birth
656562

657563
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
700606

701607
#### Testing in The Judge System
702608

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

Comments
 (0)