Created
October 7, 2018 16:18
-
-
Save fmaylinch/654ae0ddcafe928279d28616e465ee74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.bts.javaexamples; | |
/** | |
* Basic exercises to practice variable declarations and assignments. | |
*/ | |
public class BasicsExercises { | |
public static void main(String[] args) { | |
/* | |
1. Declare a variable with type `int` and name `age`. | |
2. Assign the value 15 to the variable `age`. | |
*/ | |
int age = 15; | |
//3. Declare a variable with type `double` and name `weight`. | |
//In the same line, assign the value `42.5` to that variable. | |
// 4. Declare a variable `totalWeight` and assign the value of `weight` multiplied by 2. | |
// Decide what type you should choose for `totalWeight`. | |
double weight = 42.5; | |
double totalWeight = weight / 2; | |
// 5. Declare a variable `year` and assign `2018` to it. | |
int year = 2018; | |
double price = 50.5; | |
int quantity = 30; | |
// 6. Declare the variables `price`, `quantity` and `totalPrice`. | |
// Assign any values to `price` and `quantity`, and calculate the value for `totalPrice`. | |
// 7. Apply a discount of 5% to `totalPrice` and assign the result to `finalPrice`. | |
// 8. Subtract 10 from `finalPrice` (change its value). | |
double totalPrice = price * quantity; | |
int discount = 5; | |
double finalPrice = (totalPrice * discount) /100; | |
finalPrice = finalPrice - 10; | |
// 9. Declare a variable `productName` (type String) and assign somme value, e.g. "iPhone Xs". | |
String productName; | |
productName = "NiceBook"; | |
// 10. Declare a variable `message` and assign to it a message like "iPhone Xs costs 1500". | |
// That message should be created by joining the variables `productName` and `price`. | |
String message; | |
message = ("productName" + "costs" + "price"); | |
// 11. Declare a `boolean` variable named `applyDiscount` and assign the value `true`. | |
// 12. Declare a `boolean` variable adult, that should have the result of comparing `age >= 18`. | |
boolean applyDiscount = true; | |
boolean adult = age >= 18; | |
// 13. In a similar way, declare a variable `expensive`, | |
// which should have the result of comparing `price` and `1000`. | |
boolean expensive = price >= 1000; | |
// 14. Declare a variable `age2` with some value. | |
// Declare a variable `sameAge`, and assign the result of comparing `age` and `age2`. | |
int age2 = 19; | |
boolean sameAge = age == age2; | |
// 15. Declare two integer variables `kids` and `cookies`, with some values (e.g. `5` and `22`) | |
// Calculate how many cookies each kid can have. | |
// Calculate how many cookies are left. | |
int kids = 6; | |
int cookies = 27; | |
int cookiesPerKid = cookies / kids; | |
int cookiesLeft = cookies % kids; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment