Java Basics 12 - Ternary Operator Solution

Words
111
Reading
1 min
Listen
Play
8y

Lecture 12: Ternary Operator Solution


Ternary operator problem statement


Implement the Ternary operator where wages are 3700 and earnings are a String.
Print out Exempt if earnings < 4500 and Taxable otherwise.

Ternary operator solution


String earnings = (wages < 4500) ? “Exempt” : “Taxable”;

Ternary operator output


Exempt


L12.png

Hi there, My name is Marius from AlefTav Coding. In this lecture I will look at the Ternary operator coding exercise. For homework you had to Implement the Ternary operator where wages are 3700 and earnings are a String. Print out Exempt if earnings < 4500 and Taxable otherwise.

public class Lecture12 {
    public static void main(String[] args) {
        int wages = 3700;
        String earnings = (wages < 4500) ? “Exempt” : “Taxable”;
        System.out.print(earnings);
    }
}

Looking at my solution. The String, earnings is a reference to only one of two things. If the person's wages are less than 4500, then earnings points to the text, Exempt, otherwise earnings points to the word, Taxable. Because, in many countries governments do not expect you to pay income taxes if you earn low wages. They only expect you to pay taxes if you are a middle-income earner.

I have, here, above my class, Lecture12. As always, there must be a main() method. I declare, wages as data type, double, because monetary values are fractional amounts. Next, I initialise the wages amount as 3700. Then, there follows an implementation of the ternary operator. This is then followed by printing out the text value referenced by earnings.

L12 tmp.png

My solution is all good. So, when my program is executed the output is the text, Exempt. Low wage earners are exempted from paying income taxes.

In today's lecture you learnt the solution to the ternary operator coding exercise. The next two lectures will cover advanced concepts to easily deal with big data. These are the concepts, a lambda and a stream() method.

I am Marius, from AlefTav Coding. Till the next time, KEEP CODING.


May I remind you that, if you found this tutorial helpful, please upvote, subscribe, like and share. For more of this you can find me at the following links.

https://www.udemy.com/course/1435298/manage/basics/
https://d.tube/#!/c/mariusclaassen
https://steemit.com/@mariusclaassen