Topic 2 Exercises
Exercise 1. Tell me the difference between System.out.println and System.out.print.
Solution:
If you use System.out.println(“Hello”); it will print “Hello” and then move to the next line. But if you use System.out.print(“Hello”); it will print “Hello” on the same line where the cursor is.
π‘ Use System.out.println to print messages with a line break at the end. It is useful to show information and separate different parts of the output.
Exercise 2. Display a pyramid of asterisks in your IDE console. Something like this:
*
***
*****
Solution:
System.out.println(” *”);
System.out.println(” ***”);
System.out.println(” *****”);
Exercise 3. Create a three-day schedule with three subjects and display it on screen.
Solution:
System.out.println(” Monday | Tuesday | Wednesday “);
System.out.println(” “);
System.out.println(” Mathematics | Language | Economics “);
System.out.println(” “);
System.out.println(” Programming | Philosophy | Geography “);
System.out.println(” “);
System.out.println(” Physics | Language | Chemistry “);
System.out.println(” “);
Exercise 4.
Declare two variables: one of type double called a and another of type int called b. Assign them the values 1.4 and 7, respectively. Then, display the multiplication of both on screen.
Solution:
double a;
int b;
a=1.4;
b=7;
System.out.println(a*b);
Exercise 5. Create a variable of type String and display it on screen.
Solution:
String name = “Pedro”; System.out.println(name);
Exercise 6. Create a variable of type char and display it on screen.
Solution:
char symbol = ‘@’; System.out.println(symbol);
Exercise 7. Create 4 variables of type int and name them a, b, c, and d. Assign them the values 15, 25, 5, and 8, respectively. Perform an operation that adds a + b, divides that result by c, and then multiplies everything by d.
Solution:
int a = 15;
int b = 25;
int c = 5;
int d = 8;
System.out.println(((a + b) / c) * d);
Exercise 8. Create 4 variables of type int and name them a, b, c, and d. Assign them the values 14, 24, 5, and 7, respectively. Perform an operation that adds a + b, divides that result by c, and then multiplies everything by d.
Solution:
If you notice in this case, the results are not reliable because when dividing the sum of a and b by c, decimals appear. The problem is that the int data type cannot hold decimals. This causes the decimal part to be lost. So in the end, the result is not exact. Before performing the operations, we must convert the int variables to double. We will do it as follows:
int a = 14;
int b= 24;
int c = 5;
int d = 7;
double aa=a;
double bb=b;
double cc=c;
double dd=d;
Now we can perform the operations: System.out.println(((aa+bb)/cc)*dd);
π‘ Use System.out.print when you need to print without adding a line break. Ideal for building output lines dynamically.
Exercise 9. Create a basic currency converter that converts euros to dollars. We will assume that one euro equals 1.10 dollars. For this, you should create a variable of type double and assign it the exact value of the euros you need to convert. We want the final value to be displayed in the console.
Solution:
Method 1.
double euros= 70;
System.out.println(euros *1.1);
Method 2.
double euros= 70;
double dollars = euros * 1.1;
System.out.println(dollars);
Exercise 10. Create a program that calculates the VAT of an invoice and applies it to the net amount. The program must show both the net value and the VAT separately, and then the total of the invoice. The net value will be assigned to a variable of type double. The VAT is 16%.
Solution:
double net = 120;
double vat = net * 0.16;
System.out.println(“The net value is ” + net + ” euros”);
System.out.println(“The VAT value is ” + vat + ” euros”);
System.out.println(“The total value is ” + (vat + net) + ” euros”);