ULOG: Java Programming Basics 12 - Ternary Operator SolutionsteemCreated with Sketch.

in #ulog6 years ago

Lecture 12: Ternary Operator Solution


Ternary operator coding exercise

public class Lecture12 {
    public static void main(String[] args) {
        double wages = 3900;
        // TODO: Implement the *Ternary* operator where *wages* are 3700 and *earnings* are a **String**.
        // TODO:   Print out *Exempt* if *earnings* < 4500 and *Taxable* otherwise.
        System.out.print(earnings);
    }
}

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'.

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

Looking at this solution code above. The String, earnings is a reference to only one of two things. If a person's wages are less than 4500, then earnings points to the text, Exempt, otherwise earnings points to the word, Taxable.

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 or high-income earner.

Complete solution

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

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.

Exempt

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 more advanced concepts to easily deal with big data. These are the concepts, lambda and the stream() method.

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


Join me on Trybe.one (tokenized information-sharing platform) to earn 100 tokens for signing up and 10 tokens for logging in daily:

http://trybe.one/ref/2859/

This post was made from https://ulogs.org

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

@ilovecoding, I upvoted your comment. Also, I have voted for you @justyy as a witness.

This user is on the @buildawhale blacklist for one or more of the following reasons:

  • Spam
  • Plagiarism
  • Scam or Fraud

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 64176.22
ETH 2624.19
USDT 1.00
SBD 2.78